Skip to content

Commit 005ac44

Browse files
committed
ROB: AppearanceStream: Remove -1 key from font.character_map
This patch adds back some code that got removed earlier and, at that time, did not see any test coverage. With new code that enables adding fonts, I've finally understood that, in some cases, a -1 key will be added to font.character_map. This will cause an encoding failure when generating font_glyph_byte_map.
1 parent 1d8d97a commit 005ac44

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

pypdf/generic/_appearance_stream.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,14 @@ def __init__(
393393
__name__
394394
)
395395

396-
font_glyph_byte_map: dict[str, bytes]
396+
font_glyph_byte_map: dict[str, bytes] = {}
397397
if isinstance(font.encoding, str):
398-
font_glyph_byte_map = {
399-
v: k.encode(font.encoding) for k, v in font.character_map.items()
400-
}
398+
try: # Remove -1 key that does not map a character
399+
del font.character_map[-1]
400+
except KeyError:
401+
pass
402+
for k, v in font.character_map.items():
403+
font_glyph_byte_map[v] = k.encode(font.encoding)
401404
else:
402405
font_glyph_byte_map = {v: bytes((k,)) for k, v in font.encoding.items()}
403406
font_encoding_rev = {v: bytes((k,)) for k, v in font.encoding.items()}

0 commit comments

Comments
 (0)