Skip to content

Commit e7d00d2

Browse files
committed
Merge pull request #924 from pelson/text_contains_master
Text contains (was #687)
2 parents ea3c539 + 06b5ed5 commit e7d00d2

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

lib/matplotlib/tests/test_text.py

+45-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def find_matplotlib_font(**kw):
1818
return FontProperties(fname=path)
1919

2020
from matplotlib.font_manager import FontProperties, findfont
21-
warnings.filterwarnings('ignore','findfont: Font family \[\'Foo\'\] not found. Falling back to .',UserWarning,module='matplotlib.font_manager')
21+
warnings.filterwarnings('ignore','findfont: Font family \[\'Foo\'\] '+ \
22+
'not found. Falling back to .',
23+
UserWarning,
24+
module='matplotlib.font_manager')
2225
fig = plt.figure()
2326
ax = plt.subplot( 1, 1, 1 )
2427

@@ -89,8 +92,10 @@ def test_antialiasing():
8992
matplotlib.rcParams['text.antialiased'] = True
9093

9194
fig = plt.figure(figsize=(5.25, 0.75))
92-
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center', verticalalignment='center')
93-
fig.text(0.5, 0.25, "$\sqrt{x}$", horizontalalignment='center', verticalalignment='center')
95+
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center',
96+
verticalalignment='center')
97+
fig.text(0.5, 0.25, "$\sqrt{x}$", horizontalalignment='center',
98+
verticalalignment='center')
9499
# NOTE: We don't need to restore the rcParams here, because the
95100
# test cleanup will do it for us. In fact, if we do it here, it
96101
# will turn antialiasing back off before the images are actually
@@ -105,3 +110,40 @@ def test_afm_kerning():
105110
with open(fn, 'rb') as fh:
106111
afm = AFM(fh)
107112
assert afm.string_width_height('VAVAVAVAVAVA') == (7174.0, 718)
113+
114+
115+
@image_comparison(baseline_images=['text_contains'], extensions=['png'])
116+
def test_contains():
117+
import matplotlib.backend_bases as mbackend
118+
119+
fig = plt.figure()
120+
ax = plt.axes()
121+
122+
mevent = mbackend.MouseEvent('button_press_event', fig.canvas, 0.5,
123+
0.5, 1, None)
124+
125+
xs = np.linspace(0.25, 0.75, 30)
126+
ys = np.linspace(0.25, 0.75, 30)
127+
xs, ys = np.meshgrid(xs, ys)
128+
129+
txt = plt.text(0.48, 0.52, 'hello world', ha='center', fontsize=30,
130+
rotation=30)
131+
# uncomment to draw the text's bounding box
132+
# txt.set_bbox(dict(edgecolor='black', facecolor='none'))
133+
134+
# draw the text. This is important, as the contains method can only work
135+
# when a renderer exists.
136+
plt.draw()
137+
138+
for x, y in zip(xs.flat, ys.flat):
139+
mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y])
140+
141+
contains, _ = txt.contains(mevent)
142+
143+
color = 'yellow' if contains else 'red'
144+
145+
# capture the viewLim, plot a point, and reset the viewLim
146+
vl = ax.viewLim.frozen()
147+
ax.plot(x, y, 'o', color=color)
148+
ax.viewLim.set(vl)
149+

lib/matplotlib/text.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,10 @@ def contains(self,mouseevent):
207207
return False,{}
208208

209209
l,b,w,h = self.get_window_extent().bounds
210-
211-
r = l+w
212-
t = b+h
210+
r, t = l+w, b+h
211+
213212
x, y = mouseevent.x, mouseevent.y
214-
inside = (x >= l and x <= r and y >= t and y <= b)
213+
inside = (l <= x <= r and b <= y <= t)
215214
return inside, {}
216215

217216
def _get_xy_display(self):
@@ -361,7 +360,8 @@ def get_text_width_height_descent(*kl, **kwargs):
361360
width = xmax - xmin
362361
height = ymax - ymin
363362

364-
# Now move the box to the targe position offset the display bbox by alignment
363+
# Now move the box to the target position offset the display
364+
# bbox by alignment
365365
halign = self._horizontalalignment
366366
valign = self._verticalalignment
367367

@@ -1805,17 +1805,14 @@ def __init__(self, s, xy,
18051805
else:
18061806
self.arrow_patch = None
18071807

1808-
18091808
def contains(self,event):
1810-
t,tinfo = Text.contains(self,event)
1809+
contains, tinfo = Text.contains(self,event)
18111810
if self.arrow is not None:
1812-
a,ainfo=self.arrow.contains(event)
1813-
t = t or a
1814-
1811+
in_arrow, _ = self.arrow.contains(event)
1812+
contains = contains or in_arrow
18151813
# self.arrow_patch is currently not checked as this can be a line - JJ
18161814

1817-
return t,tinfo
1818-
1815+
return contains, tinfo
18191816

18201817
def set_figure(self, fig):
18211818

0 commit comments

Comments
 (0)