Skip to content

Commit 8962946

Browse files
committed
Don't try to find TeX-only fonts when layouting TeX text.
Text.is_math_text is only ever called with self.get_usetex() as second argument, so just change it to a private method (Text._preprocess_math) that calls self.get_usetex() internally; deprecate is_math_text which is clearly a private internal helper. This also helps clarifying that usetex mode is only ever called if self.get_usetex() is True (in the textobj.is_math_text(...) line, textobj is actually the same object(!) as self so shares the same get_usetex() value). In Text._get_layout, if self.get_usetex() is True (and only in that case), self._fontproperties can refer to a TeX-only font (e.g. "Computer Modern Roman"). If that's the case, don't try to call renderer.get_text_width_height_descent with ismath=False, as that would trigger a font resolution that can fail (perhaps there's no "Computer Modern Roman" font available in the font cache; it may only be available to TeX) and cause a spurious warning.
1 parent b140c65 commit 8962946

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Deprecations
2+
````````````
3+
4+
``Text.is_math_text`` is deprecated.

lib/matplotlib/text.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,12 @@ def _get_layout(self, renderer):
286286

287287
# Full vertical extent of font, including ascenders and descenders:
288288
_, lp_h, lp_d = renderer.get_text_width_height_descent(
289-
"lp", self._fontproperties, ismath=False)
289+
"lp", self._fontproperties,
290+
ismath="TeX" if self.get_usetex() else False)
290291
min_dy = (lp_h - lp_d) * self._linespacing
291292

292293
for i, line in enumerate(lines):
293-
clean_line, ismath = self.is_math_text(line, self.get_usetex())
294+
clean_line, ismath = self._preprocess_math(line)
294295
if clean_line:
295296
w, h, d = renderer.get_text_width_height_descent(
296297
clean_line, self._fontproperties, ismath=ismath)
@@ -697,8 +698,7 @@ def draw(self, renderer):
697698
y = y + posy
698699
if renderer.flipy():
699700
y = canvash - y
700-
clean_line, ismath = textobj.is_math_text(line,
701-
self.get_usetex())
701+
clean_line, ismath = textobj._preprocess_math(line)
702702

703703
if textobj.get_path_effects():
704704
from matplotlib.patheffects import PathEffectRenderer
@@ -1152,6 +1152,7 @@ def set_text(self, s):
11521152
self.stale = True
11531153

11541154
@staticmethod
1155+
@cbook.deprecated("3.1")
11551156
def is_math_text(s, usetex=None):
11561157
"""
11571158
Returns a cleaned string and a boolean flag.
@@ -1174,6 +1175,27 @@ def is_math_text(s, usetex=None):
11741175
else:
11751176
return s.replace(r'\$', '$'), False
11761177

1178+
def _preprocess_math(self, s):
1179+
"""
1180+
Return the string *s* after mathtext preprocessing, and the kind of
1181+
mathtext support needed.
1182+
1183+
- If *self* is configured to use TeX, return *s* unchanged except that
1184+
a single space gets escaped, and the flag "TeX".
1185+
- Otherwise, if *s* is mathtext (has an even number of unescaped dollar
1186+
signs), return *s* and the flag True.
1187+
- Otherwise, return *s* with dollar signs unescaped, and the flag
1188+
False.
1189+
"""
1190+
if self.get_usetex():
1191+
if s == " ":
1192+
s = r"\ "
1193+
return s, "TeX"
1194+
if cbook.is_math_text(s):
1195+
return s, True
1196+
else:
1197+
return s.replace(r"\$", "$"), False
1198+
11771199
def set_fontproperties(self, fp):
11781200
"""
11791201
Set the font properties that control the text.

0 commit comments

Comments
 (0)