def rotated_text_draw(self, da, cairo_ctx): # Create a cairo context and set up a transformation matrix so that the user # space coordinates for the centered square where we draw are [-RADIUS, RADIUS], # [-RADIUS, RADIUS]. # We first center, then change the scale. width = da.get_allocated_width() height = da.get_allocated_height() device_radius = min(width, height) / 2.0 cairo_ctx.translate( device_radius + (width - 2 * device_radius) / 2, device_radius + (height - 2 * device_radius) / 2) cairo_ctx.scale(device_radius / self.RADIUS, device_radius / self.RADIUS) # Create a subtle gradient source and use it. pattern = cairo.LinearGradient(-self.RADIUS, -self.RADIUS, self.RADIUS, self.RADIUS) pattern.add_color_stop_rgb(0.0, 0.5, 0.0, 0.0) pattern.add_color_stop_rgb(1.0, 0.0, 0.0, 0.5) cairo_ctx.set_source(pattern) # Create a PangoContext and set up our shape renderer context = da.create_pango_context() PangoCairo.context_set_shape_renderer(context, self.fancy_shape_renderer, None) # Create a PangoLayout, set the text, font, and attributes */ layout = Pango.Layout(context=context) layout.set_text(UTF8_TEXT, -1) desc = Pango.FontDescription(self.FONT) layout.set_font_description(desc) attrs = self.create_fancy_attr_list_for_layout(layout) layout.set_attributes(attrs) # Draw the layout N_WORDS times in a circle */ for i in range(self.N_WORDS): # Inform Pango to re-layout the text with the new transformation matrix PangoCairo.update_layout(cairo_ctx, layout) width, height = layout.get_pixel_size() cairo_ctx.move_to(-width / 2, -self.RADIUS * 0.9) PangoCairo.show_layout(cairo_ctx, layout) # Rotate for the next turn cairo_ctx.rotate(math.pi * 2 / self.N_WORDS) return False
def rotated_text_draw(self, da, cairo_ctx): # Create a cairo context and set up a transformation matrix so that the user # space coordinates for the centered square where we draw are [-RADIUS, RADIUS], # [-RADIUS, RADIUS]. # We first center, then change the scale. width = da.get_allocated_width() height = da.get_allocated_height() device_radius = min(width, height) / 2.0 cairo_ctx.translate( device_radius + (width - 2 * device_radius) / 2, device_radius + (height - 2 * device_radius) / 2) cairo_ctx.scale(device_radius / self.RADIUS, device_radius / self.RADIUS) # Create a subtle gradient source and use it. pattern = cairo.LinearGradient(-self.RADIUS, -self.RADIUS, self.RADIUS, self.RADIUS) pattern.add_color_stop_rgb(0.0, 0.5, 0.0, 0.0) pattern.add_color_stop_rgb(1.0, 0.0, 0.0, 0.5) cairo_ctx.set_source(pattern) # Create a PangoContext and set up our shape renderer context = da.create_pango_context() PangoCairo.context_set_shape_renderer(context, self.fancy_shape_renderer, None) # Create a PangoLayout, set the text, font, and attributes */ layout = Pango.Layout(context=context) layout.set_text(UTF8_TEXT, -1) desc = Pango.FontDescription(self.FONT) layout.set_font_description(desc) attrs = self.create_fancy_attr_list_for_layout(layout) layout.set_attributes(attrs) # Draw the layout N_WORDS times in a circle */ for i in range(self.N_WORDS): # Inform Pango to re-layout the text with the new transformation matrix PangoCairo.update_layout(cairo_ctx, layout) width, height = layout.get_pixel_size() cairo_ctx.move_to(-width / 2, -self.RADIUS * 0.9) PangoCairo.show_layout(cairo_ctx, layout) # Rotate for the next turn cairo_ctx.rotate(math.pi * 2 / self.N_WORDS) return False
def draw_text(self, pos_y, text): """ Draw a text message, returning the Y position of the next message """ layout = self.create_pango_layout(text) layout.set_markup(text, -1) attr = layout.get_attributes() layout.set_width(Pango.SCALE * self.width) layout.set_spacing(Pango.SCALE * 3) if self.text_font: font = Pango.FontDescription( "%s %s" % (self.text_font, self.text_size)) layout.set_font_description(font) _tw, text_height = layout.get_pixel_size() self.col(self.bg_col) self.context.rectangle(0, pos_y - text_height, self.width, text_height) self.context.fill() self.context.set_operator(cairo.OPERATOR_OVER) self.col(self.fg_col) self.context.move_to(0, pos_y - text_height) PangoCairo.context_set_shape_renderer( self.get_pango_context(), self.render_custom, None) text = layout.get_text() count = 0 for loc in self.img_finder.finditer(text): idx = loc.start() if len(self.image_list) <= count: break # We f****d up. Who types ` anyway # url = self.imgList[count] attachment = Pango.attr_shape_new_with_data( self.pango_rect, self.pango_rect, count, None) attachment.start_index = idx attachment.end_index = idx + 1 attr.insert(attachment) count += 1 layout.set_attributes(attr) PangoCairo.show_layout(self.context, layout) return pos_y - text_height
def draw_text(self, y, text): (w, h) = self.get_size() layout = self.create_pango_layout(text) layout.set_markup(text, -1) attr = layout.get_attributes() layout.set_width(Pango.SCALE * w) layout.set_spacing(Pango.SCALE * 3) if(self.text_font): font = Pango.FontDescription( "%s %s" % (self.text_font, self.text_size)) layout.set_font_description(font) tw, th = layout.get_pixel_size() self.col(self.bg_col) self.context.rectangle(0, y - th, w, th) self.context.fill() self.context.set_operator(cairo.OPERATOR_OVER) self.col(self.fg_col) self.context.move_to(0, y - th) PangoCairo.context_set_shape_renderer( self.get_pango_context(), self.render_custom, None) text = layout.get_text() count = 0 for loc in self.imgFinder.finditer(text): idx = loc.start() if len(self.imgList) <= count: break # We f****d up. Who types ` anyway url = self.imgList[count] at = Pango.attr_shape_new_with_data( self.pango_rect, self.pango_rect, count, None) at.start_index = idx at.end_index = idx + 1 attr.insert(at) count += 1 layout.set_attributes(attr) PangoCairo.show_layout(self.context, layout) return y - th
def __init__(self): white = Gdk.RGBA() white.red = 1.0 white.green = 1.0 white.blue = 1.0 white.alpha = 1.0 self.window = Gtk.Window(title="Rotated Text") self.window.set_default_size(4 * self.RADIUS, 2 * self.RADIUS) self.window.connect('destroy', Gtk.main_quit) box = Gtk.HBox() box.set_homogeneous(True) self.window.add(box) # add a drawing area da = Gtk.DrawingArea() box.add(da) # override the background color from the theme da.override_background_color(0, white) da.connect('draw', self.rotated_text_draw) label = Gtk.Label(UTF8_TEXT) box.add(label) label.set_angle(45) # Setup some fancy stuff on the label layout = label.get_layout() PangoCairo.context_set_shape_renderer(layout.get_context(), self.fancy_shape_renderer, None) attrs = self.create_fancy_attr_list_for_layout(layout) label.set_attributes(attrs) self.window.show_all()
def __init__(self): white = Gdk.RGBA() white.red = 1.0 white.green = 1.0 white.blue = 1.0 white.alpha = 1.0 self.window = Gtk.Window(title="Rotated Text") self.window.set_default_size(4 * self.RADIUS, 2 * self.RADIUS) self.window.connect('destroy', Gtk.main_quit) box = Gtk.HBox() box.set_homogeneous(True) self.window.add(box) # add a drawing area da = Gtk.DrawingArea() box.add(da) # override the background color from the theme da.override_background_color(0, white) da.connect('draw', self.rotated_text_draw) label = Gtk.Label(label=UTF8_TEXT) box.add(label) label.set_angle(45) # Setup some fancy stuff on the label layout = label.get_layout() PangoCairo.context_set_shape_renderer(layout.get_context(), self.fancy_shape_renderer, None) attrs = self.create_fancy_attr_list_for_layout(layout) label.set_attributes(attrs) self.window.show_all()
def draw_text(self, pos_y, text, icon): """ Draw a text message, returning the Y position of the next message """ icon_width = self.icon_size icon_pad = self.icon_pad if not self.show_icon: icon = None if not icon: icon_pad = 0 icon_width = 0 layout = self.create_pango_layout(text) layout.set_auto_dir(True) layout.set_markup(text, -1) attr = layout.get_attributes() width = self.limit_width if self.width > self.limit_width else self.width layout.set_width((Pango.SCALE * (width - (self.border_radius * 4 + icon_width + icon_pad)))) layout.set_spacing(Pango.SCALE * 3) if self.text_font: font = Pango.FontDescription(self.text_font) layout.set_font_description(font) text_width, text_height = layout.get_pixel_size() self.col(self.bg_col) top = 0 if self.align_vert == 2: # Bottom align top = pos_y - (text_height + self.border_radius * 4) else: # Top align top = pos_y if text_height < icon_width: text_height = icon_width shape_height = text_height + self.border_radius * 4 shape_width = text_width + self.border_radius*4 + icon_width + icon_pad left = 0 if self.align_right: left = self.width - shape_width self.context.save() # Draw Background self.context.translate(left, top) # self.context.rectangle(self.border_radius, 0, # shape_width - (self.border_radius*2), shape_height) # self.context.fill() # self.context.rectangle(0, self.border_radius, # shape_width, shape_height - (self.border_radius * 2)) #self.context.arc(0.7, 0.3, 0.035, 1.25 * math.pi, 2.25 * math.pi) #self.context.arc(0.3, 0.7, 0.035, .25 * math.pi, 1.25 * math.pi) if self.border_radius == 0: self.context.move_to(0.0, 0.0) self.context.line_to(shape_width, 0.0) self.context.line_to(shape_width, shape_height) self.context.line_to(0.0, shape_height) self.context.close_path() self.context.fill() else: # Edge top self.context.move_to(self.border_radius, 0.0) self.context.line_to(shape_width - self.border_radius, 0.0) # Arc topright self.context.arc(shape_width - self.border_radius, self.border_radius, self.border_radius, 1.5 * math.pi, 2 * math.pi) # Edge right self.context.line_to( shape_width, shape_height - self.border_radius) # Arc bottomright self.context.arc(shape_width - self.border_radius, shape_height - self.border_radius, self.border_radius, 0.0, 0.5 * math.pi) # Edge bottom self.context.line_to(self.border_radius, shape_height) # Arch bottomleft self.context.arc(self.border_radius, shape_height - self.border_radius, self.border_radius, 0.5 * math.pi, math.pi) # Edge left self.context.line_to(0.0, self.border_radius) # Arc topleft self.context.arc(self.border_radius, self.border_radius, self.border_radius, math.pi, 1.5 * math.pi) # End self.context.close_path() self.context.fill() self.context.set_operator(cairo.OPERATOR_OVER) # Draw Image if icon: self.context.save() if self.icon_left: self.context.translate( self.border_radius*2, self.border_radius*2) draw_img_to_rect(icon, self.context, 0, 0, icon_width, icon_width) else: self.context.translate( self.border_radius*2 + text_width + icon_pad, self.border_radius*2) draw_img_to_rect(icon, self.context, 0, 0, icon_width, icon_width) self.context.restore() self.col(self.fg_col) if self.icon_left: self.context.translate( self.border_radius*2 + icon_width + icon_pad, self.border_radius*2) PangoCairo.context_set_shape_renderer( self.get_pango_context(), self.render_custom, None) text = layout.get_text() count = 0 layout.set_attributes(attr) PangoCairo.show_layout(self.context, layout) else: self.context.translate(self.border_radius*2, self.border_radius*2) PangoCairo.context_set_shape_renderer( self.get_pango_context(), self.render_custom, None) text = layout.get_text() count = 0 layout.set_attributes(attr) PangoCairo.show_layout(self.context, layout) self.context.restore() next_y = 0 if self.align_vert == 2: next_y = pos_y - (shape_height + self.padding) else: next_y = pos_y + shape_height + self.padding return next_y