@@ -121,16 +121,16 @@ def _move_path_to_path_or_stream(src, dst):
121121 shutil .move (src , dst , copy_function = shutil .copyfile )
122122
123123
124- def _font_to_ps_type3 (font_path , glyph_ids ):
124+ def _font_to_ps_type3 (font_path , chars ):
125125 """
126- Subset *glyph_ids * from the font at *font_path* into a Type 3 font.
126+ Subset *chars * from the font at *font_path* into a Type 3 font.
127127
128128 Parameters
129129 ----------
130130 font_path : path-like
131131 Path to the font to be subsetted.
132- glyph_ids : list of int
133- The glyph indices to include in the subsetted font.
132+ chars : str
133+ The characters to include in the subsetted font.
134134
135135 Returns
136136 -------
@@ -139,6 +139,7 @@ def _font_to_ps_type3(font_path, glyph_ids):
139139 verbatim into a PostScript file.
140140 """
141141 font = get_font (font_path , hinting_factor = 1 )
142+ glyph_ids = [font .get_char_index (c ) for c in chars ]
142143
143144 preamble = """\
144145 %!PS-Adobe-3.0 Resource-Font
@@ -201,6 +202,44 @@ def _font_to_ps_type3(font_path, glyph_ids):
201202 return preamble + "\n " .join (entries ) + postamble
202203
203204
205+ def _font_to_ps_type42 (font_path , chars , fh ):
206+ """
207+ Subset *chars* from the font at *font_path* into a Type 42 font at *fh*.
208+
209+ Parameters
210+ ----------
211+ font_path : path-like
212+ Path to the font to be subsetted.
213+ chars : str
214+ The characters to include in the subsetted font.
215+ fh : file-like
216+ Where to write the font.
217+ """
218+ subset_str = '' .join (chr (c ) for c in chars )
219+ _log .debug ("SUBSET %s characters: %s" , font_path , subset_str )
220+ try :
221+ fontdata = _backend_pdf_ps .get_glyphs_subset (font_path , subset_str )
222+ _log .debug ("SUBSET %s %d -> %d" , font_path , os .stat (font_path ).st_size ,
223+ fontdata .getbuffer ().nbytes )
224+
225+ # Give ttconv a subsetted font along with updated glyph_ids.
226+ font = FT2Font (fontdata )
227+ glyph_ids = [font .get_char_index (c ) for c in chars ]
228+ with TemporaryDirectory () as tmpdir :
229+ tmpfile = os .path .join (tmpdir , "tmp.ttf" )
230+
231+ with open (tmpfile , 'wb' ) as tmp :
232+ tmp .write (fontdata .getvalue ())
233+
234+ # TODO: allow convert_ttf_to_ps to input file objects (BytesIO)
235+ convert_ttf_to_ps (os .fsencode (tmpfile ), fh , 42 , glyph_ids )
236+ except RuntimeError :
237+ _log .warning (
238+ "The PostScript backend does not currently "
239+ "support the selected font." )
240+ raise
241+
242+
204243def _log_if_debug_on (meth ):
205244 """
206245 Wrap `RendererPS` method *meth* to emit a PS comment with the method name,
@@ -657,7 +696,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
657696 f"{ angle :f} rotate\n " )
658697 lastfont = None
659698 for font , fontsize , num , ox , oy in glyphs :
660- self ._character_tracker .track (font , chr ( num ) )
699+ self ._character_tracker .track_glyph (font , num )
661700 if (font .postscript_name , fontsize ) != lastfont :
662701 lastfont = font .postscript_name , fontsize
663702 self ._pswriter .write (
@@ -931,56 +970,15 @@ def print_figure_impl(fh):
931970 in ps_renderer ._character_tracker .used .items ():
932971 if not chars :
933972 continue
934- font = get_font (font_path )
935- glyph_ids = [font .get_char_index (c ) for c in chars ]
936973 fonttype = mpl .rcParams ['ps.fonttype' ]
937974 # Can't use more than 255 chars from a single Type 3 font.
938- if len (glyph_ids ) > 255 :
975+ if len (chars ) > 255 :
939976 fonttype = 42
940977 fh .flush ()
941978 if fonttype == 3 :
942- fh .write (_font_to_ps_type3 (font_path , glyph_ids ))
943- else :
944- try :
945- _log .debug (
946- "SUBSET %s characters: %s" , font_path ,
947- '' .join (chr (c ) for c in chars )
948- )
949- fontdata = _backend_pdf_ps .get_glyphs_subset (
950- font_path , "" .join (chr (c ) for c in chars )
951- )
952- _log .debug (
953- "SUBSET %s %d -> %d" , font_path ,
954- os .stat (font_path ).st_size ,
955- fontdata .getbuffer ().nbytes
956- )
957-
958- # give ttconv a subsetted font
959- # along with updated glyph_ids
960- with TemporaryDirectory () as tmpdir :
961- tmpfile = os .path .join (tmpdir , "tmp.ttf" )
962- font = FT2Font (fontdata )
963- glyph_ids = [
964- font .get_char_index (c ) for c in chars
965- ]
966-
967- with open (tmpfile , 'wb' ) as tmp :
968- tmp .write (fontdata .getvalue ())
969- tmp .flush ()
970-
971- # TODO: allow convert_ttf_to_ps
972- # to input file objects (BytesIO)
973- convert_ttf_to_ps (
974- os .fsencode (tmpfile ),
975- fh ,
976- fonttype ,
977- glyph_ids ,
978- )
979- except RuntimeError :
980- _log .warning (
981- "The PostScript backend does not currently "
982- "support the selected font." )
983- raise
979+ fh .write (_font_to_ps_type3 (font_path , chars ))
980+ else : # Type 42 only.
981+ _font_to_ps_type42 (font_path , chars , fh )
984982 print ("end" , file = fh )
985983 print ("%%EndProlog" , file = fh )
986984
0 commit comments