10000 Let TeX handle newlines itself. · matplotlib/matplotlib@157b0be · GitHub
[go: up one dir, main page]

Skip to content

Commit 157b0be

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 c973552 commit 157b0be

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
@@ -776,26 +776,11 @@ def test_metrics_cache():
776776

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

798784
info = mpl.text._get_text_metrics_with_cache_impl.cache_info()
799-
# Every string gets a miss for the first layouting (extents), then a hit
800-
# when drawing, but "foo\nbar" gets two hits as it's drawn twice.
801-
assert info.hits > info.misses
785+
# Each string gets drawn twice, so the second draw results in a hit.
786+
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)
0