8000 Merge pull request #18894 from anntzer/selectfont · matplotlib/matplotlib@e079bd3 · GitHub
[go: up one dir, main page]

Skip to content

Commit e079bd3

Browse files
authored
Merge pull request #18894 from anntzer/selectfont
MNT: Use selectfont instead of findfont + scalefont + setfont in PostScript.
2 parents d2b2e51 + 9cf0d2e commit e079bd3

File tree

2 files changed

+21
-47
lines changed

2 files changed

+21
-47
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``RendererPS.set_font`` is no longer a no-op in AFM mode
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
It now sets the current PostScript font in all cases.

lib/matplotlib/backends/backend_ps.py

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,8 @@ def set_linedash(self, offset, seq, store=True):
321321
self.linedash = (offset, seq)
322322

323323
def set_font(self, fontname, fontsize, store=True):
324-
if mpl.rcParams['ps.useafm']:
325-
return
326324
if (fontname, fontsize) != (self.fontname, self.fontsize):
327-
out = ("/%s findfont\n"
328-
"%1.3f scalefont\n"
329-
"setfont\n" % (fontname, fontsize))
330-
self._pswriter.write(out)
325+
self._pswriter.write(f"/{fontname} {fontsize:1.3f} selectfont\n")
331326
if store:
332327
self.fontname = fontname
333328
self.fontsize = fontsize
@@ -616,65 +611,43 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
616611
if ismath == 'TeX':
617612
return self.draw_tex(gc, x, y, s, prop, angle)
618613

619-
elif ismath:
614+
if ismath:
620615
return self.draw_mathtext(gc, x, y, s, prop, angle)
621616

622-
elif mpl.rcParams['ps.useafm']:
623-
self.set_color(*gc.get_rgb())
624-
617+
if mpl.rcParams['ps.useafm']:
625618
font = self._get_font_afm(prop)
626-
fontname = font.get_fontname()
627-
fontsize = prop.get_size_in_points()
628-
scale = 0.001 * fontsize
619+
scale = 0.001 * prop.get_size_in_points()
629620

630621
thisx = 0
631-
last_name = None
632-
lines = []
622+
last_name = None # kerns returns 0 for None.
623+
xs_names = []
633624
for c in s:
634625
name = uni2type1.get(ord(c), f"uni{ord(c):04X}")
635626
try:
636627
width = font.get_width_from_char_name(name)
637628
except KeyError:
638629
name = 'question'
639630
width = font.get_width_char('?')
640-
if last_name is not None:
641-
kern = font.get_kern_dist_from_name(last_name, name)
642-
else:
643-
kern = 0
631+
kern = font.get_kern_dist_from_name(last_name, name)
644632
last_name = name
645633
thisx += kern * scale
646-
647-
lines.append('%f 0 m /%s glyphshow' % (thisx, name))
648-
634+
xs_names.append((thisx, name))
649635
thisx += width * scale
650636

651-
thetext = "\n".join(lines)
652-
self._pswriter.write(f"""\
653-
gsave
654-
/{fontname} findfont
655-
{fontsize} scalefont
656-
setfont
657-
{x:f} {y:f} translate
658-
{angle:f} rotate
659-
{thetext}
660-
grestore
661-
""")
662-
663637
else:
664638
font = self._get_font_ttf(prop)
665639
font.set_text(s, 0, flags=LOAD_NO_HINTING)
666640
self._character_tracker.track(font, s)
641+
xs_names = [(item.x, font.get_glyph_name(item.glyph_idx))
642+
for item in _text_layout.layout(s, font)]
667643

668-
self.set_color(*gc.get_rgb())
669-
ps_name = (font.postscript_name
670-
.encode('ascii', 'replace').decode('ascii'))
671-
self.set_font(ps_name, prop.get_size_in_points())
672-
673-
thetext = '\n'.join(
674-
'{:f} 0 m /{:s} glyphshow'
675-
.format(item.x, font.get_glyph_name(item.glyph_idx))
676-
for item in _text_layout.layout(s, font))
677-
self._pswriter.write(f"""\
644+
self.set_color(*gc.get_rgb())
645+
ps_name = (font.postscript_name
646+
.encode("ascii", "replace").decode("ascii"))
647+
self.set_font(ps_name, prop.get_size_in_points())
648+
thetext = "\n".join(f"{x:f} 0 m /{name:s} glyphshow"
649+
for x, name in xs_names)
650+
self._pswriter.write(f"""\
678651
gsave
679652
{x:f} {y:f} translate
680653
{angle:f} rotate
@@ -702,9 +675,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
702675
if (font.postscript_name, fontsize) != lastfont:
703676
lastfont = font.postscript_name, fontsize
704677
self._pswriter.write(
705-
f"/{font.postscript_name} findfont\n"
706-
f"{fontsize} scalefont\n"
707-
f"setfont\n")
678+
f"/{font.postscript_name} {fontsize} selectfont\n")
708679
symbol_name = (
709680
font.get_name_char(chr(num)) if isinstance(font, AFM) else
710681
font.get_glyph_name(font.get_char_index(num)))

0 commit comments

Comments
 (0)
0