From 258de53f55f0306400d7271a4c302cd37ab530de Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 30 Aug 2025 06:38:17 -0400 Subject: [PATCH] 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`. --- lib/matplotlib/backends/backend_pdf.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index ff351e301176..6682deffb00e 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -1180,13 +1180,11 @@ def embedTTFType3(font, characters, descriptor): 'Widths': widthsObject } - from encodings import cp1252 - # Make the "Widths" array def get_char_width(charcode): - s = ord(cp1252.decoding_table[charcode]) width = font.load_char( - s, flags=LoadFlags.NO_SCALE | LoadFlags.NO_HINTING).horiAdvance + charcode, + flags=LoadFlags.NO_SCALE | LoadFlags.NO_HINTING).horiAdvance return cvt(width) with warnings.catch_warnings(): # Ignore 'Required glyph missing from current font' warning @@ -2331,9 +2329,13 @@ def draw_tex(self, gc, x, y, s, prop, angle, *, mtext=None): self.draw_path(boxgc, path, mytrans, gc._rgb) def encode_string(self, s, fonttype): - if fonttype in (1, 3): - return s.encode('cp1252', 'replace') - return s.encode('utf-16be', 'replace') + match fonttype: + case 1: + return s.encode('cp1252', 'replace') + case 3: + return s.encode('latin-1', 'replace') + case _: + return s.encode('utf-16be', 'replace') def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): # docstring inherited