From b392c9fddce69ad0f6d9d6165f7279cd4ffc1806 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Sat, 23 Feb 2019 13:35:51 -0500 Subject: [PATCH] Backport PR #13180: Various TextPath cleanups. --- doc/api/next_api_changes/2019-01-13-AL.rst | 16 +++++++++ lib/matplotlib/backend_bases.py | 2 -- lib/matplotlib/textpath.py | 40 +++++++++++++--------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/doc/api/next_api_changes/2019-01-13-AL.rst b/doc/api/next_api_changes/2019-01-13-AL.rst index 1038852ae000..87d5cf7ca523 100644 --- a/doc/api/next_api_changes/2019-01-13-AL.rst +++ b/doc/api/next_api_changes/2019-01-13-AL.rst @@ -2,3 +2,19 @@ Deprecations ```````````` ``Text.is_math_text`` is deprecated. + +``TextPath.is_math_text`` and ``TextPath.text_get_vertices_codes`` are +deprecated. As an alternative to the latter, construct a new ``TextPath`` +object. + +The ``usetex`` parameter of ``TextToPath.get_text_path`` is deprecated and +folded into the ``ismath`` parameter, which can now take the values False, +True, and "TeX", consistently with other low-level text processing functions. + +Behavior changes +```````````````` + +Previously, if :rc:`text.usetex` was True, then constructing a `TextPath` on +a non-mathtext string with ``usetex=False`` would rely on the mathtext parser +(but not on usetex support!) to parse the string. The mathtext parser is not +invoked anymore, which may cause slight changes in glyph positioning. diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index 3de25e400e27..89587c7a483d 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -556,8 +556,6 @@ def _get_text_path_transform(self, x, y, s, prop, angle, ismath): The font property. s : str The text to be converted. - usetex : bool - Whether to use matplotlib usetex mode. ismath : bool or "TeX" If True, use mathtext parser. If "TeX", use *usetex* mode. """ diff --git a/lib/matplotlib/textpath.py b/lib/matplotlib/textpath.py index 97eb3eefa95c..e1f40c7c7ccd 100644 --- a/lib/matplotlib/textpath.py +++ b/lib/matplotlib/textpath.py @@ -102,6 +102,7 @@ def get_text_width_height_descent(self, s, prop, ismath): d /= 64.0 return w * scale, h * scale, d * scale + @cbook._delete_parameter("3.1", "usetex") def get_text_path(self, prop, s, ismath=False, usetex=False): """ Convert text *s* to path (a tuple of vertices and codes for @@ -116,12 +117,11 @@ def get_text_path(self, prop, s, ismath=False, usetex=False): s : str The text to be converted. - usetex : bool, optional - Whether to use tex rendering. Defaults to ``False``. + ismath : {False, True, "TeX"} + If True, use mathtext parser. If "TeX", use tex for renderering. - ismath : bool, optional - If True, use mathtext parser. Effective only if - ``usetex == False``. + usetex : bool, optional + If set, forces *ismath* to True. This parameter is deprecated. Returns ------- @@ -146,16 +146,15 @@ def get_text_path(self, prop, s, ismath=False, usetex=False): Also see `TextPath` for a more direct way to create a path from a text. """ - if not usetex: - if not ismath: - font = self._get_font(prop) - glyph_info, glyph_map, rects = self.get_glyphs_with_font( - font, s) - else: - glyph_info, glyph_map, rects = self.get_glyphs_mathtext( - prop, s) - else: + if usetex: + ismath = "TeX" + if ismath == "TeX": glyph_info, glyph_map, rects = self.get_glyphs_tex(prop, s) + elif not ismath: + font = self._get_font(prop) + glyph_info, glyph_map, rects = self.get_glyphs_with_font(font, s) + else: + glyph_info, glyph_map, rects = self.get_glyphs_mathtext(prop, s) verts, codes = [], [] @@ -448,6 +447,8 @@ def __init__(self, xy, s, size=None, prop=None, Also see :doc:`/gallery/text_labels_and_annotations/demo_text_path`. """ + # Circular import. + from matplotlib.text import Text if args or kwargs: cbook.warn_deprecated( @@ -463,8 +464,13 @@ def __init__(self, xy, s, size=None, prop=None, self.set_size(size) self._cached_vertices = None - self._vertices, self._codes = \ - self.text_get_vertices_codes(prop, s, usetex=usetex) + s, ismath = Text(usetex=usetex)._preprocess_math(s) + if ismath == "TeX": + self._vertices, self._codes = text_to_path.get_text_path( + prop, s, usetex=True) + else: + self._vertices, self._codes = text_to_path.get_text_path( + prop, s, ismath=ismath) self._should_simplify = False self._simplify_threshold = rcParams['path.simplify_threshold'] self._interpolation_steps = _interpolation_steps @@ -507,6 +513,7 @@ def _revalidate_path(self): self._cached_vertices = tr.transform(self._vertices) self._invalid = False + @cbook.deprecated("3.1") def is_math_text(self, s): """ Returns True if the given string *s* contains any mathtext. @@ -526,6 +533,7 @@ def is_math_text(self, s): else: return s.replace(r'\$', '$'), False + @cbook.deprecated("3.1", alternative="TextPath") def text_get_vertices_codes(self, prop, s, usetex): """ Convert string *s* to a (vertices, codes) pair using font property