File: annotations.py

package info (click to toggle)
fpdf2 2.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 114,352 kB
  • sloc: python: 50,410; sh: 133; makefile: 12
file content (211 lines) | stat: -rw-r--r-- 6,992 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Usage documentation at: <https://py-pdf.github.io/fpdf2/Annotations.html>
"""

import hashlib
from datetime import datetime
from typing import TYPE_CHECKING, Any, Optional, Sequence, Union

from .actions import Action
from .enums import (
    AnnotationFlag,
    AnnotationName,
    AssociatedFileRelationship,
    FileAttachmentAnnotationName,
)
from .syntax import (
    Destination,
    Name,
    PDFContentStream,
    PDFDate,
    PDFObject,
    PDFString,
    build_obj_dict,
    create_dictionary_string as pdf_dict,
    create_list_string as pdf_list,
    iobj_ref as pdf_ref,
)

if TYPE_CHECKING:
    from .encryption import StandardSecurityHandler

# cf. https://docs.verapdf.org/validation/pdfa-part1/#rule-653-2
DEFAULT_ANNOT_FLAGS = (AnnotationFlag.PRINT,)


class AnnotationMixin:
    def __init__(
        self,
        subtype: str,
        x: float,
        y: float,
        width: float,
        height: float,
        flags: tuple[AnnotationFlag | str, ...] = DEFAULT_ANNOT_FLAGS,
        contents: Optional[str] = None,
        dest: Optional[Destination | PDFString] = None,
        action: Optional[Action] = None,
        color: Optional[tuple[float, float, float]] = None,
        modification_time: Optional[datetime] = None,
        title: Optional[str] = None,
        quad_points: Optional[Sequence[float]] = None,
        border_width: float = 0,  # PDF readers support: displayed by Acrobat but not Sumatra
        name: Union[AnnotationName, FileAttachmentAnnotationName, None] = None,
        ink_list: Optional[tuple[float, ...]] = None,  # for ink annotations
        file_spec: Optional[Union["FileSpec", str]] = None,
        field_type: Optional[str] = None,
        value: Optional[str] = None,
        default_appearance: Optional[str] = None,  # for free text annotations
    ) -> None:
        self.type = Name("Annot")
        self.subtype = Name(subtype)
        self.rect = f"[{x:.2f} {y - height:.2f} {x + width:.2f} {y:.2f}]"
        self.border = f"[0 0 {border_width}]"
        self.f_t = Name(field_type) if field_type else None
        self.v = value
        self.f = sum(tuple(AnnotationFlag.coerce(flag) for flag in flags))
        self.contents = PDFString(contents, encrypt=True) if contents else None
        self.a = action
        self.dest = dest
        self.c = f"[{color[0]} {color[1]} {color[2]}]" if color else None
        self.t = PDFString(title, encrypt=True) if title else None
        self.m = PDFDate(modification_time, encrypt=True) if modification_time else None
        self.quad_points = (
            pdf_list([f"{quad_point:.2f}" for quad_point in quad_points])
            if quad_points
            else None
        )
        self.p = None  # must always be set before calling .serialize()
        self.name = name
        self.ink_list = (
            ("[" + pdf_list([f"{coord:.2f}" for coord in ink_list]) + "]")
            if ink_list
            else None
        )
        self.f_s = file_spec
        self.d_a = default_appearance


class PDFAnnotation(AnnotationMixin, PDFObject):
    "A PDF annotation that get serialized as an obj<</>>endobj block"

    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)


class AnnotationDict(AnnotationMixin):
    "A PDF annotation that get serialized as an inline <<dictionary>>"

    __slots__ = (  # RAM usage optimization
        "type",
        "subtype",
        "rect",
        "border",
        "f_t",
        "v",
        "f",
        "contents",
        "a",
        "dest",
        "c",
        "t",
        "quad_points",
        "p",
        "name",
        "ink_list",
        "f_s",
        "d_a",
    )

    def serialize(
        self,
        _security_handler: Optional["StandardSecurityHandler"] = None,
        _obj_id: Optional[int] = None,
    ) -> str:
        obj_dict = build_obj_dict(
            {key: getattr(self, key) for key in dir(self)},
            _security_handler=_security_handler,
            _obj_id=_obj_id,
        )
        return pdf_dict(obj_dict)

    def __repr__(self) -> str:
        keys = [key for key in dir(self) if not key.startswith("__")]
        d = {key: getattr(self, key) for key in keys}
        d = {key: value for key, value in d.items() if not callable(value)}
        return f"AnnotationDict(**{d})"


class PDFEmbeddedFile(PDFContentStream):
    def __init__(
        self,
        basename: str,
        contents: bytes,
        desc: str = "",
        creation_date: Optional[datetime] = None,
        modification_date: Optional[datetime] = None,
        mime_type: Optional[str] = None,
        af_relationship: Optional[AssociatedFileRelationship] = None,
        compress: bool = False,
        checksum: bool = False,
    ):
        super().__init__(contents=contents, compress=compress)
        self.type = Name("EmbeddedFile")
        params: dict[str, object] = {"/Size": len(contents)}
        if creation_date:
            params["/CreationDate"] = PDFDate(creation_date, with_tz=True).serialize()
        if modification_date:
            params["/ModDate"] = PDFDate(modification_date, with_tz=True).serialize()
        if checksum:
            file_hash = hashlib.new("md5", usedforsecurity=False)
            file_hash.update(self._contents)
            hash_hex = file_hash.hexdigest()
            params["/CheckSum"] = f"<{hash_hex}>"
        if mime_type:
            self.subtype = Name(mime_type)
        self.params = pdf_dict(params)
        self._basename: str = basename  # private so that it does not get serialized
        self._desc: str = desc  # private so that it does not get serialized
        self._globally_enclosed: bool = True
        self._af_relationship: Optional[AssociatedFileRelationship] = af_relationship
        self._file_spec: Optional[FileSpec] = None

    def globally_enclosed(self) -> bool:
        return self._globally_enclosed

    def set_globally_enclosed(self, value: bool) -> None:
        self._globally_enclosed = value

    def basename(self) -> str:
        return self._basename

    def file_spec(self) -> "FileSpec":
        if not self._file_spec:
            self._file_spec = FileSpec(
                self, self._basename, self._desc, self._af_relationship
            )
        return self._file_spec


class FileSpec(PDFObject):

    def __init__(
        self,
        embedded_file: PDFEmbeddedFile,
        basename: str,
        desc: Optional[str] = None,
        af_relationship: Optional[AssociatedFileRelationship] = None,
    ):
        super().__init__()
        self.type = Name("Filespec")
        self.f = PDFString(basename)
        self.u_f = PDFString(basename)
        if desc:
            self.desc = PDFString(desc)
        if af_relationship:
            self.a_f_relationship = Name(af_relationship.value)
        self._embedded_file = embedded_file

    @property
    def e_f(self) -> str:
        return pdf_dict({"/F": pdf_ref(self._embedded_file.id)})