Skip to content

Commit a5cabf2

Browse files
committed
Fix mathtext font fallback for glyphs missing from math fonts
Search the font's fallback chain before substituting a dummy glyph, so that e.g. CJK characters in math expressions render like they do in regular text. Closes matplotlib#29173
1 parent bccbad9 commit a5cabf2

5 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Font fallback in mathtext
2+
-------------------------
3+
4+
Mathematical text (``mathtext`` and ``$...$`` strings) now falls back to other
5+
fonts when a character is missing from the selected math font, mirroring the
6+
behavior of regular text. Previously, characters absent from the math fonts --
7+
such as CJK characters -- were replaced by a dummy glyph; they now render using
8+
the configured fonts and their fallback chain (with the Last Resort font as a
9+
final fallback), just like regular text.

‎lib/matplotlib/_mathtext.py‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from . import cbook
3333
from ._mathtext_data import (
3434
latex_to_bakoma, stix_glyph_fixes, stix_virtual_fonts, tex2uni)
35-
from .font_manager import FontProperties, findfont, get_font
35+
from .font_manager import FontProperties, findfont, fontManager, get_font
3636
from .ft2font import FT2Font, Kerning, LoadFlags
3737

3838

@@ -683,6 +683,19 @@ def _get_glyph(self, fontname: str, font_class: str,
683683
if (fontname in ('it', 'regular', 'normal')
684684
and isinstance(self, StixFonts)):
685685
return self._get_glyph('rm', font_class, sym)
686+
# Before substituting a dummy glyph, fall back through the
687+
# configured text fonts and their fallback chain (system fonts
688+
# and the Last Resort font), mirroring the regular-text path, so
689+
# characters the math fonts lack -- such as CJK characters --
690+
# still render. See
691+
# https://github.com/matplotlib/matplotlib/issues/29173.
692+
text_font = get_font(
693+
fontManager._find_fonts_by_props(self.default_font_prop))
694+
for fallback_font in [text_font, *text_font._fallbacks]:
695+
if fallback_font.get_char_index(uniindex, _fallback=False):
696+
_log.info("Substituting symbol %a from font %s.",
697+
sym, fallback_font.family_name)
698+
return fallback_font, uniindex, False
686699
_log.warning("Font %r does not have a glyph for %a [U+%x], "
687700
"substituting with a dummy symbol.",
688701
new_fontname, sym, uniindex)

‎lib/matplotlib/ft2font.pyi‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,17 @@ class FT2Font(Buffer):
249249
features: tuple[str, ...] | None = ...,
250250
language: str | tuple[tuple[str, int, int], ...] | None = ...,
251251
) -> list[LayoutItem]: ...
252+
@property
253+
def _fallbacks(self) -> list[FT2Font]: ...
252254
def clear(self) -> None: ...
253255
def draw_glyph_to_bitmap(
254256
self, image: NDArray[np.uint8], x: int, y: int, glyph: Glyph, antialiased: bool = ...
255257
) -> None: ...
256258
def draw_glyphs_to_bitmap(self, antialiased: bool = ...) -> None: ...
257259
def get_bitmap_offset(self) -> tuple[int, int]: ...
258-
def get_char_index(self, codepoint: CharacterCodeType) -> GlyphIndexType: ...
260+
def get_char_index(
261+
self, codepoint: CharacterCodeType, *, _fallback: bool = ...
262+
) -> GlyphIndexType: ...
259263
def get_charmap(self) -> dict[CharacterCodeType, GlyphIndexType]: ...
260264
def get_descent(self) -> int: ...
261265
def get_glyph_name(self, index: GlyphIndexType) -> str: ...

‎lib/matplotlib/tests/test_mathtext.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,20 @@ def test_mathtext_fallback(fallback, fontlist):
478478
mpl.font_manager.fontManager.ttflist.pop()
479479

480480

481+
@pytest.mark.parametrize("char", ['中', '한', 'あ']) # Chinese, Korean, Japanese
482+
def test_mathtext_cjk_fallback(char):
483+
# Characters that the math fonts lack -- such as CJK characters -- should
484+
# fall back through the font's fallback chain (mirroring regular text)
485+
# rather than being replaced by a dummy glyph.
486+
# https://github.com/matplotlib/matplotlib/issues/29173
487+
fonts = _mathtext.DejaVuSansFonts(fm.FontProperties(), LoadFlags.NO_HINTING)
488+
font, num, _ = fonts._get_glyph('rm', 'normal', char)
489+
# The original code point is preserved (not the 0xA4 dummy currency sign)...
490+
assert num == ord(char)
491+
# ... and the returned font actually contains a glyph for it.
492+
assert font.get_char_index(num, _fallback=False) != 0
493+
494+
481495
def test_math_to_image(tmp_path):
482496
mathtext.math_to_image('$x^2$', tmp_path / 'example.png')
483497
mathtext.math_to_image('$x^2$', io.BytesIO())

‎src/ft2font_wrapper.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1894,6 +1894,9 @@ PYBIND11_MODULE(ft2font, m, py::mod_gil_not_used())
18941894
.def_property_readonly(
18951895
"fname", &PyFT2Font_fname,
18961896
"The original filename for this object.")
1897+
.def_property_readonly(
1898+
"_fallbacks", [](PyFT2Font *self) { return self->fallbacks; },
1899+
"The fallback fonts used to find glyphs missing from this font.")
18971900

18981901
.def_buffer([](PyFT2Font &self) -> py::buffer_info {
18991902
return self.get_image().request();

0 commit comments

Comments
 (0)