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

Skip to content

Commit f9d2918

Browse files
authored
Merge pull request #18589 from anntzer/pdft3g
Factor out pdf Type3 glyph drawing.
2 parents a3ebc54 + 74b0850 commit f9d2918

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
@@ -2095,9 +2095,6 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
20952095
width, height, descent, glyphs, rects = \
20962096
self._text2path.mathtext_parser.parse(s, 72, prop)
20972097

2098-
# When using Type 3 fonts, we can't use character codes higher
2099-
# than 255, so we use the "Do" command to render those
2100-
# instead.
21012098
global_fonttype = mpl.rcParams['pdf.fonttype']
21022099

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

21102107
self.check_gc(gc, gc._rgb)
2111-
self.file.output(Op.begin_text)
21122108
prev_font = None, None
21132109
oldx, oldy = 0, 0
2110+
type3_multibytes = []
2111+
2112+
self.file.output(Op.begin_text)
21142113
for font, fontsize, num, ox, oy in glyphs:
21152114
self.file._character_tracker.track(font, chr(num))
21162115
fontname = font.fname
2117-
if is_opentype_cff_font(fontname):
2118-
fonttype = 42
2116+
fonttype = (
2117+
42 if is_opentype_cff_font(fontname) else global_fonttype)
2118+
if fonttype == 3 and num > 255:
2119+
# For Type3 fonts, multibyte characters must be emitted
2120+
# separately (below).
2121+
type3_multibytes.append((font, fontsize, ox, oy, num))
21192122
else:
2120-
fonttype = global_fonttype
2121-
2122-
if fonttype == 42 or num <= 255:
21232123
self._setup_textpos(ox, oy, 0, oldx, oldy)
21242124
oldx, oldy = ox, oy
21252125
if (fontname, fontsize) != prev_font:
@@ -2130,27 +2130,9 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
21302130
Op.show)
21312131
self.file.output(Op.end_text)
21322132

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

21552137
# Draw any horizontal lines in the math layout
21562138
for ox, oy, width, height in rects:
@@ -2328,17 +2310,20 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23282310
self.file.output(Op.end_text)
23292311
# Then emit all the multibyte characters, one at a time.
23302312
for start_x, glyph_idx in multibyte_glyphs:
2331-
glyph_name = font.get_glyph_name(glyph_idx)
2332-
self.file.output(Op.gsave)
2333-
self.file.output(0.001 * fontsize, 0,
2334-
0, 0.001 * fontsize,
2335-
start_x, 0, Op.concat_matrix)
2336-
name = self.file._get_xobject_symbol_name(
2337-
font.fname, glyph_name)
2338-
self.file.output(Name(name), Op.use_xobject)
2339-
self.file.output(Op.grestore)
2313+
self._draw_xobject_glyph(font, fontsize, glyph_idx, start_x, 0)
23402314
self.file.output(Op.grestore)
23412315

2316+
def _draw_xobject_glyph(self, font, fontsize, glyph_idx, x, y):
2317+
"""Draw a multibyte character from a Type 3 font as an XObject."""
2318+
symbol_name = font.get_glyph_name(glyph_idx)
2319+
name = self.file._get_xobject_symbol_name(font.fname, symbol_name)
2320+
self.file.output(
2321+
Op.gsave,
2322+
0.001 * fontsize, 0, 0, 0.001 * fontsize, x, y, Op.concat_matrix,
2323+
Name(name), Op.use_xobject,
2324+
Op.grestore,
2325+
)
2326+
23422327
def new_gc(self):
23432328
# docstring inherited
23442329
return GraphicsContextPdf(self.file)

0 commit comments

Comments
 (0)