From dfc57e965f7d4d31f58d70a5e01068c6b89de8f9 Mon Sep 17 00:00:00 2001 From: jingeli Date: Fri, 9 Feb 2024 20:53:34 -0600 Subject: [PATCH] PPF-487: good thing no circular imports --- PyPDFForm/core/font.py | 33 ++------------------------------ PyPDFForm/middleware/template.py | 32 ++++++++++++++++++++++++++++++- PyPDFForm/wrapper.py | 4 ++-- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/PyPDFForm/core/font.py b/PyPDFForm/core/font.py index 66f4b8ea..50573892 100644 --- a/PyPDFForm/core/font.py +++ b/PyPDFForm/core/font.py @@ -4,20 +4,17 @@ from io import BytesIO from math import sqrt from re import findall -from typing import Dict, Tuple, Union +from typing import Tuple, Union from reportlab.pdfbase.acroform import AcroForm from reportlab.pdfbase.pdfmetrics import registerFont, standardFonts from reportlab.pdfbase.ttfonts import TTFError, TTFont -from ..middleware.constants import WIDGET_TYPES -from ..middleware.text import Text from .constants import (ANNOTATION_RECTANGLE_KEY, DEFAULT_FONT, DEFAULT_FONT_SIZE, FONT_COLOR_IDENTIFIER, FONT_SIZE_IDENTIFIER) from .patterns import TEXT_FIELD_APPEARANCE_PATTERNS -from .template import (get_paragraph_auto_wrap_length, get_paragraph_lines, - get_widget_key, get_widgets_by_page, is_text_multiline) +from .template import is_text_multiline from .utils import traverse_pattern @@ -153,29 +150,3 @@ def get_text_field_font_color( break return result - - -def update_text_field_attributes( - template_stream: bytes, - widgets: Dict[str, WIDGET_TYPES], -) -> None: - """Auto updates text fields' attributes.""" - - for _, _widgets in get_widgets_by_page(template_stream).items(): - for _widget in _widgets: - key = get_widget_key(_widget) - - if isinstance(widgets[key], Text): - if widgets[key].font is None: - widgets[key].font = auto_detect_font(_widget) - if widgets[key].font_size is None: - widgets[key].font_size = get_text_field_font_size( - _widget - ) or text_field_font_size(_widget) - if widgets[key].font_color is None: - widgets[key].font_color = get_text_field_font_color(_widget) - if is_text_multiline(_widget) and widgets[key].text_wrap_length is None: - widgets[key].text_wrap_length = get_paragraph_auto_wrap_length( - _widget, widgets[key] - ) - widgets[key].text_lines = get_paragraph_lines(_widget, widgets[key]) diff --git a/PyPDFForm/middleware/template.py b/PyPDFForm/middleware/template.py index f0800472..3fc0673a 100644 --- a/PyPDFForm/middleware/template.py +++ b/PyPDFForm/middleware/template.py @@ -4,10 +4,14 @@ from typing import Dict, List from ..core.constants import ANNOTATION_RECTANGLE_KEY +from ..core.font import (auto_detect_font, get_text_field_font_size, text_field_font_size, + get_text_field_font_color) from ..core.template import (construct_widget, get_button_style, get_character_x_paddings, get_dropdown_choices, get_text_field_max_length, get_widget_key, - get_widgets_by_page, is_text_field_comb) + get_widgets_by_page, is_text_field_comb, is_text_multiline, + get_paragraph_auto_wrap_length, + get_paragraph_lines) from ..core.watermark import create_watermarks_and_draw from .checkbox import Checkbox from .constants import WIDGET_TYPES @@ -102,3 +106,29 @@ def dropdown_to_text(dropdown: Dropdown) -> Text: ) return result + + +def update_text_field_attributes( + template_stream: bytes, + widgets: Dict[str, WIDGET_TYPES], +) -> None: + """Auto updates text fields' attributes.""" + + for _, _widgets in get_widgets_by_page(template_stream).items(): + for _widget in _widgets: + key = get_widget_key(_widget) + + if isinstance(widgets[key], Text): + if widgets[key].font is None: + widgets[key].font = auto_detect_font(_widget) + if widgets[key].font_size is None: + widgets[key].font_size = get_text_field_font_size( + _widget + ) or text_field_font_size(_widget) + if widgets[key].font_color is None: + widgets[key].font_color = get_text_field_font_color(_widget) + if is_text_multiline(_widget) and widgets[key].text_wrap_length is None: + widgets[key].text_wrap_length = get_paragraph_auto_wrap_length( + _widget, widgets[key] + ) + widgets[key].text_lines = get_paragraph_lines(_widget, widgets[key]) diff --git a/PyPDFForm/wrapper.py b/PyPDFForm/wrapper.py index 761b21aa..acf66d52 100644 --- a/PyPDFForm/wrapper.py +++ b/PyPDFForm/wrapper.py @@ -9,7 +9,7 @@ from .core.constants import DEFAULT_FONT, DEFAULT_FONT_COLOR, DEFAULT_FONT_SIZE from .core.coordinate import generate_coordinate_grid from .core.filler import fill -from .core.font import register_font, update_text_field_attributes +from .core.font import register_font from .core.image import any_image_to_jpg, rotate_image from .core.utils import (get_page_streams, merge_two_pdfs, preview_widget_to_draw, remove_all_widgets) @@ -22,7 +22,7 @@ from .middleware.dropdown import Dropdown from .middleware.template import (build_widgets, dropdown_to_text, set_character_x_paddings, - widget_rect_watermarks) + widget_rect_watermarks, update_text_field_attributes) from .middleware.text import Text from .widgets.checkbox import CheckBoxWidget from .widgets.text import TextWidget