Fix mathtext font fallback for glyphs missing from math fonts#31766
Fix mathtext font fallback for glyphs missing from math fonts#317661wos wants to merge 1 commit into
Conversation
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
|
Thank you for opening your first PR into Matplotlib! If you have not heard from us in a week or so, please leave a new comment below and that should bring it to our attention. Most of our reviewers are volunteers and sometimes things fall through the cracks. We also ask that you please finish addressing any review comments on this PR and wait for it to be merged (or closed) before opening a new one, as it can be a valuable learning experience to go through the review process. You can also join us on discourse chat for real-time discussion. For details on testing, writing docs, and our review process, please see the developer guide. We strive to be a welcoming and open project. Please follow our Code of Conduct. |
PR summary
Closes #29173.
CJK characters render correctly in regular text but are replaced by a ¤
placeholder when they appear inside a math expression (
$...$). The cause isthat mathtext has no glyph-level font fallback: when a character is missing from
the math font (and the STIX/Bakoma math fallbacks),
UnicodeFonts._get_glyphreturns a dummy glyph, whereas regular text walks the font's fallback chain.
This change makes
_get_glyphsearch the configured text fonts and theirfallback chain (the same ones regular text uses, ending in the Last Resort font)
before falling back to the dummy glyph. The search only runs when the glyph is
missing, so text that already renders is unaffected. The FT2Font fallback list is
exposed to Python as a private
FT2Font._fallbacksproperty to support this.With a CJK font configured, CJK characters render in math just like in regular
text; otherwise they fall back to the Last Resort font instead of the dummy.
AI Disclosure
I used generative AI (Claude) to help dig through the font code and draft the
implementation, tests, and this description. I reported the issue, decided on the
approach, and reviewed and tested everything locally.
PR checklist
test_mathtext_cjk_fallback)