-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathfonts.py
More file actions
74 lines (66 loc) · 2.9 KB
/
fonts.py
File metadata and controls
74 lines (66 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""
Overrides related to math fonts.
"""
import matplotlib as mpl
from matplotlib.font_manager import findfont, ttfFontProperty
from matplotlib.mathtext import MathTextParser
from . import warnings
try: # newer versions
from matplotlib._mathtext import UnicodeFonts
except ImportError: # older versions
from matplotlib.mathtext import UnicodeFonts
# Global constant
WARN_MATHPARSER = True
class _UnicodeFonts(UnicodeFonts):
"""
A simple `~matplotlib._mathtext.UnicodeFonts` subclass that
interprets ``rc['mathtext.default'] != 'regular'`` in the presence of
``rc['mathtext.fontset'] == 'custom'`` as possibly modifying the active font.
Works by permitting the ``rc['mathtext.rm']``, ``rc['mathtext.it']``,
etc. settings to have the dummy value ``'regular'`` instead of a valid family
name, e.g. ``rc['mathtext.it'] == 'regular:italic'`` (permitted through an
override of the `~matplotlib.rcsetup.validate_font_properties` validator).
When this dummy value is detected then the font properties passed to
`~matplotlib._mathtext.TrueTypeFont` are taken by replacing ``'regular'``
in the "math" fontset with the active font name.
"""
def __init__(self, *args, **kwargs):
# Initialize font
# NOTE: Could also capture the 'default_font_prop' passed as positional
# argument but want to guard against keyword changes. This entire API is
# private and it is easier to do graceful fallback with _fonts dictionary.
ctx = {} # rc context
regular = {} # styles
for texfont in ('cal', 'rm', 'tt', 'it', 'bf', 'sf'):
key = 'mathtext.' + texfont
prop = mpl.rcParams[key]
if prop.startswith('regular'):
ctx[key] = prop.replace('regular', 'sans', 1)
regular[texfont] = prop
with mpl.rc_context(ctx):
super().__init__(*args, **kwargs)
# Apply current font replacements
global WARN_MATHPARSER
if (
hasattr(self, 'fontmap')
and hasattr(self, '_fonts')
and 'regular' in self._fonts
):
font = self._fonts['regular'] # an ft2font.FT2Font instance
font = ttfFontProperty(font)
for texfont, prop in regular.items():
prop = prop.replace('regular', font.name)
self.fontmap[texfont] = findfont(prop, fallback_to_default=False)
elif WARN_MATHPARSER:
# Suppress duplicate warnings in case API changes
warnings._warn_proplot('Failed to update the math text parser.')
WARN_MATHPARSER = False
# Replace the parser
try:
mapping = MathTextParser._font_type_mapping
if mapping['custom'] is UnicodeFonts:
mapping['custom'] = _UnicodeFonts
except (KeyError, AttributeError):
warnings._warn_proplot('Failed to update math text parser.')
WARN_MATHPARSER = False