Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df3d798

Browse files
committed
Various TextPath cleanups.
In TextToPath.get_text_path, deprecate the usetex parameter in favor of a tristate ismath={False, True, "TeX"}, which is consistent with all other low-level text handling APIs (at the renderer level). (TextToPath methods should be considered as low-level APIs; the main high-level API is TextPath.) Deprecate `TextPath.text_get_vertices_codes` and `TextPath.is_math_text` which are clearly helper functions for the main constructor. Moreover, previously, if TextPath was called with `usetex=False` and `rcParams["text.usetex"]` == True, then `TextPath.is_math_text` would return "TeX" as the ismath flag, which would then be interpreted as a True ismath value (but not a True usetex value(!)) by TextToPath.get_text_path. The new implementation avoids that problem. Remove a nonexistent parameter from the docs of RendererBase._get_text_path_transform.
1 parent e90d264 commit df3d798

File tree

3 files changed

+40
-18
lines changed

3 files changed

+40
-18
lines changed

doc/api/next_api_changes/2019-01-13-AL.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,19 @@ Deprecations
22
````````````
33

44
``Text.is_math_text`` is deprecated.
5+
6+
``TextPath.is_math_text`` and ``TextPath.text_get_vertices_codes`` are
7+
deprecated. As an alternative to the latter, construct a new ``TextPath``
8+
object.
9+
10+
The ``usetex`` parameter of ``TextToPath.get_text_path`` is deprecated and
11+
folded into the ``ismath`` parameter, which can now take the values False,
12+
True, and "TeX", consistently with other low-level text processing functions.
13+
14+
Behavior changes
15+
````````````````
16+
17+
Previously, if :rc:`text.usetex` was True, then constructing a `TextPath` on
18+
a non-mathtext string with ``usetex=False`` would rely on the mathtext parser
19+
(but not on usetex support!) to parse the string. The mathtext parser is not
20+
invoked anymore, which may cause slight changes in glyph positioning.

lib/matplotlib/backend_bases.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,6 @@ def _get_text_path_transform(self, x, y, s, prop, angle, ismath):
556556
The font property.
557557
s : str
558558
The text to be converted.
559-
usetex : bool
560-
Whether to use matplotlib usetex mode.
561559
ismath : bool or "TeX"
562560
If True, use mathtext parser. If "TeX", use *usetex* mode.
563561
"""

lib/matplotlib/textpath.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def get_text_width_height_descent(self, s, prop, ismath):
107107
d /= 64.0
108108
return w * scale, h * scale, d * scale
109109

110+
@cbook._delete_parameter("3.1", "usetex")
110111
def get_text_path(self, prop, s, ismath=False, usetex=False):
111112
"""
112113
Convert text *s* to path (a tuple of vertices and codes for
@@ -121,12 +122,11 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
121122
s : str
122123
The text to be converted.
123124
124-
usetex : bool, optional
125-
Whether to use tex rendering. Defaults to ``False``.
125+
ismath : {False, True, "TeX"}
126+
If True, use mathtext parser. If "TeX", use tex for renderering.
126127
127-
ismath : bool, optional
128-
If True, use mathtext parser. Effective only if
129-
``usetex == False``.
128+
usetex : bool, optional
129+
If set, forces *ismath* to True. This parameter is deprecated.
130130
131131
Returns
132132
-------
@@ -151,16 +151,15 @@ def get_text_path(self, prop, s, ismath=False, usetex=False):
151151
152152
Also see `TextPath` for a more direct way to create a path from a text.
153153
"""
154-
if not usetex:
155-
if not ismath:
156-
font = self._get_font(prop)
157-
glyph_info, glyph_map, rects = self.get_glyphs_with_font(
158-
font, s)
159-
else:
160-
glyph_info, glyph_map, rects = self.get_glyphs_mathtext(
161-
prop, s)
162-
else:
154+
if usetex:
155+
ismath = "TeX"
156+
if ismath == "TeX":
163157
glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s)
158+
elif not ismath:
159+
font = self._get_font(prop)
160+
glyph_info, glyph_map, rects = self.get_glyphs_with_font(font, s)
161+
else:
162+
glyph_info, glyph_map, rects = self.get_glyphs_mathtext(prop, s)
164163

165164
verts, codes = [], []
166165

@@ -457,6 +456,8 @@ def __init__(self, xy, s, size=None, prop=None,
457456
458457
Also see :doc:`/gallery/text_labels_and_annotations/demo_text_path`.
459458
"""
459+
# Circular import.
460+
from matplotlib.text import Text
460461

461462
if kl or kwargs:
462463
cbook.warn_deprecated(
@@ -472,8 +473,13 @@ def __init__(self, xy, s, size=None, prop=None,
472473
self.set_size(size)
473474

474475
self._cached_vertices = None
475-
self._vertices, self._codes = \
476-
self.text_get_vertices_codes(prop, s, usetex=usetex)
476+
s, ismath = Text(usetex=usetex)._preprocess_math(s)
477+
if ismath == "TeX":
478+
self._vertices, self._codes = text_to_path.get_text_path(
479+
prop, s, usetex=True)
480+
else:
481+
self._vertices, self._codes = text_to_path.get_text_path(
482+
prop, s, ismath=ismath)
477483
self._should_simplify = False
478484
self._simplify_threshold = rcParams['path.simplify_threshold']
479485
self._interpolation_steps = _interpolation_steps
@@ -522,6 +528,7 @@ def _revalidate_path(self):
522528
self._cached_vertices = tr.transform(self._vertices)
523529
self._invalid = False
524530

531+
@cbook.deprecated("3.1")
525532
def is_math_text(self, s):
526533
"""
527534
Returns True if the given string *s* contains any mathtext.
@@ -541,6 +548,7 @@ def is_math_text(self, s):
541548
else:
542549
return s.replace(r'\$', '$'), False
543550

551+
@cbook.deprecated("3.1", alternative="TextPath")
544552
def text_get_vertices_codes(self, prop, s, usetex):
545553
"""
546554
convert the string *s* to vertices and codes using the

0 commit comments

Comments
 (0)