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

Skip to content

Text contains (was #687) #924

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 4 commits into from
Jun 6, 2012
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 45 additions & 3 deletions lib/matplotlib/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ def find_matplotlib_font(**kw):
return FontProperties(fname=path)

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

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

fig = plt.figure(figsize=(5.25, 0.75))
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center', verticalalignment='center')
fig.text(0.5, 0.25, "$\sqrt{x}$", horizontalalignment='center', verticalalignment='center')
fig.text(0.5, 0.75, "antialiased", horizontalalignment='center',
verticalalignment='center')
fig.text(0.5, 0.25, "$\sqrt{x}$", horizontalalignment='center',
verticalalignment='center')
# NOTE: We don't need to restore the rcParams here, because the
# test cleanup will do it for us. In fact, if we do it here, it
# will turn antialiasing back off before the images are actually
Expand All @@ -105,3 +110,40 @@ def test_afm_kerning():
with open(fn, 'rb') as fh:
afm = AFM(fh)
assert afm.string_width_height('VAVAVAVAVAVA') == (7174.0, 718)


@image_comparison(baseline_images=['text_contains'], extensions=['png'])
def test_contains():
import matplotlib.backend_bases as mbackend

fig = plt.figure()
ax = plt.axes()

mevent = mbackend.MouseEvent('button_press_event', fig.canvas, 0.5,
0.5, 1, None)

xs = np.linspace(0.25, 0.75, 30)
ys = np.linspace(0.25, 0.75, 30)
xs, ys = np.meshgrid(xs, ys)

txt = plt.text(0.48, 0.52, 'hello world', ha='center', fontsize=30,
rotation=30)
# uncomment to draw the text's bounding box
# txt.set_bbox(dict(edgecolor='black', facecolor='none'))

# draw the text. This is important, as the contains method can only work
# when a renderer exists.
plt.draw()

for x, y in zip(xs.flat, ys.flat):
mevent.x, mevent.y = plt.gca().transAxes.transform_point([x, y])

contains, _ = txt.contains(mevent)

color = 'yellow' if contains else 'red'

# capture the viewLim, plot a point, and reset the viewLim
vl = ax.viewLim.frozen()
ax.plot(x, y, 'o', color=color)
ax.viewLim.set(vl)

21 changes: 9 additions & 12 deletions lib/matplotlib/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,10 @@ def contains(self,mouseevent):
return False,{}

l,b,w,h = self.get_window_extent().bounds

r = l+w
t = b+h
r, t = l+w, b+h

x, y = mouseevent.x, mouseevent.y
inside = (x >= l and x <= r and y >= t and y <= b)
inside = (l <= x <= r and b <= y <= t)
return inside, {}

def _get_xy_display(self):
Expand Down Expand Up @@ -361,7 +360,8 @@ def get_text_width_height_descent(*kl, **kwargs):
width = xmax - xmin
height = ymax - ymin

# Now move the box to the targe position offset the display bbox by alignment
# Now move the box to the target position offset the display
# bbox by alignment
halign = self._horizontalalignment
valign = self._verticalalignment

Expand Down Expand Up @@ -1805,17 +1805,14 @@ def __init__(self, s, xy,
else:
self.arrow_patch = None


def contains(self,event):
t,tinfo = Text.contains(self,event)
contains, tinfo = Text.contains(self,event)
if self.arrow is not None:
a,ainfo=self.arrow.contains(event)
t = t or a

in_arrow, _ = self.arrow.contains(event)
contains = contains or in_arrow
# self.arrow_patch is currently not checked as this can be a line - JJ

return t,tinfo

return contains, tinfo

def set_figure(self, fig):

Expand Down