Skip to content

Commit

Permalink
Merge pull request #488 from chinapandaman/PPF-487
Browse files Browse the repository at this point in the history
PPF-487: move update_text_field_attributes to template middleware
  • Loading branch information
chinapandaman authored Feb 10, 2024
2 parents 445908a + dfc57e9 commit 04f9ab3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
33 changes: 2 additions & 31 deletions PyPDFForm/core/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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])
32 changes: 31 additions & 1 deletion PyPDFForm/middleware/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])
4 changes: 2 additions & 2 deletions PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 04f9ab3

Please sign in to comment.