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

Skip to content

Commit bb2924c

Browse files
committed
Let TeX handle newlines itself.
Note that it may be worth passing the baselineskip (`Text.get_linespacing()`) as argument to texmanager, but that'll wait for a bigger refactor... for now, let's stick to the old default of 1.25. test_metrics_cache changes as the cache hit pattern changed for usetex strings.
1 parent e31cd35 commit bb2924c

File tree

3 files changed

+6
-20
lines changed

3 files changed

+6
-20
lines changed

lib/matplotlib/tests/test_text.py

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

773773
fig = plt.figure()
774774
fig.text(.3, .5, "foo\nbar")
775+
fig.text(.5, .5, "foo\nbar")
775776
fig.text(.3, .5, "foo\nbar", usetex=True)
776777
fig.text(.5, .5, "foo\nbar", usetex=True)
777778
fig.canvas.draw()
778-
renderer = fig._cachedRenderer
779-
ys = {} # mapping of strings to where they were drawn in y with draw_tex.
780-
781-
def call(*args, **kwargs):
782-
renderer, x, y, s, *_ = args
783-
ys.setdefault(s, set()).add(y)
784-
785-
renderer.draw_tex = call
786-
fig.canvas.draw()
787-
assert [*ys] == ["foo", "bar"]
788-
# Check that both TeX strings were drawn with the same y-position for both
789-
# single-line substrings. Previously, there used to be an incorrect cache
790-
# collision with the non-TeX string (drawn first here) whose metrics would
791-
# get incorrectly reused by the first TeX string.
792-
assert len(ys["foo"]) == len(ys["bar"]) == 1
793779

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

lib/matplotlib/texmanager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ def make_tex(self, tex, fontsize):
217217
%% when using psfrag which gets confused by it.
218218
\fontsize{%(fontsize)f}{%(baselineskip)f}%%
219219
\ifdefined\psfrag\else\hbox{}\fi%%
220-
{%(fontcmd)s %(tex)s}\special{matplotlibbaselinemarker}
220+
{\obeylines%(fontcmd)s %(tex)s}\special{matplotlibbaselinemarker}
221221
\end{document}
222222
"""
223223
Path(texfile).write_text(tex_template % {

lib/matplotlib/text.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ def _get_layout(self, renderer):
295295
of a rotated text when necessary.
296296
"""
297297
thisx, thisy = 0.0, 0.0
298-
lines = self.get_text().split("\n") # Ensures lines is not empty.
298+
text = self.get_text()
299+
lines = [text] if self.get_usetex() else text.split("\n") # Not empty.
299300

300301
ws = []
301302
hs = []

0 commit comments

Comments
 (0)