Skip to content

Commit fbf088b

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 fbf088b

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 13 additions & 22 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+
return s.encode(
2292+
{1: 'cp1252', 3: 'latin-1', 42: 'utf-16be'}[fonttype], 'replace')
22982293

22992294
def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23002295
# docstring inherited
@@ -2312,29 +2307,16 @@ 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]:
23312313
if not mpl.rcParams['pdf.use14corefonts']:
23322314
self.file._character_tracker.track(font, s,
23332315
features=features, language=language)
23342316
self.file.output(Op.begin_text,
23352317
self.file.fontName(prop), fontsize, Op.selectfont)
23362318
self._setup_textpos(x, y, angle)
2337-
self.file.output(self.encode_string(s, fonttype),
2319+
self.file.output(self.encode_string(s, fonttype=1),
23382320
Op.show, Op.end_text)
23392321

23402322
# A sequence of characters is broken into multiple chunks. The chunking
@@ -2350,6 +2332,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23502332
# Each chunk is emitted with the regular text show command (TJ) with appropriate
23512333
# kerning between chunks.
23522334
else:
2335+
font = self._get_font_ttf(prop)
2336+
fonttype = mpl.rcParams['pdf.fonttype']
2337+
23532338
def output_singlebyte_chunk(kerns_or_chars):
23542339
if not kerns_or_chars:
23552340
return
@@ -2400,6 +2385,12 @@ def output_singlebyte_chunk(kerns_or_chars):
24002385
self.file.output(Op.end_text)
24012386
self.file.output(Op.grestore)
24022387

2388+
if gc.get_url() is not None:
2389+
font.set_text(s, features=features, language=language)
2390+
width, height = font.get_width_height()
2391+
self.file._annotations[-1][1].append(_get_link_annotation(
2392+
gc, x, y, width / 64, height / 64, angle))
2393+
24032394
def new_gc(self):
24042395
# docstring inherited
24052396
return GraphicsContextPdf(self.file)

0 commit comments

Comments
 (0)