Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63e0f3a

Browse files
committed
Use multi-font output for PS backend
1 parent 742366b commit 63e0f3a

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

lib/matplotlib/backends/backend_ps.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,8 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
640640
if ismath:
641641
return self.draw_mathtext(gc, x, y, s, prop, angle)
642642

643+
self.set_color(*gc.get_rgb())
644+
643645
if mpl.rcParams['ps.useafm']:
644646
font = self._get_font_afm(prop)
645647
scale = 0.001 * prop.get_size_in_points()
@@ -662,18 +664,27 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
662664

663665
else:
664666
font = self._get_font_ttf(prop)
665-
font.set_text(s, 0, flags=LOAD_NO_HINTING)
666-
self._character_tracker.track(font, s)
667-
xs_names = [(item.x, font.get_glyph_name(item.glyph_idx))
668-
for item in _text_helpers.layout(s, font)]
669-
670-
self.set_color(*gc.get_rgb())
671-
ps_name = (font.postscript_name
672-
.encode("ascii", "replace").decode("ascii"))
673-
self.set_font(ps_name, prop.get_size_in_points())
674-
thetext = "\n".join(f"{x:f} 0 m /{name:s} glyphshow"
675-
for x, name in xs_names)
676-
self._pswriter.write(f"""\
667+
char_to_font = font.fill_glyphs(s, 0, flags=LOAD_NO_HINTING)
668+
for char, font in char_to_font.items():
669+
self._character_tracker.track(font, chr(char))
670+
stream = []
671+
prev_font = curr_stream = None
672+
for item in _text_helpers.layout(s, font):
673+
ps_name = (item.ft_object.postscript_name.encode("ascii", "replace").decode("ascii"))
674+
if item.ft_object is not prev_font:
675+
if curr_stream:
676+
stream.append(curr_stream)
677+
prev_font = item.ft_object
678+
curr_stream = [ps_name, []]
679+
curr_stream[1].append((item.x, item.ft_object.get_glyph_name(item.glyph_idx)))
680+
# append the last entry
681+
stream.append(curr_stream)
682+
683+
for ps_name, xs_names in stream:
684+
self.set_font(ps_name, prop.get_size_in_points(), False)
685+
thetext = "\n".join(f"{x:f} 0 m /{name:s} glyphshow"
686+
for x, name in xs_names)
687+
self._pswriter.write(f"""\
677688
gsave
678689
{self._get_clip_cmd(gc)}
679690
{x:f} {y:f} translate

0 commit comments

Comments
 (0)