@@ -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+
0 commit comments