diff --git a/doc/api/next_api_changes/2019-01-13-AL.rst b/doc/api/next_api_changes/2019-01-13-AL.rst new file mode 100644 index 000000000000..1038852ae000 --- /dev/null +++ b/doc/api/next_api_changes/2019-01-13-AL.rst @@ -0,0 +1,4 @@ +Deprecations +```````````` + +``Text.is_math_text`` is deprecated. diff --git a/lib/matplotlib/contour.py b/lib/matplotlib/contour.py index 59b9fe63babb..70a6deb012aa 100644 --- a/lib/matplotlib/contour.py +++ b/lib/matplotlib/contour.py @@ -264,7 +264,7 @@ def get_label_width(self, lev, fmt, fsize): if not isinstance(lev, str): lev = self.get_text(lev, fmt) - lev, ismath = text.Text.is_math_text(lev) + lev, ismath = text.Text()._preprocess_math(lev) if ismath == 'TeX': if not hasattr(self, '_TeX_manager'): self._TeX_manager = texmanager.TexManager() diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index d264ad38cdd4..ca997485ff3d 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -286,11 +286,12 @@ def _get_layout(self, renderer): # Full vertical extent of font, including ascenders and descenders: _, lp_h, lp_d = renderer.get_text_width_height_descent( - "lp", self._fontproperties, ismath=False) + "lp", self._fontproperties, + ismath="TeX" if self.get_usetex() else False) min_dy = (lp_h - lp_d) * self._linespacing for i, line in enumerate(lines): - clean_line, ismath = self.is_math_text(line, self.get_usetex()) + clean_line, ismath = self._preprocess_math(line) if clean_line: w, h, d = renderer.get_text_width_height_descent( clean_line, self._fontproperties, ismath=ismath) @@ -697,8 +698,7 @@ def draw(self, renderer): y = y + posy if renderer.flipy(): y = canvash - y - clean_line, ismath = textobj.is_math_text(line, - self.get_usetex()) + clean_line, ismath = textobj._preprocess_math(line) if textobj.get_path_effects(): from matplotlib.patheffects import PathEffectRenderer @@ -1152,6 +1152,7 @@ def set_text(self, s): self.stale = True @staticmethod + @cbook.deprecated("3.1") def is_math_text(s, usetex=None): """ Returns a cleaned string and a boolean flag. @@ -1174,6 +1175,27 @@ def is_math_text(s, usetex=None): else: return s.replace(r'\$', '$'), False + def _preprocess_math(self, s): + """ + Return the string *s* after mathtext preprocessing, and the kind of + mathtext support needed. + + - If *self* is configured to use TeX, return *s* unchanged except that + a single space gets escaped, and the flag "TeX". + - Otherwise, if *s* is mathtext (has an even number of unescaped dollar + signs), return *s* and the flag True. + - Otherwise, return *s* with dollar signs unescaped, and the flag + False. + """ + if self.get_usetex(): + if s == " ": + s = r"\ " + return s, "TeX" + elif cbook.is_math_text(s): + return s, True + else: + return s.replace(r"\$", "$"), False + def set_fontproperties(self, fp): """ Set the font properties that control the text.