Skip to content

Commit ee6fc0f

Browse files
authored
Merge pull request #24287 from Abhijnan-Bajpai/main
Simplifying glyph stream logic in ps backend
2 parents d85e980 + 022d51b commit ee6fc0f

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

lib/matplotlib/backends/backend_ps.py

+11-22
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from enum import Enum
88
import functools
99
from io import StringIO
10+
import itertools
1011
import logging
1112
import os
1213
import pathlib
@@ -630,13 +631,15 @@ 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")
639+
.decode("ascii"))
635640
scale = 0.001 * prop.get_size_in_points()
636-
stream = []
637641
thisx = 0
638642
last_name = None # kerns returns 0 for None.
639-
xs_names = []
640643
for c in s:
641644
name = uni2type1.get(ord(c), f"uni{ord(c):04X}")
642645
try:
@@ -647,38 +650,24 @@ 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+
stream.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. \
667+
groupby(stream, lambda entry: entry[0]):
679668
self.set_font(ps_name, prop.get_size_in_points(), False)
680669
thetext = "\n".join(f"{x:g} 0 m /{name:s} glyphshow"
681-
for x, name in xs_names)
670+
for _, x, name in group)
682671
self._pswriter.write(f"""\
683672
gsave
684673
{self._get_clip_cmd(gc)}

0 commit comments

Comments
 (0)