Skip to content

Commit bc93845

Browse files
authored
Merge pull request #1809 from wittejm/jw/auto-font-pdfs
pdf has font size zero, remove resize logic
2 parents 4f6ebfb + f72efcc commit bc93845

4 files changed

Lines changed: 10 additions & 55 deletions

File tree

439 KB
Binary file not shown.
439 KB
Binary file not shown.

src/backend/expungeservice/form_filling.py

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ class PDF:
482482
BUTTON_YES = PdfName("Yes")
483483
TEXT_TYPE = "/Tx"
484484
FONT_FAMILY = "TimesNewRoman"
485-
FONT_SIZE = "10"
486-
FONT_SIZE_SMALL = "6"
485+
FONT_SIZE = "0"
487486
DATE_FORMAT = "%b %-d, %Y"
488487
STR_CONNECTOR = "; "
489488

@@ -500,7 +499,6 @@ def fill_form(mapper: PDFFieldMapper, should_validate=False):
500499
def __init__(self, mapper: PDFFieldMapper):
501500
self.set_pdf(PdfReader(mapper.pdf_source_path))
502501
self.mapper = mapper
503-
self.shrunk_fields: Dict[str, str] = {}
504502
self.writer = PdfWriter()
505503

506504
def set_pdf(self, pdf: PdfReader):
@@ -539,16 +537,7 @@ def set_text_value(self, annotation, value):
539537
annotation.update(PdfDict(AP=""))
540538

541539
def set_font(self, annotation):
542-
x1, x2 = float(annotation.Rect[0]), float(annotation.Rect[2])
543-
max_chars = min((x2 - x1) * 0.3125, 90) # Times New Roman size 10; and 90 because the equation doesn't scale well to the longest field on the form, the Charges line in the non-Multnomah form
544-
num_chars = len(annotation.V) - 2 # minus parens
545-
font_size = self.FONT_SIZE
546-
547-
if num_chars > max_chars:
548-
font_size = self.FONT_SIZE_SMALL
549-
self.shrunk_fields[annotation.T] = annotation.V
550-
551-
annotation.DA = PdfString.encode(f"/{self.FONT_FAMILY} {font_size} Tf 0 g")
540+
annotation.DA = PdfString.encode(f"/{self.FONT_FAMILY} {self.FONT_SIZE} Tf 0 g")
552541

553542
def update_annotations(self):
554543
for annotation in self.annotations:
@@ -705,19 +694,14 @@ def _unify_sids(record_summary: RecordSummary) -> str:
705694
return ""
706695

707696
@staticmethod
708-
def _generate_warnings_text(shrunk_fields: Dict[str, str], mapper: PDFFieldMapper) -> Optional[str]:
697+
def _generate_warnings_text(mapper: PDFFieldMapper) -> Optional[str]:
709698
text = None
710699
warnings: List[str] = []
711700

712701
if mapper.get("(has_ineligible_charges)"):
713702
message = "This form will attempt to expunge a case in part. This is relatively rare, and thus these forms should be reviewed particularly carefully."
714703
warnings.append(message)
715704

716-
if shrunk_fields:
717-
for field_name, value in shrunk_fields.items():
718-
message = f'* The font size of "{value[1:-1]}" was shrunk to fit the bounding box of "{field_name[1:-1]}". An addendum might be required if it still doesn\'t fit.'
719-
warnings.append(message)
720-
721705
if warnings:
722706
text = "# Warnings from RecordSponge \n"
723707
text += "Do not submit this page to the District Attorney's office. \n \n"
@@ -784,7 +768,7 @@ def _create_and_write_pdf(
784768
source_data = UserInfo(**data)
785769

786770
pdf = FormFilling._create_pdf(source_data, validate_initial_pdf_state)
787-
warnings_text = FormFilling._generate_warnings_text(pdf.shrunk_fields, pdf.mapper)
771+
warnings_text = FormFilling._generate_warnings_text(pdf.mapper)
788772

789773
if warnings_text:
790774
pdf.add_text(warnings_text)

src/backend/tests/test_form_filling.py

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,6 @@ class TestWarningsGeneration:
250250
)
251251
partial_expungement_warning = "\\* This form will attempt to expunge a case in part. This is relatively rare, and thus these forms should be reviewed particularly carefully. \n"
252252

253-
def font_warning(self, field_name, value):
254-
return f'\\* * The font size of "{value[1:-1]}" was shrunk to fit the bounding box of "{field_name[1:-1]}". An addendum might be required if it still doesn\'t fit. \n'
255-
256253
@pytest.fixture
257254
def mapper_factory(self):
258255
def factory(has_ineligible_charges=False):
@@ -263,40 +260,16 @@ def factory(has_ineligible_charges=False):
263260

264261
return factory
265262

266-
@pytest.fixture
267-
def shrunk_fields(self):
268-
shrunk_fields: Dict[str, str] = {}
269-
return shrunk_fields
270-
271-
def test_no_warnings_generated_if_no_ineligible_charges_or_shrunk_fields(self, mapper_factory, shrunk_fields):
272-
warnings = FormFilling._generate_warnings_text(shrunk_fields, mapper_factory())
263+
def test_no_warnings_generated_if_no_ineligible_charges(self, mapper_factory):
264+
warnings = FormFilling._generate_warnings_text(mapper_factory())
273265
assert warnings is None
274266

275-
def test_warnings_generated_if_there_are_ineligible_charges(self, mapper_factory, shrunk_fields):
276-
expected = self.lead_warning
277-
expected += self.partial_expungement_warning
278-
279-
mapper = mapper_factory(has_ineligible_charges=True)
280-
warnings = FormFilling._generate_warnings_text(shrunk_fields, mapper)
281-
assert warnings == expected
282-
283-
def test_warnings_generated_if_there_are_shrunk_fields(self, mapper_factory, shrunk_fields):
284-
expected = self.lead_warning
285-
expected += self.font_warning("(foo)", "(foo value)")
286-
expected += self.font_warning("(bar)", "(bar value)")
287-
288-
shrunk_fields = {"(foo)": "(foo value)", "(bar)": "(bar value)"}
289-
warnings = FormFilling._generate_warnings_text(shrunk_fields, mapper_factory())
290-
assert warnings == expected
291-
292-
def test_warnings_generated_if_there_are_shrunk_fields_and_ineligible_charges(self, mapper_factory, shrunk_fields):
267+
def test_warnings_generated_if_there_are_ineligible_charges(self, mapper_factory):
293268
expected = self.lead_warning
294269
expected += self.partial_expungement_warning
295-
expected += self.font_warning("(foo)", "(foo value)")
296270

297271
mapper = mapper_factory(has_ineligible_charges=True)
298-
shrunk_fields = {"(foo)": "(foo value)"}
299-
warnings = FormFilling._generate_warnings_text(shrunk_fields, mapper)
272+
warnings = FormFilling._generate_warnings_text(mapper)
300273
assert warnings == expected
301274

302275

@@ -729,7 +702,7 @@ def test_arrest(self, pdf_factory: Callable, dismissed_charge_factory: Callable)
729702
assert_pdf_values(pdf_factory(charges), expected_values)
730703

731704
@patch("expungeservice.form_filling.PdfWriter")
732-
def test_font_shrinking_and_pdf_write_text(self, MockPdfWriter, pdf_factory: Mock, dismissed_charge_factory: Mock):
705+
def test_long_charge_name_and_pdf_write_text(self, MockPdfWriter, pdf_factory: Mock, dismissed_charge_factory: Mock):
733706
charge_name = (
734707
"A.............. Very.................... Long................. Name......................"
735708
+ "A.............. Very.................... Long................. Name......................"
@@ -755,9 +728,7 @@ def test_font_shrinking_and_pdf_write_text(self, MockPdfWriter, pdf_factory: Moc
755728
charge.name = charge_name
756729
pdf: PDF = pdf_factory([charge])
757730

758-
assert pdf.shrunk_fields.get("(Dismissed Charges)") == f"({charge_name})"
759-
assert len(pdf.shrunk_fields) == 1
760-
assert pdf.get_annotation_dict()["(Dismissed Charges)"].DA == "(/TimesNewRoman 6 Tf 0 g)"
731+
assert pdf.get_annotation_dict()["(Dismissed Charges)"].DA == "(/TimesNewRoman 0 Tf 0 g)"
761732
assert_pdf_values(pdf, expected_values)
762733

763734
assert not MockPdfWriter.return_value.addpages.called

0 commit comments

Comments
 (0)