Skip to content

Commit

Permalink
Merge pull request #422 from chinapandaman/PPF-418
Browse files Browse the repository at this point in the history
PPF-418: fix test_paragraph_auto_wrap
  • Loading branch information
chinapandaman authored Dec 20, 2023
2 parents ff29093 + 831893d commit 0ee7074
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
2 changes: 1 addition & 1 deletion PyPDFForm/core/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,4 @@ def update_text_field_attributes(
elements[key].text_wrap_length = get_paragraph_auto_wrap_length(
_element, elements[key]
)
elements[key].text_lines = get_paragraph_lines(elements[key])
elements[key].text_lines = get_paragraph_lines(_element, elements[key])
59 changes: 40 additions & 19 deletions PyPDFForm/core/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,16 +167,44 @@ def get_character_x_paddings(element: PdfDict, element_middleware: Text) -> List
return result


def get_paragraph_lines(element_middleware: Text) -> List[str]:
def calculate_wrap_length(element: PdfDict, element_middleware: Text, v: str) -> int:
"""Increments the substring until reaching maximum horizontal width."""

width = abs(
float(element[ANNOTATION_RECTANGLE_KEY][0])
- float(element[ANNOTATION_RECTANGLE_KEY][2])
)
value = element_middleware.value or ""
value = value.replace(NEW_LINE_SYMBOL, " ")

counter = 0
_width = 0
while _width <= width and counter < len(value):
counter += 1
_width = stringWidth(
v[:counter],
element_middleware.font,
element_middleware.font_size,
)
return counter - 1


def get_paragraph_lines(element: PdfDict, element_middleware: Text) -> List[str]:
"""Splits the paragraph field's text to a list of lines."""

# pylint: disable=R0912
lines = []
result = []
text_wrap_length = element_middleware.text_wrap_length
value = element_middleware.value or ""
if element_middleware.max_length is not None:
value = value[: element_middleware.max_length]

width = abs(
float(element[ANNOTATION_RECTANGLE_KEY][0])
- float(element[ANNOTATION_RECTANGLE_KEY][2])
)

split_by_new_line_symbol = value.split(NEW_LINE_SYMBOL)
for line in split_by_new_line_symbol:
characters = line.split(" ")
Expand All @@ -194,11 +222,18 @@ def get_paragraph_lines(element_middleware: Text) -> List[str]:
else current_line
)

for line in lines:
while stringWidth(
line[:text_wrap_length],
element_middleware.font,
element_middleware.font_size,
) > width:
text_wrap_length -= 1

for each in lines:
while len(each) > text_wrap_length:
last_index = text_wrap_length - 1
result.append(each[:last_index])
each = each[last_index:]
result.append(each[:text_wrap_length])
each = each[text_wrap_length:]
if each:
if (
result
Expand All @@ -221,20 +256,6 @@ def get_paragraph_lines(element_middleware: Text) -> List[str]:
def get_paragraph_auto_wrap_length(element: PdfDict, element_middleware: Text) -> int:
"""Calculates the text wrap length of a paragraph field."""

def calculate_wrap_length(v: str) -> int:
"""Increments the substring until reaching maximum horizontal width."""

counter = 0
_width = 0
while _width <= width and counter < len(value):
counter += 1
_width = stringWidth(
v[:counter],
element_middleware.font,
element_middleware.font_size,
)
return counter - 1

value = element_middleware.value or ""
value = value.replace(NEW_LINE_SYMBOL, " ")
width = abs(
Expand All @@ -251,7 +272,7 @@ def calculate_wrap_length(v: str) -> int:
if lines > 1:
current_min = 0
while len(value) and current_min < len(value):
result = calculate_wrap_length(value)
result = calculate_wrap_length(element, element_middleware, value)
value = value[result:]
if current_min == 0:
current_min = result
Expand Down
Binary file modified pdf_samples/paragraph/test_paragraph_auto_wrap.pdf
Binary file not shown.

0 comments on commit 0ee7074

Please sign in to comment.