-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix baseline alignment when using usetex. #16476
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,6 +274,12 @@ def _output(self): | |
maxx = max(maxx, x + w) | ||
maxy = max(maxy, y + e) | ||
maxy_pure = max(maxy_pure, y) | ||
if self._baseline_v is not None: | ||
maxy_pure = self._baseline_v # This should normally be the case. | ||
self._baseline_v = None | ||
|
||
if not self.text and not self.boxes: # Avoid infs/nans from inf+/-inf. | ||
return Page(text=[], boxes=[], width=0, height=0, descent=0) | ||
|
||
if self.dpi is None: | ||
# special case for ease of debugging: output raw dvi coordinates | ||
|
@@ -301,9 +307,24 @@ def _read(self): | |
Read one page from the file. Return True if successful, | ||
False if there were no more pages. | ||
""" | ||
# Pages appear to start with the sequence | ||
# bop (begin of page) | ||
# xxx comment | ||
# down | ||
# push | ||
# down, down | ||
# push | ||
# down (possibly multiple) | ||
# push <= here, v is the baseline position. | ||
# etc. | ||
# (dviasm is useful to explore this structure.) | ||
self._baseline_v = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to put this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'd already have errored out earlier anyways, because self.text wouldn't have been defined either. |
||
while True: | ||
byte = self.file.read(1)[0] | ||
self._dtable[byte](self, byte) | ||
if (self._baseline_v is None | ||
and len(getattr(self, "stack", [])) == 3): | ||
self._baseline_v = self.v | ||
if byte == 140: # end of page | ||
return True | ||
if self.state is _dvistate.post_post: # end of file | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import platform | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
|
@@ -12,24 +10,38 @@ | |
pytestmark = pytest.mark.skip('Missing TeX of Ghostscript or dvipng') | ||
|
||
|
||
@image_comparison(baseline_images=['test_usetex'], | ||
extensions=['pdf', 'png'], | ||
tol={'aarch64': 2.868, 'x86_64': 2.868}.get( | ||
platform.machine(), 0.3 | ||
)) | ||
@image_comparison( | ||
baseline_images=['test_usetex'], | ||
extensions=['pdf', 'png'], | ||
style="mpl20") | ||
def test_usetex(): | ||
mpl.rcParams['text.usetex'] = True | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111) | ||
ax.text(0.1, 0.2, | ||
kwargs = {"verticalalignment": "baseline", "size": 24, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test is significantly better 👍 |
||
"bbox": dict(pad=0, edgecolor="k", facecolor="none")} | ||
ax.text(0.2, 0.7, | ||
# the \LaTeX macro exercises character sizing and placement, | ||
# \left[ ... \right\} draw some variable-height characters, | ||
# \sqrt and \frac draw horizontal rules, \mathrm changes the font | ||
r'\LaTeX\ $\left[\int\limits_e^{2e}' | ||
r'\sqrt\frac{\log^3 x}{x}\,\mathrm{d}x \right\}$', | ||
fontsize=24) | ||
ax.set_xticks([]) | ||
ax.set_yticks([]) | ||
**kwargs) | ||
ax.text(0.2, 0.3, "lg", **kwargs) | ||
ax.text(0.4, 0.3, r"$\frac{1}{2}\pi$", **kwargs) | ||
ax.text(0.6, 0.3, "$p^{3^A}$", **kwargs) | ||
ax.text(0.8, 0.3, "$p_{3_2}$", **kwargs) | ||
for x in {t.get_position()[0] for t in ax.texts}: | ||
ax.axvline(x) | ||
for y in {t.get_position()[1] for t in ax.texts}: | ||
ax.axhline(y) | ||
ax.set_axis_off() | ||
|
||
|
||
@check_figures_equal() | ||
def test_empty(fig_test, fig_ref): | ||
mpl.rcParams['text.usetex'] = True | ||
fig_test.text(.5, .5, "% a comment") | ||
|
||
|
||
@check_figures_equal() | ||
|
Uh oh!
There was an error while loading. Please reload this page.