From 66b5fea65c5f9e87c64cad19dc845a4d50792de4 Mon Sep 17 00:00:00 2001 From: Ariel Sosnovsky Date: Mon, 29 Oct 2018 13:03:11 -0400 Subject: [PATCH] - compatible xml-converter with char-blocks - consitent attr naming convention for fonts --- pdfmajor/converters/XMLConverter.py | 47 ++++++++++++++++++----------- samples/convert_samples.py | 2 +- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/pdfmajor/converters/XMLConverter.py b/pdfmajor/converters/XMLConverter.py index 363873b..fa35440 100644 --- a/pdfmajor/converters/XMLConverter.py +++ b/pdfmajor/converters/XMLConverter.py @@ -1,4 +1,5 @@ import logging +import re from typing import Dict @@ -30,6 +31,12 @@ def get_color(col: PDFGraphicStateColor): return f'gray({color_val})' return "" +FIRST_CAP_RE = re.compile('(.)([A-Z][a-z]+)') +ALL_CAP_RE = re.compile('([a-z0-9])([A-Z])') +def convert_capcase_to_camelcase(text: str) -> str: + s1 = FIRST_CAP_RE.sub(r'\1_\2', text) + return ALL_CAP_RE.sub(r'\1_\2', s1).lower() + class XMLConverter(PDFConverter): def __init__(self, rsrcmgr, @@ -113,23 +120,29 @@ class XMLConverter(PDFConverter): }) def place_text(self, char: LTChar): - tag_name = 'char' - if isinstance(char, LTCharBlock): - tag_name = 'char-block' - attr = { - 'size': char.size, - "color": get_color(char.graphicstate.ncolor), - "x0": char.x0, - "x1": char.x1, - "y0": char.y0, - "y1": char.y1, - } - 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(tag_name, attr, no_additional_char=True): + with self.place_elm_with_child('char', { + 'width': char.width, + 'height': char.height, + }, no_additional_char=True): self.write(char.get_text(), lineend='', deep_space="") + def render_char_block(self, char_block: LTCharBlock): + attr = { + 'size': char_block.size, + "color": get_color(char_block.graphicstate.ncolor), + "x0": char_block.x0, + "x1": char_block.x1, + "y0": char_block.y0, + "y1": char_block.y1, + "is_bold": char_block.font.is_bold, + } + for key, value in char_block.font.descriptor.items(): + if key != "Type" and "FontFile" not in key: + attr[convert_capcase_to_camelcase(key)] = value + with self.place_elm_with_child("char-block", attr): + for char in char_block: + self.place_text(char) + def render_curve(self, item: LTCurve): attr = { "x0": item.x0, @@ -164,8 +177,8 @@ class XMLConverter(PDFConverter): render(child) elif isinstance(item, LTImage): self.place_image(item) - elif isinstance(item, LTChar) or isinstance(item, LTCharBlock): - self.place_text(item) + elif isinstance(item, LTCharBlock): + self.render_char_block(item) render(ltpage) def close(self): diff --git a/samples/convert_samples.py b/samples/convert_samples.py index 8f217c2..de96361 100644 --- a/samples/convert_samples.py +++ b/samples/convert_samples.py @@ -7,7 +7,7 @@ from pdfmajor.converters import convert_file def rel_path(*arg: List[str]) -> str: return os.path.join(os.path.dirname(__file__), *arg) -EXT = 'html' +EXT = 'xml' INPUT_FOLDER = rel_path("./pdf") OUTPUT_FOLDER = rel_path(f"./output/{EXT}")