77from enum import Enum
88import functools
99from io import StringIO
10+ import itertools
1011import logging
1112import os
1213import pathlib
@@ -630,10 +631,12 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
630631 if ismath :
631632 return self .draw_mathtext (gc , x , y , s , prop , angle )
632633
634+ stream = [] # list of (ps_name, x, char_name)
635+
633636 if mpl .rcParams ['ps.useafm' ]:
634637 font = self ._get_font_afm (prop )
638+ ps_name = (font .postscript_name .encode ("ascii" , "replace" ).decode ("ascii" ))
635639 scale = 0.001 * prop .get_size_in_points ()
636- stream = []
637640 thisx = 0
638641 last_name = None # kerns returns 0 for None.
639642 xs_names = []
@@ -647,38 +650,23 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
647650 kern = font .get_kern_dist_from_name (last_name , name )
648651 last_name = name
649652 thisx += kern * scale
650- xs_names .append ((thisx , name ))
653+ xs_names .append ((ps_name , thisx , name ))
651654 thisx += width * scale
652- ps_name = (font .postscript_name
653- .encode ("ascii" , "replace" ).decode ("ascii" ))
654- stream .append ((ps_name , xs_names ))
655655
656656 else :
657657 font = self ._get_font_ttf (prop )
658658 self ._character_tracker .track (font , s )
659- stream = []
660- prev_font = curr_stream = None
661659 for item in _text_helpers .layout (s , font ):
662660 ps_name = (item .ft_object .postscript_name
663661 .encode ("ascii" , "replace" ).decode ("ascii" ))
664- if item .ft_object is not prev_font :
665- if curr_stream :
666- stream .append (curr_stream )
667- prev_font = item .ft_object
668- curr_stream = [ps_name , []]
669- curr_stream [1 ].append (
670- (item .x , item .ft_object .get_glyph_name (item .glyph_idx ))
671- )
672- # append the last entry if exists
673- if curr_stream :
674- stream .append (curr_stream )
675-
662+ glyph_name = item .ft_object .get_glyph_name (item .glyph_idx )
663+ stream .append ((ps_name , item .x , glyph_name ))
676664 self .set_color (* gc .get_rgb ())
677665
678- for ps_name , xs_names in stream :
666+ for ps_name , group in itertools . groupby ( stream , lambda entry : entry [ 0 ]) :
679667 self .set_font (ps_name , prop .get_size_in_points (), False )
680668 thetext = "\n " .join (f"{ x :g} 0 m /{ name :s} glyphshow"
681- for x , name in xs_names )
669+ for _ , x , name in group )
682670 self ._pswriter .write (f"""\
683671 gsave
684672{ self ._get_clip_cmd (gc )}
0 commit comments