Skip to content

Commit 74b0850

Browse files
committed
Factor out pdf Type3 glyph drawing.
1 parent 214ba58 commit 74b0850

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,9 +2089,6 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
20892089
width, height, descent, glyphs, rects = \
20902090
self._text2path.mathtext_parser.parse(s, 72, prop)
20912091

2092-
# When using Type 3 fonts, we can't use character codes higher
2093-
# than 255, so we use the "Do" command to render those
2094-
# instead.
20952092
global_fonttype = mpl.rcParams['pdf.fonttype']
20962093

20972094
# Set up a global transformation matrix for the whole math expression
@@ -2102,18 +2099,21 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
21022099
x, y, Op.concat_matrix)
21032100

21042101
self.check_gc(gc, gc._rgb)
2105-
self.file.output(Op.begin_text)
21062102
prev_font = None, None
21072103
oldx, oldy = 0, 0
2104+
type3_multibytes = []
2105+
2106+
self.file.output(Op.begin_text)
21082107
for font, fontsize, num, ox, oy in glyphs:
21092108
self.file._character_tracker.track(font, chr(num))
21102109
fontname = font.fname
2111-
if is_opentype_cff_font(fontname):
2112-
fonttype = 42
2110+
fonttype = (
2111+
42 if is_opentype_cff_font(fontname) else global_fonttype)
2112+
if fonttype == 3 and num > 255:
2113+
# For Type3 fonts, multibyte characters must be emitted
2114+
# separately (below).
2115+
type3_multibytes.append((font, fontsize, ox, oy, num))
21132116
else:
2114-
fonttype = global_fonttype
2115-
2116-
if fonttype == 42 or num <= 255:
21172117
self._setup_textpos(ox, oy, 0, oldx, oldy)
21182118
oldx, oldy = ox, oy
21192119
if (fontname, fontsize) != prev_font:
@@ -2124,27 +2124,9 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
21242124
Op.show)
21252125
self.file.output(Op.end_text)
21262126

2127-
# If using Type 3 fonts, render all of the multi-byte characters
2128-
# as XObjects using the 'Do' command.
2129-
if global_fonttype == 3:
2130-
for font, fontsize, num, ox, oy in glyphs:
2131-
fontname = font.fname
2132-
if is_opentype_cff_font(fontname):
2133-
fonttype = 42
2134-
else:
2135-
fonttype = global_fonttype
2136-
2137-
if fonttype == 3 and num > 255:
2138-
self.file.fontName(fontname)
2139-
self.file.output(Op.gsave,
2140-
0.001 * fontsize, 0,
2141-
0, 0.001 * fontsize,
2142-
ox, oy, Op.concat_matrix)
2143-
symbol_name = font.get_glyph_name(font.get_char_index(num))
2144-
name = self.file._get_xobject_symbol_name(
2145-
fontname, symbol_name)
2146-
self.file.output(Name(name), Op.use_xobject)
2147-
self.file.output(Op.grestore)
2127+
for font, fontsize, ox, oy, num in type3_multibytes:
2128+
self._draw_xobject_glyph(
2129+
font, fontsize, font.get_char_index(num), ox, oy)
21482130

21492131
# Draw any horizontal lines in the math layout
21502132
for ox, oy, width, height in rects:
@@ -2322,17 +2304,20 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23222304
self.file.output(Op.end_text)
23232305
# Then emit all the multibyte characters, one at a time.
23242306
for start_x, glyph_idx in multibyte_glyphs:
2325-
glyph_name = font.get_glyph_name(glyph_idx)
2326-
self.file.output(Op.gsave)
2327-
self.file.output(0.001 * fontsize, 0,
2328-
0, 0.001 * fontsize,
2329-
start_x, 0, Op.concat_matrix)
2330-
name = self.file._get_xobject_symbol_name(
2331-
font.fname, glyph_name)
2332-
self.file.output(Name(name), Op.use_xobject)
2333-
self.file.output(Op.grestore)
2307+
self._draw_xobject_glyph(font, fontsize, glyph_idx, start_x, 0)
23342308
self.file.output(Op.grestore)
23352309

2310+
def _draw_xobject_glyph(self, font, fontsize, glyph_idx, x, y):
2311+
"""Draw a multibyte character from a Type 3 font as an XObject."""
2312+
symbol_name = font.get_glyph_name(glyph_idx)
2313+
name = self.file._get_xobject_symbol_name(font.fname, symbol_name)
2314+
self.file.output(
2315+
Op.gsave,
2316+
0.001 * fontsize, 0, 0, 0.001 * fontsize, x, y, Op.concat_matrix,
2317+
Name(name), Op.use_xobject,
2318+
Op.grestore,
2319+
)
2320+
23362321
def new_gc(self):
23372322
# docstring inherited
23382323
return GraphicsContextPdf(self.file)

0 commit comments

Comments
 (0)