7
7
from enum import Enum
8
8
import functools
9
9
from io import StringIO
10
+ import itertools
10
11
import logging
11
12
import os
12
13
import pathlib
@@ -630,13 +631,15 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
630
631
if ismath :
631
632
return self .draw_mathtext (gc , x , y , s , prop , angle )
632
633
634
+ stream = [] # list of (ps_name, x, char_name)
635
+
633
636
if mpl .rcParams ['ps.useafm' ]:
634
637
font = self ._get_font_afm (prop )
638
+ ps_name = (font .postscript_name .encode ("ascii" , "replace" )
639
+ .decode ("ascii" ))
635
640
scale = 0.001 * prop .get_size_in_points ()
636
- stream = []
637
641
thisx = 0
638
642
last_name = None # kerns returns 0 for None.
639
- xs_names = []
640
643
for c in s :
641
644
name = uni2type1 .get (ord (c ), f"uni{ ord (c ):04X} " )
642
645
try :
@@ -647,38 +650,24 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
647
650
kern = font .get_kern_dist_from_name (last_name , name )
648
651
last_name = name
649
652
thisx += kern * scale
650
- xs_names .append ((thisx , name ))
653
+ stream .append ((ps_name , thisx , name ))
651
654
thisx += width * scale
652
- ps_name = (font .postscript_name
653
- .encode ("ascii" , "replace" ).decode ("ascii" ))
654
- stream .append ((ps_name , xs_names ))
655
655
656
656
else :
657
657
font = self ._get_font_ttf (prop )
658
658
self ._character_tracker .track (font , s )
659
- stream = []
660
- prev_font = curr_stream = None
661
659
for item in _text_helpers .layout (s , font ):
662
660
ps_name = (item .ft_object .postscript_name
663
661
.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 ))
676
664
self .set_color (* gc .get_rgb ())
677
665
678
- for ps_name , xs_names in stream :
666
+ for ps_name , group in itertools . \
667
+ groupby (stream , lambda entry : entry [0 ]):
679
668
self .set_font (ps_name , prop .get_size_in_points (), False )
680
669
thetext = "\n " .join (f"{ x :g} 0 m /{ name :s} glyphshow"
681
- for x , name in xs_names )
670
+ for _ , x , name in group )
682
671
self ._pswriter .write (f"""\
683
672
gsave
684
673
{ self ._get_clip_cmd (gc )}
0 commit comments