Skip to content

Commit 2cb6bc7

Browse files
authored
Merge pull request #15150 from anntzer/pgfincludegraphics
Autodetect whether pgf can use \includegraphics[interpolate].
2 parents b965c4d + 4939e6f commit 2cb6bc7

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PGF backend changes
2+
```````````````````
3+
4+
The pgf backend now includes images using ``\includegraphics`` instead of
5+
``\pgfimage`` if the version of ``graphicx`` is recent enough to support the
6+
``interpolate`` option (this is detected automatically).
7+
8+
``RendererPgf.latexManager`` is deprecated.

lib/matplotlib/backends/backend_pgf.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ def _build_latex_header():
220220
# Include TeX program name as a comment for cache invalidation.
221221
# TeX does not allow this to be the first line.
222222
r"% !TeX program = {}".format(rcParams["pgf.texsystem"]),
223+
# Test whether \includegraphics supports interpolate option.
224+
r"\usepackage{graphicx}",
223225
latex_preamble,
224226
latex_fontspec,
225227
r"\begin{document}",
@@ -376,6 +378,22 @@ def get_width_height_descent(self, text, prop):
376378
return w, h + o, o
377379

378380

381+
@functools.lru_cache(1)
382+
def _get_image_inclusion_command():
383+
man = LatexManager._get_cached_or_new()
384+
man._stdin_writeln(
385+
r"\includegraphics[interpolate=true]{%s}"
386+
# Don't mess with backslashes on Windows.
387+
% cbook._get_data_path("images/matplotlib.png").as_posix())
388+
try:
389+
prompt = man._expect_prompt()
390+
return r"\includegraphics"
391+
except LatexError:
392+
# Discard the broken manager.
393+
LatexManager._get_cached_or_new_impl.cache_clear()
394+
return r"\pgfimage"
395+
396+
379397
class RendererPgf(RendererBase):
380398

381399
def __init__(self, figure, fh, dummy=False):
@@ -397,8 +415,7 @@ def __init__(self, figure, fh, dummy=False):
397415
self.figure = figure
398416
self.image_counter = 0
399417

400-
# get LatexManager instance
401-
self.latexManager = LatexManager._get_cached_or_new()
418+
self._latexManager = LatexManager._get_cached_or_new() # deprecated
402419

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

433+
@cbook.deprecated("3.2")
434+
def latexManager(self):
435+
return self._latexManager
436+
416437
def draw_markers(self, gc, marker_path, marker_trans, path, trans,
417438
rgbFace=None):
418439
# docstring inherited
@@ -660,8 +681,9 @@ def draw_image(self, gc, x, y, im, transform=None):
660681
interp = str(transform is None).lower() # interpolation in PDF reader
661682
writeln(self.fh,
662683
r"\pgftext[left,bottom]"
663-
r"{\pgfimage[interpolate=%s,width=%fin,height=%fin]{%s}}" %
664-
(interp, w, h, fname_img))
684+
r"{%s[interpolate=%s,width=%fin,height=%fin]{%s}}" %
685+
(_get_image_inclusion_command(),
686+
interp, w, h, fname_img))
665687
writeln(self.fh, r"\end{pgfscope}")
666688

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

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

0 commit comments

Comments
 (0)