Skip to content

Commit 771d604

Browse files
committed
Remove redundant character-to-glyph index conversion.
With Type 42 PS subsetting, the glyph is based on the subsetted font, not the original, so this initial conversion is just thrown away. With Type 3 subsetting, the font is opened again to subset, so we can avoid the second one of those (though `get_font is in an LRU cache, the second call uses different parameters that don't affect the glyph index, but do bust the cache.)
1 parent dcdd2c3 commit 771d604

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,16 @@ def _move_path_to_path_or_stream(src, dst):
135135
shutil.move(src, dst, copy_function=shutil.copyfile)
136136

137137

138-
def _font_to_ps_type3(font_path, glyph_ids):
138+
def _font_to_ps_type3(font_path, chars):
139139
"""
140-
Subset *glyph_ids* from the font at *font_path* into a Type 3 font.
140+
Subset *chars* from the font at *font_path* into a Type 3 font.
141141
142142
Parameters
143143
----------
144144
font_path : path-like
145145
Path to the font to be subsetted.
146-
glyph_ids : list of int
147-
The glyph indices to include in the subsetted font.
146+
chars : str
147+
The characters to include in the subsetted font.
148148
149149
Returns
150150
-------
@@ -153,6 +153,7 @@ def _font_to_ps_type3(font_path, glyph_ids):
153153
verbatim into a PostScript file.
154154
"""
155155
font = get_font(font_path, hinting_factor=1)
156+
glyph_ids = [font.get_char_index(c) for c in chars]
156157

157158
preamble = """\
158159
%!PS-Adobe-3.0 Resource-Font
@@ -983,15 +984,13 @@ def print_figure_impl(fh):
983984
in ps_renderer._character_tracker.used.items():
984985
if not chars:
985986
continue
986-
font = get_font(font_path)
987-
glyph_ids = [font.get_char_index(c) for c in chars]
988987
fonttype = mpl.rcParams['ps.fonttype']
989988
# Can't use more than 255 chars from a single Type 3 font.
990-
if len(glyph_ids) > 255:
989+
if len(chars) > 255:
991990
fonttype = 42
992991
fh.flush()
993992
if fonttype == 3:
994-
fh.write(_font_to_ps_type3(font_path, glyph_ids))
993+
fh.write(_font_to_ps_type3(font_path, chars))
995994
else: # Type 42 only.
996995
_font_to_ps_type42(font_path, chars, fh)
997996
print("end", file=fh)

0 commit comments

Comments
 (0)