diff --git a/pdfmajor/converters/XMLConverter.py b/pdfmajor/converters/XMLConverter.py index 5dd29db..59dccd1 100644 --- a/pdfmajor/converters/XMLConverter.py +++ b/pdfmajor/converters/XMLConverter.py @@ -113,16 +113,18 @@ class XMLConverter(PDFConverter): }) def place_text(self, char: LTChar): - with self.place_elm_with_child('char', { - 'font-size': char.size, - 'font-family': char.fontname, + attr = { + 'size': char.size, + "color": get_color(char.graphicstate.ncolor), "x0": char.x0, "x1": char.x1, "y0": char.y0, "y1": char.y1, - "stroke-color": get_color(char.graphicstate.scolor), - "fill-color": get_color(char.graphicstate.ncolor), - }, no_additional_char=True): + } + for key, value in char.font.descriptor.items(): + if key != "Type" and "FontFile" not in key: + attr["font:" + key] = value + with self.place_elm_with_child('char', attr, no_additional_char=True): self.write(char.get_text(), lineend='', deep_space="") def render_curve(self, item: LTCurve): diff --git a/pdfmajor/interpreter/PDFFont.py b/pdfmajor/interpreter/PDFFont.py index 203a1cb..1c2320c 100644 --- a/pdfmajor/interpreter/PDFFont.py +++ b/pdfmajor/interpreter/PDFFont.py @@ -23,6 +23,7 @@ from .PDFStream import list_value from .PDFStream import dict_value from .PDFStream import PDFStream from ..utils import settings, FONT_METRICS, apply_matrix_norm, nunpack, choplist, isnumber +from .constants import COMMON_FONT_ATTRIBUTES def get_widths(seq): widths = {} diff --git a/pdfmajor/interpreter/PDFPage.py b/pdfmajor/interpreter/PDFPage.py index e6098ea..b0a7a97 100644 --- a/pdfmajor/interpreter/PDFPage.py +++ b/pdfmajor/interpreter/PDFPage.py @@ -72,7 +72,7 @@ class PDFPage(object): INHERITABLE_ATTRS = set(['Resources', 'MediaBox', 'CropBox', 'Rotate']) @classmethod - def create_pages(cls, document): + def create_pages(cls, document: PDFDocument): def search(obj, parent): if isinstance(obj, int): objid = obj diff --git a/pdfmajor/interpreter/constants.py b/pdfmajor/interpreter/constants.py index ec008d2..e474703 100644 --- a/pdfmajor/interpreter/constants.py +++ b/pdfmajor/interpreter/constants.py @@ -3,8 +3,33 @@ from .PSStackParser import LIT ## Constants ## -LITERAL_PDF = LIT('PDF') -LITERAL_TEXT = LIT('Text') -LITERAL_FONT = LIT('Font') -LITERAL_FORM = LIT('Form') -LITERAL_IMAGE = LIT('Image') +LITERAL_PDF = LIT("PDF") +LITERAL_TEXT = LIT("Text") +LITERAL_FONT = LIT("Font") +LITERAL_FORM = LIT("Form") +LITERAL_IMAGE = LIT("Image") + +COMMON_FONT_ATTRIBUTES = [ + "Type", + "FontName", + "FontFamily", + "FontStretch", + "FontWeight", + "Flags", + "FontBBox", + "ItalicAngle", + "Ascent", + "Descent", + "Leading", + "CapHeight", + "XHeight", + "StemV", + "StemH", + "AvgWidth", + "MaxWidth", + "MissingWidth", + # "FontFile", + # "FontFile2", + # "FontFile3", + "Charset", +] \ No newline at end of file diff --git a/pdfmajor/layouts/LTChar.py b/pdfmajor/layouts/LTChar.py index f35694a..d227fc2 100644 --- a/pdfmajor/layouts/LTChar.py +++ b/pdfmajor/layouts/LTChar.py @@ -1,6 +1,8 @@ from ._base import LTComponent from ..utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox from ..interpreter.PDFGraphicState import PDFGraphicState +from ..interpreter.PDFFont import PDFFont +from ..interpreter.PDFColorSpace import PDFColorSpace ## LTChar ## @@ -8,19 +10,19 @@ class LTChar(LTComponent): def __init__(self, matrix, - font, - fontsize, - scaling, - rise, - text, - textwidth, - textdisp, - ncs, + font: PDFFont, + fontsize: float, + scaling: float, + rise: float, + text: str, + textwidth: float, + textdisp: tuple, + ncs: PDFColorSpace, graphicstate: PDFGraphicState ): self._text = text self.matrix = matrix - self.fontname = font.fontname + self.font = font self.ncs = ncs self.graphicstate = graphicstate self.adv = textwidth * fontsize * scaling @@ -71,4 +73,8 @@ class LTChar(LTComponent): def is_compatible(self, obj): """Returns True if two characters can coexist in the same line.""" - return True \ No newline at end of file + return True + + @property + def fontname(self) -> str: + return self.font.fontname \ No newline at end of file diff --git a/samples/convert_samples.py b/samples/convert_samples.py index 9234d15..8f217c2 100644 --- a/samples/convert_samples.py +++ b/samples/convert_samples.py @@ -7,16 +7,18 @@ from pdfmajor.converters import convert_file def rel_path(*arg: List[str]) -> str: return os.path.join(os.path.dirname(__file__), *arg) -EXT = 'xml' +EXT = 'html' INPUT_FOLDER = rel_path("./pdf") OUTPUT_FOLDER = rel_path(f"./output/{EXT}") if not os.path.exists(OUTPUT_FOLDER): os.makedirs(OUTPUT_FOLDER) -files = os.listdir(INPUT_FOLDER) -# files = ['colors.pdf'] +# files = os.listdir(INPUT_FOLDER) +# files = ['fonts.pdf'] # files = ['bar-charts.pdf'] +# files = ['font-color.pdf'] +files = ['tables.pdf'] TOTAL = len(files) for idx, file_name in enumerate(files): diff --git a/samples/pdf/chars.pdf b/samples/pdf/chars.pdf new file mode 100644 index 0000000..f626067 Binary files /dev/null and b/samples/pdf/chars.pdf differ diff --git a/samples/pdf/chars.text b/samples/pdf/chars.text new file mode 100644 index 0000000..f626067 Binary files /dev/null and b/samples/pdf/chars.text differ diff --git a/samples/pdf/font-color.pdf b/samples/pdf/font-color.pdf new file mode 100644 index 0000000..0574912 Binary files /dev/null and b/samples/pdf/font-color.pdf differ diff --git a/samples/pdf/fonts.pdf b/samples/pdf/fonts.pdf new file mode 100644 index 0000000..f6b84e9 Binary files /dev/null and b/samples/pdf/fonts.pdf differ diff --git a/setup.py b/setup.py index 6189b06..417c82e 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ # ============== # Dependencies # ============== -from setuptools import setup, find_namespace_packages +from setuptools import setup, find_packages import sys # ============== @@ -11,7 +11,7 @@ with open('README.md') as readme_file: SETUP = { "name": "pdfmajor", "version": "1.0.7", - "packages": find_namespace_packages(include=['pdfmajor*']), + "packages": find_packages(include=['pdfmajor*']), "install_requires": [ 'pycryptodome', 'chardet'