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

Skip to content

Small cleanups around TexManager usage. #22547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/next_api_changes/deprecations/22547-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
``TextToPath.get_texmanager``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
... is deprecated; directly construct a `.texmanager.TexManager` instead.
12 changes: 5 additions & 7 deletions lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from matplotlib.backend_managers import ToolManager
from matplotlib.cbook import _setattr_cm
from matplotlib.path import Path
from matplotlib.texmanager import TexManager
from matplotlib.transforms import Affine2D
from matplotlib._enums import JoinStyle, CapStyle

Expand Down Expand Up @@ -605,13 +606,12 @@ def get_text_width_height_descent(self, s, prop, ismath):
to the baseline), in display coords, of the string *s* with
`.FontProperties` *prop*.
"""
fontsize = prop.get_size_in_points()
Copy link
Member

@oscargus oscargus Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this was defined multiple times as the second return does not depend on it. But with the very simple function, it should not be a factor.

The similar change in textpath has no potential drawback at all though.

Edit: deleted confused follow-up... Hard to remember the difference between a PR and what I get if I search my local copy...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both returns depend on fontsize?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not this:

        if ismath:
            dims = self._text2path.mathtext_parser.parse(s, dpi, prop)
            return dims[0:3]  # return width, height, descent

but no big deal.


if ismath == 'TeX':
# todo: handle props
texmanager = self._text2path.get_texmanager()
fontsize = prop.get_size_in_points()
w, h, d = texmanager.get_text_width_height_descent(
return TexManager().get_text_width_height_descent(
s, fontsize, renderer=self)
return w, h, d

dpi = self.points_to_pixels(72)
if ismath:
Expand All @@ -620,8 +620,7 @@ def get_text_width_height_descent(self, s, prop, ismath):

flags = self._text2path._get_hinting_flag()
font = self._text2path._get_font(prop)
size = prop.get_size_in_points()
font.set_size(size, dpi)
font.set_size(fontsize, dpi)
# the width and height of unrotated string
font.set_text(s, 0.0, flags=flags)
w, h = font.get_width_height()
Expand All @@ -646,7 +645,6 @@ def get_canvas_width_height(self):
def get_texmanager(self):
"""Return the `.TexManager` instance."""
if self._texmanager is None:
from matplotlib.texmanager import TexManager
self._texmanager = TexManager()
return self._texmanager

Expand Down
16 changes: 7 additions & 9 deletions lib/matplotlib/textpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

import numpy as np

from matplotlib import _text_helpers, dviread, font_manager
from matplotlib import _api, _text_helpers, dviread, font_manager
from matplotlib.font_manager import FontProperties, get_font
from matplotlib.ft2font import LOAD_NO_HINTING, LOAD_TARGET_LIGHT
from matplotlib.mathtext import MathTextParser
from matplotlib.path import Path
from matplotlib.texmanager import TexManager
from matplotlib.transforms import Affine2D

_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -44,14 +45,11 @@ def _get_char_id(self, font, ccode):
return urllib.parse.quote(f"{font.postscript_name}-{ccode:x}")

def get_text_width_height_descent(self, s, prop, ismath):
fontsize = prop.get_size_in_points()

if ismath == "TeX":
texmanager = self.get_texmanager()
fontsize = prop.get_size_in_points()
w, h, d = texmanager.get_text_width_height_descent(s, fontsize,
renderer=None)
return w, h, d
return TexManager().get_text_width_height_descent(s, fontsize)

fontsize = prop.get_size_in_points()
scale = fontsize / self.FONT_SCALE

if ismath:
Expand Down Expand Up @@ -215,10 +213,10 @@ def get_glyphs_mathtext(self, prop, s, glyph_map=None,
return (list(zip(glyph_ids, xpositions, ypositions, sizes)),
glyph_map_new, myrects)

@_api.deprecated("3.6", alternative="TexManager()")
def get_texmanager(self):
"""Return the cached `~.texmanager.TexManager` instance."""
if self._texmanager is None:
from matplotlib.texmanager import TexManager
self._texmanager = TexManager()
return self._texmanager

Expand All @@ -227,7 +225,7 @@ def get_glyphs_tex(self, prop, s, glyph_map=None,
"""Convert the string *s* to vertices and codes using usetex mode."""
# Mostly borrowed from pdf backend.

dvifile = self.get_texmanager().make_dvi(s, self.FONT_SCALE)
dvifile = TexManager().make_dvi(s, self.FONT_SCALE)
with dviread.Dvi(dvifile, self.DPI) as dvi:
page, = dvi

Expand Down