Skip to content

[Bug]: savefig to PNG raises 'failed to load glyph' for small text in macOS Helvetica (Regular) with matplotlib 3.11.0 and 3.11.2 #32650

Open

Description

@pablobernabeu

Bug summary

With matplotlib 3.11.0 and 3.11.2 and default rcParams, saving a figure to PNG raises RuntimeError: failed to load glyph when it has text in the regular face of macOS's Helvetica at 7 pixels per em or fewer, such as 5 pt text at 72 dpi. matplotlib 3.10.9 saves the same figure. With 3.11.2, saving it as PDF, SVG, PS or EPS works.

Code for reproduction

# macOS only: 'Helvetica' must resolve to /System/Library/Fonts/Helvetica.ttc
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(2, 1))
fig.text(0.1, 0.5, "yield", fontfamily="Helvetica", fontsize=5)
fig.savefig("out.png", dpi=72)

Actual outcome

Traceback (most recent call last):
  File "/Users/runner/work/depictr-py/depictr-py/diag/report_snippet.py", line 7, in <module>
    fig.savefig("out.png", dpi=72)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/figure.py", line 3515, in savefig
    self.canvas.print_figure(fname, **kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/backend_bases.py", line 2289, in print_figure
    result = print_method(
        filename,
    ...<3 lines>...
        bbox_inches_restore=_bbox_inches_restore,
        **kwargs)
  File ".../site-packages/matplotlib/backend_bases.py", line 2146, in <lambda>
    print_method = functools.wraps(meth)(lambda *args, **kwargs: meth(
                                                                 ~~~~^
        *args, **{k: v for k, v in kwargs.items() if k not in skip}))
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/backends/backend_agg.py", line 537, in print_png
    self._print_pil(filename_or_obj, "png", pil_kwargs, metadata)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/backends/backend_agg.py", line 485, in _print_pil
    FigureCanvasAgg.draw(self)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".../site-packages/matplotlib/backends/backend_agg.py", line 438, in draw
    self.figure.draw(self.renderer)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/artist.py", line 94, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File ".../site-packages/matplotlib/artist.py", line 71, in draw_wrapper
    return draw(artist, renderer)
  File ".../site-packages/matplotlib/figure.py", line 3282, in draw
    mimage._draw_list_compositing_images(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        renderer, self, artists, self.suppressComposite)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/image.py", line 133, in _draw_list_compositing_images
    a.draw(renderer)
    ~~~~~~^^^^^^^^^^
  File ".../site-packages/matplotlib/artist.py", line 71, in draw_wrapper
    return draw(artist, renderer)
  File ".../site-packages/matplotlib/text.py", line 853, in draw
    bbox, info, _ = self._get_layout(renderer)
                    ~~~~~~~~~~~~~~~~^^^^^^^^^^
  File ".../site-packages/matplotlib/text.py", line 479, in _get_layout
    w, h, d = _get_text_metrics_with_cache(
              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        renderer, clean_line, self._fontproperties,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        ismath=ismath, dpi=self.get_figure(root=True).dpi)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/text.py", line 59, in _get_text_metrics_with_cache
    return get_text_metrics(text, fontprop.copy(), ismath, dpi)
  File ".../site-packages/matplotlib/text.py", line 125, in _text_metrics
    return local_renderer.get_text_width_height_descent(text, fontprop, ismath)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../site-packages/matplotlib/backends/backend_agg.py", line 263, in get_text_width_height_descent
    font.set_text(s, 0.0, flags=get_hinting_flag())
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: failed to load glyph

(3.11.2 on macOS 26.7.2. ... stands for /Library/Frameworks/Python.framework/Versions/3.13/lib/python3.13.)

Expected outcome

The figure is saved, as it is with 3.10.9.

Additional information

FT2Font.load_glyph on the same face, size and flags reports the FreeType error that set_text leaves out of its message (src/ft2font.cpp lines 477 to 479 in 3.11.2):

from matplotlib.ft2font import FT2Font, LoadFlags

font = FT2Font("/System/Library/Fonts/Helvetica.ttc")
font.set_size(5, 72)
font.load_glyph(font.get_char_index(ord("y")), flags=LoadFlags.DEFAULT)
# RuntimeError: FT_Load_Glyph (ft2font.cpp line 646) failed with error 0x85: division by zero

The error appears only with the font's native hinter (LoadFlags.DEFAULT or NO_AUTOHINT), only in face 0 of the six faces in Helvetica.ttc and only at 7 px/em or fewer (whole sizes from 1 to 15 tested). FreeType 2.14.3 is the copy bundled with 3.11.0 and 3.11.2. FreeType 2.6.1 (the copy in 3.10.9, with FT2Font(path, hinting_factor=1)), 2.13.2 and 2.15.0 (both called through ctypes) return the same error, and they load the glyph when the horizontal resolution is 8 times the vertical one, as 3.10.9's default hinting_factor of 8 made it. So the font's own hinting instructions appear to fail at these sizes in every FreeType version tried. 3.10.9's defaults did not run them at these sizes: its text.hinting default was 'force_autohint', and its hinting factor raised the horizontal resolution. 3.11 changed the text.hinting default to 'default' (#29816) and dropped the hinting factor (#31521). I could not find the text.hinting change in the 3.11.0 API changes.

As a workaround, setting plt.rcParams["text.hinting"] to "force_autohint" or "no_hinting" saves the figure with 3.11.0 and 3.11.2. A downstream project, Tavotto, hit the same error with matplotlib 3.11.2 and now retries a failing call with text.hinting set to "no_hinting" (Tavotto/Tavotto#1872). This differs from #32063, where text in fonts with embedded bitmap strikes comes out blank: here the figure is not saved at all.

Two changes may be worth considering. One is, when FT_Load_Glyph fails with hinting enabled, to lay out and load the text again without hinting or with the auto-hinter, with a warning, because FT2Font::layout passes the same load flags to raqm (src/ft2font.cpp line 328). The other is to mention the new text.hinting default in the 3.11 API changes. On main, #32120 loads these glyphs through FT_CHECK, so the message will name FreeType's error from 3.12. The hinting default and the load flags are unchanged there, so I expect the same failure, although I have not run main.

Tested on GitHub Actions runners with macOS 15.8.2 and 26.7.2 (arm64), Python 3.13.17 and wheels from PyPI. Scripts: https://github.com/pablobernabeu/depictr-py/tree/61ad513081f9000392838bd7e975e5232b5c3864/diag. Logs: mpl_only.py, ft_versions.py, hinting_factor.py, the snippets above and font_info.py. I found it through plotnine, whose default theme lists Helvetica first among its sans-serif fonts.

Operating system

macOS 15.8.2 and macOS 26.7.2 (arm64)

Matplotlib Version

3.11.2 and 3.11.0 (3.11.1 not tested). 3.10.9 is not affected.

Matplotlib Backend

agg

Python version

3.13.17

Jupyter version

Not used

Installation

pip

Activity

  1. iccir commented on Nov 23, 2026

    @iccir
    Member

    I can reproduce on macOS 14 running on main.

  2. QuLogic commented on Nov 27, 2026

    @QuLogic
    Member

    I'm uncertain whether the problem here is a regression in FreeType (since we updated it by such a large amount in 3.11), or in the way we are doing things (again since we changed that procedure so much for 3.11).

    Since this has to do with smaller font sizes, I do wonder if it has something to do with bitmap strikes. I do see you mentioned #32063, but not whether you tried to disable them (see #32631).

  3. QuLogic commented on Dec 1, 2026

    @QuLogic
    Member

    OK, I was able to find a copy of Helvetica.ttc, and thus confirm that it is not fixed by disabling bitmap strikes. I have been able to reproduce it with FreeType directly, so I have reported a bug upstream.

  4. QuLogic commented on Dec 1, 2026

    @QuLogic
    Member

    @pablobernabeu/@iccir can you please respond to the upstream request and/or cross check with the font file I managed to find that's mentioned there?

  5. iccir commented on Dec 2, 2026

    @iccir
    Member

    @pablobernabeu/@iccir can you please respond to the upstream request and/or cross check with the font file I managed to find that's mentioned there?

    I'm not able to create an account upstream for some reason. The Helvetica you found doesn't appear to be a match:

    [~] shasum /Users/iccir/Downloads/Helvetica.ttc 
    98c12f0d63168b20635fb51d638929753b4e6aed  /Users/iccir/Downloads/Helvetica.ttc
    
    [~] shasum /System/Library/Fonts/Helvetica.ttc 
    72674ba8e77aa1e013f75a0b2241069fba539ba3  /System/Library/Fonts/Helvetica.ttc
    

    I sent you a DM on Discourse with a download link to it.

  6. pablobernabeu commented on Dec 3, 2026

    @pablobernabeu
    Author

    Thanks @QuLogic for tracking down a copy of the font, ruling out the bitmap strikes and reporting it to FreeType, and @iccir for the checksums.

    On the cross-check: the Helvetica.ttc on the GitHub runners where I found this (macOS 15.8.2 and 26.7.2, arm64) is the same file as iccir's: SHA-1 72674ba8e77aa1e013f75a0b2241069fba539ba3, SHA-256 25eceb458d4baf628ee0b6a135a9f8ac5ec7b2826646720834bd2bf00dfc825a, 2369592 bytes, version 17.0d1e1 in all six faces. So the file iccir sent is the one in my report, and the copy you found (98c12f0d…) is a different one. If that copy fails too, more than one version of the font is affected. In face 0 (Regular), the hinting tables have SHA-1 b41046de2c8bcf0391d47a859958881c48660841 (fpgm), acddd09a07833dcc76a4b8ba4648816fe5cc99df (prep) and 6d0ef0e36eb1b869101540f27538539299eb1e5a (cvt ), in case that helps to tell whether your copy has the same instructions.

    With 3.11.2's FreeType 2.14.3 and LoadFlags.DEFAULT, all 62 ASCII letters and digits fail in face 0 at every size from 1 to 7 px/em, and none fails in the other five faces from 1 to 15 px/em. Log: https://github.com/pablobernabeu/depictr-py/actions/runs/46990505188 (script: diag/font_hashes.py at fbbe21f).

    I'll try to add this to the FreeType work item. If I can't get an account there either, could you pass the checksums on, @QuLogic?

  7. QuLogic commented on Dec 3, 2026

    @QuLogic
    Member

    I have passed along the other font file to the upstream tracker.

  8. added this to the v3.11.3 milestone on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions