Skip to content
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
4 changes: 2 additions & 2 deletions src/pdf_edit_engine/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ def delete_annotation(
if "/Annots" not in page_obj:
_save_pdf(pdf, out)
return
annots_list: list[Any] = list(page_obj["/Annots"]) # type: ignore[call-overload]
annots_list: list[Any] = list(page_obj["/Annots"])
if 0 <= annot.index < len(annots_list):
annots_list.pop(annot.index)
if annots_list:
page_obj["/Annots"] = pdf.make_indirect(pikepdf.Array(annots_list))
else:
del page_obj["/Annots"] # type: ignore[operator]
del page_obj["/Annots"]
_save_pdf(pdf, out)


Expand Down
4 changes: 2 additions & 2 deletions src/pdf_edit_engine/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def _init_widths_bounds(self, font_dict: pikepdf.Dictionary) -> None:
# an explicit width when first_char + i == b for some i in
# range(len(widths)) AND widths[i] is non-zero (zero typically
# means the slot is reserved but unmapped).
widths_list = list(widths) # type: ignore[call-overload]
widths_list = list(widths)
self._widths_keys = frozenset(
self._first_char + i for i, w in enumerate(widths_list) if float(w) != 0.0
)
Expand Down Expand Up @@ -506,7 +506,7 @@ def _init_custom(self, encoding_dict: pikepdf.Dictionary) -> None:
if "/Differences" in encoding_dict:
diffs = encoding_dict["/Differences"]
code = 0
for item in list(diffs): # type: ignore[call-overload]
for item in list(diffs):
item_str = str(item)
if not item_str.startswith("/"):
code = int(item)
Expand Down
18 changes: 9 additions & 9 deletions src/pdf_edit_engine/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def _get_font_objects(

if subtype == "/Type0":
desc_fonts = font_obj.get("/DescendantFonts")
if desc_fonts is None or len(list(desc_fonts)) == 0: # type: ignore[call-overload]
if desc_fonts is None or len(list(desc_fonts)) == 0:
msg = f"Type0 font {font_name} has no DescendantFonts"
raise FontNotFoundError(msg)
cid_font = desc_fonts[0]
Expand Down Expand Up @@ -944,7 +944,7 @@ def _append_w_entries(

existing: list[object] = []
if "/W" in cid_font:
existing = list(cid_font["/W"]) # type: ignore[call-overload]
existing = list(cid_font["/W"])

for cid, width in sorted(deduped.items()):
existing.append(cid)
Expand Down Expand Up @@ -2292,7 +2292,7 @@ def _extend_simple_tier_one_five(
diffs_obj = enc_pre.get("/Differences")
if diffs_obj is not None:
cur = 0
for item in list(diffs_obj): # type: ignore[call-overload]
for item in list(diffs_obj):
s = str(item)
if not s.startswith("/"):
cur = int(item)
Expand All @@ -2302,7 +2302,7 @@ def _extend_simple_tier_one_five(

raw_widths: list[object] = []
if "/Widths" in font_dict:
raw_widths = list(font_dict["/Widths"]) # type: ignore[call-overload]
raw_widths = list(font_dict["/Widths"])
widths_pre: list[float] = [float(w) for w in raw_widths] # type: ignore[arg-type]

# Per-char categorization with dedup. Prior code passed
Expand Down Expand Up @@ -2456,7 +2456,7 @@ def _used_bytes_in_encoding(font_dict: pikepdf.Object) -> set[int]:
enc = font_dict.get("/Encoding")
if isinstance(enc, pikepdf.Dictionary) and "/Differences" in enc:
cur = 0
for item in list(enc["/Differences"]): # type: ignore[call-overload]
for item in list(enc["/Differences"]):
s = str(item)
if not s.startswith("/"):
cur = int(item)
Expand Down Expand Up @@ -2531,7 +2531,7 @@ def _extend_simple_encoding(
)
enc = font_dict["/Encoding"]

diffs: list[object] = list(enc.get("/Differences") or []) # type: ignore[arg-type]
diffs: list[object] = list(enc.get("/Differences") or [])
# PDF /Differences format: [byte_start /name1 /name2 ... byte_start2 /name3 ...]
# Consecutive bytes can share a single byte_start prefix. We emit
# one prefix per assignment for simplicity (no correctness risk;
Expand Down Expand Up @@ -2585,7 +2585,7 @@ def _extend_simple_widths(

raw_widths: list[object] = []
if "/Widths" in font_dict:
raw_widths = list(font_dict["/Widths"]) # type: ignore[call-overload]
raw_widths = list(font_dict["/Widths"])
widths: list[float] = [float(w) for w in raw_widths] # type: ignore[arg-type]

# Sort assignments by byte to handle the 127-skip case (allocation
Expand Down Expand Up @@ -2666,7 +2666,7 @@ def _build_simple_effective_byte_to_unicode(font_dict: pikepdf.Object) -> dict[i
diffs_obj = enc.get("/Differences")
if diffs_obj is not None:
cur = 0
for item in list(diffs_obj): # type: ignore[call-overload]
for item in list(diffs_obj):
s = str(item)
if not s.startswith("/"):
cur = int(item)
Expand Down Expand Up @@ -2719,7 +2719,7 @@ def _heal_simple_widths(

raw_widths: list[object] = []
if "/Widths" in font_dict:
raw_widths = list(font_dict["/Widths"]) # type: ignore[call-overload]
raw_widths = list(font_dict["/Widths"])
widths: list[float] = [float(w) for w in raw_widths] # type: ignore[arg-type]

for byte_slot, _glyph_name, width in healed:
Expand Down
12 changes: 6 additions & 6 deletions src/pdf_edit_engine/structural.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def _is_annotation_orphaned(annot: pikepdf.Object, new_lower: str) -> bool:
if not new_lower:
return False
a_dict = annot.get("/A")
uri = str(a_dict.get("/URI", "")) if a_dict else "" # type: ignore[call-overload]
uri = str(a_dict.get("/URI", "")) if a_dict else ""
if not uri:
return False
path = uri.rstrip("/").rsplit("/", 1)[-1]
Expand Down Expand Up @@ -613,13 +613,13 @@ def _remove_orphaned_annotations(
annots_obj = page_obj.get("/Annots")
if not annots_obj:
return
annots: list[pikepdf.Object] = list(annots_obj) # type: ignore[call-overload]
annots: list[pikepdf.Object] = list(annots_obj)
new_lower = new_text.lower()
kept: list[pikepdf.Object] = []
removed = False
for annot in annots:
try:
rect = [float(r) for r in annot["/Rect"]] # type: ignore[attr-defined]
rect = [float(r) for r in annot["/Rect"]]
except (KeyError, TypeError):
kept.append(annot)
continue
Expand Down Expand Up @@ -661,7 +661,7 @@ def _sync_annotations_in_bbox(
annots_obj = page_obj.get("/Annots")
if not annots_obj:
return
for annot in list(annots_obj): # type: ignore[call-overload]
for annot in list(annots_obj):
try:
rect = [float(r) for r in annot["/Rect"]]
except (KeyError, TypeError):
Expand Down Expand Up @@ -846,7 +846,7 @@ def _shift_content_below_inplace(
annots_key = pikepdf.Name("/Annots")
if annots_key in page_obj:
try:
annots: list[Any] = list(page_obj[annots_key]) # type: ignore[call-overload]
annots: list[Any] = list(page_obj[annots_key])
for annot_ref in annots:
annot = annot_ref.resolve() if hasattr(annot_ref, "resolve") else annot_ref
rect_key = pikepdf.Name("/Rect")
Expand Down Expand Up @@ -2478,7 +2478,7 @@ def delete_block(
annots_key = pikepdf.Name("/Annots")
if annots_key in page_obj:
try:
annots: list[Any] = list(page_obj[annots_key]) # type: ignore[call-overload]
annots: list[Any] = list(page_obj[annots_key])
kept: list[Any] = []
for annot_ref in annots:
annot = annot_ref.resolve() if hasattr(annot_ref, "resolve") else annot_ref
Expand Down
2 changes: 1 addition & 1 deletion src/pdf_edit_engine/widths.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def parse_cid_widths(cid_font: pikepdf.Dictionary) -> dict[int, float]:
if "/W" not in cid_font:
return widths
w_array: pikepdf.Array = cid_font["/W"] # type: ignore[assignment]
w_items: list[pikepdf.Object] = list(w_array) # type: ignore[call-overload]
w_items: list[pikepdf.Object] = list(w_array)
i = 0
while i < len(w_items):
cid_start = int(w_items[i])
Expand Down
2 changes: 1 addition & 1 deletion src/pdf_edit_engine/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def fill_form(pdf_path: str, field_values: dict[str, str], output_path: str) ->
raise PDFEditError(msg)

def _fill_fields(fields: pikepdf.Object) -> None:
for field in list(fields): # type: ignore[call-overload]
for field in list(fields):
name = str(field.get("/T", ""))
if name in field_values:
field["/V"] = pikepdf.String(field_values[name])
Expand Down
6 changes: 5 additions & 1 deletion tests/_identity_h_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ def text_advance(text: str, size: float) -> float:
"/ItalicAngle": 0,
"/Ascent": round(full["OS/2"].sTypoAscender * 1000 / units_per_em),
"/Descent": round(full["OS/2"].sTypoDescender * 1000 / units_per_em),
"/CapHeight": round(full["OS/2"].sCapHeight * 1000 / units_per_em),
"/CapHeight": round(
getattr(full["OS/2"], "sCapHeight", full["OS/2"].sTypoAscender)
* 1000
/ units_per_em
),
"/StemV": 80,
"/FontFile2": font_stream,
}
Expand Down
6 changes: 5 additions & 1 deletion tests/_simple_font_fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ def _build_simple_winansi_pdf(
"/ItalicAngle": 0,
"/Ascent": round(full["OS/2"].sTypoAscender * 1000 / units_per_em),
"/Descent": round(full["OS/2"].sTypoDescender * 1000 / units_per_em),
"/CapHeight": round(full["OS/2"].sCapHeight * 1000 / units_per_em),
"/CapHeight": round(
getattr(full["OS/2"], "sCapHeight", full["OS/2"].sTypoAscender)
* 1000
/ units_per_em
),
"/StemV": 80,
"/FontFile2": font_stream,
}
Expand Down
4 changes: 3 additions & 1 deletion tests/corpus_builders/_truetype_assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def _subset_truetype(ttf_path: Path, corpus: str) -> _SubsetData:
bbox_1000 = [round(v * 1000 / units_per_em) for v in bbox]
ascent_1000 = round(full["OS/2"].sTypoAscender * 1000 / units_per_em)
descent_1000 = round(full["OS/2"].sTypoDescender * 1000 / units_per_em)
cap_height_1000 = round(full["OS/2"].sCapHeight * 1000 / units_per_em)
cap_height_1000 = round(
getattr(full["OS/2"], "sCapHeight", full["OS/2"].sTypoAscender) * 1000 / units_per_em
)
finally:
full.close()

Expand Down
4 changes: 3 additions & 1 deletion tests/corpus_builders/no_tounicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def _build_consistent_subset(ttf_path: Path, *, pua: bool) -> _ConsistentSubset:
bbox_1000 = [round(v * 1000 / units_per_em) for v in bbox]
ascent_1000 = round(full["OS/2"].sTypoAscender * 1000 / units_per_em)
descent_1000 = round(full["OS/2"].sTypoDescender * 1000 / units_per_em)
cap_height_1000 = round(full["OS/2"].sCapHeight * 1000 / units_per_em)
cap_height_1000 = round(
getattr(full["OS/2"], "sCapHeight", full["OS/2"].sTypoAscender) * 1000 / units_per_em
)
finally:
full.close()

Expand Down
6 changes: 5 additions & 1 deletion tests/generate_complex_corpus.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,11 @@ def _encode_line(text: str) -> bytes:
"/ItalicAngle": 0,
"/Ascent": ascent_1000,
"/Descent": descent_1000,
"/CapHeight": round(font["OS/2"].sCapHeight * 1000 / units_per_em),
"/CapHeight": round(
getattr(font["OS/2"], "sCapHeight", font["OS/2"].sTypoAscender)
* 1000
/ units_per_em
),
"/StemV": 80,
"/FontFile2": font_stream,
}
Expand Down