Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pdf_annotate/annotations/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pdfrw import PdfDict
from pdfrw import PdfName
from pdfrw import PdfString
from pdfrw import PdfObject

from pdf_annotate.annotations.base import _make_border_dict
from pdf_annotate.annotations.base import Annotation
Expand Down Expand Up @@ -42,6 +43,31 @@
'Helvetica.ttf',
)

class NoteText(Annotation):
subtype = 'Text'

def make_rect(self):
L = self._location
return [L.x1, L.y1, L.x2, L.y2]

def add_additional_pdf_object_data(self, obj):
obj.Contents = self._appearance.content
obj.C = self._appearance.fill
# NOTE: See PDF "Text" Annot section in Ref Document for valid values.
obj.Name = PdfName(self._appearance.text_name)
obj.F = 0
# NOTE: We currently need to override what gets inserted by default.
obj.AP = ContentStream([]).resolve()
obj.Open = PdfObject('true')

def make_default_appearance(self):
obj.C = [0.5, 0.5, 0.5]
return ContentStream([])

def make_appearance_stream(self):
# Not needed. Necessary to satisfy Annotation interface
return ContentStream([])


class FreeText(Annotation):
"""FreeText annotation. Right now, we only support writing text in the
Expand Down
2 changes: 2 additions & 0 deletions pdf_annotate/annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from pdf_annotate.annotations.rect import Circle
from pdf_annotate.annotations.rect import Square
from pdf_annotate.annotations.text import FreeText
from pdf_annotate.annotations.text import NoteText
from pdf_annotate.config.metadata import Metadata
from pdf_annotate.config.metadata import UNSET
from pdf_annotate.graphics import ContentStream
Expand All @@ -40,6 +41,7 @@
'polyline': Polyline,
'ink': Ink,
'text': FreeText,
'note': NoteText,
'image': Image,
}

Expand Down
2 changes: 2 additions & 0 deletions pdf_annotate/config/appearance.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pdf_annotate.config.constants import BLACK
from pdf_annotate.config.constants import DEFAULT_BORDER_STYLE
from pdf_annotate.config.constants import DEFAULT_CONTENT
from pdf_annotate.config.constants import DEFAULT_TEXT_ANNOT_NAME
from pdf_annotate.config.constants import DEFAULT_FONT_SIZE
from pdf_annotate.config.constants import DEFAULT_LINE_SPACING
from pdf_annotate.config.constants import DEFAULT_STROKE_WIDTH
Expand Down Expand Up @@ -71,6 +72,7 @@ class Appearance(object):
text_baseline = Enum(ALLOWED_BASELINES, default=TEXT_BASELINE_MIDDLE)
line_spacing = Number(default=DEFAULT_LINE_SPACING, validator=positive)
wrap_text = Boolean(default=True)
text_name = String(default=DEFAULT_TEXT_ANNOT_NAME)

# Image attributes
image = String(default=None)
Expand Down
1 change: 1 addition & 0 deletions pdf_annotate/config/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
DEFAULT_CONTENT = ''
DEFAULT_FONT_SIZE = 12
DEFAULT_LINE_SPACING = 1.2
DEFAULT_TEXT_ANNOT_NAME = 'Note'

DEFAULT_STROKE_WIDTH = 1
DEFAULT_BORDER_STYLE = 'S'
Expand Down
20 changes: 19 additions & 1 deletion tests/annotations/test_text.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from pdf_annotate.annotations.text import FreeText, HELVETICA_PATH
from pdf_annotate.annotations.text import FreeText, NoteText, HELVETICA_PATH
from pdf_annotate.config.appearance import Appearance
from pdf_annotate.config.constants import PDF_ANNOTATOR_FONT
from pdf_annotate.config.location import Location
Expand Down Expand Up @@ -31,6 +31,24 @@ def test_pdf_object(self):
assert obj.AP.N.BBox == [x1, y1, x2, y2]
assert obj.AP.N.Matrix == translate(-x1, -y1)


def test_text_note_pdf_object(self):
x1, y1, x2, y2 = 10, 20, 100, 200
annotation = NoteText(
Location(x1=x1, y1=y1, x2=x2, y2=y2, page=0),
Appearance(
fill=[0.4, 0, 0],
content='Hi',
text_name='Comment',
),
)
obj = annotation.as_pdf_object(identity(), page=None)
assert obj.Rect == [x1, y1, x2, y2]
assert obj.C == [0.4, 0, 0]
assert obj.Name == '/Comment'
assert obj.Contents == 'Hi'


def test_make_composite_font(self):
font = FreeText.make_composite_font_object(HELVETICA_PATH)
keys = font.keys()
Expand Down
7 changes: 7 additions & 0 deletions tests/end_to_end/test_annotate_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ def _add_text_annotations(self, a, y1=220, y2=300):
Location(x1=x, y1=y1, x2=(x + 40), y2=y2, page=0),
appearance,
)
# TODO: Proper way to enable?
# a.add_annotation(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks right. You'd have to run the test and then actually open the PDF and make sure the annotation appears as expected.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok great! I'll test it out and will post an update.

# 'note',
# Location(x1=(x + 10), y1=y1, x2=(x + 40), y2=y2, page=0),
# appearance,
# )


def _add_rounded_rectangles(self, a):
"""Add a few rounded rectangles with different border radii."""
Expand Down