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

Skip to content

Commit 846fc30

Browse files
authored
Merge pull request #16366 from anntzer/usetex-minus-depth
FIX: Special-case usetex minus to zero depth.
2 parents 7f60002 + 4c6d9af commit 846fc30

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

lib/matplotlib/dviread.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,12 @@ def _height_depth_of(self, char):
566566
result.append(0)
567567
else:
568568
result.append(_mul2012(value, self._scale))
569+
# cmsy10 glyph 0 ("minus") has a nonzero descent so that TeX aligns
570+
# equations properly (https://tex.stackexchange.com/questions/526103/),
571+
# but we actually care about the rasterization depth to align the
572+
# dvipng-generated images.
573+
if self.texname == b"cmsy10" and char == 0:
574+
result[-1] = 0
569575
return result
570576

571577

lib/matplotlib/tests/test_usetex.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import pytest
21
import platform
32

3+
import numpy as np
4+
import pytest
5+
46
import matplotlib as mpl
57
from matplotlib.testing.decorators import check_figures_equal, image_comparison
68
import matplotlib.pyplot as plt
@@ -43,3 +45,21 @@ def test_mathdefault():
4345
# problems when later switching usetex on.
4446
mpl.rcParams['text.usetex'] = True
4547
fig.canvas.draw()
48+
49+
50+
def test_minus_no_descent():
51+
# Test special-casing of minus descent in DviFont._height_depth_of, by
52+
# checking that overdrawing a 1 and a -1 results in an overall height
53+
# equivalent to drawing either of them separately.
54+
mpl.style.use("mpl20")
55+
heights = {}
56+
fig = plt.figure()
57+
for vals in [(1,), (-1,), (-1, 1)]:
58+
fig.clf()
59+
for x in vals:
60+
fig.text(.5, .5, f"${x}$", usetex=True)
61+
fig.canvas.draw()
62+
# The following counts the number of non-fully-blank pixel rows.
63+
heights[vals] = ((np.array(fig.canvas.buffer_rgba())[..., 0] != 255)
64+
.any(axis=1).sum())
65+
assert len({*heights.values()}) == 1

0 commit comments

Comments
 (0)