Changeset 991

Show
Ignore:
Timestamp:
09/03/06 13:22:17 (2 years ago)
Author:
arjanmol
Message:

updates util.py, added checks on update calls

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • gaphas/trunk/gaphas/canvas.py

    r985 r991  
    272272                            cairo=cairo_context) 
    273273                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() 
    275280 
    276281            self.update_matrices() 
     
    290295                            cairo=cairo_context) 
    291296                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() 
    293303 
    294304        finally: 
  • gaphas/trunk/gaphas/util.py

    r987 r991  
    44 
    55from math import pi 
     6import cairo 
    67 
    7  
    8 def text_extents(cr, text): 
     8def text_extents(cr, text, font=None): 
    99    """Simple way to determine the size of a piece of text. 
    1010    """ 
    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() 
    1220    return width + x_bearing, height + y_bearing 
    1321 
    1422 
    15 def text_center(cr, x, y, text): 
     23def text_center(cr, x, y, text, align_x=0, align_y=0): 
    1624    """Draw text using (x, y) as center. 
    1725    x    - center x 
    1826    y    - center y 
    1927    text - text to print (utf8) 
     28    align_x - -1 (top), 0 (middle), 1 (bottom) 
     29    align_y - -1 (left), 0 (center), 1 (right) 
    2030    """ 
     31    if not text: 
     32        return 
     33 
    2134    x_bear, y_bear, w, h, x_adv, y_adv = cr.text_extents(text) 
     35    #if align_x == 0: 
    2236    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: 
    2340    y = 0.5 - (h / 2 + y_bear) + y 
     41    #elif align_y < 0: 
     42    #    y = 0.5 - (h + y_bear) + y 
    2443    cr.move_to(x, y) 
    2544    cr.show_text(text) 
     45 
     46 
     47def 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) 
    2658 
    2759