Skip to content

Autodetect whether pgf can use \includegraphics[interpolate]. #15150

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
Sep 4, 2019
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
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/2019-08-28-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
PGF backend changes
```````````````````

The pgf backend now includes images using ``\includegraphics`` instead of
``\pgfimage`` if the version of ``graphicx`` is recent enough to support the
``interpolate`` option (this is detected automatically).

``RendererPgf.latexManager`` is deprecated.
33 changes: 28 additions & 5 deletions lib/matplotlib/backends/backend_pgf.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ def _build_latex_header():
# Include TeX program name as a comment for cache invalidation.
# TeX does not allow this to be the first line.
r"% !TeX program = {}".format(rcParams["pgf.texsystem"]),
# Test whether \includegraphics supports interpolate option.
r"\usepackage{graphicx}",
latex_preamble,
latex_fontspec,
r"\begin{document}",
Expand Down Expand Up @@ -376,6 +378,22 @@ def get_width_height_descent(self, text, prop):
return w, h + o, o


@functools.lru_cache(1)
def _get_image_inclusion_command():
man = LatexManager._get_cached_or_new()
man._stdin_writeln(
r"\includegraphics[interpolate=true]{%s}"
# Don't mess with backslashes on Windows.
% cbook._get_data_path("images/matplotlib.png").as_posix())
try:
prompt = man._expect_prompt()
return r"\includegraphics"
except LatexError:
# Discard the broken manager.
LatexManager._get_cached_or_new_impl.cache_clear()
return r"\pgfimage"


class RendererPgf(RendererBase):

def __init__(self, figure, fh, dummy=False):
Expand All @@ -397,8 +415,7 @@ def __init__(self, figure, fh, dummy=False):
self.figure = figure
self.image_counter = 0

# get LatexManager instance
self.latexManager = LatexManager._get_cached_or_new()
self._latexManager = LatexManager._get_cached_or_new() # deprecated

if dummy:
# dummy==True deactivate all methods
Expand All @@ -413,6 +430,10 @@ def __init__(self, figure, fh, dummy=False):
"pgf-to-pdf option", UserWarning)
self.__dict__["draw_image"] = lambda *args, **kwargs: None

@cbook.deprecated("3.2")
def latexManager(self):
return self._latexManager

def draw_markers(self, gc, marker_path, marker_trans, path, trans,
rgbFace=None):
# docstring inherited
Expand Down Expand Up @@ -660,8 +681,9 @@ def draw_image(self, gc, x, y, im, transform=None):
interp = str(transform is None).lower() # interpolation in PDF reader
writeln(self.fh,
r"\pgftext[left,bottom]"
r"{\pgfimage[interpolate=%s,width=%fin,height=%fin]{%s}}" %
(interp, w, h, fname_img))
r"{%s[interpolate=%s,width=%fin,height=%fin]{%s}}" %
(_get_image_inclusion_command(),
interp, w, h, fname_img))
writeln(self.fh, r"\end{pgfscope}")

def draw_tex(self, gc, x, y, s, prop, angle, ismath="TeX!", mtext=None):
Expand Down Expand Up @@ -724,7 +746,8 @@ def get_text_width_height_descent(self, s, prop, ismath):
s = common_texification(s)

# get text metrics in units of latex pt, convert to display units
w, h, d = self.latexManager.get_width_height_descent(s, prop)
w, h, d = (LatexManager._get_cached_or_new()
.get_width_height_descent(s, prop))
# TODO: this should be latex_pt_to_in instead of mpl_pt_to_in
# but having a little bit more space around the text looks better,
# plus the bounding box reported by LaTeX is VERY narrow
Expand Down