@@ -631,7 +631,7 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
631631 if mpl .rcParams ['ps.useafm' ]:
632632 font = self ._get_font_afm (prop )
633633 scale = 0.001 * prop .get_size_in_points ()
634-
634+ stream = []
635635 thisx = 0
636636 last_name = None # kerns returns 0 for None.
637637 xs_names = []
@@ -647,21 +647,36 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
647647 thisx += kern * scale
648648 xs_names .append ((thisx , name ))
649649 thisx += width * scale
650+ ps_name = (font .postscript_name
651+ .encode ("ascii" , "replace" ).decode ("ascii" ))
652+ stream .append ((ps_name , xs_names ))
650653
651654 else :
652655 font = self ._get_font_ttf (prop )
653- font .set_text (s , 0 , flags = LOAD_NO_HINTING )
654656 self ._character_tracker .track (font , s )
655- xs_names = [(item .x , font .get_glyph_name (item .glyph_idx ))
656- for item in _text_helpers .layout (s , font )]
657+ stream = []
658+ prev_font = curr_stream = None
659+ for item in _text_helpers .layout (s , font ):
660+ ps_name = (item .ft_object .postscript_name
661+ .encode ("ascii" , "replace" ).decode ("ascii" ))
662+ if item .ft_object is not prev_font :
663+ if curr_stream :
664+ stream .append (curr_stream )
665+ prev_font = item .ft_object
666+ curr_stream = [ps_name , []]
667+ curr_stream [1 ].append (
668+ (item .x , item .ft_object .get_glyph_name (item .glyph_idx ))
669+ )
670+ # append the last entry
671+ stream .append (curr_stream )
657672
658673 self .set_color (* gc .get_rgb ())
659- ps_name = ( font . postscript_name
660- . encode ( "ascii" , "replace" ). decode ( "ascii" ))
661- self .set_font (ps_name , prop .get_size_in_points ())
662- thetext = "\n " .join (f"{ x :g} 0 m /{ name :s} glyphshow"
663- for x , name in xs_names )
664- self ._pswriter .write (f"""\
674+
675+ for ps_name , xs_names in stream :
676+ self .set_font (ps_name , prop .get_size_in_points (), False )
677+ thetext = "\n " .join (f"{ x :g} 0 m /{ name :s} glyphshow"
678+ for x , name in xs_names )
679+ self ._pswriter .write (f"""\
665680 gsave
666681{ self ._get_clip_cmd (gc )}
667682{ x :g} { y :g} translate
0 commit comments