def svg(self, filename, x=0, y=0, width=None, height=None): try: handle = self.svg_handles[filename] except KeyError: handle = self.svg_handles[filename] = rsvg_handle_new_from_file(filename) cairo_save(self.cr) cairo_translate(self.cr, x, y) if width is not None or height is not None: dim = RsvgDimensionData() rsvg_handle_get_dimensions(handle, byref(dim)) if width is not None: scale_x = float(width) / dim.width else: scale_x = 1.0 if height is not None: scale_y = float(height) / dim.height else: scale_y = 1.0 cairo_scale(self.cr, scale_x, scale_y) rsvg_handle_render_cairo(handle, self.cr) cairo_restore(self.cr)
def __init__(self, screen, width, height, drawable): self.connection = screen.connection self.screen = screen self.width = width self.height = height self.drawable = drawable self.visual = screen.root_visual_type self.surface = cairo.cairo_xcb_surface_create( byref(self.connection._connection), drawable._xid, byref(self.visual._visualtype), width, height ) self.cr = cairo.cairo_create(self.surface) cairo.cairo_set_operator(self.cr, cairo.CAIRO_OPERATOR_SOURCE) cairo.cairo_set_source_surface(self.cr, self.surface, 0, 0) cairo.cairo_set_source_rgba(self.cr, 255, 0, 0, 0) self.default_font = "snap" self.default_font_size = 10 log.debug('created drawcontext %s' % self)
def hex2xlibcolor(screen, color): # "CIEXYZ:0.3227/0.28133/0.2493" # "RGBi:1.0/0.0/0.0" # "rgb:00/ff/00" # "CIELuv:50.0/0.0/0.0" color = "rgb:%s/%s/%s" % (color[:2], color[2:4], color[4:6]) ecolor = xlib.XColor() if not xlib.XParseColor(samuraix.display, screen.default_colormap, color, byref(ecolor)): raise ValueError("cant parse color %s" % color) if not xlib.XAllocColor(samuraix.display, screen.default_colormap, byref(ecolor)): raise "Cant alloc color" # DYNAMIC = 1 # if (ecolor.pixel != screen.black_pixel and # ecolor.pixel != screen.white_pixel and # screen.default_visual.class & DYNAMIC): return ecolor.pixel
def text(self, x, y, string, color=(0.0, 0.0, 0.0), font=None, bold=False, align=None, font_size=None): cairo.cairo_set_source_rgb(self.cr, color[0], color[1], color[2]) if font is None: font = self.default_font if font_size is None: font_size = self.default_font_size if bold: weight = cairo.CAIRO_FONT_WEIGHT_BOLD else: weight = cairo.CAIRO_FONT_WEIGHT_NORMAL cairo.cairo_select_font_face(self.cr, font, cairo.CAIRO_FONT_SLANT_NORMAL, weight) cairo.cairo_set_font_size(self.cr, font_size) if align and align != 'left': #typedef struct { # double x_bearing; # double y_bearing; # double width; # double height; # double x_advance; # double y_advance; #} cairo.cairo_text_extents_t; extents = cairo.cairo_text_extents_t() cairo.cairo_text_extents(self.cr, string, byref(extents)) if align == "right": x -= extents.x_advance cairo.cairo_move_to(self.cr, x, y) cairo.cairo_show_text(self.cr, string) self.connection.flush()