diff --git a/lib/matplotlib/backends/backend_pdf.py b/lib/matplotlib/backends/backend_pdf.py index 42fa04b2fd47..26a12764da38 100644 --- a/lib/matplotlib/backends/backend_pdf.py +++ b/lib/matplotlib/backends/backend_pdf.py @@ -2287,24 +2287,26 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath=False, mtext=None): } self.file._annotations[-1][1].append(link_annotation) - # If fonttype != 3 or there are no multibyte characters, emit the whole - # string at once. - if fonttype != 3 or all(ord(char) <= 255 for char in s): + # If fonttype != 3 emit the whole string at once without manual + # kerning. + if fonttype != 3: self.file.output(Op.begin_text, self.file.fontName(prop), fontsize, Op.selectfont) self._setup_textpos(x, y, angle) - self.file.output(self.encode_string(s, fonttype), Op.show, - Op.end_text) + self.file.output(self.encode_string(s, fonttype), + Op.show, Op.end_text) # There is no way to access multibyte characters of Type 3 fonts, as # they cannot have a CIDMap. Therefore, in this case we break the # string into chunks, where each chunk contains either a string of - # consecutive 1-byte characters or a single multibyte character. Each - # chunk is emitted with a separate command: 1-byte characters use the - # regular text show command (Tj), whereas multibyte characters use - # the XObject command (Do). (If using Type 42 fonts, all of this - # complication is avoided, but of course, those fonts can not be - # subsetted.) + # consecutive 1-byte characters or a single multibyte character. + # A sequence of 1-byte characters is broken into multiple chunks to + # adjust the kerning between adjacent chunks. Each chunk is emitted + # with a separate command: 1-byte characters use the regular text show + # command (TJ) with appropriate kerning between chunks, whereas + # multibyte characters use the XObject command (Do). (If using Type + # 42 fonts, all of this complication is avoided, but of course, + # subsetting those fonts is complex/hard to implement.) else: # List of (start_x, [prev_kern, char, char, ...]), w/o zero kerns. singlebyte_chunks = [] diff --git a/lib/matplotlib/tests/baseline_images/test_text/text_pdf_kerning.pdf b/lib/matplotlib/tests/baseline_images/test_text/text_pdf_kerning.pdf new file mode 100644 index 000000000000..7db9a1b44fad Binary files /dev/null and b/lib/matplotlib/tests/baseline_images/test_text/text_pdf_kerning.pdf differ diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index 72fec721a43a..435da825a2cd 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -714,3 +714,9 @@ def test_update_mutate_input(): def test_invalid_color(): with pytest.raises(ValueError): plt.figtext(.5, .5, "foo", c="foobar") + + +@image_comparison(['text_pdf_kerning.pdf'], style='mpl20') +def test_pdf_kerning(): + plt.figure() + plt.figtext(0.1, 0.5, "ATATATATATATATATATA", size=30)