From cc8513060ef2a901d438372eb1606399e2cf4970 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Sun, 16 Jun 2024 01:19:42 +0200 Subject: [PATCH] FIX: Fix text wrapping `_get_dist_to_box()` assumed that the figure box (x0, y0) coordinates are 0, which was correct at the time of writing, but does not hold anymore since the introduction of subfigures. Closes #28378 Closes #28358 --- lib/matplotlib/tests/test_text.py | 9 +++++++-- lib/matplotlib/text.py | 13 ++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index f8837d8a5f1b..8904337f68ba 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -15,6 +15,7 @@ from matplotlib.font_manager import FontProperties import matplotlib.patches as mpatches import matplotlib.pyplot as plt +from matplotlib.gridspec import GridSpec import matplotlib.transforms as mtransforms from matplotlib.testing.decorators import check_figures_equal, image_comparison from matplotlib.testing._markers import needs_usetex @@ -707,9 +708,13 @@ def test_large_subscript_title(): (0.3, 0, 'right'), (0.3, 185, 'left')]) def test_wrap(x, rotation, halign): - fig = plt.figure(figsize=(6, 6)) + fig = plt.figure(figsize=(18, 18)) + gs = GridSpec(nrows=3, ncols=3, figure=fig) + subfig = fig.add_subfigure(gs[1, 1]) + # we only use the central subfigure, which does not align with any + # figure boundary, to ensure only subfigure boundaries are relevant s = 'This is a very long text that should be wrapped multiple times.' - text = fig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign) + text = subfig.text(x, 0.7, s, wrap=True, rotation=rotation, ha=halign) fig.canvas.draw() assert text._get_wrapped_text() == ('This is a very long\n' 'text that should be\n' diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 7fc19c042a1f..af990ec1bf9f 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -606,9 +606,8 @@ def set_wrap(self, wrap): """ Set whether the text can be wrapped. - Wrapping makes sure the text is completely within the figure box, i.e. - it does not extend beyond the drawing area. It does not take into - account any other artists. + Wrapping makes sure the text is confined to the (sub)figure box. It + does not take into account any other artists. Parameters ---------- @@ -657,16 +656,16 @@ def _get_dist_to_box(self, rotation, x0, y0, figure_box): """ if rotation > 270: quad = rotation - 270 - h1 = y0 / math.cos(math.radians(quad)) + h1 = (y0 - figure_box.y0) / math.cos(math.radians(quad)) h2 = (figure_box.x1 - x0) / math.cos(math.radians(90 - quad)) elif rotation > 180: quad = rotation - 180 - h1 = x0 / math.cos(math.radians(quad)) - h2 = y0 / math.cos(math.radians(90 - quad)) + h1 = (x0 - figure_box.x0) / math.cos(math.radians(quad)) + h2 = (y0 - figure_box.y0) / math.cos(math.radians(90 - quad)) elif rotation > 90: quad = rotation - 90 h1 = (figure_box.y1 - y0) / math.cos(math.radians(quad)) - h2 = x0 / math.cos(math.radians(90 - quad)) + h2 = (x0 - figure_box.x0) / math.cos(math.radians(90 - quad)) else: h1 = (figure_box.x1 - x0) / math.cos(math.radians(rotation)) h2 = (figure_box.y1 - y0) / math.cos(math.radians(90 - rotation))