Skip to content

Commit 3e97c0d

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 43d5d4e commit 3e97c0d

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
@@ -2376,47 +2376,48 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
23762376
# the regular text show command (TJ) with appropriate kerning between
23772377
# chunks, whereas multibyte characters use the XObject command (Do).
23782378
else:
2379-
# List of (ft_object, start_x, [prev_kern, char, char, ...]),
2380-
# w/o zero kerns.
2381-
singlebyte_chunks = []
2382-
# List of (ft_object, start_x, glyph_index).
2383-
multibyte_glyphs = []
2384-
prev_was_multibyte = True
2385-
prev_font = font
2386-
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
2387-
if _font_supports_glyph(fonttype, ord(item.char)):
2388-
if prev_was_multibyte or item.ft_object != prev_font:
2389-
singlebyte_chunks.append((item.ft_object, item.x, []))
2390-
prev_font = item.ft_object
2391-
if item.prev_kern:
2392-
singlebyte_chunks[-1][2].append(item.prev_kern)
2393-
singlebyte_chunks[-1][2].append(item.char)
2394-
prev_was_multibyte = False
2395-
else:
2396-
multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index))
2397-
prev_was_multibyte = True
2379+
def output_singlebyte_chunk(kerns_or_chars):
2380+
self.file.output(
2381+
# See pdf spec "Text space details" for the 1000/fontsize
2382+
# (aka. 1000/T_fs) factor.
2383+
[(-1000 * next(group) / fontsize) if tp == float # a kern
2384+
else self.encode_string("".join(group), fonttype)
2385+
for tp, group in itertools.groupby(kerns_or_chars, type)],
2386+
Op.showkern)
23982387
# Do the rotation and global translation as a single matrix
23992388
# concatenation up front
24002389
self.file.output(Op.gsave)
24012390
a = math.radians(angle)
24022391
self.file.output(math.cos(a), math.sin(a),
24032392
-math.sin(a), math.cos(a),
24042393
x, y, Op.concat_matrix)
2394+
# List of [prev_kern, char, char, ...] w/o zero kerns.
2395+
singlebyte_chunk = []
2396+
# List of (ft_object, start_x, glyph_index).
2397+
multibyte_glyphs = []
2398+
prev_font = None
2399+
prev_start_x = 0
24052400
# Emit all the 1-byte characters in a BT/ET group.
24062401
self.file.output(Op.begin_text)
2407-
prev_start_x = 0
2408-
for ft_object, start_x, kerns_or_chars in singlebyte_chunks:
2409-
ft_name = self.file.fontName(ft_object.fname)
2410-
self.file.output(ft_name, fontsize, Op.selectfont)
2411-
self._setup_textpos(start_x, 0, 0, prev_start_x, 0, 0)
2412-
self.file.output(
2413-
# See pdf spec "Text space details" for the 1000/fontsize
2414-
# (aka. 1000/T_fs) factor.
2415-
[-1000 * next(group) / fontsize if tp == float # a kern
2416-
else self.encode_string("".join(group), fonttype)
2417-
for tp, group in itertools.groupby(kerns_or_chars, type)],
2418-
Op.showkern)
2419-
prev_start_x = start_x
2402+
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
2403+
if _font_supports_glyph(fonttype, ord(item.char)):
2404+
if item.ft_object != prev_font:
2405+
if singlebyte_chunk:
2406+
output_singlebyte_chunk(singlebyte_chunk)
2407+
ft_name = self.file.fontName(item.ft_object.fname)
2408+
self.file.output(ft_name, fontsize, Op.selectfont)
2409+
self._setup_textpos(item.x, 0, 0, prev_start_x, 0, 0)
2410+
singlebyte_chunk = []
2411+
prev_font = item.ft_object
2412+
prev_start_x = item.x
2413+
if item.prev_kern:
2414+
singlebyte_chunk.append(item.prev_kern)
2415+
singlebyte_chunk.append(item.char)
2416+
else:
2417+
prev_font = None
2418+
multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index))
2419+
if singlebyte_chunk:
2420+
output_singlebyte_chunk(singlebyte_chunk)
24202421
self.file.output(Op.end_text)
24212422
# Then emit all the multibyte characters, one at a time.
24222423
for ft_object, start_x, glyph_index in multibyte_glyphs:

0 commit comments

Comments
 (0)