Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PPF-403: consolidate font and font size functions #406

Merged
merged 6 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions PyPDFForm/core/filler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ..middleware.checkbox import Checkbox
from ..middleware.constants import ELEMENT_TYPES
from ..middleware.radio import Radio
from . import font_size as font_size_core
from .font import checkbox_radio_font_size
from . import template, utils
from . import watermark as watermark_core

Expand All @@ -34,7 +34,7 @@ def fill(
needs_to_be_drawn = False

if isinstance(elements[key], (Checkbox, Radio)):
font_size = font_size_core.checkbox_radio_font_size(_element)
font_size = checkbox_radio_font_size(_element)
_to_draw = utils.checkbox_radio_to_draw(elements[key], font_size)
x, y = template.get_draw_checkbox_radio_coordinates(_element, _to_draw)
if isinstance(elements[key], Checkbox) and elements[key].value:
Expand Down
52 changes: 45 additions & 7 deletions PyPDFForm/core/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

import re
from io import BytesIO
from typing import Union
from math import sqrt

import pdfrw
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFError, TTFont

from . import constants
from .patterns import TEXT_FIELD_APPEARANCE_PATTERNS
from .template import traverse_pattern
from .template import traverse_pattern, is_text_multiline
from .constants import DEFAULT_FONT_SIZE


def register_font(font_name: str, ttf_stream: bytes) -> bool:
Expand Down Expand Up @@ -38,15 +41,16 @@ def auto_detect_font(element: pdfrw.PdfDict) -> str:
for pattern in TEXT_FIELD_APPEARANCE_PATTERNS:
text_appearance = traverse_pattern(pattern, element)

if not text_appearance:
return result
if text_appearance:
break

text_appearance = text_appearance.replace("(", "").replace(")", "").split(" ")
if not text_appearance:
return result

for each in text_appearance:
if not each.startswith("/"):
return result
text_appearance = text_appearance.replace("(", "").replace(")", "").split(" ")

for each in text_appearance:
if each.startswith("/"):
text_segments = re.findall("[A-Z][^A-Z]*", each.replace("/", ""))

for font in pdfmetrics.standardFonts:
Expand All @@ -63,3 +67,37 @@ def auto_detect_font(element: pdfrw.PdfDict) -> str:
return font

return result


def text_field_font_size(element: pdfrw.PdfDict) -> Union[float, int]:
"""
Calculates the font size it should be drawn with
given a text field element.
"""

if is_text_multiline(element):
return DEFAULT_FONT_SIZE

height = abs(
float(element[constants.ANNOTATION_RECTANGLE_KEY][1])
- float(element[constants.ANNOTATION_RECTANGLE_KEY][3])
)

return height * 2 / 3


def checkbox_radio_font_size(element: pdfrw.PdfDict) -> Union[float, int]:
"""
Calculates the font size it should be drawn with
given a checkbox/radio button element.
"""

area = abs(
float(element[constants.ANNOTATION_RECTANGLE_KEY][0])
- float(element[constants.ANNOTATION_RECTANGLE_KEY][2])
) * abs(
float(element[constants.ANNOTATION_RECTANGLE_KEY][1])
- float(element[constants.ANNOTATION_RECTANGLE_KEY][3])
)

return sqrt(area) * 72 / 96
44 changes: 0 additions & 44 deletions PyPDFForm/core/font_size.py

This file was deleted.

5 changes: 3 additions & 2 deletions PyPDFForm/core/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def is_text_field_comb(element: pdfrw.PdfDict) -> bool:

try:
return "{0:b}".format(int(element[constants.FIELD_FLAG_KEY]))[::-1][24] == "1"
except (TypeError, IndexError):
except (IndexError, TypeError):
return False


Expand All @@ -217,7 +217,7 @@ def is_text_multiline(element: pdfrw.PdfDict) -> bool:

try:
return "{0:b}".format(int(field_flag))[::-1][12] == "1"
except (TypeError, IndexError):
except (IndexError, TypeError):
return False


Expand All @@ -234,6 +234,7 @@ def get_dropdown_choices(element: pdfrw.PdfDict) -> Union[Tuple[str], None]:
.replace(")", "")
for each in choices
)
break

return result

Expand Down
7 changes: 3 additions & 4 deletions PyPDFForm/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from ..middleware.radio import Radio
from ..middleware.text import Text
from . import constants
from . import font as font_core
from . import font_size as font_size_core
from .font import auto_detect_font, text_field_font_size
from . import template


Expand Down Expand Up @@ -45,11 +44,11 @@ def update_text_field_attributes(

if isinstance(elements[key], Text):
if elements[key].font is None:
elements[key].font = font_core.auto_detect_font(_element)
elements[key].font = auto_detect_font(_element)
if elements[key].font_size is None:
elements[key].font_size = template.get_text_field_font_size(
_element
) or font_size_core.text_field_font_size(_element)
) or text_field_font_size(_element)
if elements[key].font_color is None:
elements[key].font_color = template.get_text_field_font_color(
_element
Expand Down