From 87bc31f347114ccc1242ca2a0684905cd6455e3e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 11 Feb 2021 14:50:46 -0500 Subject: [PATCH 1/3] Fix text.usetex when using xcolor in preamble. --- lib/matplotlib/dviread.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index b55dacbb2eca..579fe0e11ece 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -313,18 +313,26 @@ def _read(self): # xxx comment # down # push - # down, down + # down + # # if using xcolor + # down # push # down (possibly multiple) # push <= here, v is the baseline position. # etc. # (dviasm is useful to explore this structure.) + # Thus, we use the vertical position at the first time the stack depth + # reaches 3, while at least three "downs" have been executed, as the + # baseline (the "down" count is necessary to handle xcolor). + downs = 0 self._baseline_v = None while True: byte = self.file.read(1)[0] self._dtable[byte](self, byte) + downs += self._dtable[byte].__name__ == "_down" if (self._baseline_v is None - and len(getattr(self, "stack", [])) == 3): + and len(getattr(self, "stack", [])) == 3 + and downs >= 4): self._baseline_v = self.v if byte == 140: # end of page return True From e475af9417d5be14d5d3d999e7623ae4c4266b77 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Thu, 11 Feb 2021 14:52:39 -0500 Subject: [PATCH 2/3] Test that text is located correctly with xcolor. --- lib/matplotlib/testing/__init__.py | 4 ++++ lib/matplotlib/tests/test_backend_pgf.py | 7 +------ lib/matplotlib/tests/test_usetex.py | 18 ++++++++++++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/matplotlib/testing/__init__.py b/lib/matplotlib/testing/__init__.py index 274adbc671b7..ca51fbc0fd00 100644 --- a/lib/matplotlib/testing/__init__.py +++ b/lib/matplotlib/testing/__init__.py @@ -75,3 +75,7 @@ def check_for_pgf(texsystem): except (OSError, subprocess.CalledProcessError): return False return True + + +def _has_tex_package(package): + return bool(mpl.dviread.find_tex_file(f"{package}.sty")) diff --git a/lib/matplotlib/tests/test_backend_pgf.py b/lib/matplotlib/tests/test_backend_pgf.py index ae04caaa8bea..7d10c13ce3a8 100644 --- a/lib/matplotlib/tests/test_backend_pgf.py +++ b/lib/matplotlib/tests/test_backend_pgf.py @@ -2,14 +2,13 @@ from io import BytesIO import os import shutil -import subprocess import numpy as np import pytest import matplotlib as mpl import matplotlib.pyplot as plt -from matplotlib.testing import check_for_pgf +from matplotlib.testing import _has_tex_package, check_for_pgf from matplotlib.testing.compare import compare_images, ImageComparisonFailure from matplotlib.backends.backend_pgf import PdfPages, common_texification from matplotlib.testing.decorators import (_image_directories, @@ -29,10 +28,6 @@ reason="This test needs a ghostscript installation") -def _has_tex_package(package): - return bool(mpl.dviread.find_tex_file(f"{package}.sty")) - - def compare_figure(fname, savefig_kwargs={}, tol=0): actual = os.path.join(result_dir, fname) plt.savefig(actual, **savefig_kwargs) diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index 668a994e31a4..d5d46de4089a 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -2,6 +2,7 @@ import pytest import matplotlib as mpl +from matplotlib.testing import _has_tex_package from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.pyplot as plt @@ -80,6 +81,23 @@ def test_minus_no_descent(fontsize): assert len({*heights.values()}) == 1 +@pytest.mark.skipif(not _has_tex_package('xcolor'), + reason='xcolor is not available') +def test_usetex_xcolor(): + mpl.rcParams['text.usetex'] = True + + fig = plt.figure() + t = fig.text(0.5, 0.5, "Some text 0123456789") + fig.canvas.draw() + pos = t.get_window_extent() + + mpl.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}' + fig = plt.figure() + t = fig.text(0.5, 0.5, "Some text 0123456789") + fig.canvas.draw() + np.testing.assert_array_equal(t.get_window_extent(), pos) + + def test_textcomp_full(): plt.rcParams["text.latex.preamble"] = r"\usepackage[full]{textcomp}" fig = plt.figure() From c5fc9d8a1b08a6f576cd048f810eb4d89df1abcf Mon Sep 17 00:00:00 2001 From: "Dr. Thomas A Caswell" Date: Mon, 15 Feb 2021 20:35:19 -0500 Subject: [PATCH 3/3] TST: fix skip logic and make test symmetric Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> --- lib/matplotlib/tests/test_usetex.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/matplotlib/tests/test_usetex.py b/lib/matplotlib/tests/test_usetex.py index d5d46de4089a..2d79e155e72e 100644 --- a/lib/matplotlib/tests/test_usetex.py +++ b/lib/matplotlib/tests/test_usetex.py @@ -87,15 +87,15 @@ def test_usetex_xcolor(): mpl.rcParams['text.usetex'] = True fig = plt.figure() - t = fig.text(0.5, 0.5, "Some text 0123456789") + text = fig.text(0.5, 0.5, "Some text 0123456789") fig.canvas.draw() - pos = t.get_window_extent() mpl.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}' fig = plt.figure() - t = fig.text(0.5, 0.5, "Some text 0123456789") + text2 = fig.text(0.5, 0.5, "Some text 0123456789") fig.canvas.draw() - np.testing.assert_array_equal(t.get_window_extent(), pos) + np.testing.assert_array_equal(text2.get_window_extent(), + text.get_window_extent()) def test_textcomp_full():