Skip to content

Commit dbf8c17

Browse files
committed
Clarify fonttype switch in backend_pdf.
Make it clearer that switches are only over 3 possible fonttypes (1, 3, 42). In draw_text, the logic is easier to follow if one directly switches over rcParams['pdf.use14corefonts']; this requires putting the url handling at the end, which doesn't matter.
1 parent 4efa3c0 commit dbf8c17

File tree

1 file changed

+13
-25
lines changed

1 file changed

+13
-25
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,13 +2288,8 @@ def _encode_glyphs(self, subset, fonttype):
22882288
return b''.join(glyph.to_bytes(2, 'big') for glyph in subset)
22892289

22902290
def encode_string(self, s, fonttype):
2291-
match fonttype:
2292-
case 1:
2293-
return s.encode('cp1252', 'replace')
2294-
case 3:
2295-
return s.encode('latin-1', 'replace')
2296-
case _:
2297-
return s.encode('utf-16be', 'replace')
2291+
encoding = {1: 'cp1252', 3: 'latin-1', 42: 'utf-16be'}[fonttype]
2292+
return s.encode(encoding, 'replace')
22982293

22992294
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23002295
# docstring inherited
@@ -2312,29 +2307,13 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23122307
else:
23132308
features = language = None
23142309

2310+
# For Type-1 fonts, emit the whole string at once without manual kerning.
23152311
if mpl.rcParams['pdf.use14corefonts']:
23162312
font = self._get_font_afm(prop)
2317-
fonttype = 1
2318-
else:
2319-
font = self._get_font_ttf(prop)
2320-
fonttype = mpl.rcParams['pdf.fonttype']
2321-
2322-
if gc.get_url() is not None:
2323-
font.set_text(s, features=features, language=language)
2324-
width, height = font.get_width_height()
2325-
self.file._annotations[-1][1].append(_get_link_annotation(
2326-
gc, x, y, width / 64, height / 64, angle))
2327-
2328-
# If fonttype is neither 3 nor 42, emit the whole string at once
2329-
# without manual kerning.
2330-
if fonttype not in [3, 42]:
2331-
if not mpl.rcParams['pdf.use14corefonts']:
2332-
self.file._character_tracker.track(font, s,
2333-
features=features, language=language)
23342313
self.file.output(Op.begin_text,
23352314
self.file.fontName(prop), fontsize, Op.selectfont)
23362315
self._setup_textpos(x, y, angle)
2337-
self.file.output(self.encode_string(s, fonttype),
2316+
self.file.output(self.encode_string(s, fonttype=1),
23382317
Op.show, Op.end_text)
23392318

23402319
# A sequence of characters is broken into multiple chunks. The chunking
@@ -2350,6 +2329,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23502329
# Each chunk is emitted with the regular text show command (TJ) with appropriate
23512330
# kerning between chunks.
23522331
else:
2332+
font = self._get_font_ttf(prop)
2333+
fonttype = mpl.rcParams['pdf.fonttype']
2334+
23532335
def output_singlebyte_chunk(kerns_or_chars):
23542336
if not kerns_or_chars:
23552337
return
@@ -2400,6 +2382,12 @@ def output_singlebyte_chunk(kerns_or_chars):
24002382
self.file.output(Op.end_text)
24012383
self.file.output(Op.grestore)
24022384

2385+
if gc.get_url() is not None:
2386+
font.set_text(s, features=features, language=language)
2387+
width, height = font.get_width_height()
2388+
self.file._annotations[-1][1].append(_get_link_annotation(
2389+
gc, x, y, width / 64, height / 64, angle))
2390+
24032391
def new_gc(self):
24042392
# docstring inherited
24052393
return GraphicsContextPdf(self.file)

0 commit comments

Comments
 (0)