@@ -278,79 +278,36 @@ def draw_rectangle(self, gc, rgbFace, x, y, width, height):
278278 self .gdkDrawable .draw_rectangle (gc .gdkGC , False , x , y , w , h )
279279
280280
281- #def _draw_rotated_text(self, gc, x, y, s, prop, angle, ismath): # ismath is not used
282- def _draw_rotated_text (self , gc , x , y , s , prop , angle ): # ismath is not used
283- """
284- Draw the text rotated 90 degrees
285- """
286-
287- gdrawable = self .gdkDrawable
288- ggc = gc .gdkGC
289-
290- layout = self .get_pango_layout (s , prop )
291- inkRect , logicalRect = layout .get_pixel_extents ()
292- rect = inkRect
293- l , b , w , h = rect
294-
295- x = int (x - h )
296- y = int (y - w )
297- # get the background image
298-
299- # todo: cache rotation for dynamic redraw until pygtk mem leak
300- # fixed
301- key = (x ,y ,s ,angle ,hash (prop ))
302- imageOut = self .rotated .get (key )
303- if imageOut is not None :
304- gdrawable .draw_image (ggc , imageOut , 0 , 0 , x , y , h , w )
305- return
306-
307- # save the background
308- imageBack = gdrawable .get_image (x , y , w , h )
309- imageVert = gdrawable .get_image (x , y , h , w )
281+ def draw_text (self , gc , x , y , s , prop , angle , ismath ):
282+
283+ x , y = int (x ), int (y )
310284
311- # transform the vertical image, write it onto the renderer,
312- # and draw the layout onto it
313- imageFlip = gtk .gdk .Image (type = gdk .IMAGE_NORMAL ,
314- visual = gdrawable .get_visual (),
315- width = w , height = h )
316- if imageFlip is None or imageBack is None or imageVert is None :
317- print >> sys .stderr , "Could not renderer vertical text" , s
318- return
319- imageFlip .set_colormap (gdrawable .get_colormap ())
320- for i in range (w ):
321- for j in range (h ):
322- imageFlip .put_pixel (i , j , imageVert .get_pixel (j ,w - i - 1 ) )
285+ if angle not in (0 ,90 ):
286+ verbose .report_error ('The GTK backend cannot draw text at a %i degree angle, try GtkAgg instead' % angle )
323287
324- gdrawable . draw_image ( ggc , imageFlip , 0 , 0 , x , y , w , h )
325- gdrawable . draw_layout ( ggc , x , y - b , layout )
288+ elif ismath :
289+ self . _draw_mathtext ( gc , x , y , s , prop , angle )
326290
327- # now get that image and flip it vertical
328- imageIn = gdrawable .get_image (x , y , w , h )
329- imageOut = gtk .gdk .Image (type = gdk .IMAGE_NORMAL ,
330- visual = gdrawable .get_visual (),
331- width = h , height = w )
332- imageOut .set_colormap (gdrawable .get_colormap ())
333- for i in range (w ):
334- for j in range (h ):
335- imageOut .put_pixel (j , i , imageIn .get_pixel (w - i - 1 ,j ) )
291+ elif angle == 90 :
292+ self ._draw_rotated_text (gc , x , y , s , prop , angle )
336293
337- # draw the old background and the flipped text
338- gdrawable .draw_image (ggc , imageBack , 0 , 0 , x , y , w , h )
339- gdrawable .draw_image (ggc , imageOut , 0 , 0 , x , y , h , w )
340- self .rotated [key ] = imageOut
341- return True
294+ else :
295+ layout = self .get_pango_layout (s , prop )
296+ inkRect , logicalRect = layout .get_pixel_extents ()
297+ l , b , w , h = inkRect
342298
299+ self .gdkDrawable .draw_layout (gc .gdkGC , x = x , y = y - h - b ,
300+ layout = layout )
343301
344- def draw_mathtext (self , gc , x , y , s , prop , angle ):
302+
303+ def _draw_mathtext (self , gc , x , y , s , prop , angle ):
345304
346305 size = prop .get_size_in_points ()
347306 width , height , fonts = math_parse_s_ft2font (
348307 s , self .dpi .get (), size )
349308
350309 if angle == 90 :
351310 width , height = height , width
352- x = int (x )
353- y = int (y )
354311
355312 rgb = gc .get_rgb ()
356313 #rgba = (rgb[0], rgb[1], rgb[2], gc.get_alpha())
@@ -371,9 +328,7 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
371328 Xs = numerix .max (Xall ,1 )
372329
373330 # convert it to it's proper shape
374-
375331 Xs .shape = imh , imw
376-
377332
378333 pb = gtk .gdk .Pixbuf (gtk .gdk .COLORSPACE_RGB ,
379334 has_alpha = 1 , bits_per_sample = 8 , width = imw , height = imh )
@@ -382,16 +337,15 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
382337 pa = pb .get_pixels_array ()
383338 except AttributeError :
384339 pa = pb .pixel_array
385- except RuntimeError , exc : # pygtk was not compiled with Numeric Python support
386- print >> sys . stderr , 'Error:' , exc
340+ except RuntimeError , exc : # ' pygtk was not compiled with Numeric Python support'
341+ verbose . report_error ( 'mathtext not supported: %s' % exc )
387342 return
388343
389344 pa [:,:,0 ]= int (rgb [0 ]* 255 )
390345 pa [:,:,1 ]= int (rgb [1 ]* 255 )
391346 pa [:,:,2 ]= int (rgb [2 ]* 255 )
392347 pa [:,:,3 ]= Xs
393348
394-
395349 if angle == 90 :
396350 x -= width
397351 y -= height
@@ -400,30 +354,68 @@ def draw_mathtext(self, gc, x, y, s, prop, angle):
400354 gdk .RGB_DITHER_NONE , 0 , 0 )
401355
402356
403- def draw_text (self , gc , x , y , s , prop , angle , ismath ):
404-
405- if ismath :
406- self .draw_mathtext (gc , x , y , s , prop , angle )
407- return
408-
409- w , h = self .get_text_width_height (s , prop , ismath )
410- x = int (x )
411- y = int (y )
357+ def _draw_rotated_text (self , gc , x , y , s , prop , angle ):
358+ """
359+ Draw the text rotated 90 degrees
360+ """
412361
413- if angle == 90 :
414- #self._draw_rotated_text(gc, x, y, s, prop, angle, ismath)
415- self ._draw_rotated_text (gc , x , y , s , prop , angle )
416- return
362+ gdrawable = self .gdkDrawable
363+ ggc = gc .gdkGC
417364
418365 layout = self .get_pango_layout (s , prop )
419366 inkRect , logicalRect = layout .get_pixel_extents ()
420367 rect = inkRect
421368 l , b , w , h = rect
422369
423- self .gdkDrawable .draw_layout (gc .gdkGC , x = x , y = y - h - b ,
424- layout = layout )
370+ x = int (x - h )
371+ y = int (y - w )
372+ # get the background image
373+
374+ # todo: cache rotation for dynamic redraw until pygtk mem leak
375+ # fixed
376+ key = (x ,y ,s ,angle ,hash (prop ))
377+ imageOut = self .rotated .get (key )
378+ if imageOut is not None :
379+ gdrawable .draw_image (ggc , imageOut , 0 , 0 , x , y , h , w )
380+ return
381+
382+ # save the background
383+ imageBack = gdrawable .get_image (x , y , w , h )
384+ imageVert = gdrawable .get_image (x , y , h , w )
385+
386+ # transform the vertical image, write it onto the renderer,
387+ # and draw the layout onto it
388+ imageFlip = gtk .gdk .Image (type = gdk .IMAGE_NORMAL ,
389+ visual = gdrawable .get_visual (),
390+ width = w , height = h )
391+ if imageFlip is None or imageBack is None or imageVert is None :
392+ print >> sys .stderr , "Could not renderer vertical text" , s
393+ return
394+ imageFlip .set_colormap (gdrawable .get_colormap ())
395+ for i in range (w ):
396+ for j in range (h ):
397+ imageFlip .put_pixel (i , j , imageVert .get_pixel (j ,w - i - 1 ) )
398+
399+ gdrawable .draw_image (ggc , imageFlip , 0 , 0 , x , y , w , h )
400+ gdrawable .draw_layout (ggc , x , y - b , layout )
401+
402+ # now get that image and flip it vertical
403+ imageIn = gdrawable .get_image (x , y , w , h )
404+ imageOut = gtk .gdk .Image (type = gdk .IMAGE_NORMAL ,
405+ visual = gdrawable .get_visual (),
406+ width = h , height = w )
407+ imageOut .set_colormap (gdrawable .get_colormap ())
408+ for i in range (w ):
409+ for j in range (h ):
410+ imageOut .put_pixel (j , i , imageIn .get_pixel (w - i - 1 ,j ) )
411+
412+ # draw the old background and the flipped text
413+ gdrawable .draw_image (ggc , imageBack , 0 , 0 , x , y , w , h )
414+ gdrawable .draw_image (ggc , imageOut , 0 , 0 , x , y , h , w )
415+ self .rotated [key ] = imageOut
416+ return True
417+
425418
426-
427419 def get_pango_layout (self , s , prop ):
428420 """
429421 Return a pango layout instance for Text instance t. cache to
@@ -446,7 +438,6 @@ def get_pango_layout(self, s, prop):
446438 scale = self .get_text_scale ()
447439 size = prop .get_size_in_points ()
448440 font .set_size (int (scale * size * 1024 ))
449- #context = self.gtkDA.create_pango_context() # not used
450441 layout = self .gtkDA .create_pango_layout (s )
451442 layout .set_font_description (font )
452443
0 commit comments