From 016017bc7d6a479b6dc5629dd6d70f43ce863bd7 Mon Sep 17 00:00:00 2001 From: jingeli Date: Tue, 19 Dec 2023 22:05:51 -0600 Subject: [PATCH 1/4] PPF-418: small change --- PyPDFForm/core/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PyPDFForm/core/template.py b/PyPDFForm/core/template.py index c976d713..13fc0171 100644 --- a/PyPDFForm/core/template.py +++ b/PyPDFForm/core/template.py @@ -196,7 +196,7 @@ def get_paragraph_lines(element_middleware: Text) -> List[str]: for each in lines: while len(each) > text_wrap_length: - last_index = text_wrap_length - 1 + last_index = text_wrap_length result.append(each[:last_index]) each = each[last_index:] if each: From c3c80efe07c9c895d96cf4b9f8f0badc7cc0ed26 Mon Sep 17 00:00:00 2001 From: jingeli Date: Tue, 19 Dec 2023 22:32:11 -0600 Subject: [PATCH 2/4] PPF-418: will worry about pylint later --- PyPDFForm/core/font.py | 2 +- PyPDFForm/core/template.py | 54 +++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/PyPDFForm/core/font.py b/PyPDFForm/core/font.py index 48b2f249..d3c0edca 100644 --- a/PyPDFForm/core/font.py +++ b/PyPDFForm/core/font.py @@ -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]) diff --git a/PyPDFForm/core/template.py b/PyPDFForm/core/template.py index 13fc0171..c1c7cacb 100644 --- a/PyPDFForm/core/template.py +++ b/PyPDFForm/core/template.py @@ -167,9 +167,32 @@ 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 @@ -177,6 +200,11 @@ def get_paragraph_lines(element_middleware: Text) -> List[str]: 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(" ") @@ -194,6 +222,14 @@ 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 @@ -221,20 +257,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( @@ -251,7 +273,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 From 49aaaa624f5b03267566d52459b2f34a6361dcff Mon Sep 17 00:00:00 2001 From: chinapandaman Date: Tue, 19 Dec 2023 22:33:46 -0600 Subject: [PATCH 3/4] PPF-418: update pdf sample --- .../paragraph/test_paragraph_auto_wrap.pdf | Bin 625066 -> 625063 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/pdf_samples/paragraph/test_paragraph_auto_wrap.pdf b/pdf_samples/paragraph/test_paragraph_auto_wrap.pdf index afe553c705370189ab995442bfc9ebf1b819ace5..9efc31487c7f5032058f7cfd6641950889fe3f47 100644 GIT binary patch delta 860 zcmZvZOG_J37=}@6wEiHV*$_mdbqMUrR+BQcrO<|1Qrv6a@~*mY;og>*fO zcHzRT1XtpwzoS3G|KVv68s~H1h2h~n-|fBet9-BzS2m)+cgW-(}Q*AAj&q5iQ7Y~XSQh{72PwL-6@%$?C+cY-@C;h*66|- zO=(SQ&1k*Qn$>!#HTTCF&G+v;DVEW{Z~1f~H2$Fm!3Z#lRghuMIbh-$CYcfR5k%RT zc@vEZp%NI`qjAzF85ocv_?8kzg7qkSI-sX_rj!eC1u6yDju{dR$cTW9OB`&>gn~>- z51f@_1(}{ESda?^uE3QJa-8D-I0b_B1T`KbIh-(Z1ai_nz!fAi08<2RtT^SrLO);q Np2wkBtgu%MJpeT8z#aeq delta 850 zcmZvZOD_Xa6vyLPnplX$QVfbRGI!>_t4f*bs8%(kI@CLTP_IU9&B8*gXTickR}w2b z@qv5~qp_$SH~A+wzjJc_=ke-}zq#Y@zLz%YI?sdd&`Z&=&y(rG*y*}G5!$&meMyxDU?ekPBY>Kn0o?p{cC3<2-aXG$~m^W2xbW$j!D5)}EWfe=UY{u0ah1=3JF9+g)!RI^d+PAi z>8Z<8wGX^f`Xn1qIzpxf++)x!k!2dU~VFKV;-k@8B~|Rgkk|16SUQsatWD~V37=? zX6E@LL>PgWVO)YOFo^);`k#W0dYtIs3Y;OxWCVe;3Zw%jF`QU>T|fFBdY^izKM;te Gm;K)i48B4D From 831893dae8cfa4ccf436fb6adb8ee573a127024f Mon Sep 17 00:00:00 2001 From: jingeli Date: Tue, 19 Dec 2023 22:35:56 -0600 Subject: [PATCH 4/4] PPF-418: just slightly more clean --- PyPDFForm/core/template.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/PyPDFForm/core/template.py b/PyPDFForm/core/template.py index c1c7cacb..71de94ed 100644 --- a/PyPDFForm/core/template.py +++ b/PyPDFForm/core/template.py @@ -232,9 +232,8 @@ def get_paragraph_lines(element: PdfDict, element_middleware: Text) -> List[str] for each in lines: while len(each) > text_wrap_length: - last_index = text_wrap_length - result.append(each[:last_index]) - each = each[last_index:] + result.append(each[:text_wrap_length]) + each = each[text_wrap_length:] if each: if ( result