-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
mathtext: add support for unicode mathematics fonts #31064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
llohse
wants to merge
5
commits into
matplotlib:main
Choose a base branch
from
llohse:mathtext-unicode-textoverhaul
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d437313
mathtext: add basic support for OpenType mathematics fonts
llohse 1037cb3
add baseline images for testing the OpenType unicode math fonts
llohse 38b00d5
bundle STIX Two Math in tests
llohse 069859a
handle mathnormal
llohse 7e2dd3e
revise comments
llohse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,7 +31,8 @@ | |
| import matplotlib as mpl | ||
| from . import cbook | ||
| from ._mathtext_data import ( | ||
| latex_to_bakoma, stix_glyph_fixes, stix_virtual_fonts, tex2uni) | ||
| latex_to_bakoma, stix_glyph_fixes, stix_virtual_fonts, tex2uni, | ||
| unicode_math_lut, greek_uppercase_domain, greek_lowercase_domain) | ||
| from .font_manager import FontProperties, findfont, get_font | ||
| from .ft2font import FT2Font, Kerning, LoadFlags | ||
|
|
||
|
|
@@ -782,6 +783,134 @@ def get_font_constants(self) -> type[FontConstantsBase]: | |
| return DejaVuSansFontConstants | ||
|
|
||
|
|
||
| class UnicodeMathFonts(TruetypeFonts): | ||
| """ | ||
| A font handling class for Unicode mathematics fonts. | ||
|
|
||
| In addition to what TruetypeFonts provides, this class: | ||
|
|
||
| - supports mapping alphanumeric characters (latin and greek) to different alphabet | ||
| styles (such as bold, italic, fraktur, script, double-struck, ...) defined in | ||
| the Unicode standard. | ||
|
|
||
| """ | ||
|
|
||
| def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlags): | ||
| super().__init__(default_font_prop, load_glyph_flags) | ||
| prop = mpl.rcParams['mathtext.mathfont'] # type: ignore[index] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will break the caching for |
||
| font = findfont(prop) | ||
| self.fontmap['mathfont'] = font | ||
|
|
||
| _slanted_symbols = set(r"\int \oint".split()) | ||
|
|
||
| def _get_font(self, font: str | int) -> FT2Font: | ||
| # work around, since we only populate 'mathfont' in the fontmap | ||
| if font not in ('default', 'regular'): | ||
| font = 'mathfont' | ||
|
|
||
| return super()._get_font(font) | ||
|
|
||
| def _get_glyph(self, fontname: str, font_class: str, | ||
| sym: str) -> tuple[FT2Font, CharacterCodeType, bool]: | ||
| # `fontname' is one of rm cal it tt sf bf bfit default bb frak scr regular | ||
| # font_class is not currently supported | ||
|
|
||
| # 1) map symbol to unicode index | ||
| try: | ||
| uniindex = get_unicode_index(sym) | ||
| found_symbol = True | ||
| except ValueError: | ||
| uniindex = ord('?') | ||
| found_symbol = False | ||
| _log.warning("No TeX to Unicode mapping for %a.", sym) | ||
|
|
||
| if fontname in ('default', 'regular'): | ||
| slanted = False | ||
| font = self._get_font(fontname) | ||
| return font, uniindex, slanted | ||
|
|
||
| # 2) remap unicode index based on fontname and font_class (bf, it, ...) | ||
| # Unicode mathematical alphanumeric symbols define all (relevant) variants | ||
|
|
||
| # from here on: use the Math font | ||
| new_fontname = 'mathfont' | ||
|
|
||
| def _is_digit(codepoint: CharacterCodeType) -> bool: | ||
| return 0x30 <= codepoint <= 0x39 | ||
|
|
||
| def _is_latin_uppercase(codepoint: CharacterCodeType) -> bool: | ||
| return 0x41 <= codepoint <= 0x5a | ||
|
|
||
| def _is_latin_lowercase(codepoint: CharacterCodeType) -> bool: | ||
| return 0x61 <= codepoint <= 0x7a | ||
|
|
||
| def _is_greek_uppercase(codepoint: CharacterCodeType) -> bool: | ||
| return codepoint in greek_uppercase_domain | ||
|
|
||
| def _is_greek_lowercase(codepoint: CharacterCodeType) -> bool: | ||
| return codepoint in greek_lowercase_domain | ||
|
|
||
| # digits, latin letters, and greek letter are mapped by separate rules | ||
| if _is_digit(uniindex): | ||
| _alphabet_map = { | ||
| 'normal': 'up', | ||
| 'rm': 'up', | ||
| 'it': 'up', # italic digits are not available | ||
| 'tt': 'tt', | ||
| 'sf': 'sfup', | ||
| 'bf': 'bfup', | ||
| 'bfit': 'bfup', # bold italic digits are not available | ||
| 'bb': 'bb', | ||
| } | ||
| alphabet = _alphabet_map.get(fontname, 'up') | ||
| elif _is_latin_uppercase(uniindex) or _is_latin_lowercase(uniindex): | ||
| _alphabet_map = { | ||
| 'normal': 'it', | ||
| 'rm': 'up', | ||
| 'cal': 'scr', | ||
| 'it': 'it', | ||
| 'tt': 'tt', | ||
| 'sf': 'sfup', | ||
| 'bf': 'bfup', | ||
| 'bfit': 'bfit', | ||
| 'bb': 'bb', | ||
| 'frak': 'frak', | ||
| 'scr': 'scr' | ||
| } | ||
| alphabet = _alphabet_map.get(fontname, 'up') | ||
| elif _is_greek_uppercase(uniindex): | ||
| _alphabet_map = { | ||
| 'normal': 'up', | ||
| 'rm': 'up', | ||
| 'it': 'it', | ||
| 'bf': 'bfup', | ||
| 'bfit': 'bfit', | ||
| } | ||
| alphabet = _alphabet_map.get(fontname, 'up') | ||
| elif _is_greek_lowercase(uniindex): | ||
| _alphabet_map = { | ||
| 'normal': 'it', | ||
| 'rm': 'up', | ||
| 'it': 'it', | ||
| 'bf': 'bfup', | ||
| 'bfit': 'bfit', | ||
| } | ||
| alphabet = _alphabet_map.get(fontname, 'up') | ||
| else: | ||
| alphabet = 'up' | ||
|
|
||
| if alphabet != 'up': | ||
| alphabet_lut = unicode_math_lut.get(alphabet, {}) | ||
| new_uniindex = alphabet_lut.get(uniindex, uniindex) | ||
| else: | ||
| new_uniindex = uniindex | ||
|
|
||
| slanted = (alphabet == 'it') or sym in self._slanted_symbols | ||
| font = self._get_font(new_fontname) | ||
|
|
||
| return font, new_uniindex, slanted | ||
|
|
||
|
|
||
| class StixFonts(UnicodeFonts): | ||
| """ | ||
| A font handling class for the STIX fonts. | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, math fonts should have tables with various layout metrics. We currently have those hard-coded in the various
FontsConstantsBasesubclasses, and they are likely incorrect for an arbitrary math font.So this will likely need to parse this data out of the font and implement at least
get_axis_heightthat was added in #31046,get_xheightmaybe using #31050, andget_quadfrom #31110. But it is likely that you will want to refactor some of those remaining uses of the constants so that they fetch the information from the fonts as well.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fully agree. Doing this may involve some refactoring though, because the FontsConstantsBase subclass could not be determined purely from fontname but would be dynamically populated based on the loaded OpenType font.
That said, I made some experiments locally. Unfortunately, Freetype does not parse the MATH table. We could use fonttools, which is a hard dependency anyway.
There are several open questions how to map the OpenType layout metrics to the legacy TeX-inspired variables used in mathtext. Does it make sense to postpone that to a separate PR and focus on the basics here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I would like it to happen, I'm not sure this will be ready for 3.11. So if we're aiming for 3.12, I think it's okay to spend some time getting everything worked out in separate PRs.