Skip to content

Commit 7a5286a

Browse files
committed
Avoid redundant chr/ord calls in font tracking.
Almost every caller to `_font_supports_char` already has a code point, and has to convert to a character, only for the former to convert it back to a code point again. Similarly, some callers to `CharacterTracker.track` only have a single code point to add, so add some API to accept the glyph directly.
1 parent 6bac5ef commit 7a5286a

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

lib/matplotlib/backends/_backend_pdf_ps.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ def track(self, font, s):
6363
"""Record that string *s* is being typeset using font *font*."""
6464
self.used.setdefault(font.fname, set()).update(map(ord, s))
6565

66+
def track_glyph(self, font, glyph):
67+
"""Record that codepoint *glyph* is being typeset using font *font*."""
68+
self.used.setdefault(font.fname, set()).add(glyph)
69+
6670

6771
class RendererPDFPSBase(RendererBase):
6872
# The following attributes must be defined by the subclasses:

lib/matplotlib/backends/backend_pdf.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,18 @@ def pdfRepr(obj):
323323
.format(type(obj)))
324324

325325

326-
def _font_supports_char(fonttype, char):
326+
def _font_supports_glyph(fonttype, glyph):
327327
"""
328-
Returns True if the font is able to provide *char* in a PDF.
328+
Returns True if the font is able to provide codepoint *glyph* in a PDF.
329329
330330
For a Type 3 font, this method returns True only for single-byte
331-
chars. For Type 42 fonts this method return True if the char is from
332-
the Basic Multilingual Plane.
331+
characters. For Type 42 fonts this method return True if the character is
332+
from the Basic Multilingual Plane.
333333
"""
334334
if fonttype == 3:
335-
return ord(char) <= 255
335+
return glyph <= 255
336336
if fonttype == 42:
337-
return ord(char) <= 65535
337+
return glyph <= 65535
338338
raise NotImplementedError()
339339

340340

@@ -1227,13 +1227,9 @@ def embedTTFType42(font, characters, descriptor):
12271227
wObject = self.reserveObject('Type 0 widths')
12281228
toUnicodeMapObject = self.reserveObject('ToUnicode map')
12291229

1230-
_log.debug(
1231-
"SUBSET %s characters: %s",
1232-
filename, "".join(chr(c) for c in characters)
1233-
)
1234-
fontdata = _backend_pdf_ps.get_glyphs_subset(
1235-
filename, "".join(chr(c) for c in characters)
1236-
)
1230+
subset_str = "".join(chr(c) for c in characters)
1231+
_log.debug("SUBSET %s characters: %s", filename, subset_str)
1232+
fontdata = _backend_pdf_ps.get_glyphs_subset(filename, subset_str)
12371233
_log.debug(
12381234
"SUBSET %s %d -> %d", filename,
12391235
os.stat(filename).st_size, fontdata.getbuffer().nbytes
@@ -1327,7 +1323,7 @@ def embedTTFType42(font, characters, descriptor):
13271323
# Add XObjects for unsupported chars
13281324
glyph_ids = []
13291325
for ccode in characters:
1330-
if not _font_supports_char(fonttype, chr(ccode)):
1326+
if not _font_supports_glyph(fonttype, ccode):
13311327
gind = full_font.get_char_index(ccode)
13321328
glyph_ids.append(gind)
13331329

@@ -2193,10 +2189,9 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
21932189

21942190
self.file.output(Op.begin_text)
21952191
for font, fontsize, num, ox, oy in glyphs:
2196-
char = chr(num)
2197-
self.file._character_tracker.track(font, char)
2192+
self.file._character_tracker.track_glyph(font, num)
21982193
fontname = font.fname
2199-
if not _font_supports_char(fonttype, char):
2194+
if not _font_supports_glyph(fonttype, num):
22002195
# Unsupported chars (i.e. multibyte in Type 3 or beyond BMP in
22012196
# Type 42) must be emitted separately (below).
22022197
unsupported_chars.append((font, fontsize, ox, oy, num))
@@ -2383,7 +2378,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23832378
prev_was_multibyte = True
23842379
for item in _text_helpers.layout(
23852380
s, font, kern_mode=KERNING_UNFITTED):
2386-
if _font_supports_char(fonttype, item.char):
2381+
if _font_supports_glyph(fonttype, ord(item.char)):
23872382
if prev_was_multibyte:
23882383
singlebyte_chunks.append((item.x, []))
23892384
if item.prev_kern:

lib/matplotlib/backends/backend_ps.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
670670
f"{angle:f} rotate\n")
671671
lastfont = None
672672
for font, fontsize, num, ox, oy in glyphs:
673-
self._character_tracker.track(font, chr(num))
673+
self._character_tracker.track_glyph(font, num)
674674
if (font.postscript_name, fontsize) != lastfont:
675675
lastfont = font.postscript_name, fontsize
676676
self._pswriter.write(
@@ -955,13 +955,13 @@ def print_figure_impl(fh):
955955
fh.write(_font_to_ps_type3(font_path, glyph_ids))
956956
else:
957957
try:
958+
subset_str = ''.join(chr(c) for c in chars)
958959
_log.debug(
959960
"SUBSET %s characters: %s", font_path,
960-
''.join(chr(c) for c in chars)
961+
subset_str
961962
)
962963
fontdata = _backend_pdf_ps.get_glyphs_subset(
963-
font_path, "".join(chr(c) for c in chars)
964-
)
964+
font_path, subset_str)
965965
_log.debug(
966966
"SUBSET %s %d -> %d", font_path,
967967
os.stat(font_path).st_size,

0 commit comments

Comments
 (0)