def do_draw(self, cr): a = self.get_allocation() #state = self.get_state_flags() context = self.get_style_context() ds_h = self._dropshadow.get_height() y = (a.height - ds_h) / 2 Gdk.cairo_set_source_pixbuf(cr, self._dropshadow, 0, y) cr.paint() # layout circle x = self._margin y = (a.height - ds_h) / 2 + self._margin w = a.width - 2 * self._margin h = a.width - 2 * self._margin cr.new_path() r = min(w, h) * 0.5 x += int((w - 2 * r) / 2) y += int((h - 2 * r) / 2) from math import pi cr.arc(r + x, r + y, r, 0, 2 * pi) cr.close_path() if self.is_active: color = context.get_background_color(Gtk.StateFlags.SELECTED) else: color = context.get_background_color(Gtk.StateFlags.INSENSITIVE) Gdk.cairo_set_source_rgba(cr, color) cr.fill() for child in self: self.propagate_draw(child, cr)
def draw(self, widget, context): name = self.imagename.strip("\n") #print name if name.lower().endswith("png") : self.image = cairo.ImageSurface.create_from_png(name) context.set_source_surface(self.image, self.space_left, self.space_top) context.paint() width = self.image.get_width() height = self.image.get_height() self.set_size_request(width, height) #print 'png image', width, height if name.lower().endswith("jpg") or name.lower().endswith("jpeg"): self.pb = GdkPixbuf.Pixbuf.new_from_file(name) Gdk.cairo_set_source_pixbuf(context, self.pb, self.space_left, self.space_top) context.paint() self.dim = GdkPixbuf.Pixbuf.get_file_info(name) self.set_size_request(self.dim.width, self.dim.height) #print 'jpg image' if self.draw_enum == self.ENUM_BOXES: context.set_line_width(1) context.set_source_rgb(1, 0, 0) self.box_list(context, self.boxlist_red) context.set_source_rgb(0, 1, 0) self.box_list(context, self.boxlist_green) context.set_source_rgb(0, 0, 1) self.box_list(context, self.boxlist_blue) return False elif self.draw_enum == self.ENUM_GRADIENT_RGB: self.gradient_rgb(context, widget) pass
def _draw_cb(self, da, cr): """Paint a preview of the current brush to the view.""" if not self._brush_preview: cr.set_source_rgb(1, 0, 1) cr.paint() return aw = da.get_allocated_width() ah = da.get_allocated_height() # Work in a temporary group so that # the result can be masked with a gradient later. cr.push_group() # Paint a shadow line around the edge of # where the the brush preview will go. # There's an additional top border of one pixel # for alignment with the color preview widget # in the other corner. cr.rectangle(1.5, 2.5, aw-3, ah) cr.set_line_join(cairo.LINE_JOIN_ROUND) cr.set_source_rgba(*self._OUTLINE_RGBA) cr.set_line_width(3) cr.stroke() # Scale and align the brush preview in its own saved context cr.save() # Clip rectangle for the bit in the middle of the shadow. # Note that the bottom edge isn't shadowed. cr.rectangle(1, 2, aw-2, ah) cr.clip() # Scale and align the preview to the top of that clip rect. preview = self._brush_preview pw = preview.get_width() ph = preview.get_height() area_size = float(max(aw, ah)) - 2 preview_size = float(max(pw, ph)) x = math.floor(-pw/2.0) y = 0 cr.translate(aw/2.0, 2) scale = area_size / preview_size cr.scale(scale, scale) Gdk.cairo_set_source_pixbuf(cr, preview, x, y) cr.paint() cr.restore() # Finally a highlight around the edge in the house style # Note that the bottom edge isn't highlighted. cr.rectangle(1.5, 2.5, aw-3, ah) cr.set_line_width(1) cr.set_source_rgba(*self._EDGE_HIGHLIGHT_RGBA) cr.stroke() # Paint the group within a gradient mask cr.pop_group_to_source() mask = cairo.LinearGradient(0, 0, 0, ah) mask.add_color_stop_rgba(0.0, 1, 1, 1, 1.0) mask.add_color_stop_rgba(0.8, 1, 1, 1, 1.0) mask.add_color_stop_rgba(0.95, 1, 1, 1, 0.5) mask.add_color_stop_rgba(1.0, 1, 1, 1, 0.1) cr.mask(mask)
def draw(self, cr=None): ''' Draw the sprite (and label) ''' if cr is None: cr = self._sprites.cr if cr is None: print 'sprite.draw: no Cairo context.' return for i, img in enumerate(self.images): if isinstance(img, GdkPixbuf.Pixbuf): Gdk.cairo_set_source_pixbuf(cr, img, self.rect[0] + self._dx[i], self.rect[1] + self._dy[i]) cr.rectangle(self.rect[0] + self._dx[i], self.rect[1] + self._dy[i], self.rect[2], self.rect[3]) cr.fill() elif type(img) == cairo.ImageSurface: cr.set_source_surface(img, self.rect[0] + self._dx[i], self.rect[1] + self._dy[i]) cr.rectangle(self.rect[0] + self._dx[i], self.rect[1] + self._dy[i], self.rect[2], self.rect[3]) cr.fill() else: print 'sprite.draw: source not a pixbuf (%s)' % (type(img)) if len(self.labels) > 0: self.draw_label(cr)
def add_border(pixbuf, color, round=False, width=1): """Add a border to the pixbuf and round of the edges. color is a Gdk.RGBA The resulting pixbuf will be width * 2px higher and wider. Can not fail. """ w, h = pixbuf.get_width(), pixbuf.get_height() w += width * 2 h += width * 2 surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(surface) pi = math.pi r = min(w, h) / 10.0 if round else 0 ctx.new_path() ctx.arc(w - r, r, r, -pi / 2, 0) ctx.arc(w - r, h - r, r, 0, pi / 2) ctx.arc(r, h - r, r, pi / 2, pi) ctx.arc(r, r, r, pi, pi * 3 / 2) ctx.close_path() Gdk.cairo_set_source_pixbuf(ctx, pixbuf, width, width) ctx.clip_preserve() ctx.paint() ctx.set_source_rgba(color.red, color.green, color.blue, color.alpha) ctx.set_line_width(width * 2) ctx.stroke() return Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h)
def do_draw (self, cr): if not self.ensure_merged_pixbuf (): return False if self.merged_pixbuf.props.width != self.size: draw_pb = self.merged_pixbuf.scale_simple (self.size, self.size, GdkPixbuf.InterpType.NEAREST) else: draw_pb = self.merged_pixbuf # center the image if we're wider than we are tall pad = (self.get_allocation().width - self.size) / 2 left = pad right = pad + self.size top = 0 bottom = self.size if right > left and bottom > top: Gdk.cairo_set_source_pixbuf(cr, draw_pb, pad, 0) cr.rectangle(left, top, right - left, bottom - top) cr.fill() if self.anim: x, y, w, h = self.anim_rect () Gdk.cairo_set_source_pixbuf(cr, self.anim, max(0, x), max(0, y)) cr.rectangle(max(0, x), max(0, y), w, h) cr.fill() return False
def _new_dot(self, color): ''' generate a dot of a color color ''' if True: # not color in self._dot_cache: self._stroke = color[0] self._fill = color[1] self._svg_width = int(60 * self._scale) self._svg_height = int(60 * self._scale) pixbuf = svg_str_to_pixbuf( self._header() + \ '<circle cx="%f" cy="%f" r="%f" stroke="%s" fill="%s" \ stroke-width="%f" visibility="visible" />' % ( 30 * self._scale, 30 * self._scale, self._radius * self._scale, self._stroke, self._fill, self._stroke_width * self._scale) + \ self._footer()) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self._svg_width, self._svg_height) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) context.rectangle(0, 0, self._svg_width, self._svg_height) context.fill() # self._dot_cache[color] = surface return surface # self._dot_cache[color]
def set_image_cairo_komadai(self, y, piece, side): if side == BLACK: piece = piece.lower() keb = gv.gui.get_komadai_event_box(side, y) w = keb.get_window() cr = w.cairo_create() a = keb.get_allocation() # clear square to bg colour r, g, b = self.get_cairo_colour(set_board_colours.get_ref().get_komadai_colour()) cr.set_source_rgb(r, g, b) cr.rectangle(0, 0, a.width, a.height) cr.fill() # set offset so piece is centered in the square cr.translate(a.width * (1.0 - SCALE) / 2.0, a.height * (1.0 - SCALE) / 2.0) # scale piece so it is smaller than the square pb = gv.pieces.getpixbuf(piece) sfw = (a.width * 1.0 / pb.get_width()) * SCALE sfh = (a.height * 1.0 / pb.get_height()) * SCALE cr.scale(sfw, sfh) Gdk.cairo_set_source_pixbuf(cr, pb, 0, 0) cr.paint()
def render_round_floating_button(cr, x, y, color, pixbuf, z=2, radius=gui.style.FLOATING_BUTTON_RADIUS): """Draw a round floating button with a standard size. :param cairo.Context cr: Context in which to draw. :param float x: X coordinate of the center pixel. :param float y: Y coordinate of the center pixel. :param lib.color.UIColor color: Color for the button base. :param GdkPixbuf.Pixbuf pixbuf: Icon to render. :param int z: Simulated height of the button above the canvas. :param float radius: Button radius, in pixels. These are used within certain overlays tightly associated with particular interaction modes for manipulating things on the canvas. """ x = round(float(x)) y = round(float(y)) render_round_floating_color_chip(cr, x, y, color, radius=radius, z=z) cr.save() w = pixbuf.get_width() h = pixbuf.get_height() x -= w/2 y -= h/2 Gdk.cairo_set_source_pixbuf(cr, pixbuf, x, y) cr.rectangle(x, y, w, h) cr.clip() cr.paint() cr.restore()
def set_shapes(self, shapes, i=0): ''' Reskin the turtle ''' n = len(shapes) if n == 1 and i > 0: # set shape[i] if i < len(self._shapes): self._shapes[i] = shapes[0] elif n == SHAPES: # all shapes have been precomputed self._shapes = shapes[:] else: # rotate shapes if n != 1: debug_output("%d images passed to set_shapes: ignoring" % (n), self._turtles.turtle_window.running_sugar) if self._heading == 0.0: # rotate the shapes images = [] w, h = shapes[0].get_width(), shapes[0].get_height() nw = nh = int(sqrt(w * w + h * h)) for i in range(SHAPES): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, nw, nh) context = cairo.Context(surface) context.translate(nw / 2.0, nh / 2.0) context.rotate(i * 10 * pi / 180.) context.translate(-nw / 2.0, -nh / 2.0) Gdk.cairo_set_source_pixbuf( context, shapes[0], (nw - w) / 2.0, (nh - h) / 2.0) context.rectangle(0, 0, nw, nh) context.fill() images.append(surface) self._shapes = images[:] else: # associate shape with image at current heading j = int(self._heading + 5) % 360 / (360 / SHAPES) self._shapes[j] = shapes[0] self._custom_shapes = True self.show() self._calculate_sizes()
def draw (self, widget, cr): #draw pixbuf width = self.pixbuf.get_width() height = self.pixbuf.get_height() cr.rectangle(0, 0, width, height) Gdk.cairo_set_source_pixbuf(cr, self.pixbuf, 0, 0) cr.fill() #draw overlay if not MAC: self.create_overlay(cr, width, height) #draw selection cr.rectangle(self._x, self._y, self._width, self._height) Gdk.cairo_set_source_pixbuf(cr, self.pixbuf, 0, 0) cr.fill () #draw selection border cr.set_line_width(2) cr.rectangle(self._x, self._y, self._width, self._height) cr.set_source_rgba(1, 1, 0.74, 0.5) cr.stroke() return False
def __getitem__(self, fn): """Converts a filename into a numpy array of the texture, or returns None if there is no such file.""" # Handle it already being in the cache... if fn in self.cache: ret = self.cache[fn] del self.cache[fn] self.cache[fn] = ret # Put it to the back of the list. return ret # Load the file and convert it into a numpy array... alt_fn = os.path.splitext(fn)[0] + '_alpha.png' if os.path.exists(alt_fn): pixbuf = GdkPixbuf.Pixbuf.new_from_file(alt_fn) elif os.path.exists(fn): pixbuf = GdkPixbuf.Pixbuf.new_from_file(fn) else: return None texture = cairo.ImageSurface(cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height()) ctx = cairo.Context(texture) ctx.set_operator(cairo.OPERATOR_SOURCE) Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0) ctx.paint() del pixbuf ret = numpy.fromstring(texture.get_data(), dtype=numpy.uint8) ret = ret.reshape((texture.get_height(), texture.get_width(), -1)) # Handle cache expiry and return... self.cache[fn] = ret if len(self.cache) > self.limit: self.cache.popitem(False) return ret
def __draw_cb(self, widget, ctx): ctx.save() ctx.set_source_rgb(0, 0, 0) alloc = self.get_allocation() ctx.rectangle(alloc.width // 2 - self.sliderBorder - 1, self.sliderBorder, 2, alloc.height - self.sliderBorder * 2) ctx.fill() ctx.restore() val = self.get_value() if self.snap: val = round(self.snap * val) / self.snap adj = self.get_adjustment() if self.get_inverted(): sliderY = int((alloc.height - self.pixbufHeight) * \ (adj.get_upper() - val) / (adj.get_upper() - adj.get_lower())) else: sliderY = int((alloc.height - self.pixbufHeight) * \ val / (adj.get_upper() - adj.get_lower())) ctx.save() ctx.translate(0, sliderY) if self.insensitivePixbuf != None and \ self.state == Gtk.StateType.INSENSITIVE: Gdk.cairo_set_source_pixbuf(ctx, self.insensitivePixbuf, 0, 0) ctx.paint() else: Gdk.cairo_set_source_pixbuf(ctx, self.sliderPixbuf, 0, 0) ctx.paint() ctx.restore() return True
def draw(self, cr=None): ''' Draw the sprite (and label) ''' if cr is None: cr = self._sprites.cr if cr is None: # print 'sprite.draw: no Cairo context.' return for i, img in enumerate(self.cached_surfaces): cr.set_source_surface(img, self.rect[0] + self._dx[i], self.rect[1] + self._dy[i]) cr.rectangle(self.rect[0] + self._dx[i], self.rect[1] + self._dy[i], self.rect[2], self.rect[3]) cr.fill() if self._embedded_picture is not None: w = self.rect[2] h = self.rect[3] x = self.rect[0] + w / 2 - self._embedded_picture.get_width() / 2 y = self.rect[1] + h / 2 - self._embedded_picture.get_height() / 2 Gdk.cairo_set_source_pixbuf(cr, self._embedded_picture, x, y) cr.rectangle(x, y, w, h) cr.fill() if len(self.labels) > 0: self.draw_label(cr)
def _draw_image(self, context, pixbuf, rect, rgba): Gdk.cairo_set_source_pixbuf(context, pixbuf, rect.x, rect.y) pattern = context.get_source() context.rectangle(*rect) context.set_source_rgba(*rgba) context.mask(pattern) context.new_path()
def do_render(self, ctx, widget, background_area, cell_area, flags): if not self.image: return if self.image.get_storage_type() == Gtk.ImageType.ANIMATION: if self.image not in self.iters: if not isinstance(widget, Gtk.TreeView): return animation = self.image.get_animation() timeval = GLib.TimeVal() timeval.tv_sec = GLib.get_monotonic_time() / 1000000 iter_ = animation.get_iter(timeval) self.iters[self.image] = iter_ GLib.timeout_add(iter_.get_delay_time(), self.animation_timeout, widget, self.image) pix = self.iters[self.image].get_pixbuf() elif self.image.get_storage_type() == Gtk.ImageType.PIXBUF: pix = self.image.get_pixbuf() else: return calc_width = self.get_property('xpad') * 2 + pix.get_width() calc_height = self.get_property('ypad') * 2 + pix.get_height() x_pos = cell_area.x + self.get_property('xalign') * \ (cell_area.width - calc_width - self.get_property('xpad')) y_pos = cell_area.y + self.get_property('yalign') * \ (cell_area.height - calc_height - self.get_property('ypad')) Gdk.cairo_set_source_pixbuf(ctx, pix, x_pos, y_pos) ctx.paint()
def set_image(self, image, i=0, dx=0, dy=0): if type(image) == list: self._embedded_picture = image[1] image = image[0] ''' Add an image to the sprite. ''' while len(self.cached_surfaces) < i + 1: self.cached_surfaces.append(None) self._dx.append(0) self._dy.append(0) self._dx[i] = dx self._dy[i] = dy if hasattr(image, 'get_width'): w = image.get_width() h = image.get_height() else: w, h = image.get_size() if i == 0: # Always reset width and height when base image changes. self.rect[2] = w + dx self.rect[3] = h + dy else: if w + dx > self.rect[2]: self.rect[2] = w + dx if h + dy > self.rect[3]: self.rect[3] = h + dy if isinstance(image, cairo.ImageSurface): self.cached_surfaces[i] = image else: surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, self.rect[2], self.rect[3]) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, image, 0, 0) context.rectangle(0, 0, self.rect[2], self.rect[3]) context.fill() self.cached_surfaces[i] = surface
def _new_xo_man(self, color): ''' generate a xo-man of a color color ''' if True: # not color in self._xo_cache: self._stroke = color[0] self._fill = color[1] self._svg_width = int(240. * self._scale) self._svg_height = int(260. * self._scale) string = \ self._header() + \ '<g>' + \ '<g id="XO">' + \ '<path id="Line1" d="M%f,%f C%f,%f %f,%f %f,%f" stroke="%s" \ stroke-width="%f" stroke-linecap="round" fill="none" visibility="visible" />' \ % ( 165.5 * self._scale, 97 * self._scale, 120 * self._scale, 140.5 * self._scale, 120 * self._scale, 140.5 * self._scale, 74.5 * self._scale, 188 * self._scale, self._stroke, 37 * self._scale) + \ '<path id="Line2" d="M%f,%f C%f,%f %f,%f %f,%f" stroke="%s" \ stroke-width="%f" stroke-linecap="round" fill="none" visibility="visible" />' \ % ( 165.5 * self._scale, 188 * self._scale, 120 * self._scale, 140.5 * self._scale, 120 * self._scale, 140.5 * self._scale, 74.5 * self._scale, 97 * self._scale, self._stroke, 37 * self._scale) + \ '<path id="Fill1" d="M%f,%f C%f,%f %f,%f %f,%f" stroke="%s" \ stroke-width="%f" stroke-linecap="round" fill="none" visibility="visible" />' \ % ( 165.5 * self._scale, 97 * self._scale, 120 * self._scale, 140.5 * self._scale, 120 * self._scale, 140.5 * self._scale, 74.5 * self._scale, 188 * self._scale, self._fill, 17 * self._scale) + \ '<path id="Fill2" d="M%f,%f C%f,%f %f,%f %f,%f" stroke="%s" \ stroke-width="%f" stroke-linecap="round" fill="none" visibility="visible" />' \ % ( 165.5 * self._scale, 188 * self._scale, 120 * self._scale, 140.5 * self._scale, 120 * self._scale, 140.5 * self._scale, 74.5 * self._scale, 97 * self._scale, self._fill, 17 * self._scale) + \ '<circle id="Circle" cx="%f" cy="%f" r="%f" \ fill="%s" stroke="%s" stroke-width="%f" visibility="visible" />' % ( 120 * self._scale, 61.5 * self._scale, 27.5 * self._scale, self._fill, self._stroke, 11 * self._scale) + \ '</g></g>' + \ self._footer() pixbuf = svg_str_to_pixbuf(string) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self._svg_width, self._svg_height) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) context.rectangle(0, 0, self._svg_width, self._svg_height) context.fill() # self._xo_cache[color] = surface return surface # self._xo_cache[color]
def small_draw(self, w, cr): if not self.pb: self.centered_text(w, cr, 10, "Open a Camera!") return width = w.get_allocated_width() height = w.get_allocated_height() Gdk.cairo_set_source_pixbuf( cr, self.pb.scale_simple( width, height, GdkPixbuf.InterpType.BILINEAR), 0, 0) cr.paint() cr.set_source_rgb(0.42, 0.65, 0.80) cr.set_line_width(2) cr.rectangle(self.view_off_x, self.view_off_y, self.view_width, self.view_height) cr.stroke() if self.cross: cr.set_source_rgb(0.80, 0, 0) cr.set_line_width(1) x = self.px * width / self.im_width y = self.py * height / self.im_height cr.move_to(0, y) cr.line_to(width, y) cr.move_to(x, 0) cr.line_to(x, height) cr.stroke()
def stamp(self, widget, coords, last, stamp_size=20): """Paint with stamp. @param self -- Desenho.Desenho instance @param last -- last of oldx @param widget -- Area object (GtkDrawingArea) @param coords -- Two value tuple @param stamp_size -- integer (default 20) """ widget.desenha = False width = widget.resized_stamp.get_width() height = widget.resized_stamp.get_height() dx = coords[0] - width / 2 dy = coords[1] - height / 2 widget.drawing_ctx.save() widget.drawing_ctx.translate(dx, dy) widget.drawing_ctx.rectangle(dx, dy, width, height) Gdk.cairo_set_source_pixbuf(widget.drawing_ctx, widget.resized_stamp, 0, 0) widget.drawing_ctx.paint() widget.drawing_ctx.restore() widget.queue_draw_area(dx, dy, width, height)
def on_draw(self, widget, cr, layout): a = widget.get_allocation() # paint the current animation frame x = (a.width - self.h_stride) * 0.5 y = (a.height - self.v_stride) * 0.5 Gdk.cairo_set_source_pixbuf(cr, self.frame, x, y) cr.paint() if self.transaction_count <= 0: return # paint a bubble with the transaction count layout.set_markup('<small>%i</small>' % self.transaction_count, -1) # determine bubble size extents = layout.get_pixel_extents()[1] width = extents.width + (2 * self.BUBBLE_XPADDING) height = extents.height + (2 * self.BUBBLE_YPADDING) # now render the bubble and layout context = self.get_style_context() x += self.h_stride + self.BUBBLE_XPADDING y += (self.v_stride - height) / 2 rounded_rect(cr, x, y, width, height, self.BUBBLE_BORDER_RADIUS) cr.set_source_rgba(0,0,0,0.2) cr.fill() Gtk.render_layout(context, cr, x + self.BUBBLE_XPADDING, y + self.BUBBLE_YPADDING, layout) return
def draw_image(self, image, context): x = 0 y = 0 rect = self.drawing_area.get_allocation() width_scale = image.get_width() / float(rect.width) height_scale = image.get_height() / float(rect.height) if ((width_scale < 1.0 and height_scale < 1.0) or (width_scale >= 1.0 and height_scale >= 1.0)): if width_scale < height_scale: divisor = height_scale x = (rect.width - int(image.get_width() / divisor)) / 2 else: divisor = width_scale y = (rect.height - int(image.get_height() / divisor)) / 2 elif width_scale > 1.0: divisor = width_scale y = (rect.height - int(image.get_height() / divisor)) / 2 else: divisor = height_scale x = (rect.width - int(image.get_width() / divisor)) / 2 scaled_image = image.scale_simple(int(image.get_width() / divisor), int(image.get_height() / divisor), GdkPixbuf.InterpType.BILINEAR) Gdk.cairo_set_source_pixbuf (context, scaled_image, x, y) context.paint()
def load_image_to_pixbuf(filename): pixbuf = GdkPixbuf.Pixbuf.new_from_file(filename) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, pixbuf.get_width(), pixbuf.get_height()) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) context.paint() return surface
def _new_dot(self, color): ''' generate a dot of a color color ''' self._dot_cache = {} if color not in self._dot_cache: self._stroke = color self._fill = color self._svg_width = self._dot_size self._svg_height = self._dot_size i = self._colors.index(color) if PATHS[i] is False: pixbuf = svg_str_to_pixbuf( self._header() + self._circle(self._dot_size / 2., self._dot_size / 2., self._dot_size / 2.) + self._footer()) else: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size( os.path.join(self._path, PATHS[i]), self._svg_width, self._svg_height) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self._svg_width, self._svg_height) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) context.rectangle(0, 0, self._svg_width, self._svg_height) context.fill() self._dot_cache[color] = surface return self._dot_cache[color]
def run(self): try: response = requests.get(self.url) if response.status_code == 200: loader = GdkPixbuf.PixbufLoader() loader.write(response.content) loader.close() pixbuf = loader.get_pixbuf() height = pixbuf.get_height() * self.scale / 100.0 width = pixbuf.get_width() * self.scale / 100.0 scaled_buf = pixbuf.scale_simple( width, height, GdkPixbuf.InterpType.BILINEAR) if scaled_buf: surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, scaled_buf.get_width(), scaled_buf.get_height()) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, scaled_buf, 0, 0) context.paint() self.emit('downloaded', True, surface, width, height) return except Exception as e: print(e) self.emit('downloaded', False, None, -1, -1) return
def _render_icon(self, cr, app, cell_area, xpad, ypad, is_rtl): # calc offsets so icon is nicely centered icon = self.model.get_icon(app) xo = (self.pixbuf_width - icon.get_width())/2 if not is_rtl: x = cell_area.x + xo + xpad else: x = cell_area.x + cell_area.width + xo - self.pixbuf_width - xpad y = cell_area.y + ypad # draw appicon pixbuf Gdk.cairo_set_source_pixbuf(cr, icon, x, y) cr.paint() # draw overlay if application is installed if self.model.is_installed(app): if not is_rtl: x += (self.pixbuf_width - self.OVERLAY_SIZE + self.OVERLAY_XO) else: x -= self.OVERLAY_XO y += (self.pixbuf_width - self.OVERLAY_SIZE + self.OVERLAY_YO) Gdk.cairo_set_source_pixbuf(cr, self._installed, x, y) cr.paint() return
def do_draw(self, cr): cr.save() A = self.get_allocation() if self._pressed: cr.translate(1, 1) if self.has_focus(): Gtk.render_focus(self.get_style_context(), cr, 3, 3, A.width-6, A.height-6) for child in self: self.propagate_draw(child, cr) if self.is_installed: # paint installed tick overlay if self.get_direction() != Gtk.TextDirection.RTL: x = y = 36 else: x = A.width - 56 y = 36 Gdk.cairo_set_source_pixbuf(cr, self._overlay, x, y) cr.paint() cr.restore() return
def _get_preview(self): # This code borrows from sugar3.activity.Activity.get_preview # to make the preview with cairo, and also uses GdkPixbuf to # load any GdkPixbuf supported format. pixbuf = GdkPixbuf.Pixbuf.new_from_file(self._dest_path) image_width = pixbuf.get_width() image_height = pixbuf.get_height() preview_width, preview_height = activity.PREVIEW_SIZE preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, preview_width, preview_height) cr = cairo.Context(preview_surface) scale_w = preview_width * 1.0 / image_width scale_h = preview_height * 1.0 / image_height scale = min(scale_w, scale_h) translate_x = int((preview_width - (image_width * scale)) / 2) translate_y = int((preview_height - (image_height * scale)) / 2) cr.translate(translate_x, translate_y) cr.scale(scale, scale) cr.set_source_rgba(1, 1, 1, 0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.paint() Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0) cr.paint() preview_str = StringIO.StringIO() preview_surface.write_to_png(preview_str) return preview_str.getvalue()
def _draw_badge(self, context, size, sensitive, widget): theme = Gtk.IconTheme.get_default() badge_info = theme.lookup_icon(self.badge_name, int(size), 0) if badge_info: badge_file_name = badge_info.get_filename() if badge_file_name.endswith('.svg'): handle = self._loader.load(badge_file_name, {}, self.cache) icon_width = handle.props.width icon_height = handle.props.height pixbuf = handle.get_pixbuf() else: pixbuf = GdkPixbuf.Pixbuf.new_from_file(badge_file_name) icon_width = pixbuf.get_width() icon_height = pixbuf.get_height() context.scale(float(size) / icon_width, float(size) / icon_height) if not sensitive: pixbuf = self._get_insensitive_pixbuf(pixbuf, widget) Gdk.cairo_set_source_pixbuf(context, pixbuf, 0, 0) context.paint()
def set_image(self, image, i=0, dx=0, dy=0): ''' Add an image to the sprite. ''' while len(self.cached_surfaces) < i + 1: self.cached_surfaces.append(None) self._dx.append(0) self._dy.append(0) self._dx[i] = dx self._dy[i] = dy if isinstance(image, GdkPixbuf.Pixbuf) or \ isinstance(image, cairo.ImageSurface): w = image.get_width() h = image.get_height() else: w, h = image.get_size() if i == 0: # Always reset width and height when base image changes. self.rect.width = w + dx self.rect.height = h + dy else: if w + dx > self.rect.width: self.rect.width = w + dx if h + dy > self.rect.height: self.rect.height = h + dy if isinstance(image, cairo.ImageSurface): self.cached_surfaces[i] = image else: # Convert to Cairo surface surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, self.rect.width, self.rect.height) context = cairo.Context(surface) Gdk.cairo_set_source_pixbuf(context, image, 0, 0) context.rectangle(0, 0, self.rect.width, self.rect.height) context.fill() self.cached_surfaces[i] = surface
def load_image_at_size(name, width, height): """Loads an image file at the specified size. Does NOT handle exceptions!""" from gi.repository import GdkPixbuf, Gdk from cairo import ImageSurface, FORMAT_ARGB32, Context pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(name, width, height) surface = ImageSurface(FORMAT_ARGB32, width, height) ctx = Context(surface) Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0) ctx.paint() return surface
def __do_draw(self, widget, context): rect = self.get_allocation() src = self.imagen dst = GdkPixbuf.Pixbuf.new_from_file_at_size(self.temp_path, rect.width, rect.height) GdkPixbuf.Pixbuf.scale(src, dst, 0, 0, 100, 100, 0, 0, 1.5, 1.5, GdkPixbuf.InterpType.BILINEAR) x = rect.width / 2 - dst.get_width() / 2 y = rect.height / 2 - dst.get_height() / 2 Gdk.cairo_set_source_pixbuf(context, dst, x, y) context.paint()
def render_background_cb(self, cr, wd, ht, icon_border=None): self._backend.set_brush_color(*self._hsv) size = self._backend.get_size() pixbuf = GdkPixbuf.Pixbuf.new( GdkPixbuf.Colorspace.RGB, True, 8, size, size, ) arr = gdkpixbuf2numpy(pixbuf) self._backend.render(arr) self._dx = (wd-size)/2 self._dy = (ht-size)/2 cr.translate(self._dx, self._dy) Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0) cr.paint()
def rounded(self, pixbuf, size): surface = cairo.ImageSurface(cairo.Format.ARGB32, size, size) ctx = cairo.Context(surface) radius = size / 10 ctx.arc(size - radius, radius, radius, -90 * (pi / 180), 0.0) ctx.arc(size - radius, size - radius, radius, 0.0, 90 * (pi / 180)) ctx.arc(radius, size - radius, radius, 90 * (pi / 180), 180 * (pi / 180)) ctx.arc(radius, radius, radius, 180 * (pi / 180), 270 * (pi / 180)) ctx.clip() ctx.new_path() Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0) ctx.paint() return Gdk.pixbuf_get_from_surface(surface, 0, 0, size, size)
def _common_line_icons(cr, type_id, slot_id, icon_actor, icon_object, icon_performer, icon_gs): # Slot Type cr.translate(10, -1) if type_id == 3: Gdk.cairo_set_source_pixbuf(cr, icon_actor, 0, 0) cr.paint() cr.set_source_rgb(*COLOR_ACTOR[:3]) elif type_id == 4: Gdk.cairo_set_source_pixbuf(cr, icon_object, 0, 0) cr.paint() cr.set_source_rgb(*COLOR_OBJECTS[:3]) elif type_id == 5: Gdk.cairo_set_source_pixbuf(cr, icon_performer, 0, 0) cr.paint() cr.set_source_rgb(*COLOR_PERFORMER[:3]) else: Gdk.cairo_set_source_pixbuf(cr, icon_gs, 0, 0) cr.paint() cr.set_source_rgb(255, 255, 255) cr.translate(-10, 1) # Slot ID if slot_id > -1 and type_id > 1: cr.move_to(0, 11) cr.select_font_face("cairo:monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size(12) cr.show_text(str(slot_id))
def _draw_gi(self, cr): Gdk.cairo_set_source_pixbuf(cr, self._pixbuf, 0, 0) cr.paint() cr.set_source_rgb(.8, .8, .8) layout = PangoCairo.create_layout(cr) desc = Pango.FontDescription('Sans 12') layout.set_font_description(desc) layout.set_alignment(Pango.Alignment.CENTER) layout.set_markup(self._get_label(), -1) PangoCairo.update_layout(cr, layout) w, h = layout.get_pixel_size() cr.move_to((WIDTH - w) / 2, (HEIGHT / 2) + h) PangoCairo.show_layout(cr, layout)
def create_surface(self, pixbuf): pix_w = pixbuf.get_width() pix_h = pixbuf.get_height() img = cairo.ImageSurface(0, pix_w, pix_h) img_cr = cairo.Context(img) Gdk.cairo_set_source_pixbuf(img_cr, pixbuf, 0, 0) img_cr.paint() self.screenPixbuf = pixbuf self.screenPixbufResult = pixbuf self.screenSurface = img self.screenWidth = pix_w self.screenHeight = pix_h self.aspectframe.set(0.5, 0.5, pix_w / pix_h, False)
def set_image(self, image): stream = image.read(None) pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream) self.iw = pixbuf.get_width() self.ih = pixbuf.get_height() self.displayed_image.set_size_request(self.iw * self.sf, self.ih * self.sf) self.csurface = cairo.ImageSurface(cairo.Format.RGB24, self.iw, self.ih) ctx = cairo.Context(self.csurface) Gdk.cairo_set_source_pixbuf(ctx, pixbuf, 0, 0) ctx.paint() self.displayed_image.queue_draw()
def drawCell(self, cr, cell, cellNum): img = self.items[cellNum] fname = img.file thumb = self.thumbCache.get(fname) print('draw thumb: ', fname) if not thumb: thumb = GdkPixbuf.Pixbuf.new_from_file(fname) thumb = thumb.scale_simple(self.thumbnail_width, self.thumbnail_height, GdkPixbuf.InterpType.BILINEAR) self.thumbCache[fname] = thumb x = cell.x - self.visible.x y = cell.y - self.visible.y Gdk.cairo_set_source_pixbuf(cr, thumb, x, y) cr.paint() cr.stroke()
def do_draw(self, cr): """This scales the QR Code up to the widget's size. You may define your own size, but you must be careful not to cause too many resizing events. When you request a too big size, it may loop to death trying to fit the image. """ data = self.data box = self.get_allocation() width, height = box.width, box.height size = min(width, height) - 10 if data is not None: pixbuf = self.image_to_pixbuf(self.create_qrcode(data, size)) Gdk.cairo_set_source_pixbuf(cr, pixbuf, width // 2 - size // 2, height // 2 - size // 2) cr.paint()
def drawHistogram(self,area,context): global __HISTOGRAM__ if __HISTOGRAM__ is not None: pix=GdkPixbuf.Pixbuf() img=pix.new_from_data(__HISTOGRAM__.tostring(),\ GdkPixbuf.Colorspace.RGB,\ False,8,\ __HISTOGRAM__.shape[1],\ __HISTOGRAM__.shape[0],\ __HISTOGRAM__.shape[1]*3,\ None,None) gdk.cairo_set_source_pixbuf(context,img,0,0) context.fill() context.paint() __HISTOGRAM__=None
def do_render(self, cr, widget, bg_area, cell_area, flags): pixbuf = GdkPixbuf.Pixbuf.new(Colorspace.RGB, True, 8, cell_area.width, cell_area.height) Gdk.cairo_set_source_pixbuf(cr, pixbuf, cell_area.x, cell_area.y) ## Draw a filled square cr.set_source_rgb(*self.rgb_triplet) cr.rectangle(cell_area.x + 1, cell_area.y + 1, cell_area.width - 2, cell_area.height - 2) cr.fill() ## Outline it black cr.set_source_rgb(0, .8, 0) cr.rectangle(cell_area.x + 1, cell_area.y + 1, cell_area.width - 2, cell_area.height - 2) cr.stroke()