Skip to content

Commit

Permalink
BUG: Fix font specificier for FreeText annotation (#2893)
Browse files Browse the repository at this point in the history
Closes #1435.
  • Loading branch information
ssjkamei authored Oct 7, 2024
1 parent 96b46ad commit 8245fbc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pypdf/annotations/_markup_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
20 changes: 20 additions & 0 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,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
Expand Down

0 comments on commit 8245fbc

Please sign in to comment.