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

Skip to content

Commit a988fb0

Browse files
committed
Add fix to Gdk backend's draw_text to prevent crashing when drawing off the edge of the image.
1 parent a329dcd commit a988fb0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lib/matplotlib/backends/backend_gdk.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def draw_image(self, gc, x, y, im):
142142
def draw_text(self, gc, x, y, s, prop, angle, ismath):
143143
x, y = int(x), int(y)
144144

145-
if x <0 or y <0: # window has shrunk and text is off the edge
145+
if x < 0 or y < 0: # window has shrunk and text is off the edge
146146
return
147147

148148
if angle not in (0,90):
@@ -157,6 +157,9 @@ def draw_text(self, gc, x, y, s, prop, angle, ismath):
157157
else:
158158
layout, inkRect, logicalRect = self._get_pango_layout(s, prop)
159159
l, b, w, h = inkRect
160+
if (x + w > self.width or y + h > self.height):
161+
return
162+
160163
self.gdkDrawable.draw_layout(gc.gdkGC, x, y-h-b, layout)
161164

162165

@@ -225,10 +228,8 @@ def _draw_rotated_text(self, gc, x, y, s, prop, angle):
225228
x = int(x-h)
226229
y = int(y-w)
227230

228-
if x < 0 or y < 0: # window has shrunk and text is off the edge
229-
return
230-
231-
if x + w > self.width or y + h > self.height:
231+
if (x < 0 or y < 0 or: # window has shrunk and text is off the edge
232+
x + w > self.width or y + h > self.height):
232233
return
233234

234235
key = (x,y,s,angle,hash(prop))

0 commit comments

Comments
 (0)