Skip to content

Commit

Permalink
PPF-403: removed font_size.py
Browse files Browse the repository at this point in the history
  • Loading branch information
chinapandaman committed Nov 13, 2023
1 parent ca08699 commit a145205
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 49 deletions.
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
39 changes: 38 additions & 1 deletion 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 @@ -63,3 +66,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.

3 changes: 1 addition & 2 deletions PyPDFForm/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from ..middleware.text import Text
from . import constants
from . import font as font_core
from . import font_size as font_size_core
from . import template


Expand Down Expand Up @@ -49,7 +48,7 @@ def update_text_field_attributes(
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 font_core.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

0 comments on commit a145205

Please sign in to comment.