Page MenuHomec4science

page_creator.py
No OneTemporary

File Metadata

Created
Mon, May 6, 10:48

page_creator.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" This class can be used to create a page.
"""
# Copyright (C) University of Basel 2019 {{{1
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/> 1}}}
__author__ = "Christian Steiner"
__maintainer__ = __author__
__copyright__ = 'University of Basel'
__email__ = "christian.steiner@unibas.ch"
__status__ = "Development"
__license__ = "GPL v3"
__version__ = "0.0.1"
from lxml import etree as ET
from os.path import isfile
from progress.bar import Bar
from svgpathtools import svg2paths2, svg_to_paths
from svgpathtools.parser import parse_path
import sys
import warnings
from .box import Box
from .image import Image, SVGImage
from .faksimile_image import FaksimileImage
from .faksimile_position import FaksimilePosition
from .lineNumber import LineNumber
from .line import Line
from .mark_foreign_hands import MarkForeignHands
from .matrix import Matrix
from .path import Path
from .positional_word_part import PositionalWordPart
from .super_page import SuperPage
from .text_connection_mark import TextConnectionMark
from .text_field import TextField
from .transkriptionField import TranskriptionField
from .writing_process import WritingProcess
from .word import Word
from .word_insertion_mark import WordInsertionMark
FILE_TYPE_SVG_WORD_POSITION = SuperPage.FILE_TYPE_SVG_WORD_POSITION
FILE_TYPE_XML_MANUSCRIPT = SuperPage.FILE_TYPE_XML_MANUSCRIPT
class PageCreator(SuperPage):
"""
This class represents a page.
Args:
xml_target_file (str): name of the xml file to which page info will be written.
"""
UNITTESTING = False
WARNING_MISSING_USE_NODE4PWP = PositionalWordPart.WARN_NO_USE_NODE_FOUND
WARNING_MISSING_GLYPH_ID4WIM = WordInsertionMark.WARN_NO_GLYPH_ID
PAGE_RECTO = 'recto'
PAGE_VERSO = 'verso'
def __init__(self, xml_target_file, title=None, page_number=None, faksimile_image=None, faksimile_svgFile=None, pdfFile=None, svg_file=None, orientation='North', page_type=PAGE_VERSO, source=None):
super(PageCreator,self).__init__(xml_target_file, title=title, page_number=page_number, orientation=orientation, page_type=page_type)
self.update_property_dictionary('faksimile_image', faksimile_image)
self.update_property_dictionary('faksimile_svgFile', faksimile_svgFile)
self.update_property_dictionary('pdfFile', pdfFile)
self.update_property_dictionary('svg_file', svg_file)
self.update_property_dictionary('source', source)
if svg_file is not None and isfile(svg_file):
tf = TranskriptionField(svg_file)
width = round(tf.documentWidth, 3)
height = round(tf.documentHeight, 3)
self.update_property_dictionary('width', width)
self.update_property_dictionary('height', height)
self.update_property_dictionary('svg_image', SVGImage(file_name=svg_file, width=width, height=height))
for xpath2remove in [ 'word', 'style', 'freehand', LineNumber.XML_TAG, WordInsertionMark.XML_TAG, WritingProcess.XML_TAG, Path.WORD_DELETION_PATH_TAG]:
for node in self.page_tree.xpath('//' + xpath2remove):
node.getparent().remove(node)
self.init_all_properties()
def create_writing_processes_and_attach2tree(self):
"""Creates three stages of Nietzsche's process of writing.
"""
self.writing_processes = [ WritingProcess(version=WritingProcess.FIRST_VERSION),\
WritingProcess(version=WritingProcess.INSERTION_AND_ADDITION),\
WritingProcess(version=WritingProcess.LATER_INSERTION_AND_ADDITION) ]
for writing_process in self.writing_processes:
writing_process.attach_object_to_tree(self.page_tree)
#for word in self.words:
# for transkription_position in word.transkription_positions:
# for font_key in transkription_position.positional_word_parts[0].style_class.split(' '):
# if font_key in self.fontsizekey2stage_mapping.keys():
# transkription_position.writing_process_id = self.fontsizekey2stage_mapping.get(font_key)
def init_line_numbers(self, line_numbers, document_bottom):
"""Init line numbers.
"""
even_index = 0
MINABOVE = 1
self.line_numbers = []
if len(line_numbers) > 0:
first_line_bottom = line_numbers[even_index].top - MINABOVE
self.line_numbers.append(LineNumber(id=1, top=0, bottom=first_line_bottom))
self.line_numbers.append(line_numbers[even_index])
even_index += 1
while even_index < len(line_numbers):
self.line_numbers.append(LineNumber(id=line_numbers[even_index].id-1,\
top=line_numbers[even_index-1].bottom+MINABOVE,\
bottom=line_numbers[even_index].top-MINABOVE))
self.line_numbers.append(line_numbers[even_index])
even_index += 1
self.line_numbers.append(LineNumber(id=line_numbers[even_index-1].id+1,\
top=line_numbers[even_index-1].bottom+MINABOVE,\
bottom=document_bottom))
for line_number in self.line_numbers:
line_number.attach_object_to_tree(self.page_tree)

Event Timeline