Advertisement
Guest User

Pango/Cairo Python Bug -- This will leak lots of memory

a guest
Jun 10th, 2011
3,588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. import gtk
  2. import cairo
  3. import gobject
  4. import pangocairo
  5. import pango
  6. import time
  7.  
  8. class MyWidget(gtk.DrawingArea):
  9.  
  10.     __gtype_name__ = "MyWidget"
  11.  
  12.     def __init__(self):
  13.         gtk.DrawingArea.__init__(self)
  14.         self.connect("size-allocate", self.size_allocate_cb)
  15.         self.connect("expose-event", self.do_expose)
  16.         self.set_size_request(320, 240)
  17.         self.set_events(gtk.gdk.ALL_EVENTS_MASK)
  18.         self.show()
  19.  
  20.         def update():
  21.             self.queue_draw()
  22.             return True
  23.         gobject.timeout_add(100, update)
  24.  
  25.     def size_allocate_cb(self, widget, allocation):
  26.         self.width = allocation.width
  27.         self.height = allocation.height
  28.  
  29.     def draw_text(self, cr, text, x, y, width):
  30.         pcr = pangocairo.CairoContext(cr)
  31.         lyt = pcr.create_layout()
  32.         lyt.set_text(text)
  33.         lyt.set_width(pango.units_from_double(width))
  34.         lyt.set_wrap(pango.WRAP_WORD_CHAR)
  35.         cr.move_to(x, y)
  36.         pcr.show_layout(lyt)
  37.         return lyt.get_pixel_size()[1]
  38.  
  39.     def do_expose(self, widget, event):
  40.         cr = self.window.cairo_create()
  41.         cr.rectangle(0, 0, self.width, self.height)
  42.         cr.set_source_rgba(1, 1, 1, 1)
  43.         cr.fill()
  44.  
  45.         cr.set_source_rgba(0, 0, 0, 1)
  46.        
  47.         pcr = pangocairo.CairoContext(cr)
  48.        
  49.         text_width = 175
  50.         spacing = 50
  51.  
  52.         text = str(time.time())
  53.         for i in xrange(0, self.width / text_width):
  54.             x = i * text_width
  55.             y = 0
  56.             while y < self.height:
  57.                 y += self.draw_text(cr, text, x, y, text_width - spacing) + spacing
  58.  
  59.  
  60. w = gtk.Window()
  61. w.add(MyWidget())
  62. w.show()
  63. gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement