From 4318fa048f40c8394db1099b015534d9551f2477 Mon Sep 17 00:00:00 2001 From: ryo kamei Date: Mon, 7 Oct 2024 01:54:02 +0900 Subject: [PATCH 1/3] BUG: The font list order of pypdf.annotations.FreeText is different(#1435) --- pypdf/annotations/_markup_annotations.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pypdf/annotations/_markup_annotations.py b/pypdf/annotations/_markup_annotations.py index 580b8bf58..5d7da5659 100644 --- a/pypdf/annotations/_markup_annotations.py +++ b/pypdf/annotations/_markup_annotations.py @@ -103,12 +103,17 @@ def __init__( self[NameObject("/Subtype")] = NameObject("/FreeText") self[NameObject("/Rect")] = RectangleObject(rect) + # Table 225 of the 1.7 reference ("CSS2 style attributes used in rich text strings") font_str = "font: " - if bold: - font_str = f"{font_str}bold " if italic: font_str = f"{font_str}italic " - font_str = f"{font_str}{font} {font_size}" + else: + font_str = f"{font_str}normal " + if bold: + font_str = f"{font_str}bold " + else: + font_str = f"{font_str}normal " + font_str = f"{font_str}{font_size} {font}" font_str = f"{font_str};text-align:left;color:#{font_color}" default_appearance_string = "" From c77a0f5c4f1d643a10ad14fb29bd2caa81f2a62e Mon Sep 17 00:00:00 2001 From: Ryo Kamei Date: Mon, 7 Oct 2024 10:39:58 +0900 Subject: [PATCH 2/3] Add tests --- tests/test_annotations.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_annotations.py b/tests/test_annotations.py index f6d14c5e4..6c8c83f6b 100644 --- a/tests/test_annotations.py +++ b/tests/test_annotations.py @@ -93,6 +93,24 @@ def test_text_annotation(pdf_file_path): def test_free_text_annotation(pdf_file_path): + free_text_annotation = FreeText( + text="Hello World", + rect=(0, 0, 0, 0), + ) + assert free_text_annotation["/DS"] == "font: normal normal 14pt Helvetica;text-align:left;color:#000000" + free_text_annotation = FreeText( + text="Hello World", + rect=(50, 550, 200, 650), + font="Arial", + bold=True, + italic=True, + font_size="20pt", + font_color="00ff00", + border_color=None, + background_color=None, + ) + assert free_text_annotation["/DS"] == "font: italic bold 20pt Arial;text-align:left;color:#00ff00" + # Arrange pdf_path = RESOURCE_ROOT / "crazyones.pdf" reader = PdfReader(pdf_path) From 423876d757598f29ec92c21418b93cdb027e9902 Mon Sep 17 00:00:00 2001 From: Ryo Kamei Date: Mon, 7 Oct 2024 16:37:27 +0900 Subject: [PATCH 3/3] Split the test --- tests/test_annotations.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/tests/test_annotations.py b/tests/test_annotations.py index 6c8c83f6b..c5df2328d 100644 --- a/tests/test_annotations.py +++ b/tests/test_annotations.py @@ -93,24 +93,6 @@ def test_text_annotation(pdf_file_path): def test_free_text_annotation(pdf_file_path): - free_text_annotation = FreeText( - text="Hello World", - rect=(0, 0, 0, 0), - ) - assert free_text_annotation["/DS"] == "font: normal normal 14pt Helvetica;text-align:left;color:#000000" - free_text_annotation = FreeText( - text="Hello World", - rect=(50, 550, 200, 650), - font="Arial", - bold=True, - italic=True, - font_size="20pt", - font_color="00ff00", - border_color=None, - background_color=None, - ) - assert free_text_annotation["/DS"] == "font: italic bold 20pt Arial;text-align:left;color:#00ff00" - # Arrange pdf_path = RESOURCE_ROOT / "crazyones.pdf" reader = PdfReader(pdf_path) @@ -150,6 +132,26 @@ def test_free_text_annotation(pdf_file_path): writer.write(fp) +def test_free_text_annotation__font_specifier(): + free_text_annotation = FreeText( + text="Hello World", + rect=(0, 0, 0, 0), + ) + assert free_text_annotation["/DS"] == "font: normal normal 14pt Helvetica;text-align:left;color:#000000" + free_text_annotation = FreeText( + text="Hello World", + rect=(50, 550, 200, 650), + font="Arial", + bold=True, + italic=True, + font_size="20pt", + font_color="00ff00", + border_color=None, + background_color=None, + ) + assert free_text_annotation["/DS"] == "font: italic bold 20pt Arial;text-align:left;color:#00ff00" + + def test_annotationdictionary(): a = AnnotationDictionary() a.flags = 123