Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
  • Loading branch information
QuLogic committed Sep 17, 2025
commit 3e97c0d649550d805c25c71bf49f5f456fec284f
65 changes: 33 additions & 32 deletions lib/matplotlib/backends/backend_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2376,47 +2376,48 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None):
# the regular text show command (TJ) with appropriate kerning between
# chunks, whereas multibyte characters use the XObject command (Do).
else:
# List of (ft_object, start_x, [prev_kern, char, char, ...]),
# w/o zero kerns.
singlebyte_chunks = []
# List of (ft_object, start_x, glyph_index).
multibyte_glyphs = []
prev_was_multibyte = True
prev_font = font
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
if _font_supports_glyph(fonttype, ord(item.char)):
if prev_was_multibyte or item.ft_object != prev_font:
singlebyte_chunks.append((item.ft_object, item.x, []))
prev_font = item.ft_object
if item.prev_kern:
singlebyte_chunks[-1][2].append(item.prev_kern)
singlebyte_chunks[-1][2].append(item.char)
prev_was_multibyte = False
else:
multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index))
prev_was_multibyte = True
def output_singlebyte_chunk(kerns_or_chars):
self.file.output(
# See pdf spec "Text space details" for the 1000/fontsize
# (aka. 1000/T_fs) factor.
[(-1000 * next(group) / fontsize) if tp == float # a kern
else self.encode_string("".join(group), fonttype)
for tp, group in itertools.groupby(kerns_or_chars, type)],
Op.showkern)
# Do the rotation and global translation as a single matrix
# concatenation up front
self.file.output(Op.gsave)
a = math.radians(angle)
self.file.output(math.cos(a), math.sin(a),
-math.sin(a), math.cos(a),
x, y, Op.concat_matrix)
# List of [prev_kern, char, char, ...] w/o zero kerns.
singlebyte_chunk = []
# List of (ft_object, start_x, glyph_index).
multibyte_glyphs = []
prev_font = None
prev_start_x = 0
# Emit all the 1-byte characters in a BT/ET group.
self.file.output(Op.begin_text)
prev_start_x = 0
for ft_object, start_x, kerns_or_chars in singlebyte_chunks:
ft_name = self.file.fontName(ft_object.fname)
self.file.output(ft_name, fontsize, Op.selectfont)
self._setup_textpos(start_x, 0, 0, prev_start_x, 0, 0)
self.file.output(
# See pdf spec "Text space details" for the 1000/fontsize
# (aka. 1000/T_fs) factor.
[-1000 * next(group) / fontsize if tp == float # a kern
else self.encode_string("".join(group), fonttype)
for tp, group in itertools.groupby(kerns_or_chars, type)],
Op.showkern)
prev_start_x = start_x
for item in _text_helpers.layout(s, font, kern_mode=Kerning.UNFITTED):
if _font_supports_glyph(fonttype, ord(item.char)):
if item.ft_object != prev_font:
if singlebyte_chunk:
output_singlebyte_chunk(singlebyte_chunk)
ft_name = self.file.fontName(item.ft_object.fname)
self.file.output(ft_name, fontsize, Op.selectfont)
self._setup_textpos(item.x, 0, 0, prev_start_x, 0, 0)
singlebyte_chunk = []
prev_font = item.ft_object
prev_start_x = item.x
if item.prev_kern:
singlebyte_chunk.append(item.prev_kern)
singlebyte_chunk.append(item.char)
else:
prev_font = None
multibyte_glyphs.append((item.ft_object, item.x, item.glyph_index))
if singlebyte_chunk:
output_singlebyte_chunk(singlebyte_chunk)
self.file.output(Op.end_text)
# Then emit all the multibyte characters, one at a time.
for ft_object, start_x, glyph_index in multibyte_glyphs:
Expand Down
Loading