Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STY: Rename local annotation variables #2980

Merged
merged 8 commits into from
Dec 3, 2024
Merged
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
77 changes: 38 additions & 39 deletions pypdf/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,16 +873,16 @@ def append_pages_from_reader(
def _update_field_annotation(
self,
field: DictionaryObject,
anno: DictionaryObject,
annotation: DictionaryObject,
font_name: str = "",
font_size: float = -1,
) -> None:
# Calculate rectangle dimensions
_rct = cast(RectangleObject, anno[AA.Rect])
_rct = cast(RectangleObject, annotation[AA.Rect])
rct = RectangleObject((0, 0, abs(_rct[2] - _rct[0]), abs(_rct[3] - _rct[1])))

# Extract font information
da = anno.get_inherited(
da = annotation.get_inherited(
AA.DA,
cast(DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]).get(
AA.DA, None
Expand Down Expand Up @@ -917,7 +917,7 @@ def _update_field_annotation(
DictionaryObject,
cast(
DictionaryObject,
anno.get_inherited(
annotation.get_inherited(
"/DR",
cast(
DictionaryObject, self.root_object[CatalogDictionary.ACRO_FORM]
Expand All @@ -942,7 +942,7 @@ def _update_field_annotation(
font_subtype, _, font_encoding, font_map = build_char_map_from_dict(
200, font_res
)
try: # get rid of width stored in -1 key
try: # remove width stored in -1 key
del font_map[-1]
except KeyError:
pass
Expand All @@ -963,14 +963,14 @@ def _update_field_annotation(
# Retrieve field text and selected values
field_flags = field.get(FA.Ff, 0)
if field.get(FA.FT, "/Tx") == "/Ch" and field_flags & FA.FfBits.Combo == 0:
txt = "\n".join(anno.get_inherited(FA.Opt, []))
txt = "\n".join(annotation.get_inherited(FA.Opt, []))
sel = field.get("/V", [])
if not isinstance(sel, list):
sel = [sel]
else: # /Tx
txt = field.get("/V", "")
sel = []
# Escape parentheses (pdf 1.7 reference, table 3.2 Literal Strings)
# Escape parentheses (PDF 1.7 reference, table 3.2, Literal Strings)
txt = txt.replace("\\", "\\\\").replace("(", r"\(").replace(")", r"\)")
# Generate appearance stream
ap_stream = generate_appearance_stream(
Expand All @@ -987,8 +987,8 @@ def _update_field_annotation(
"/Length": 0,
}
)
if AA.AP in anno:
for k, v in cast(DictionaryObject, anno[AA.AP]).get("/N", {}).items():
if AA.AP in annotation:
for k, v in cast(DictionaryObject, annotation[AA.AP]).get("/N", {}).items():
if k not in {"/BBox", "/Length", "/Subtype", "/Type", "/Filter"}:
dct[k] = v

Expand All @@ -1005,16 +1005,16 @@ def _update_field_annotation(
)
}
)
if AA.AP not in anno:
anno[NameObject(AA.AP)] = DictionaryObject(
if AA.AP not in annotation:
annotation[NameObject(AA.AP)] = DictionaryObject(
{NameObject("/N"): self._add_object(dct)}
)
elif "/N" not in cast(DictionaryObject, anno[AA.AP]):
cast(DictionaryObject, anno[NameObject(AA.AP)])[
elif "/N" not in cast(DictionaryObject, annotation[AA.AP]):
cast(DictionaryObject, annotation[NameObject(AA.AP)])[
NameObject("/N")
] = self._add_object(dct)
else: # [/AP][/N] exists
n = anno[AA.AP]["/N"].indirect_reference.idnum # type: ignore
n = annotation[AA.AP]["/N"].indirect_reference.idnum # type: ignore
self._objects[n - 1] = dct
dct.indirect_reference = IndirectObject(n, 0, self)

Expand Down Expand Up @@ -1071,61 +1071,60 @@ def update_page_form_field_values(
if PG.ANNOTS not in page:
logger_warning("No fields to update on this page", __name__)
return
for writer_annot in page[PG.ANNOTS]: # type: ignore
writer_annot = cast(DictionaryObject, writer_annot.get_object())
if writer_annot.get("/Subtype", "") != "/Widget":
for annotation in page[PG.ANNOTS]: # type: ignore
annotation = cast(DictionaryObject, annotation.get_object())
if annotation.get("/Subtype", "") != "/Widget":
continue
if "/FT" in writer_annot and "/T" in writer_annot:
writer_parent_annot = writer_annot
if "/FT" in annotation and "/T" in annotation:
parent_annotation = annotation
else:
writer_parent_annot = writer_annot.get(
parent_annotation = annotation.get(
PG.PARENT, DictionaryObject()
).get_object()

for field, value in fields.items():
if not (
self._get_qualified_field_name(writer_parent_annot) == field
or writer_parent_annot.get("/T", None) == field
self._get_qualified_field_name(parent_annotation) == field
or parent_annotation.get("/T", None) == field
):
continue
if (
writer_parent_annot.get("/FT", None) == "/Ch"
and "/I" in writer_parent_annot
parent_annotation.get("/FT", None) == "/Ch"
and "/I" in parent_annotation
):
del writer_parent_annot["/I"]
del parent_annotation["/I"]
if flags:
writer_annot[NameObject(FA.Ff)] = NumberObject(flags)
annotation[NameObject(FA.Ff)] = NumberObject(flags)
if isinstance(value, list):
lst = ArrayObject(TextStringObject(v) for v in value)
writer_parent_annot[NameObject(FA.V)] = lst
parent_annotation[NameObject(FA.V)] = lst
elif isinstance(value, tuple):
writer_annot[NameObject(FA.V)] = TextStringObject(
annotation[NameObject(FA.V)] = TextStringObject(
value[0],
)
else:
writer_parent_annot[NameObject(FA.V)] = TextStringObject(value)
if writer_parent_annot.get(FA.FT) in ("/Btn"):
# case of Checkbox button (no /FT found in Radio widgets
parent_annotation[NameObject(FA.V)] = TextStringObject(value)
if parent_annotation.get(FA.FT) in ("/Btn"):
# Checkbox button (no /FT found in Radio widgets)
v = NameObject(value)
if v not in writer_annot[NameObject(AA.AP)][NameObject("/N")]:
if v not in annotation[NameObject(AA.AP)][NameObject("/N")]:
v = NameObject("/Off")
# other cases will be updated through the for loop
writer_annot[NameObject(AA.AS)] = v
annotation[NameObject(AA.AS)] = v
elif (
writer_parent_annot.get(FA.FT) == "/Tx"
or writer_parent_annot.get(FA.FT) == "/Ch"
parent_annotation.get(FA.FT) == "/Tx"
or parent_annotation.get(FA.FT) == "/Ch"
):
# textbox
if isinstance(value, tuple):
self._update_field_annotation(
writer_parent_annot, writer_annot, value[1], value[2]
parent_annotation, annotation, value[1], value[2]
)
else:
self._update_field_annotation(writer_parent_annot, writer_annot)
self._update_field_annotation(parent_annotation, annotation)
elif (
writer_annot.get(FA.FT) == "/Sig"
annotation.get(FA.FT) == "/Sig"
): # deprecated # not implemented yet
# signature
logger_warning("Signature forms not implemented yet", __name__)

def reattach_fields(
Expand Down
Loading