Skip to content

Commit 01d3149

Browse files
authored
Merge pull request #19501 from QuLogic/usetex-xcolor
FIX: text position with usetex and xcolor
2 parents d57a457 + c5fc9d8 commit 01d3149

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

lib/matplotlib/dviread.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,26 @@ def _read(self):
313313
# xxx comment
314314
# down
315315
# push
316-
# down, down
316+
# down
317+
# <push, push, xxx, right, xxx, pop, pop> # if using xcolor
318+
# down
317319
# push
318320
# down (possibly multiple)
319321
# push <= here, v is the baseline position.
320322
# etc.
321323
# (dviasm is useful to explore this structure.)
324+
# Thus, we use the vertical position at the first time the stack depth
325+
# reaches 3, while at least three "downs" have been executed, as the
326+
# baseline (the "down" count is necessary to handle xcolor).
327+
downs = 0
322328
self._baseline_v = None
323329
while True:
324330
byte = self.file.read(1)[0]
325331
self._dtable[byte](self, byte)
332+
downs += self._dtable[byte].__name__ == "_down"
326333
if (self._baseline_v is None
327-
and len(getattr(self, "stack", [])) == 3):
334+
and len(getattr(self, "stack", [])) == 3
335+
and downs >= 4):
328336
self._baseline_v = self.v
329337
if byte == 140: # end of page
330338
return True

lib/matplotlib/testing/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,7 @@ def check_for_pgf(texsystem):
7575
except (OSError, subprocess.CalledProcessError):
7676
return False
7777
return True
78+
79+
80+
def _has_tex_package(package):
81+
return bool(mpl.dviread.find_tex_file(f"{package}.sty"))

lib/matplotlib/tests/test_backend_pgf.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
from io import BytesIO
33
import os
44
import shutil
5-
import subprocess
65

76
import numpy as np
87
import pytest
98

109
import matplotlib as mpl
1110
import matplotlib.pyplot as plt
12-
from matplotlib.testing import check_for_pgf
11+
from matplotlib.testing import _has_tex_package, check_for_pgf
1312
from matplotlib.testing.compare import compare_images, ImageComparisonFailure
1413
from matplotlib.backends.backend_pgf import PdfPages, common_texification
1514
from matplotlib.testing.decorators import (_image_directories,
@@ -29,10 +28,6 @@
2928
reason="This test needs a ghostscript installation")
3029

3130

32-
def _has_tex_package(package):
33-
return bool(mpl.dviread.find_tex_file(f"{package}.sty"))
34-
35-
3631
def compare_figure(fname, savefig_kwargs={}, tol=0):
3732
actual = os.path.join(result_dir, fname)
3833
plt.savefig(actual, **savefig_kwargs)

lib/matplotlib/tests/test_usetex.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33

44
import matplotlib as mpl
5+
from matplotlib.testing import _has_tex_package
56
from matplotlib.testing.decorators import check_figures_equal, image_comparison
67
import matplotlib.pyplot as plt
78

@@ -80,6 +81,23 @@ def test_minus_no_descent(fontsize):
8081
assert len({*heights.values()}) == 1
8182

8283

84+
@pytest.mark.skipif(not _has_tex_package('xcolor'),
85+
reason='xcolor is not available')
86+
def test_usetex_xcolor():
87+
mpl.rcParams['text.usetex'] = True
88+
89+
fig = plt.figure()
90+
text = fig.text(0.5, 0.5, "Some text 0123456789")
91+
fig.canvas.draw()
92+
93+
mpl.rcParams['text.latex.preamble'] = r'\usepackage[dvipsnames]{xcolor}'
94+
fig = plt.figure()
95+
text2 = fig.text(0.5, 0.5, "Some text 0123456789")
96+
fig.canvas.draw()
97+
np.testing.assert_array_equal(text2.get_window_extent(),
98+
text.get_window_extent())
99+
100+
83101
def test_textcomp_full():
84102
plt.rcParams["text.latex.preamble"] = r"\usepackage[full]{textcomp}"
85103
fig = plt.figure()

0 commit comments

Comments
 (0)