Changeset 991
- Timestamp:
- 09/03/06 13:22:17 (2 years ago)
- Files:
-
- gaphas/trunk/gaphas/canvas.py (modified) (2 diffs)
- gaphas/trunk/gaphas/util.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
gaphas/trunk/gaphas/canvas.py
r985 r991 272 272 cairo=cairo_context) 273 273 context_map[item] = c 274 item.pre_update(c) 274 try: 275 item.pre_update(c) 276 except Exception, e: 277 print 'Error while updating item %s' % item 278 import traceback 279 traceback.print_exc() 275 280 276 281 self.update_matrices() … … 290 295 cairo=cairo_context) 291 296 context_map[item] = c 292 item.update(c) 297 try: 298 item.update(c) 299 except Exception, e: 300 print 'Error while updating item %s' % item 301 import traceback 302 traceback.print_exc() 293 303 294 304 finally: gaphas/trunk/gaphas/util.py
r987 r991 4 4 5 5 from math import pi 6 import cairo 6 7 7 8 def text_extents(cr, text): 8 def text_extents(cr, text, font=None): 9 9 """Simple way to determine the size of a piece of text. 10 10 """ 11 x_bearing, y_bearing, width, height, x_adv, y_adv = c.text_extents(text) 11 if not text: 12 return 0, 0 13 if font: 14 cr.save() 15 text_set_font(font) 16 17 x_bearing, y_bearing, width, height, x_adv, y_adv = cr.text_extents(text) 18 if font: 19 cr.restore() 12 20 return width + x_bearing, height + y_bearing 13 21 14 22 15 def text_center(cr, x, y, text ):23 def text_center(cr, x, y, text, align_x=0, align_y=0): 16 24 """Draw text using (x, y) as center. 17 25 x - center x 18 26 y - center y 19 27 text - text to print (utf8) 28 align_x - -1 (top), 0 (middle), 1 (bottom) 29 align_y - -1 (left), 0 (center), 1 (right) 20 30 """ 31 if not text: 32 return 33 21 34 x_bear, y_bear, w, h, x_adv, y_adv = cr.text_extents(text) 35 #if align_x == 0: 22 36 x = 0.5 - (w / 2 + x_bear) + x 37 #elif align_x > 0: 38 # x = 0.5 - (w + x_bear) + x 39 #if align_y == 0: 23 40 y = 0.5 - (h / 2 + y_bear) + y 41 #elif align_y < 0: 42 # y = 0.5 - (h + y_bear) + y 24 43 cr.move_to(x, y) 25 44 cr.show_text(text) 45 46 47 def text_set_font(cr, font): 48 """Set the font from a string. E.g. 'sans 10' or 'sans italic bold 12' 49 only restriction is that the font name should be the first option and 50 the font size as last argument 51 """ 52 cr.select_font_face(font[0], 53 'italic' in font and cairo.FONT_SLANT_ITALIC \ 54 or cairo.FONT_SLANT_NORMAL, 55 'bold' in font and cairo.FONT_WEIGHT_BOLD \ 56 or cairo.FONT_WEIGHT_NORMAL) 57 cr.set_font_size(int(font[-1]) / 10.0) 26 58 27 59
