Skip to content

Commit 8b22e7a

Browse files
committed
pdf: Simplify Type 3 font character encoding
For a Type 3 font, its encoding is entirely defined by its `Encoding` dictionary (which we create), so there's no reason to use a specific encoding like `cp1252`. Instead, switch to Latin-1, which corresponds exactly to the first 256 character codes in Unicode, and can be mapped directly with `ord`.
1 parent fea9209 commit 8b22e7a

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,13 +1179,11 @@ def embedTTFType3(font, glyphs, descriptor):
11791179
'Widths': widthsObject
11801180
}
11811181

1182-
from encodings import cp1252
1183-
11841182
# Make the "Widths" array
11851183
def get_char_width(charcode):
1186-
s = ord(cp1252.decoding_table[charcode])
11871184
width = font.load_char(
1188-
s, flags=LoadFlags.NO_SCALE | LoadFlags.NO_HINTING).horiAdvance
1185+
charcode,
1186+
flags=LoadFlags.NO_SCALE | LoadFlags.NO_HINTING).horiAdvance
11891187
return cvt(width)
11901188
with warnings.catch_warnings():
11911189
# Ignore 'Required glyph missing from current font' warning
@@ -2325,9 +2323,13 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None):
23252323
self.draw_path(boxgc, path, mytrans, gc._rgb)
23262324

23272325
def encode_string(self, s, fonttype):
2328-
if fonttype in (1, 3):
2329-
return s.encode('cp1252', 'replace')
2330-
return s.encode('utf-16be', 'replace')
2326+
match fonttype:
2327+
case 1:
2328+
return s.encode('cp1252', 'replace')
2329+
case 3:
2330+
return s.encode('latin-1', 'replace')
2331+
case _:
2332+
return s.encode('utf-16be', 'replace')
23312333

23322334
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23332335
# docstring inherited

0 commit comments

Comments
 (0)