Skip to content

Commit

Permalink
[skip ci]: black/isort
Browse files Browse the repository at this point in the history
  • Loading branch information
black-isort-bot committed Oct 31, 2022
1 parent cd09827 commit e39e086
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 30 deletions.
11 changes: 5 additions & 6 deletions PyPDFForm/core/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Contains helpers for template."""

import uuid
from math import sqrt
from typing import Dict, List, Tuple, Union

import pdfrw
Expand All @@ -11,7 +12,6 @@
from .constants import Template as TemplateCoreConstants
from .patterns import ELEMENT_KEY_PATTERNS, ELEMENT_TYPE_PATTERNS
from .utils import Utils
from math import sqrt


class Template:
Expand Down Expand Up @@ -382,17 +382,16 @@ def get_draw_text_with_max_length_coordinates(
width = font_size * length * 96 / 72
height = font_size * 96 / 72
c_half_width = (
float(element[TemplateCoreConstants().annotation_rectangle_key][0])
+ float(element[TemplateCoreConstants().annotation_rectangle_key][2])
float(element[TemplateCoreConstants().annotation_rectangle_key][0])
+ float(element[TemplateCoreConstants().annotation_rectangle_key][2])
) / 2
c_half_height = (
float(element[TemplateCoreConstants().annotation_rectangle_key][1])
+ float(element[TemplateCoreConstants().annotation_rectangle_key][3])
) / 2

return (
(c_half_width - width / 2 + c_half_width) / 2 - (
font_size * 0.5 * 96 / 72 if (comb is True and length % 2 == 0) else 0
),
(c_half_width - width / 2 + c_half_width) / 2
- (font_size * 0.5 * 96 / 72 if (comb is True and length % 2 == 0) else 0),
(c_half_height - height / 2 + c_half_height) / 2,
)
2 changes: 1 addition & 1 deletion PyPDFForm/core/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def draw_text(
text_to_draw = ""

if element.max_length is not None:
text_to_draw = text_to_draw[:element.max_length]
text_to_draw = text_to_draw[: element.max_length]

canv.setFont(element.font, element.font_size)
canv.setFillColorRGB(
Expand Down
15 changes: 11 additions & 4 deletions PyPDFForm/middleware/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,20 @@ def build_elements_v2(pdf_stream: bytes) -> Dict[str, "Element"]:
)

if _element.type == ElementType.text:
_element.max_length = TemplateCore().get_text_field_max_length(element)
if _element.max_length is not None and TemplateCore().is_text_field_comb(element):
_element.max_length = TemplateCore().get_text_field_max_length(
element
)
if (
_element.max_length is not None
and TemplateCore().is_text_field_comb(element)
):
_element.comb = True

if _element.max_length is not None:
_element.font_size = TemplateCore().font_size_for_text_field_with_max_length(
element, _element.max_length
_element.font_size = (
TemplateCore().font_size_for_text_field_with_max_length(
element, _element.max_length
)
)

results[key] = _element
Expand Down
9 changes: 7 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@ def sejda_template(pdf_samples):

@pytest.fixture
def sample_template_with_max_length_text_field(pdf_samples):
with open(os.path.join(pdf_samples, "sample_template_with_max_length_text_field.pdf"), "rb+") as f:
with open(
os.path.join(pdf_samples, "sample_template_with_max_length_text_field.pdf"),
"rb+",
) as f:
return f.read()


@pytest.fixture
def sample_template_with_comb_text_field(pdf_samples):
with open(os.path.join(pdf_samples, "sample_template_with_comb_text_field.pdf"), "rb+") as f:
with open(
os.path.join(pdf_samples, "sample_template_with_comb_text_field.pdf"), "rb+"
) as f:
return f.read()


Expand Down
44 changes: 31 additions & 13 deletions tests/functional/test_fill_max_length_text_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,28 @@

@pytest.fixture
def expected_directory():
return os.path.join(os.path.dirname(__file__), "..", "..", "pdf_samples", "max_length_text_field_related")
return os.path.join(
os.path.dirname(__file__),
"..",
"..",
"pdf_samples",
"max_length_text_field_related",
)


def test_fill_max_length_text_field_all_chars(
sample_template_with_max_length_text_field, expected_directory
):
with open(os.path.join(expected_directory, "max_length_text_field_all_chars.pdf"), "rb+") as f:
with open(
os.path.join(expected_directory, "max_length_text_field_all_chars.pdf"), "rb+"
) as f:
obj = PyPDFForm2(sample_template_with_max_length_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXXXXXXXX",
"Awesomeness": True,
"Gender": 0
"Gender": 0,
}
)

Expand All @@ -35,14 +43,16 @@ def test_fill_max_length_text_field_all_chars(
def test_fill_max_length_text_field_odd_chars(
sample_template_with_max_length_text_field, expected_directory
):
with open(os.path.join(expected_directory, "max_length_text_field_odd_chars.pdf"), "rb+") as f:
with open(
os.path.join(expected_directory, "max_length_text_field_odd_chars.pdf"), "rb+"
) as f:
obj = PyPDFForm2(sample_template_with_max_length_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXX",
"Awesomeness": True,
"Gender": 0
"Gender": 0,
}
)

Expand All @@ -55,14 +65,16 @@ def test_fill_max_length_text_field_odd_chars(
def test_fill_max_length_text_field_even_chars(
sample_template_with_max_length_text_field, expected_directory
):
with open(os.path.join(expected_directory, "max_length_text_field_even_chars.pdf"), "rb+") as f:
with open(
os.path.join(expected_directory, "max_length_text_field_even_chars.pdf"), "rb+"
) as f:
obj = PyPDFForm2(sample_template_with_max_length_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXX",
"Awesomeness": True,
"Gender": 0
"Gender": 0,
}
)

Expand All @@ -75,14 +87,16 @@ def test_fill_max_length_text_field_even_chars(
def test_fill_comb_text_field_all_chars(
sample_template_with_comb_text_field, expected_directory
):
with open(os.path.join(expected_directory, "comb_text_field_all_chars.pdf"), "rb+") as f:
with open(
os.path.join(expected_directory, "comb_text_field_all_chars.pdf"), "rb+"
) as f:
obj = PyPDFForm2(sample_template_with_comb_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXXXXXXXX",
"Awesomeness": True,
"Gender": 0
"Gender": 0,
}
)

Expand All @@ -95,14 +109,16 @@ def test_fill_comb_text_field_all_chars(
def test_fill_comb_text_field_odd_chars(
sample_template_with_comb_text_field, expected_directory
):
with open(os.path.join(expected_directory, "comb_text_field_odd_chars.pdf"), "rb+") as f:
with open(
os.path.join(expected_directory, "comb_text_field_odd_chars.pdf"), "rb+"
) as f:
obj = PyPDFForm2(sample_template_with_comb_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXX",
"Awesomeness": True,
"Gender": 0
"Gender": 0,
}
)

Expand All @@ -115,14 +131,16 @@ def test_fill_comb_text_field_odd_chars(
def test_fill_comb_text_field_even_chars(
sample_template_with_comb_text_field, expected_directory
):
with open(os.path.join(expected_directory, "comb_text_field_even_chars.pdf"), "rb+") as f:
with open(
os.path.join(expected_directory, "comb_text_field_even_chars.pdf"), "rb+"
) as f:
obj = PyPDFForm2(sample_template_with_comb_text_field).fill(
{
"FirstName": "John",
"MiddleName": "Joe",
"LastName": "XXXXXX",
"Awesomeness": True,
"Gender": 0
"Gender": 0,
}
)

Expand Down
13 changes: 9 additions & 4 deletions tests/unit/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,13 +495,18 @@ def test_build_elements_v2_sejda(sejda_template, sejda_data):
assert data_dict[k]


def test_build_elements_v2_with_comb_text_field(sample_template_with_max_length_text_field,
sample_template_with_comb_text_field):
result = TemplateMiddleware().build_elements_v2(sample_template_with_max_length_text_field)
def test_build_elements_v2_with_comb_text_field(
sample_template_with_max_length_text_field, sample_template_with_comb_text_field
):
result = TemplateMiddleware().build_elements_v2(
sample_template_with_max_length_text_field
)
assert result["LastName"].max_length == 8
assert result["LastName"].comb is None

result = TemplateMiddleware().build_elements_v2(sample_template_with_comb_text_field)
result = TemplateMiddleware().build_elements_v2(
sample_template_with_comb_text_field
)
assert result["LastName"].max_length == 7
assert result["LastName"].comb is True

Expand Down

0 comments on commit e39e086

Please sign in to comment.