Skip to content

Commit

Permalink
PPF-404: moved some font methods
Browse files Browse the repository at this point in the history
  • Loading branch information
chinapandaman committed Nov 19, 2023
1 parent 65c82e0 commit 9cd368c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 46 deletions.
46 changes: 44 additions & 2 deletions PyPDFForm/core/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import re
from io import BytesIO
from math import sqrt
from typing import Dict, Union
from typing import Dict, Union, Tuple

import pdfrw
from reportlab.pdfbase import pdfmetrics
Expand All @@ -17,7 +17,6 @@
from .patterns import TEXT_FIELD_APPEARANCE_PATTERNS
from .template import (get_element_key, get_elements_by_page,
get_paragraph_auto_wrap_length, get_paragraph_lines,
get_text_field_font_color, get_text_field_font_size,
is_text_multiline)
from .utils import traverse_pattern

Expand Down Expand Up @@ -110,6 +109,49 @@ def checkbox_radio_font_size(element: pdfrw.PdfDict) -> Union[float, int]:
return sqrt(area) * 72 / 96


def get_text_field_font_size(element: pdfrw.PdfDict) -> Union[float, int]:
"""Returns the font size of the text field if presented or zero."""

result = 0
for pattern in TEXT_FIELD_APPEARANCE_PATTERNS:
text_appearance = traverse_pattern(pattern, element)
if text_appearance:
text_appearance = text_appearance.replace("(", "").replace(")", "")
properties = text_appearance.split(" ")
for i, val in enumerate(properties):
if val == constants.FONT_SIZE_IDENTIFIER:
return float(properties[i - 1])

return result


def get_text_field_font_color(
element: pdfrw.PdfDict,
) -> Union[Tuple[float, float, float], None]:
"""Returns the font color tuple of the text field if presented or black."""

result = (0, 0, 0)
for pattern in TEXT_FIELD_APPEARANCE_PATTERNS:
text_appearance = traverse_pattern(pattern, element)
if text_appearance:
if constants.FONT_COLOR_IDENTIFIER not in text_appearance:
return result

text_appearance = (
text_appearance.replace("(", "").replace(")", "").split(" ")
)
for i, val in enumerate(text_appearance):
if val == constants.FONT_COLOR_IDENTIFIER.replace(" ", ""):
result = (
float(text_appearance[i - 3]),
float(text_appearance[i - 2]),
float(text_appearance[i - 1]),
)
break

return result


def update_text_field_attributes(
template_stream: bytes,
elements: Dict[str, ELEMENT_TYPES],
Expand Down
44 changes: 0 additions & 44 deletions PyPDFForm/core/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from .utils import find_pattern_match, traverse_pattern
from .patterns import (DROPDOWN_CHOICE_PATTERNS, ELEMENT_ALIGNMENT_PATTERNS,
ELEMENT_KEY_PATTERNS, ELEMENT_TYPE_PATTERNS,
TEXT_FIELD_APPEARANCE_PATTERNS,
TEXT_FIELD_FLAG_PATTERNS)


Expand Down Expand Up @@ -111,49 +110,6 @@ def get_draw_checkbox_radio_coordinates(
)


def get_text_field_font_size(element: pdfrw.PdfDict) -> Union[float, int]:
"""Returns the font size of the text field if presented or zero."""

result = 0
for pattern in TEXT_FIELD_APPEARANCE_PATTERNS:
text_appearance = traverse_pattern(pattern, element)
if text_appearance:
text_appearance = text_appearance.replace("(", "").replace(")", "")
properties = text_appearance.split(" ")
for i, val in enumerate(properties):
if val == constants.FONT_SIZE_IDENTIFIER:
return float(properties[i - 1])

return result


def get_text_field_font_color(
element: pdfrw.PdfDict,
) -> Union[Tuple[float, float, float], None]:
"""Returns the font color tuple of the text field if presented or black."""

result = (0, 0, 0)
for pattern in TEXT_FIELD_APPEARANCE_PATTERNS:
text_appearance = traverse_pattern(pattern, element)
if text_appearance:
if constants.FONT_COLOR_IDENTIFIER not in text_appearance:
return result

text_appearance = (
text_appearance.replace("(", "").replace(")", "").split(" ")
)
for i, val in enumerate(text_appearance):
if val == constants.FONT_COLOR_IDENTIFIER.replace(" ", ""):
result = (
float(text_appearance[i - 3]),
float(text_appearance[i - 2]),
float(text_appearance[i - 1]),
)
break

return result


def get_text_field_max_length(element: pdfrw.PdfDict) -> Union[int, None]:
"""Returns the max length of the text field if presented or None."""

Expand Down

0 comments on commit 9cd368c

Please sign in to comment.