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

Skip to content

Commit 25d23e9

Browse files
committed
Revert "Let TeX handle newlines itself."
This reverts commit 157b0be.
1 parent d5a5823 commit 25d23e9

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

lib/matplotlib/tests/test_text.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -806,11 +806,26 @@ def test_metrics_cache():
806806

807807
fig = plt.figure()
808808
fig.text(.3, .5, "foo\nbar")
809-
fig.text(.5, .5, "foo\nbar")
810809
fig.text(.3, .5, "foo\nbar", usetex=True)
811810
fig.text(.5, .5, "foo\nbar", usetex=True)
812811
fig.canvas.draw()
812+
renderer = fig._get_renderer()
813+
ys = {} # mapping of strings to where they were drawn in y with draw_tex.
814+
815+
def call(*args, **kwargs):
816+
renderer, x, y, s, *_ = args
817+
ys.setdefault(s, set()).add(y)
818+
819+
renderer.draw_tex = call
820+
fig.canvas.draw()
821+
assert [*ys] == ["foo", "bar"]
822+
# Check that both TeX strings were drawn with the same y-position for both
823+
# single-line substrings. Previously, there used to be an incorrect cache
824+
# collision with the non-TeX string (drawn first here) whose metrics would
825+
# get incorrectly reused by the first TeX string.
826+
assert len(ys["foo"]) == len(ys["bar"]) == 1
813827

814828
info = mpl.text._get_text_metrics_with_cache_impl.cache_info()
815-
# Each string gets drawn twice, so the second draw results in a hit.
816-
assert info.hits == info.misses
829+
# Every string gets a miss for the first layouting (extents), then a hit
830+
# when drawing, but "foo\nbar" gets two hits as it's drawn twice.
831+
assert info.hits > info.misses

lib/matplotlib/texmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def _get_tex_source(cls, tex, fontsize):
230230
r"% last line's baseline.",
231231
rf"\fontsize{{{fontsize}}}{{{baselineskip}}}%",
232232
r"\ifdefined\psfrag\else\hbox{}\fi%",
233-
rf"{{\obeylines{fontcmd} {tex}}}%",
233+
rf"{{{fontcmd} {tex}}}%",
234234
r"\special{matplotlibbaselinemarker}%",
235235
r"\end{document}",
236236
])

lib/matplotlib/text.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,7 @@ def _get_layout(self, renderer):
334334
of a rotated text when necessary.
335335
"""
336336
thisx, thisy = 0.0, 0.0
337-
text = self.get_text()
338-
lines = [text] if self.get_usetex() else text.split("\n") # Not empty.
337+
lines = self.get_text().split("\n") # Ensures lines is not empty.
339338

340339
ws = []
341340
hs = []

0 commit comments

Comments
 (0)