def compute_shadow(self, rect): self.surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, rect.width, rect.height) surface_cr = cairo.Context(self.surface) draw_round_rectangle_with_triangle(surface_cr, rect, self.radius, self.arrow_width, self.arrow_height, self.offset, border=5, pos_type=self.pos_type) surface_cr.set_line_width(2) surface_cr.set_source_rgba(*alpha_color_hex_to_cairo(self.shadow_color)) surface_cr.stroke_preserve() gaussian_blur(self.surface, 2) # border. # out border. surface_cr.clip() draw_round_rectangle_with_triangle(surface_cr, rect, self.radius, self.arrow_width, self.arrow_height, self.offset, border=6, pos_type=self.pos_type) surface_cr.set_source_rgba(*alpha_color_hex_to_cairo(self.mask_color)) surface_cr.set_line_width(1) surface_cr.fill()
def draw_text(cr, markup, x, y, w, h, text_size=DEFAULT_FONT_SIZE, text_color="#000000", text_font=DEFAULT_FONT, alignment=pango.ALIGN_LEFT, gaussian_radious=None, gaussian_color=None, border_radious=None, border_color=None, wrap_width=None, ): if border_radious == None and border_color == None and gaussian_radious == None and gaussian_color == None: render_text(cr, markup, x, y, w, h, text_size, text_color, text_font, alignment, wrap_width=wrap_width) elif (border_radious != None and border_color != None) or (gaussian_radious != None and gaussian_color != None): # Create text cairo context. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) text_cr = cairo.Context(surface) # Draw gaussian light. if gaussian_radious != None and gaussian_color != None: text_cr.save() render_text(text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, gaussian_color, alignment=alignment, wrap_width=wrap_width) dtk_cairo_blur.gaussian_blur(surface, gaussian_radious) text_cr.restore() # Make sure border can render correctly. if gaussian_radious == None: gaussian_radious = 0 # Draw gaussian border. if border_radious != None and border_radious != 0 and border_color != None: render_text(text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, border_color, alignment=alignment, wrap_width=wrap_width) dtk_cairo_blur.gaussian_blur(surface, border_radious) # Draw font. render_text(text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, text_color, alignment=alignment, wrap_width=wrap_width) # Render gaussian text to target cairo context. cr.set_source_surface(surface, x, y) cr.paint()
def __draw_mpris_art_img(self, widget, event): x, y, w, h = widget.allocation cr = widget.window.cairo_create() surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) surface_cr = gtk.gdk.CairoContext(cairo.Context(surface)) surface_cr.set_source_rgba(0, 0, 0, 1.0) surface_cr.rectangle(5, 5, w-10, h-10) surface_cr.stroke() dtk_cairo_blur.gaussian_blur(surface, 3) cr.set_source_surface(surface, 0, 0) cr.paint() cr.set_source_rgb(1, 1, 1) cr.rectangle(2, 2, w-4, h-4) cr.fill() if widget.pixbuf: cr.set_source_pixbuf(widget.pixbuf, 4, 4) cr.paint() return True
def __compute_shadow(self, w, h): # sahow. cairo_popover_rectangle(self, self.surface_cr, self.trayicon_x, self.trayicon_y, w, h, self.radius) self.surface_cr.set_source_rgba( # set sahow color. *alpha_color_hex_to_cairo((self.sahow_color))) self.surface_cr.fill_preserve() gaussian_blur(self.surface, SAHOW_VALUE) # border. if self.draw_rectangle_bool: # out border. self.surface_cr.clip() cairo_popover_rectangle(self, self.surface_cr, self.trayicon_x + self.trayicon_border, self.trayicon_y + self.trayicon_border, w, h + 1, self.radius) self.surface_cr.set_source_rgba( # set out border color. *alpha_color_hex_to_cairo(self.border_out_color)) self.surface_cr.set_line_width(self.border_width) self.surface_cr.fill() self.draw_in_border(w, h)
def paint_text(self, cr, text, xpos, ypos): self.set_text(text) xpos += self.get_outline_width() / 2 + self.get_blur_radius() ypos += self.get_outline_width() + self.get_blur_radius() cr = pangocairo.CairoContext(cr) width, height = self.get_pixel_size(text) # Draw the outline of the text. cr.move_to(xpos, ypos) cr.save() cr.layout_path(self.pango_layout) cr.set_source_rgb(*self.get_blur_color()) if self.get_outline_width() > 0: cr.set_line_width(self.get_outline_width()) if self.get_blur_radius() > math.e - 4: cr.stroke_preserve() cr.fill() dtk_cairo_blur.gaussian_blur(cr.get_target(), self.get_blur_radius()) else: cr.stroke() cr.restore() cr.save() cr.new_path() # Create the linear pattern. pattern = cairo.LinearGradient(xpos, ypos, xpos, ypos + height) for i, each_linear in enumerate(self.linear_colors): pattern.add_color_stop_rgb(self.linear_pos[i], each_linear[0], each_linear[1], each_linear[2]) cr.set_source(pattern) cr.set_operator(cairo.OPERATOR_OVER) # Draw the text. cr.move_to(xpos, ypos) cr.show_layout(self.pango_layout) cr.restore()
def compute_shadow(self, w, h): # sahow. cairo_popover(self, self.surface_cr, self.trayicon_x, self.trayicon_y, w, h, self.radius, self.arrow_width, self.arrow_height, self.offset, pos_type=self.tray_pos_type) gaussian_blur(self.surface, SAHOW_VALUE) self.surface_cr.set_source_rgba( # set sahow color. *alpha_color_hex_to_cairo((self.sahow_color))) self.surface_cr.fill_preserve() gaussian_blur(self.surface, SAHOW_VALUE) # border. # out border. self.surface_cr.clip() cairo_popover(self, self.surface_cr, self.trayicon_x + self.trayicon_border, self.trayicon_y + self.trayicon_border, w, h + 1, self.radius, self.arrow_width, self.arrow_height, self.offset, pos_type=self.tray_pos_type) self.surface_cr.set_source_rgba( # set out border color. *alpha_color_hex_to_cairo(self.border_out_color)) self.surface_cr.set_line_width(self.border_width) self.surface_cr.fill() # in border. self.surface_cr.reset_clip() padding_h = 0.7 cairo_popover(self, self.surface_cr, self.trayicon_x + self.trayicon_border, self.trayicon_y + self.trayicon_border, w, h + padding_h, self.radius, self.arrow_width, self.arrow_height, self.offset, self.tray_pos_type) self.surface_cr.set_source_rgba(1, 1, 1, 1.0) # set in border color. self.surface_cr.set_line_width(self.border_width) self.surface_cr.fill()
def __render_blur(self, cr, rect): x, y, w, h = rect img_surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) img_surf_cr = cairo.Context(img_surf) draw_round_rectangle(img_surf_cr, x + 3, y + 3, w - 6, h - 6, BORDER_RADIOUS) img_surf_cr.set_source_rgb(0, 0, 0) img_surf_cr.set_line_width(1) img_surf_cr.stroke() gaussian_blur(img_surf, 2) cr.set_operator(cairo.OPERATOR_SOURCE) cr.set_source_surface(img_surf, 0, 0) cr.rectangle(*rect) cr.fill() cr.set_source_rgb(0, 0, 0) cr.set_line_width(1) draw_round_rectangle(cr, x + 3, y + 3, w - 6, h - 6, BORDER_RADIOUS) cr.stroke_preserve() cr.clip()
def draw_text( cr, markup, x, y, w, h, text_size=DEFAULT_FONT_SIZE, text_color="#000000", text_font=DEFAULT_FONT, alignment=pango.ALIGN_LEFT, gaussian_radious=None, gaussian_color=None, border_radious=None, border_color=None, wrap_width=None, underline=False, vertical_alignment=TEXT_ALIGN_MIDDLE, clip_line_count=None, ellipsize=pango.ELLIPSIZE_END, ): ''' Standard function for draw text. @param cr: Cairo context. @param markup: Pango markup string. @param x: X coordinate of draw area. @param y: Y coordinate of draw area. @param w: Width of draw area. @param h: Height of draw area. @param text_size: Text size, default is DEFAULT_FONT_SIZE. @param text_color: Text color, default is \"#000000\". @param text_font: Text font, default is DEFAULT_FONT. @param alignment: Font alignment option, default is pango.ALIGN_LEFT. You can set pango.ALIGN_MIDDLE or pango.ALIGN_RIGHT. @param gaussian_radious: Gaussian radious, default is None. @param gaussian_color: Gaussian color, default is None. @param border_radious: Border radious, default is None. @param border_color: Border color, default is None. @param wrap_width: Wrap width of text, default is None. @param underline: Whether draw underline for text, default is False. @param vertical_alignment: Vertical alignment value, default is TEXT_ALIGN_MIDDLE, can use below value: - TEXT_ALIGN_TOP - TEXT_ALIGN_MIDDLE - TEXT_ALIGN_BOTTOM @param clip_line_count: The line number to clip text area, if set 2, all lines that above 2 will clip out, default is None. @param ellipsize: Ellipsize style of text when text width longer than draw area, it can use below value: - pango.ELLIPSIZE_START - pango.ELLIPSIZE_CENTER - pango.ELLIPSIZE_END ''' if border_radious == None and border_color == None and gaussian_radious == None and gaussian_color == None: render_text( cr, markup, x, y, w, h, text_size, text_color, text_font, alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) elif (border_radious != None and border_color != None) or (gaussian_radious != None and gaussian_color != None): # Create text cairo context. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) text_cr = cairo.Context(surface) # Draw gaussian light. if gaussian_radious != None and gaussian_color != None: text_cr.save() render_text( text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, gaussian_color, alignment=alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) dtk_cairo_blur.gaussian_blur(surface, gaussian_radious) text_cr.restore() # Make sure border can render correctly. if gaussian_radious == None: gaussian_radious = 0 # Draw gaussian border. if border_radious != None and border_radious != 0 and border_color != None: render_text( text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, border_color, alignment=alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) dtk_cairo_blur.gaussian_blur(surface, border_radious) # Draw font. render_text( text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, text_color, alignment=alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) # Render gaussian text to target cairo context. cr.set_source_surface(surface, x, y) cr.paint()
def draw_text(cr, markup, x, y, w, h, text_size=DEFAULT_FONT_SIZE, text_color="#000000", text_font=DEFAULT_FONT, alignment=pango.ALIGN_LEFT, gaussian_radious=None, gaussian_color=None, border_radious=None, border_color=None, wrap_width=None, underline=False, vertical_alignment=TEXT_ALIGN_MIDDLE, clip_line_count=None, ellipsize=pango.ELLIPSIZE_END, ): ''' Standard function for draw text. @param cr: Cairo context. @param markup: Pango markup string. @param x: X coordinate of draw area. @param y: Y coordinate of draw area. @param w: Width of draw area. @param h: Height of draw area. @param text_size: Text size, default is DEFAULT_FONT_SIZE. @param text_color: Text color, default is \"#000000\". @param text_font: Text font, default is DEFAULT_FONT. @param alignment: Font alignment option, default is pango.ALIGN_LEFT. You can set pango.ALIGN_MIDDLE or pango.ALIGN_RIGHT. @param gaussian_radious: Gaussian radious, default is None. @param gaussian_color: Gaussian color, default is None. @param border_radious: Border radious, default is None. @param border_color: Border color, default is None. @param wrap_width: Wrap width of text, default is None. @param underline: Whether draw underline for text, default is False. @param vertical_alignment: Vertical alignment value, default is TEXT_ALIGN_MIDDLE, can use below value: - TEXT_ALIGN_TOP - TEXT_ALIGN_MIDDLE - TEXT_ALIGN_BOTTOM @param clip_line_count: The line number to clip text area, if set 2, all lines that above 2 will clip out, default is None. @param ellipsize: Ellipsize style of text when text width longer than draw area, it can use below value: - pango.ELLIPSIZE_START - pango.ELLIPSIZE_CENTER - pango.ELLIPSIZE_END ''' if border_radious == None and border_color == None and gaussian_radious == None and gaussian_color == None: render_text(cr, markup, x, y, w, h, text_size, text_color, text_font, alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) elif (border_radious != None and border_color != None) or (gaussian_radious != None and gaussian_color != None): # Create text cairo context. surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) text_cr = cairo.Context(surface) # Draw gaussian light. if gaussian_radious != None and gaussian_color != None: text_cr.save() render_text(text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, gaussian_color, alignment=alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) dtk_cairo_blur.gaussian_blur(surface, gaussian_radious) text_cr.restore() # Make sure border can render correctly. if gaussian_radious == None: gaussian_radious = 0 # Draw gaussian border. if border_radious != None and border_radious != 0 and border_color != None: render_text(text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, border_color, alignment=alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) dtk_cairo_blur.gaussian_blur(surface, border_radious) # Draw font. render_text(text_cr, markup, gaussian_radious, gaussian_radious, w - gaussian_radious * 2, h - gaussian_radious * 2, text_size, text_color, alignment=alignment, wrap_width=wrap_width, underline=underline, vertical_alignment=vertical_alignment, clip_line_count=clip_line_count, ellipsize=ellipsize, ) # Render gaussian text to target cairo context. cr.set_source_surface(surface, x, y) cr.paint()
LIST_CONTENT_HEIGHT = 50 LIST_TIME_WIDTH = 100 COUNT_PER_PAGE = 2 FONT_SIZE = 10 img_surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, WINDOW_WIDHT, WINDOW_HEIGHT) img_surf_cr = cairo.Context(img_surf) draw_round_rectangle_with_triangle(img_surf_cr, BORDER_LINE_WIDTH, BORDER_LINE_WIDTH, WINDOW_WIDHT - 2 * BORDER_LINE_WIDTH, WINDOW_HEIGHT - 2 * BORDER_LINE_WIDTH, ROUND_RADIUS, ARROW_WIDHT, ARROW_HEIGHT) img_surf_cr.set_line_width(1) img_surf_cr.stroke() gaussian_blur(img_surf, 2) class SelectButton(gtk.Button): def __init__(self, label="", bg_color="#ebf4fd", line_color="#7da2ce"): gtk.Button.__init__(self) self.label = label self.bg_color = bg_color self.line_color = line_color self.text_color = "#000000" self.set_size_request(-1, 25)