Skip to content

Commit 88dc315

Browse files
committed
pdf: Merge loops for single byte text chunk output
Currently, we split text into single byte chunks and multi-byte glyphs, then iterate through single byte chunks for output and multi-byte glyphs for output. Instead, output the single byte chunks as we finish them, then do the multi-byte glyphs at the end.
1 parent f630b48 commit 88dc315

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

lib/matplotlib/backends/backend_pdf.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,47 +2384,48 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23842384
# the regular text show command (TJ) with appropriate kerning between
23852385
# chunks, whereas multibyte characters use the XObject command (Do).
23862386
else:
2387-
# List of (ft_object, start_x, [prev_kern, char, char, ...]),
2388-
# w/o zero kerns.
2389-
singlebyte_chunks = []
2390-
# List of (ft_object, start_x, glyph_index).
2391-
multibyte_glyphs = []
2392-
prev_was_multibyte = True
2393-
prev_font = font
2394-
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
2395-
if _font_supports_glyph(fonttype, ord(item.char)):
2396-
if prev_was_multibyte or item.ft_object != prev_font:
2397-
singlebyte_chunks.append((item.ft_object, item.x, []))
2398-
prev_font = item.ft_object
2399-
if item.prev_kern:
2400-
singlebyte_chunks[-1][2].append(item.prev_kern)
2401-
singlebyte_chunks[-1][2].append(item.char)
2402-
prev_was_multibyte = False
2403-
else:
2404-
multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index))
2405-
prev_was_multibyte = True
2387+
def output_singlebyte_chunk(kerns_or_chars):
2388+
self.file.output(
2389+
# See pdf spec "Text space details" for the 1000/fontsize
2390+
# (aka. 1000/T_fs) factor.
2391+
[(-1000 * next(group) / fontsize) if tp == float # a kern
2392+
else self.encode_string("".join(group), fonttype)
2393+
for tp, group in itertools.groupby(kerns_or_chars, type)],
2394+
Op.showkern)
24062395
# Do the rotation and global translation as a single matrix
24072396
# concatenation up front
24082397
self.file.output(Op.gsave)
24092398
a = math.radians(angle)
24102399
self.file.output(math.cos(a), math.sin(a),
24112400
-math.sin(a), math.cos(a),
24122401
x, y, Op.concat_matrix)
2402+
# List of [prev_kern, char, char, ...] w/o zero kerns.
2403+
singlebyte_chunk = []
2404+
# List of (ft_object, start_x, glyph_index).
2405+
multibyte_glyphs = []
2406+
prev_font = None
2407+
prev_start_x = 0
24132408
# Emit all the 1-byte characters in a BT/ET group.
24142409
self.file.output(Op.begin_text)
2415-
prev_start_x = 0
2416-
for ft_object, start_x, kerns_or_chars in singlebyte_chunks:
2417-
ft_name = self.file.fontName(ft_object.fname)
2418-
self.file.output(ft_name, fontsize, Op.selectfont)
2419-
self._setup_textpos(start_x, 0, 0, prev_start_x, 0, 0)
2420-
self.file.output(
2421-
# See pdf spec "Text space details" for the 1000/fontsize
2422-
# (aka. 1000/T_fs) factor.
2423-
[-1000 * next(group) / fontsize if tp == float # a kern
2424-
else self.encode_string("".join(group), fonttype)
2425-
for tp, group in itertools.groupby(kerns_or_chars, type)],
2426-
Op.showkern)
2427-
prev_start_x = start_x
2410+
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
2411+
if _font_supports_glyph(fonttype, ord(item.char)):
2412+
if item.ft_object != prev_font:
2413+
if singlebyte_chunk:
2414+
output_singlebyte_chunk(singlebyte_chunk)
2415+
ft_name = self.file.fontName(item.ft_object.fname)
2416+
self.file.output(ft_name, fontsize, Op.selectfont)
2417+
self._setup_textpos(item.x, 0, 0, prev_start_x, 0, 0)
2418+
singlebyte_chunk = []
2419+
prev_font = item.ft_object
2420+
prev_start_x = item.x
2421+
if item.prev_kern:
2422+
singlebyte_chunk.append(item.prev_kern)
2423+
singlebyte_chunk.append(item.char)
2424+
else:
2425+
prev_font = None
2426+
multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index))
2427+
if singlebyte_chunk:
2428+
output_singlebyte_chunk(singlebyte_chunk)
24282429
self.file.output(Op.end_text)
24292430
# Then emit all the multibyte characters, one at a time.
24302431
for ft_object, start_x, glyph_index in multibyte_glyphs:

0 commit comments

Comments
 (0)