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

Skip to content

FIX: Fix text wrapping #28401

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

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down
13 changes: 6 additions & 7 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down Expand Up @@ -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))
Expand Down
Loading