FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use more specific patterns for bundled mathtext fonts by QuLogic · Pull Request #32302 · matplotlib/matplotlib · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 17 additions & 11 deletions lib/matplotlib/_mathtext.py
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
Expand Up @@ -364,6 +364,12 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
self._fonts['default'] = default_font
self._fonts['regular'] = default_font

def _normalize_font_pattern(self, pattern: str) -> str:
for option in ['stretch', 'style', 'variant', 'weight']:
if f':{option}=' not in pattern:
pattern = f'{pattern}:{option}=normal'
return pattern

def _get_font(self, font: str) -> FT2Font:
basename = self.fontmap.get(font, font)
cached_font = self._fonts.get(basename)
Expand Down Expand Up @@ -492,7 +498,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag

super().__init__(default_font_prop, load_glyph_flags)
for key, val in self._fontmap.items():
fullpath = findfont(val)
fullpath = findfont(self._normalize_font_pattern(val))
self.fontmap[key] = fullpath
self.fontmap[val] = fullpath

Expand Down Expand Up @@ -617,7 +623,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
}

for size, name in stixsizedaltfonts.items():
fullpath = findfont(name)
fullpath = findfont(self._normalize_font_pattern(name))
self.fontmap[size] = fullpath
self.fontmap[name] = fullpath

Expand Down Expand Up @@ -719,7 +725,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
'5': 'STIXSizeFiveSym',
})
for key, name in self._fontmap.items():
fullpath = findfont(name)
fullpath = findfont(self._normalize_font_pattern(name))
self.fontmap[key] = fullpath
self.fontmap[name] = fullpath

Expand Down Expand Up @@ -748,9 +754,9 @@ class DejaVuSerifFonts(DejaVuFonts):
"""
_fontmap = {
'rm': 'DejaVu Serif',
'it': 'DejaVu Serif:italic',
'it': 'DejaVu Serif:style=italic',
'bf': 'DejaVu Serif:weight=bold',
'bfit': 'DejaVu Serif:italic:bold',
'bfit': 'DejaVu Serif:style=italic:weight=bold',
'sf': 'DejaVu Sans',
'tt': 'DejaVu Sans Mono',
'ex': 'DejaVu Serif Display',
Expand All @@ -769,9 +775,9 @@ class DejaVuSansFonts(DejaVuFonts):
"""
_fontmap = {
'rm': 'DejaVu Sans',
'it': 'DejaVu Sans:italic',
'it': 'DejaVu Sans:style=italic',
'bf': 'DejaVu Sans:weight=bold',
'bfit': 'DejaVu Sans:italic:bold',
'bfit': 'DejaVu Sans:style=italic:weight=bold',
'sf': 'DejaVu Sans',
'tt': 'DejaVu Sans Mono',
'ex': 'DejaVu Sans Display',
Expand All @@ -796,11 +802,11 @@ class StixFonts(UnicodeFonts):
"""
_fontmap = {
'rm': 'STIXGeneral',
'it': 'STIXGeneral:italic',
'it': 'STIXGeneral:style=italic',
'bf': 'STIXGeneral:weight=bold',
'bfit': 'STIXGeneral:italic:bold',
'bfit': 'STIXGeneral:style=italic:weight=bold',
'nonunirm': 'STIXNonUnicode',
'nonuniit': 'STIXNonUnicode:italic',
'nonuniit': 'STIXNonUnicode:style=italic',
'nonunibf': 'STIXNonUnicode:weight=bold',
'0': 'STIXGeneral',
'1': 'STIXSizeOneSym',
Expand All @@ -815,7 +821,7 @@ class StixFonts(UnicodeFonts):
def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlags):
TruetypeFonts.__init__(self, default_font_prop, load_glyph_flags)
for key, name in self._fontmap.items():
fullpath = findfont(name)
fullpath = findfont(self._normalize_font_pattern(name))
self.fontmap[key] = fullpath
self.fontmap[name] = fullpath

Expand Down
38 changes: 30 additions & 8 deletions lib/matplotlib/tests/test_mathtext.py
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
@@ -1,6 +1,7 @@
from __future__ import annotations

import io
import logging
from pathlib import Path
import platform
import re
Expand Down Expand Up @@ -292,14 +293,6 @@ def test_short_long_accents(fig_test, fig_ref):
0, .5, "$" + "".join(fr"\{l} a" for l in corresponding_long_accs) + "$")


def test_fontinfo():
fontpath = mpl.font_manager.findfont("DejaVu Sans")
font = mpl.ft2font.FT2Font(fontpath)
table = font.get_sfnt_table("head")
assert table is not None
assert table['version'] == (1, 0)


# See gh-26152 for more context on this xfail
@pytest.mark.xfail(pyparsing_version.release == (3, 1, 0),
reason="Error messages are incorrect for this version")
Expand Down Expand Up @@ -379,6 +372,35 @@ def test_mathtext_exceptions(math, msg):
parser.parse(math)


def test_mathtext_fonts(caplog):
# Changing these `font.*` rcParams should not affect mathtext, as that uses bundled
# fonts with a specific mapping to style/weight. TODO: font.stretch and font.variant
# are difficult to test as we don't have anything that might accidentally be
# substituted there.
mpl.rcParams['font.style'] = 'italic'
mpl.rcParams['font.weight'] = 100
# Use explicitly available default so that it doesn't warn.
default_font = fm.FontProperties('DejaVu Sans:style=normal:weight=normal')

_mathtext.BakomaFonts(default_font, LoadFlags.DEFAULT)
dejavusans = _mathtext.DejaVuSansFonts(default_font, LoadFlags.DEFAULT)
dejavuserif = _mathtext.DejaVuSerifFonts(default_font, LoadFlags.DEFAULT)
stix = _mathtext.StixFonts(default_font, LoadFlags.DEFAULT)
stixsans = _mathtext.StixSansFonts(default_font, LoadFlags.DEFAULT)

# Setting font.style should not have accidentally changed any roman font to italic.
assert dejavusans.fontmap['rm'].endswith('DejaVuSans.ttf')
assert dejavuserif.fontmap['rm'].endswith('DejaVuSerif.ttf')
assert stix.fontmap['rm'].endswith('STIXGeneral.ttf')
assert stixsans.fontmap['rm'].endswith('STIXGeneral.ttf')
# No bakoma font is actually italic, so that setting won't affect it.

# If `font.weight` leaked into the mathtext setup, then it should cause a warning to
# be logged from the font manager for a missing font weight.
records = [record for record in caplog.records if record.levelno == logging.WARNING]
assert records == []


def test_get_unicode_index_exception():
with pytest.raises(ValueError):
_mathtext.get_unicode_index(r'\foo')
Expand Down
Loading

Back | FazBrowse Home | New Git URL