def do_render(self, cr, treeview, background_area, cell_area, flags): mouse_x, mouse_y = treeview.get_pointer() cell_render_x = mouse_x - cell_area.x # # Update the rating if it was clicked (the liststore is updated too) # if self._clicked != (): x_click_coord=self._clicked[0] self._clicked=() # Check if the click was inside the cell # if x_click_coord >= cell_area.x and x_click_coord <= cell_area.x+self.font_size*5 and 'SELECTED' in str(flags): rating=round(cell_render_x/self.font_size) if rating >= 0 and rating <= 5 and rating != self.rating: self.rating=rating try: pointing_treepath=treeview.get_path_at_pos(mouse_x, mouse_y-self.font_size)[0] self._liststore[pointing_treepath][self._column]=self.rating except Exception as e: print(e) if self._clicked_function: self._clicked_function(self._liststore, pointing_treepath, rating) # # Draw the rating # cr.translate (0, 0) layout = PangoCairo.create_layout(cr) layout.set_font_description(FONT_CELLRATING_DESCRIPTION) y_height_correction=self.font_size/3 cell_height=self.font_size+1 if 'GTK_CELL_RENDERER_FOCUSED' in str(flags) and self.rating < 5: for i in range(5): if i < self.rating: layout.set_text("★", -1) else: layout.set_text("☆", -1) cr.save() PangoCairo.update_layout (cr, layout) cr.move_to (cell_area.x+i*cell_height, cell_area.y-y_height_correction) PangoCairo.show_layout (cr, layout) cr.restore() else: for i in range(self.rating): layout.set_text("★", -1) cr.save() PangoCairo.update_layout (cr, layout) cr.move_to (cell_area.x+i*cell_height, cell_area.y-y_height_correction) PangoCairo.show_layout (cr, layout) cr.restore()
def draw(self, cr): """ Draw the signal block with label and inputs/outputs. """ border_color = colors.HIGHLIGHT_COLOR if self.highlighted else self._border_color cr.translate(*self.coordinate) for port in self.active_ports(): # ports first cr.save() port.draw(cr) cr.restore() cr.rectangle(*self._area) cr.set_source_rgba(*self._bg_color) cr.fill_preserve() cr.set_source_rgba(*border_color) cr.stroke() # title and params label if self.is_vertical(): cr.rotate(-math.pi / 2) cr.translate(-self.width, 0) cr.set_source_rgba(*self._font_color) for layout, offset in zip(self._surface_layouts, self._surface_layouts_offsets): cr.save() cr.translate(*offset) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) cr.restore()
def do_draw(self, cairo_ctx): cairo_ctx.save() try: layout = PangoCairo.create_layout(cairo_ctx) if self.font: font = Pango.FontDescription() font.set_family(self.font) layout.set_font_description(font) layout.set_text(self.text, -1) txt_size = layout.get_size() if 0 in txt_size: return txt_factor = float(self.height) / txt_size[1] self.size = ( int(txt_size[0] * txt_factor), int(txt_size[1] * txt_factor) ) self.position = ( int(self.center_position[0] - (self.size[0] / 2)), int(self.center_position[1] - (self.size[1] / 2)) ) cairo_ctx.translate(self.position[0], self.position[1]) if txt_factor <= 0.0: return cairo_ctx.scale(txt_factor * Pango.SCALE, txt_factor * Pango.SCALE) cairo_ctx.set_source_rgb(0.0, 0.0, 0.0) PangoCairo.update_layout(cairo_ctx, layout) PangoCairo.show_layout(cairo_ctx, layout) finally: cairo_ctx.restore()
def write(c, text, name, size, centered=False, at_top=False): font = Pango.FontDescription(name) font.set_size(int(round(size * Pango.SCALE))) lo = PangoCairo.create_layout(c) lo.set_font_description(font) lo.set_text('X', -1) baseline_offset = lo.get_baseline() / Pango.SCALE if not at_top: c.rel_move_to(0, -baseline_offset) lo.set_font_description(font) lo.set_text(text, -1) if hasattr(lo, 'get_logical_extents'): extents = lo.get_logical_extents() ex = extents.get_width() / Pango.SCALE else: ex = size ex *= len(text) if centered: c.rel_move_to(-ex / 2, 0) PangoCairo.update_layout(c, lo) PangoCairo.show_layout(c, lo) c.rel_move_to(ex, 0) if not at_top: c.rel_move_to(0, -baseline_offset)
def _render(self, ctx=None): if not self._doRender: return ctx = ctx or self._get_context() if GI: ctx = _UNSAFE_cairocffi_context_to_pycairo(ctx) # we build a PangoCairo context linked to cairo context # then we create a pango layout # we update the context as we already used a null one on the pre-rendering # supposedly there should not be a big performance penalty self._pang_ctx = PangoCairo.create_context(ctx) if self._fillcolor is not None: # Go to initial point (CORNER or CENTER): transform = self._call_transform_mode(self._transform) if GI: transform = pycairo.Matrix(*transform.as_tuple()) ctx.set_matrix(transform) ctx.translate(self.x, self.y-self.baseline) if self._outline is False: ctx.set_source_rgba(*self._fillcolor) PangoCairo.show_layout(ctx, self.layout) PangoCairo.update_layout(ctx, self.layout)
def _draw_text(cc, label, x, y, size, width, scale, heading, rgb, wrap=False): import textwrap final_scale = int(size * scale) * Pango.SCALE label = str(label) if wrap: label = '\n'.join(textwrap.wrap(label, int(width / scale))) pl = PangoCairo.create_layout(cc) fd = Pango.FontDescription(self._font) fd.set_size(final_scale) pl.set_font_description(fd) if isinstance(label, (str, unicode)): text = label.replace('\0', ' ') elif isinstance(label, (float, int)): text = str(label) else: text = label pl.set_text(str(label), len(str(label))) pl.set_width(int(width) * Pango.SCALE) cc.save() cc.translate(x, y) cc.rotate(heading * DEGTOR) cc.set_source_rgb(rgb[0] / 255., rgb[1] / 255., rgb[2] / 255.) PangoCairo.update_layout(cc, pl) PangoCairo.show_layout(cc, pl) cc.restore()
def do_draw_cb(self, widget, cr): # The do_draw_cb is called when the widget is asked to draw itself # with the 'draw' as opposed to old function 'expose event' # Remember that this will be called a lot of times, so it's usually # a good idea to write this code as optimized as it can be, don't # Create any resources in here. cr.translate ( RADIUS, RADIUS) layout = PangoCairo.create_layout (cr) layout.set_text("శ్రీమదాంధ్రమహాభారతము", -1) desc = Pango.font_description_from_string (FONT) layout.set_font_description( desc) rangec = range(0, N_WORDS) for i in rangec: width, height = 0,0 angle = (360. * i) / N_WORDS; cr.save () red = (1 + math.cos ((angle - 60) * math.pi / 180.)) / 2 cr.set_source_rgb ( red, 0, 1.0 - red) cr.rotate ( angle * math.pi / 180.) #/* Inform Pango to re-layout the text with the new transformation */ PangoCairo.update_layout (cr, layout) width, height = layout.get_size() cr.move_to ( - (float(width) / 1024.) / 2, - RADIUS) PangoCairo.show_layout (cr, layout) cr.restore()
def do_draw(self, cairo_ctx): cairo_ctx.save() try: layout = PangoCairo.create_layout(cairo_ctx) if self.font: font = Pango.FontDescription() font.set_family(self.font) layout.set_font_description(font) layout.set_text(self.text, -1) txt_size = layout.get_size() if 0 in txt_size: return txt_factor = float(self.height) / txt_size[1] self.size = ( int(txt_size[0] * txt_factor), int(txt_size[1] * txt_factor) ) self.position = ( int(self.center_position[0] - (self.size[0] / 2)), int(self.center_position[1] - (self.size[1] / 2)) ) cairo_ctx.translate(self.position[0], self.position[1]) if txt_factor <= 0.0: return cairo_ctx.scale(txt_factor * Pango.SCALE, txt_factor * Pango.SCALE) cairo_ctx.set_source_rgb(0.0, 0.0, 0.0) PangoCairo.update_layout(cairo_ctx, layout) PangoCairo.show_layout(cairo_ctx, layout) finally: cairo_ctx.restore()
def gen_sticker(self, ctx, obj, x, y, w, h, font, text_color, template): ctx.save() ctx.rectangle(x, y, w, h) ctx.stroke_preserve() ctx.clip() lout = PangoCairo.create_layout(ctx) lout.set_font_description(Pango.FontDescription(font)) lout.set_width(Pango.SCALE * w) lout.set_alignment(Pango.Alignment.CENTER) type_name = type(obj).__name__.lower() text = template.format(**{type_name: obj}) lout.set_markup(text, -1) extent = lout.get_size() xtrans = x ytrans = y - extent.height / Pango.SCALE / 2 + h / 2 ctx.translate(xtrans, ytrans) ctx.set_source_rgb(*text_color) PangoCairo.update_layout(ctx, lout) PangoCairo.show_layout(ctx, lout) ctx.translate(-xtrans, -ytrans) ctx.restore()
def draw_balloon(self, cr, b): x = int(b.x) y = int(b.y) # Draw the string. cr.set_source_rgb(0, 0, 0) cr.move_to(int(b.x), int(b.y + b.size / 2)) cr.line_to(int(b.x), int(b.y + b.size)) cr.stroke() # Draw the balloon. cr.save() cr.set_source_rgb(b.color[0], b.color[1], b.color[2]) cr.arc(b.x, b.y, b.size / 2, 0, 2 * math.pi) cr.fill() cr.restore() cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) fd = Pango.FontDescription('Sans') fd.set_size(12 * Pango.SCALE) pango_layout.set_font_description(fd) pango_layout.set_text(b.word, len(b.word)) size = pango_layout.get_size() x = x - (size[0] / Pango.SCALE) / 2 y = y - (size[1] / Pango.SCALE) / 2 cr.move_to(x, y) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout)
def create_labels(self, cr=None): """Create the labels for the socket.""" self.label_layout = Gtk.DrawingArea().create_pango_layout('') self.label_layout.set_alignment(Pango.Alignment.CENTER) if cr: PangoCairo.update_layout(cr, self.label_layout) if self.domain in (Constants.GR_MESSAGE_DOMAIN, Constants.GR_STREAM_DOMAIN): self._line_width_factor = 1.0 else: self._line_width_factor = 2.0 self._update_colors() layout = self.label_layout layout.set_markup('<span font_desc="{font}">{name}</span>'.format( name=Utils.encode(self.name), font=Constants.PORT_FONT )) label_width, label_height = self.label_layout.get_size() self.width = 2 * Constants.PORT_LABEL_PADDING + label_width / Pango.SCALE self.height = 2 * Constants.PORT_LABEL_PADDING + label_height / Pango.SCALE self._label_layout_offsets = [0, Constants.PORT_LABEL_PADDING] # if self.dtype == 'bus': # self.height += Constants.PORT_EXTRA_BUS_HEIGHT # self._label_layout_offsets[1] += Constants.PORT_EXTRA_BUS_HEIGHT / 2 self.height += self.height % 2 # uneven height
def draw(self, cr): """ Draw the socket with a label. """ border_color = self._border_color cr.set_line_width(self._line_width_factor * cr.get_line_width()) cr.translate(*self.coordinate) cr.rectangle(*self._area) cr.set_source_rgba(*self._bg_color) cr.fill_preserve() cr.set_source_rgba(*border_color) cr.stroke() if not self._show_label: return # this port is folded (no label) if self.is_vertical(): cr.rotate(-math.pi / 2) cr.translate(-self.width, 0) cr.translate(*self._label_layout_offsets) cr.set_source_rgba(*self._font_color) PangoCairo.update_layout(cr, self.label_layout) PangoCairo.show_layout(cr, self.label_layout)
def make_layout(self, ctx): layout = PangoCairo.create_layout(ctx) layout.set_text(self.text, -1) for k, v in self.attrs.items(): getattr(layout, "set_" + k)(v) PangoCairo.update_layout(ctx, layout) return layout
def draw_page(self, operation, context, page_number): """ Render the QSO details on the page. :arg Gtk.PrintOperation operation: The printing API. :arg Gtk.PrintContext context: Used to draw/render the pages to print. :arg int page_number: The current page number. """ cr = context.get_cairo_context() cr.set_source_rgb(0, 0, 0) layout = context.create_pango_layout() layout.set_font_description(Pango.FontDescription("monospace expanded 10")) layout.set_width(int(context.get_width()*Pango.SCALE)) current_line_number = 1 for line in self.text_to_print: layout.set_text(line, -1) cr.move_to(5, current_line_number*self.line_height) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) current_line_number += 1 if((current_line_number+1)*self.line_height >= context.get_height()): for j in range(0, current_line_number-1): self.text_to_print.pop(0) # Remove what has been printed already before draw_page is called again. break return
def write_image(self, filename): w = 8 + len(self.ref) * 7 h = CONFIG.image_size[1] # create an image where the text fits img = cairo.SVGSurface(filename, w, h) ctx = cairo.Context(img) # background fill ctx.rectangle(0, 0, w, h) ctx.set_source_rgb(*CONFIG.swiss_mobile_bgcolor) ctx.fill_preserve() # border ctx.set_line_width(CONFIG.image_border_width) levcol = CONFIG.level_colors[self.level] ctx.set_source_rgb(*levcol) ctx.stroke() # text ctx.set_source_rgb(*CONFIG.swiss_mobile_color) layout = PangoCairo.create_layout(ctx) layout.set_font_description( Pango.FontDescription(CONFIG.swiss_mobile_font)) layout.set_text(self.ref, -1) tw, th = layout.get_pixel_size() PangoCairo.update_layout(ctx, layout) ctx.move_to( w - tw - CONFIG.image_border_width / 2, h - layout.get_iter().get_baseline() / Pango.SCALE - CONFIG.image_border_width / 2) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def print_in_square(cr, square, ratio, samplechar, fname, color=(0, 0, 0)): cr.save() x = square['x'] y = square['y'] W = square['W'] H = square['H'] cr.translate(x, y) layout = PangoCairo.create_layout(cr) fsize = W * ratio #remove size rstring = fname.split(' ') if rstring[-1].isnumeric(): font_desc = ' '.join(rstring[:-1]) else: font_desc = fname new_fname = '{} {:0.1f}'.format(font_desc, fsize) layout.set_font_description(Pango.font_description_from_string(new_fname)) layout.set_text(samplechar, -1) PangoCairo.update_layout(cr, layout) pixel_extents = layout.get_pixel_extents() left = (W - pixel_extents[0].width) / 2 - pixel_extents[0].x top = (H - pixel_extents[0].height) / 2 - pixel_extents[0].y cr.move_to(left, top) cr.set_source_rgb(*color) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) cr.restore()
def _draw_time(self, cr): """Draw the time in colors (digital display). """ # TRANS: The format used to display the time for digital clock # You can add AM/PM indicator or use 12/24 format, for example # "%I:%M:%S %p". See # http://docs.python.org/lib/module-time.html for available # strftime formats If the display of the time is moving # horizontally, it means that the glyphs of the digits used in # the font don't have the same width. Try to use a Monospace # font. xgettext:no-python-format cr.save() markup = _('<markup>\ <span lang="en" font_desc="Sans,Monospace Bold 96">\ <span foreground="#005FE4">%I</span>:\ <span foreground="#00B20D">%M</span>:\ <span foreground="#E6000A">%S</span>%p</span></markup>') markup_time = self._time.strftime(markup) cr.set_source_rgba(*style.Color(self._COLOR_BLACK).get_rgba()) pango_layout = PangoCairo.create_layout(cr) d = int(self._center_y + 0.3 * self._radius) pango_layout.set_markup(markup_time) dx, dy = pango_layout.get_pixel_size() pango_layout.set_alignment(Pango.Alignment.CENTER) cr.translate(self._center_x - dx / 2.0, d - dy / 2.0) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout) cr.restore()
def _print_text(self, context, x, y, text, font_size, max_width=0, alignment=None, color=None): if text is None: return context.save() context.translate(x, y) layout = self.create_pango_layout(text) if max_width > 0: layout.set_width(max_width * Pango.SCALE) layout.set_wrap(Pango.WrapMode.WORD_CHAR) if alignment is not None: layout.set_alignment(alignment) font_desc = Pango.FontDescription("Sans %s" % font_size) layout.set_font_description(font_desc) if color is None: context.set_source_rgb(0, 0, 0) else: context.set_source_rgba(*style.Color(color).get_rgba()) PangoCairo.update_layout(context, layout) PangoCairo.show_layout(context, layout) context.fill() context.restore()
def _draw_osd(self, layout, rect): """ Draws the text specified in C{layout} into a box at C{rect}. """ draw_region = Gdk.Rectangle() draw_region.x, draw_region.y, draw_region.width, draw_region.height = rect if self._last_osd_rect: last_region = Gdk.Rectangle() last_region.x, last_region.y, last_region.width, last_region.height = self._last_osd_rect draw_region = Gdk.rectangle_union(draw_region, last_region) gdk_rect = Gdk.Rectangle() gdk_rect.x = draw_region.x gdk_rect.y = draw_region.y gdk_rect.width = draw_region.width gdk_rect.height = draw_region.height window = self._window._main_layout.get_bin_window() window.begin_paint_rect(gdk_rect) self._clear_osd() cr = window.cairo_create() cr.set_source_rgb(*image_tools.GTK_GDK_COLOR_BLACK.to_floats()) cr.rectangle(*rect) cr.fill() extents = layout.get_extents()[0] cr.set_source_rgb(*image_tools.GTK_GDK_COLOR_WHITE.to_floats()) cr.translate(rect[0] + extents.x / Pango.SCALE, rect[1] + extents.y / Pango.SCALE) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) window.end_paint()
def _draw_osd(self, layout, rect): ''' Draws the text specified in C{layout} into a box at C{rect}. ''' draw_region = Gdk.Rectangle() draw_region.x, draw_region.y, draw_region.width, draw_region.height = rect if self._last_osd_rect: last_region = Gdk.Rectangle() last_region.x, last_region.y, last_region.width, last_region.height = self._last_osd_rect draw_region = Gdk.rectangle_union(draw_region, last_region) gdk_rect = Gdk.Rectangle() gdk_rect.x = draw_region.x gdk_rect.y = draw_region.y gdk_rect.width = draw_region.width gdk_rect.height = draw_region.height window = self._window._main_layout.get_bin_window() window.begin_paint_rect(gdk_rect) self._clear_osd() cr = window.cairo_create() cr.set_source_rgba(*prefs['osd bg color']) cr.rectangle(*rect) cr.fill() extents = layout.get_extents()[0] cr.set_source_rgba(*prefs['osd color']) cr.translate(rect[0] + extents.x / Pango.SCALE, rect[1] + extents.y / Pango.SCALE) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) window.end_paint()
def _draw_typed(self, ctx, layout, hurigana): attr_list_preedit = Pango.AttrList().new() typed = get_prefix(self.engine.get_plain(), self.engine.get_typed()) correct_length = len(typed) typed = hurigana.adjust_typed(typed) formatted = '<span foreground="#0066CC">' + typed + '</span>' if correct_length < len(self.engine.get_typed()): formatted += '<span foreground="#FF0000" background="#FFCCFF">' + \ self.engine.get_typed()[correct_length:] + \ '</span>' typed += self.engine.get_typed()[correct_length:] preedit = self.engine.get_preedit() if preedit[0] and 0 < preedit[2]: attr_list_preedit.splice(preedit[1], len(typed.encode()), len(preedit[0][:preedit[2]].encode())) formatted += '<span foreground="#0066FF">' + preedit[ 0][:preedit[2]] + '</span>' typed += preedit[0][:preedit[2]] layout.set_markup(formatted, -1) if preedit[0] and 0 < preedit[2]: attr_list = layout.get_attributes() attr_list.splice(attr_list_preedit, 0, 0) layout.set_attributes(attr_list) PangoCairo.update_layout(ctx, layout) PangoCairo.show_layout(ctx, layout) return layout, typed
def _draw_time(self, cr): """Draw the time in colors (digital display). """ # TRANS: The format used to display the time for digital clock # You can add AM/PM indicator or use 12/24 format, for example # "%I:%M:%S %p". See # http://docs.python.org/lib/module-time.html for available # strftime formats If the display of the time is moving # horizontally, it means that the glyphs of the digits used in # the font don't have the same width. Try to use a Monospace # font. xgettext:no-python-format cr.save() markup = _('<markup>\ <span lang="en" font_desc="Sans,Monospace Bold 96">\ <span foreground="#005FE4">%I</span>:\ <span foreground="#00B20D">%M</span>:\ <span foreground="#E6000A">%S</span>%p</span></markup>') markup_time = self._time.strftime(markup) cr.set_source_rgba(*style.Color(self._COLOR_BLACK).get_rgba()) pango_layout = PangoCairo.create_layout(cr) d = int(self._center_y + 0.3 * self._radius) pango_layout.set_markup(markup_time) dx, dy = pango_layout.get_pixel_size() pango_layout.set_alignment(Pango.Alignment.CENTER) cr.translate(self._center_x - dx / 2.0, d - dy / 2.0) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout) cr.restore()
def _draw_text(cc, label, x, y, size, width, scale, heading, rgb, wrap=False): import textwrap final_scale = int(size * scale) * Pango.SCALE label = str(label) if wrap: label = '\n'.join(textwrap.wrap(label, int(width / scale))) pl = PangoCairo.create_layout(cc) fd = Pango.FontDescription(self._font) fd.set_size(final_scale) pl.set_font_description(fd) if isinstance(label, str): text = label.replace('\0', ' ') elif isinstance(label, (float, int)): text = str(label) else: text = label pl.set_text(text, -1) pl.set_width(int(width) * Pango.SCALE) cc.save() cc.translate(x, y) cc.rotate(heading * DEGTOR) cc.set_source_rgb(rgb[0] / 255., rgb[1] / 255., rgb[2] / 255.) PangoCairo.update_layout(cc, pl) PangoCairo.show_layout(cc, pl) cc.restore()
def draw_text(ctx, pc, layout, fascent, fheight, baseline_x, baseline_y, text, pango_alignment): """Draws the given text into the provided Cairo context through the Pango layout (get_width() expected to be correct in order to position the text correctly) with the specified pango.ALIGN_x alignment. Args: ctx (cairo.Context): cairo context to use pc (pangocairo.CairoContext): pango context layout (pango.Layout): pango layout to draw into (get_with() important) fascent, fheight (int): current font ascent/height metrics baseline_x/baseline_y (int): coordinate of the left baseline cairo point pango_alignment (enum): pango.ALIGN_ constant value Results: A 3-uple text_width, text_height (cairo units) """ layout.set_auto_dir(False) # Make sure ALIGN_RIGHT is independent on RTL... layout.set_alignment(pango_alignment) layout.set_text(text, -1) width, height = [x/Pango.SCALE for x in layout.get_size()] ctx.move_to(baseline_x, baseline_y - fascent) PangoCairo.update_layout(ctx, layout) PangoCairo.show_layout(ctx, layout) return width, height
def draw(self, cr): """ Draw the signal block with label and inputs/outputs. """ border_color = colors.HIGHLIGHT_COLOR if self.highlighted else self._border_color cr.translate(*self.coordinate) for port in self.active_ports(): # ports first cr.save() port.draw(cr) cr.restore() cr.rectangle(*self._area) cr.set_source_rgba(*self._bg_color) cr.fill_preserve() cr.set_source_rgba(*border_color) cr.stroke() # title and params label if self.is_vertical(): cr.rotate(-math.pi / 2) cr.translate(-self.width, 0) cr.set_source_rgba(*self._font_color) for layout, offset in zip(self._surface_layouts, self._surface_layouts_offsets): cr.save() cr.translate(*offset) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) cr.restore()
def write(c, text, name, size, centered=False, at_top=False): pc = PangoCairo.create_context(c) font = Pango.FontDescription(name) font.set_size(int(round(size * Pango.SCALE))) lo = PangoCairo.create_layout(pc) lo.set_font_description(font) lo.set_text('X', -1) baseline_offset = lo.get_baseline() / Pango.SCALE if not at_top: c.rel_move_to(0, -baseline_offset) lo.set_font_description(font) lo.set_text(text, -1) if hasattr(lo, 'get_logical_extents'): extents = lo.get_logical_extents() ex = extents.get_width() / Pango.SCALE else: ex = size ex *= len(text) if centered: c.rel_move_to(-ex / 2, 0) PangoCairo.update_layout(c, lo) PangoCairo.show_layout(c, lo) c.rel_move_to(ex, 0) if not at_top: c.rel_move_to(0, -baseline_offset)
def write_text(self): if not self.text: # Nothing useful to draw, return now! return if self.resized() or self.text != self.prev_text or self.text_overlay is None: # Need to recreate the cache drawing self.text_overlay = cairo.ImageSurface( cairo.FORMAT_ARGB32, self.width, self.height ) ctx = cairo.Context(self.text_overlay) pl = PangoCairo.create_layout(ctx) pl.set_font_description(self.text_font) pl.set_width(-1) # No text wrapping pl.set_text(self.text, -1) plsize = pl.get_size() text_width = plsize[0] // SCALE text_height = plsize[1] // SCALE area_width_without_text = self.width - text_width area_height_without_text = self.height - text_height ctx.move_to(area_width_without_text // 2, area_height_without_text // 2) ctx.set_source_rgb(1, 1, 1) PangoCairo.update_layout(ctx, pl) PangoCairo.show_layout(ctx, pl) self.cr.set_source_surface(self.text_overlay) self.cr.paint()
def write_image(self, filename): w = 8 + len(self.ref)*7 h = CONFIG.image_size[1] # create an image where the text fits img = cairo.SVGSurface(filename, w, h) ctx = cairo.Context(img) # background fill ctx.rectangle(0, 0, w, h) ctx.set_source_rgb(*CONFIG.swiss_mobile_bgcolor) ctx.fill_preserve() # border ctx.set_line_width(CONFIG.image_border_width) levcol = CONFIG.level_colors[self.level] ctx.set_source_rgb(*levcol) ctx.stroke() # text ctx.set_source_rgb(*CONFIG.swiss_mobile_color) layout = PangoCairo.create_layout(ctx) layout.set_font_description(Pango.FontDescription(CONFIG.swiss_mobile_font)) layout.set_text(self.ref, -1) tw, th = layout.get_pixel_size() PangoCairo.update_layout(ctx, layout) ctx.move_to(w - tw - CONFIG.image_border_width/2, h - layout.get_iter().get_baseline()/Pango.SCALE - CONFIG.image_border_width/2) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def draw(self,sender, c, data=None): c.set_source_rgb(0,0,0) self.drawBackground(c) pc = PangoCairo.create_context(c) #c.set_antialias( cairo.Antialias.SUBPIXEL ) l = Pango.Layout(pc) l.set_font_description( Pango.FontDescription(self.font)) l.set_text( self.text, -1) l.set_alignment( Pango.Alignment.CENTER ) l.set_wrap( Pango.WrapMode.WORD ) l.set_width( (self.width-5) * Pango.SCALE ) l.set_height( (self.height-5) * Pango.SCALE ) l.set_ellipsize( Pango.EllipsizeMode.END ) w = 0 h = 0 w,h = l.get_pixel_size() c.move_to( 0, (self.height/2) - (h/2) ) c.set_source_rgb(0, 0, 0) PangoCairo.update_layout(c, l) PangoCairo.show_layout(c, l) import storage if(storage.window.focusedItem == self): c.set_source_rgb(0,0,200) c.move_to(0,0) c.rectangle(0,0,self.width-1,self.height-1) c.stroke() return False
def _print_text(self, context, x, y, text, font_size, max_width=0, alignment=None, color=None): if text is None: return context.save() context.translate(x, y) layout = self.create_pango_layout(text) if max_width > 0: layout.set_width(max_width * Pango.SCALE) layout.set_wrap(Pango.WrapMode.WORD_CHAR) if alignment is not None: layout.set_alignment(alignment) font_desc = Pango.FontDescription("Sans %s" % font_size) layout.set_font_description(font_desc) if color is None: context.set_source_rgb(0, 0, 0) else: context.set_source_rgba(*style.Color(color).get_rgba()) PangoCairo.update_layout(context, layout) PangoCairo.show_layout(context, layout) context.fill() context.restore()
def write_image(self, filename): # get text size tw, th = _get_text_size(self.ref) # create an image where the text fits w = int(tw + CONFIG.text_border_width + 2 * CONFIG.image_border_width) h = CONFIG.image_size[1] img = cairo.SVGSurface(filename, w, h) ctx = cairo.Context(img) # background fill ctx.rectangle(0, 0, w, h) ctx.set_source_rgb(*CONFIG.text_bgcolor) ctx.fill_preserve() # border ctx.set_line_width(CONFIG.text_border_width) levcol = CONFIG.level_colors[self.level] ctx.set_source_rgb(*levcol) ctx.stroke() # reference text ctx.set_source_rgb(*CONFIG.text_color) layout = PangoCairo.create_layout(ctx) layout.set_font_description(Pango.FontDescription(CONFIG.text_font)) layout.set_text(self.ref, -1) PangoCairo.update_layout(ctx, layout) ctx.move_to((w-tw)/2, (h-CONFIG.text_border_width-layout.get_iter().get_baseline()/Pango.SCALE)/2.0) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def do_draw_cb(self, widget, cr): # The do_draw_cb is called when the widget is asked to draw itself # with the 'draw' as opposed to old function 'expose event' # Remember that this will be called a lot of times, so it's usually # a good idea to write this code as optimized as it can be, don't # Create any resources in here. cr.translate(RADIUS, RADIUS) layout = PangoCairo.create_layout(cr) layout.set_text("శ్రీమదాంధ్రమహాభారతము", -1) desc = Pango.font_description_from_string(FONT) layout.set_font_description(desc) rangec = range(0, N_WORDS) for i in rangec: width, height = 0, 0 angle = (360. * i) / N_WORDS cr.save() red = (1 + math.cos((angle - 60) * math.pi / 180.)) / 2 cr.set_source_rgb(red, 0, 1.0 - red) cr.rotate(angle * math.pi / 180.) #/* Inform Pango to re-layout the text with the new transformation */ PangoCairo.update_layout(cr, layout) width, height = layout.get_size() cr.move_to(-(float(width) / 1024.) / 2, -RADIUS) PangoCairo.show_layout(cr, layout) cr.restore()
def _show_text_at_coords(self, cairo_c, pango_l, text, text_x, text_y): """Use a pango layout (pango_l) to show a line of text (text) on a cairo context (cairo_c) at given coordinates (text_x, text_y).""" cairo_c.move_to(text_x, text_y) pango_l.set_text(text, -1) PangoCairo.update_layout(cairo_c, pango_l) PangoCairo.show_layout(cairo_c, pango_l)
def create_labels(self, cr=None): """Create the labels for the socket.""" self.label_layout = Gtk.DrawingArea().create_pango_layout('') self.label_layout.set_alignment(Pango.Alignment.CENTER) if cr: PangoCairo.update_layout(cr, self.label_layout) if self.domain in (Constants.GR_MESSAGE_DOMAIN, Constants.GR_STREAM_DOMAIN): self._line_width_factor = 1.0 else: self._line_width_factor = 2.0 self._update_colors() layout = self.label_layout layout.set_markup('<span font_desc="{font}">{name}</span>'.format( name=Utils.encode(self.name), font=Constants.PORT_FONT)) label_width, label_height = self.label_layout.get_size() self.width = 2 * Constants.PORT_LABEL_PADDING + label_width / Pango.SCALE self.height = (2 * Constants.PORT_LABEL_PADDING + label_height * (3 if self.dtype == 'bus' else 1)) / Pango.SCALE self._label_layout_offsets = [0, Constants.PORT_LABEL_PADDING] self.height += self.height % 2 # uneven height
def coloring_special_cell(self, cr, cell, special): ls = [(_('Start'), config.getv('bg_start')), (_('DLS'), config.getv('bg_hX2')), (_('TLS'), config.getv('bg_hX3')), (_('DWS'), config.getv('bg_kX2')), (_('TWS'), config.getv('bg_kX3'))] x = int(cell % 15) y = int(cell / 15) x0 = self.st + (x * self.bwn) y0 = self.br + (y * self.bwn) text = ls[special][0] #--bg--# cr.rectangle(x0, y0, self.bwn, self.bwn) R, G, B = rgb(ls[special][1]) cr.set_source_rgba(R, G, B, 1.0) cr.fill() #--text--# szfont, fmfont = split_font(config.getv('font_special')) font = '{} {}'.format(fmfont, int((self.bwn / 64) * int(szfont))) layout = PangoCairo.create_layout(cr) layout.set_text(text, -1) desc = Pango.font_description_from_string(font) layout.set_font_description(desc) layout.set_alignment(1) cr.save() w, h = layout.get_pixel_size() PangoCairo.update_layout(cr, layout) R, G, B = rgb(config.getv('fg_cell')) cr.set_source_rgb(R, G, B) cr.move_to(x0 + (self.bwn - w) / 2, y0 + (self.bwn - h) / 2) PangoCairo.show_layout(cr, layout) cr.restore()
def draw_balloon(self, cr, b): x = int(b.x) y = int(b.y) # Draw the string. cr.set_source_rgb(0, 0, 0) cr.move_to(int(b.x), int(b.y + b.size / 2)) cr.line_to(int(b.x), int(b.y + b.size)) cr.stroke() # Draw the balloon. cr.save() cr.set_source_rgb(b.color[0], b.color[1], b.color[2]) cr.arc(b.x, b.y, b.size / 2, 0, 2 * math.pi) cr.fill() cr.restore() cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) fd = Pango.FontDescription('Sans') fd.set_size(12 * Pango.SCALE) pango_layout.set_font_description(fd) pango_layout.set_text(b.word, len(b.word)) size = pango_layout.get_size() x = x - (size[0] / Pango.SCALE) / 2 y = y - (size[1] / Pango.SCALE) / 2 cr.move_to(x, y) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout)
def write_image(self, filename): # get text size tw, th = _get_text_size(self.ref) # create an image where the text fits w = int(tw + 2 * CONFIG.cai_border_width) h = int(CONFIG.image_size[1] + 0.5 * CONFIG.cai_border_width) w = max(h, w) img = cairo.SVGSurface(filename, w, h) ctx = cairo.Context(img) # background fill ctx.set_source_rgb(1, 1, 1) ctx.rectangle(0, 0, w, h) ctx.fill() # bars ctx.set_source_rgb(*CONFIG.osmc_colors['red']) ctx.rectangle(0, 0, w, h) ctx.set_line_width(CONFIG.image_border_width) ctx.stroke() ctx.set_line_width(0) if self.typ == 'stripe': ctx.rectangle(0, 0, CONFIG.cai_border_width, h) ctx.fill() ctx.rectangle(w - CONFIG.cai_border_width, 0, CONFIG.cai_border_width, h) ctx.fill() else: ctx.rectangle(0, 0, w, 0.9 * CONFIG.cai_border_width) ctx.fill() ctx.rectangle(0, h - 0.9 * CONFIG.cai_border_width, w, 0.9 * CONFIG.cai_border_width) ctx.fill() # border ctx.rectangle(0, 0, w, h) if self.level == 99: levcol = (255, 0, 0) ctx.set_line_width(0.5 * CONFIG.image_border_width) else: levcol = CONFIG.level_colors[self.level] ctx.set_line_width(0.8 * CONFIG.image_border_width) ctx.set_source_rgb(*levcol) ctx.stroke() # reference text ctx.set_source_rgb(*CONFIG.text_color) layout = PangoCairo.create_layout(ctx) layout.set_font_description(Pango.FontDescription(CONFIG.text_font)) layout.set_text(self.ref, -1) PangoCairo.update_layout(ctx, layout) y = (h - layout.get_iter().get_baseline() / Pango.SCALE) / 2.0 if self.typ == 'bar': y -= 1 ctx.move_to((w - tw) / 2, y) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def draw_text(ctx, x, y, w, h, text, color, fontname, size=12, justification='left'): # ctx.save() # ctx.translate(x, y) # ctx.rectangle(0, 0, w, h) # ctx.clip() layout = PangoCairo.create_layout(ctx) font = Pango.FontDescription('%s %s' % (fontname, size)) layout.set_font_description(font) layout.set_text(u'%s' % text, -1) layout.set_ellipsize(Pango.EllipsizeMode.END) layout.set_width(Pango.SCALE * w) PangoCairo.update_layout(ctx, layout) tw, th = layout.get_pixel_size() ctx.set_source_rgb(*color) if justification == 'center': ctx.move_to(x + (w*0.5 - tw*0.5), y + (h*0.5 - th*0.5)) elif justification == 'left': ctx.move_to(x, y + (h*0.5 - th*0.5)) elif justification == 'right': ctx.move_to(x+w-tw, y + (h*0.5 - th*0.5)) PangoCairo.show_layout(ctx, layout)
def write_image(self, filename): # get text size tw, th = _get_text_size(self.ref) # create an image where the text fits w = int(tw + CONFIG.text_border_width + 2 * CONFIG.image_border_width) h = CONFIG.image_size[1] img = cairo.SVGSurface(filename, w, h) ctx = cairo.Context(img) # background fill ctx.rectangle(0, 0, w, h) ctx.set_source_rgb(*CONFIG.text_bgcolor) ctx.fill_preserve() # border ctx.set_line_width(CONFIG.text_border_width) levcol = CONFIG.level_colors[self.level] ctx.set_source_rgb(*levcol) ctx.stroke() # reference text ctx.set_source_rgb(*CONFIG.text_color) layout = PangoCairo.create_layout(ctx) layout.set_font_description(Pango.FontDescription(CONFIG.text_font)) layout.set_text(self.ref, -1) PangoCairo.update_layout(ctx, layout) ctx.move_to((w - tw) / 2, (h - CONFIG.text_border_width - layout.get_iter().get_baseline() / Pango.SCALE) / 2.0) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def get_text_surface(self, ctx, text): if self.cached.text == text: return self.cached.surface layout = PangoCairo.create_layout(ctx) layout.set_text(text, -1) descr = Pango.font_description_from_string(self.FONT_FACE) descr.set_size(Pango.SCALE * self.FONT_SIZE) layout.set_font_description(descr) PangoCairo.update_layout(ctx, layout) t = layout.get_pixel_size() scale = min(self.TEXT_FILL_FACTOR * self.width / (t.width + 1), self.TEXT_FILL_FACTOR * self.height / (t.height + 1)) if scale < 1: descr.set_size(scale * Pango.SCALE * self.FONT_SIZE) layout.set_font_description(descr) PangoCairo.update_layout(ctx, layout) t = layout.get_pixel_size() surface = ctx.get_target().create_similar(cairo.Content.COLOR_ALPHA, t.width, t.height) new_ctx = cairo.Context(surface) new_ctx.set_source_rgb(*self.TEXT_COLOR) PangoCairo.show_layout(new_ctx, layout) self.cached.surface = surface self.cached.text = text return self.cached.surface
def _draw_cb(self, widget, cr): w = self.get_allocated_width() h = self.get_allocated_height() # clear to black cr.rectangle(0, 0, w, h) cr.set_source_rgb(0, 0, 0) cr.fill() if self._value == 0: return False # draw white circle from file, and centre in allocation dx = (w - self._circle_surface.get_width()) / 2 dy = (h - self._circle_surface.get_height()) / 2 cr.translate(dx, dy) cr.set_source_surface(self._circle_surface, 0, 0) cr.paint() cr.translate(-dx, -dy) # draw white digits of value, and centre in allocation layout = PangoCairo.create_layout(cr) layout.set_font_description(Pango.FontDescription("sans bold 26")) layout.set_text(str(self._value), -1) layout.set_alignment(Pango.Alignment.CENTER) ink_rect, logical_rect = layout.get_pixel_extents() dx = (w - ink_rect.width) / 2 - ink_rect.x dy = (h - ink_rect.height) / 2 - ink_rect.y cr.translate(dx, dy) cr.set_source_rgb(255, 255, 255) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) return False
def draw(self, cr): """ Draw the socket with a label. """ if self.hidden: return border_color = self._border_color cr.set_line_width(self._line_width_factor * cr.get_line_width()) cr.translate(*self.coordinate) cr.rectangle(*self._area) cr.set_source_rgba(*self._bg_color) cr.fill_preserve() cr.set_source_rgba(*border_color) cr.stroke() if not self._show_label: return # this port is folded (no label) if self.is_vertical(): cr.rotate(-math.pi / 2) cr.translate(-self.width, 0) cr.translate(*self._label_layout_offsets) cr.set_source_rgba(*self._font_color) PangoCairo.update_layout(cr, self.label_layout) PangoCairo.show_layout(cr, self.label_layout)
def draw_box_txt(self, cairo_context, box): (a, b, w, h) = self._get_real_box(box) cairo_context.save() try: cairo_context.set_source_rgb(1.0, 1.0, 1.0) cairo_context.rectangle(a, b, w, h) cairo_context.clip() cairo_context.paint() finally: cairo_context.restore() cairo_context.save() try: cairo_context.translate(a, b) cairo_context.set_source_rgb(0.0, 0.0, 0.0) layout = PangoCairo.create_layout(cairo_context) layout.set_text(box.content, -1) txt_size = layout.get_size() if 0 in txt_size: return txt_factor = min( float(w) * Pango.SCALE / txt_size[0], float(h) * Pango.SCALE / txt_size[1], ) cairo_context.scale(txt_factor, txt_factor) PangoCairo.update_layout(cairo_context, layout) PangoCairo.show_layout(cairo_context, layout) finally: cairo_context.restore()
def _render(self, ctx=None): if not self._doRender: return ctx = ctx or self._get_context() if GI: ctx = _UNSAFE_cairocffi_context_to_pycairo(ctx) # we build a PangoCairo context linked to cairo context # then we create a pango layout # we update the context as we already used a null one on the pre-rendering # supposedly there should not be a big performance penalty self._pang_ctx = PangoCairo.create_context(ctx) if self._fillcolor is not None: # Go to initial point (CORNER or CENTER): transform = self._call_transform_mode(self._transform) if GI: transform = pycairo.Matrix(*transform.as_tuple()) ctx.set_matrix(transform) ctx.translate(self.x, self.y - self.baseline) if self._outline is False: ctx.set_source_rgba(*self._fillcolor) PangoCairo.show_layout(ctx, self.layout) PangoCairo.update_layout(ctx, self.layout)
def _draw_key(self, k, cr): bounds = self.get_allocation() # HACK: this is a hack used when the widget is not shown yet, # in that case bounds will be gtk.gdk.Rectangle(-1, -1, 1, 1) # and the key will be outside the canvas. This is used only # for the first key that appears below the instructions if bounds.x == -1: screen_x = screen_y = 0 else: screen_x = int(bounds.width - self.image.width) / 2 screen_y = int(bounds.height - self.image.height) / 2 x1 = k['key-x'] + screen_x y1 = k['key-y'] + screen_y x2 = x1 + k['key-width'] y2 = y1 + k['key-height'] corner = 5 points = [ (x1 + corner, y1), (x2 - corner, y1), (x2, y1 + corner), (x2, y2 - corner), (x2 - corner, y2), (x1 + corner, y2), (x1, y2 - corner), (x1, y1 + corner) ] cr.new_path() cr.set_source_rgb(0.396, 0.698, 0.392) cr.set_line_width(2) for point in points: cr.line_to(*point) cr.close_path() cr.fill_preserve() cr.stroke() text = '' if k['key-label']: text = k['key-label'] else: text = self.get_letter_for_key_state_group( k, self.active_state, self.active_group) cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) fd = Pango.FontDescription('Monospace') fd.set_size(10 * Pango.SCALE) pango_layout.set_font_description(fd) if type(text) is bytes: pango_layout.set_text(text.decode('utf-8'), len(text.decode('utf-8'))) else: pango_layout.set_text(text, len(text)) cr.move_to(x1 + 8, y2 - 23) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout)
def draw_letters(self, text, context, xval, yval, fontdesc): context.move_to(xval, yval) layout = PangoCairo.create_layout(context) font = Pango.FontDescription(fontdesc) layout.set_font_description(font) layout.set_text(text, -1) PangoCairo.update_layout(context, layout) PangoCairo.show_layout(context, layout)
def calc_text_width(cr, text, size, fontname): layout = PangoCairo.create_layout(cr) font = Pango.FontDescription('%s %s' % (fontname, size)) layout.set_font_description(font) layout.set_text(u'%s' % text, -1) PangoCairo.update_layout(cr, layout) tw, th = layout.get_pixel_size() return tw
def draw_letters(self, text, context, xval, yval, fontdesc): context.move_to(xval, yval) layout = PangoCairo.create_layout(context) font = Pango.FontDescription(fontdesc) layout.set_font_description(font) layout.set_text(text, -1) PangoCairo.update_layout(context, layout) PangoCairo.show_layout(context, layout)
def write_image(self, filename): # get text size tw, th = _get_text_size(self.ref) # create an image where the text fits w = int(tw + 2 * CONFIG.cai_border_width) h = int(CONFIG.image_size[1] + 0.5 * CONFIG.cai_border_width) w = max(h, w) img = cairo.SVGSurface(filename, w, h) ctx = cairo.Context(img) # background fill ctx.set_source_rgb(1, 1, 1) ctx.rectangle(0, 0, w, h) ctx.fill() # bars ctx.set_source_rgb(*CONFIG.osmc_colors['red']) ctx.rectangle(0, 0, w, h) ctx.set_line_width(CONFIG.image_border_width) ctx.stroke() ctx.set_line_width(0) if self.typ == 'stripe': ctx.rectangle(0, 0, CONFIG.cai_border_width, h) ctx.fill() ctx.rectangle(w - CONFIG.cai_border_width, 0, CONFIG.cai_border_width, h) ctx.fill() else: ctx.rectangle(0, 0, w, 0.9 * CONFIG.cai_border_width) ctx.fill() ctx.rectangle(0, h - 0.9 * CONFIG.cai_border_width, w, 0.9 * CONFIG.cai_border_width) ctx.fill() # border ctx.rectangle(0, 0, w, h) if self.level == 99: levcol = (255, 0, 0) ctx.set_line_width(0.5 * CONFIG.image_border_width) else: levcol = CONFIG.level_colors[self.level] ctx.set_line_width(0.8 * CONFIG.image_border_width) ctx.set_source_rgb(*levcol) ctx.stroke() # reference text ctx.set_source_rgb(*CONFIG.text_color) layout = PangoCairo.create_layout(ctx) layout.set_font_description(Pango.FontDescription(CONFIG.text_font)) layout.set_text(self.ref, -1) PangoCairo.update_layout(ctx, layout) y = (h-layout.get_iter().get_baseline()/Pango.SCALE)/2.0 if self.typ == 'bar': y -= 1 ctx.move_to((w-tw)/2, y) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def _draw_key(self, k, cr): bounds = self.get_allocation() # HACK: this is a hack used when the widget is not shown yet, # in that case bounds will be gtk.gdk.Rectangle(-1, -1, 1, 1) # and the key will be outside the canvas. This is used only # for the first key that appears below the instructions if bounds.x == -1: screen_x = screen_y = 0 else: screen_x = int(bounds.width - self.image.width) / 2 screen_y = int(bounds.height - self.image.height) / 2 x1 = k['key-x'] + screen_x y1 = k['key-y'] + screen_y x2 = x1 + k['key-width'] y2 = y1 + k['key-height'] corner = 5 points = [ (x1 + corner, y1), (x2 - corner, y1), (x2, y1 + corner), (x2, y2 - corner), (x2 - corner, y2), (x1 + corner, y2), (x1, y2 - corner), (x1, y1 + corner) ] cr.new_path() cr.set_source_rgb(0.396, 0.698, 0.392) cr.set_line_width(2) for point in points: cr.line_to(*point) cr.close_path() cr.fill_preserve() cr.stroke() text = '' if k['key-label']: text = k['key-label'] else: text = self.get_letter_for_key_state_group( k, self.active_state, self.active_group) cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) fd = Pango.FontDescription('Monospace') fd.set_size(10 * Pango.SCALE) pango_layout.set_font_description(fd) pango_layout.set_text(text, len(text)) cr.move_to(x1 + 8, y2 - 23) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout)
def _prepare_cached_surface(self, context, flipped): self._cached_surface[flipped] = \ context.get_target().create_similar(cairo.CONTENT_COLOR_ALPHA, self.size, self.size) cache_context = cairo.Context(self._cached_surface[flipped]) if flipped: icon_data = self.props['front'] else: icon_data = self.props['back'] cache_context.save() radio = self.size / 3 self.draw_round_rect(cache_context, 0, 0, self.size, self.size, radio) r, g, b, a = icon_data['fill_color'].get_rgba() cache_context.set_source_rgb(r, g, b) cache_context.fill_preserve() r, g, b, a = icon_data['stroke_color'].get_rgba() cache_context.set_source_rgb(r, g, b) cache_context.set_line_width(BORDER_WIDTH) cache_context.stroke() cache_context.restore() text_props = self.props[flipped and 'front_text' or 'back_text'] if self._image_path is not None: if self.jpeg is None: image_size = self.size - style.DEFAULT_SPACING * 2 self.jpeg = GdkPixbuf.Pixbuf.new_from_file_at_size( self._image_path, image_size, image_size) if self.jpeg is not None and flipped: Gdk.cairo_set_source_pixbuf( cache_context, self.jpeg, style.DEFAULT_SPACING, style.DEFAULT_SPACING) cache_context.paint() elif text_props['card_text']: cache_context.save() layout = self.text_layouts[flipped] if not layout: layout = self.text_layouts[flipped] = \ self.create_text_layout(text_props['card_text']) width, height = layout.get_pixel_size() y = (self.size - height) / 2 x = (self.size - width) / 2 cache_context.set_source_rgb(1, 1, 1) cache_context.translate(x, y) PangoCairo.update_layout(cache_context, layout) PangoCairo.show_layout(cache_context, layout) cache_context.fill() cache_context.restore()
def draw_text(self, text, font_desc, cr, x, y, color): layout = PangoCairo.create_layout(cr) layout.set_text(text, -1) desc = Pango.FontDescription(font_desc) layout.set_font_description(desc) cr.set_source_rgb(*color) cr.move_to(x, y) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout)
def draw_text(self): # Animated Typing Turtle title. window = self.get_window() if window is None: return cr = window.cairo_create() cr.move_to(self.x_text, self.y_text) self.pango_layout.set_text(unicode(self.title_text), len(self.title_text)) PangoCairo.update_layout(cr, self.pango_layout) PangoCairo.show_layout(cr, self.pango_layout)
def draw_instructions(self, cr): # Draw instructions. cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) pango_layout.set_font_description(Pango.FontDescription('Times 14')) text = _('Type the words to pop the balloons!') pango_layout.set_text(text, len(text)) size = pango_layout.get_size() x = (self.bounds.width - size[0] / Pango.SCALE) / 2 y = self.bounds.height - 20 - size[1] / Pango.SCALE cr.move_to(x, y) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout)
def draw_results(self, cr): # Draw background. w = self.bounds.width - 400 h = self.bounds.height - 200 x = self.bounds.width/2 - w/2 y = self.bounds.height/2 - h/2 cr.set_source_rgb(0.762, 0.762, 0.762) cr.rectangle(x, y, w, h) cr.fill() cr.set_source_rgb(0, 0, 0) cr.rectangle(x, y, w, h) cr.stroke() # Draw text title = _('You finished!') + '\n' cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) fd = Pango.FontDescription('Serif Bold') fd.set_size(16 * Pango.SCALE) pango_layout.set_font_description(fd) pango_layout.set_text(title.encode('utf-8'), len(title.encode('utf-8'))) size = pango_layout.get_size() tx = x + (w / 2) - (size[0] / Pango.SCALE) / 2 ty = y + 100 cr.move_to(tx, ty) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout) report = '' report += _('Your score was %(score)d.') % { 'score': self.score } + '\n' if self.medal: report += _('You earned a %(type)s medal!') % self.medal + '\n' report += '\n' report += _('Press the ENTER key to continue.') cr.set_source_rgb(0, 0, 0) pango_layout = PangoCairo.create_layout(cr) fd = Pango.FontDescription('Times') fd.set_size(12 * Pango.SCALE) pango_layout.set_font_description(fd) pango_layout.set_text(report, len(report)) size = pango_layout.get_size() sx = x + w / 2 - (size[0] / Pango.SCALE) / 2 sy = y + 200 cr.move_to(sx, sy) PangoCairo.update_layout(cr, pango_layout) PangoCairo.show_layout(cr, pango_layout)
def canvas_draw(self, widget, cr): """ draw the word centered in the window. """ w, h = self.draw_size # center of the text cx, cy = w/2., h/2. # circle cr.set_line_width(9) cr.set_source_rgb(1.0, 1.0, 1.0) ctx = pangocairo.create_context(cr) layout = pango.Layout.new(ctx) prefixlayout = pango.Layout.new(ctx) desc = pango.font_description_from_string(self.font) for l in (layout, prefixlayout): l.set_font_description(desc) txt = self.current_word center = self.current_center if len(txt) > 0: markup = "%s<span foreground='red'>%s</span>%s" % tuple( t.replace("<", "<").replace(">", ">") for t in (txt[:center], txt[center], txt[center+1:])) prefix = txt[:center] else: markup = "" prefix = "" e, attr, txt, accel = pango.parse_markup(markup, -1, '\x00') layout.set_text(txt, -1) layout.set_attributes(attr) prefixlayout.set_text(prefix, -1) pangocairo.update_layout(cr, layout) pangocairo.update_layout(cr, prefixlayout) # text metrics _, txth = (x/1024. for x in layout.get_size()) prew, _ = (x/1024. for x in prefixlayout.get_size()) cr.move_to(cx - (prew), cy - txth/2) # render the text pangocairo.show_layout(cr, layout)
def draw_label(self, cr): ''' Draw the label based on its attributes ''' my_width = self.rect[2] - self._margins[0] - self._margins[2] if my_width < 0: my_width = 0 my_height = self.rect[3] - self._margins[1] - self._margins[3] for i in range(len(self.labels)): pl = PangoCairo.create_layout(cr) pl.set_text(str(self.labels[i]), -1) self._fd.set_size(int(self._scale[i] * Pango.SCALE)) pl.set_font_description(self._fd) w = pl.get_size()[0] / Pango.SCALE if w > my_width: if self._rescale[i]: self._fd.set_size( int(self._scale[i] * Pango.SCALE * my_width / w)) pl.set_font_description(self._fd) w = pl.get_size()[0] / Pango.SCALE else: j = len(self.labels[i]) - 1 while(w > my_width and j > 0): pl.set_text( "…" + self.labels[i][len(self.labels[i]) - j:], -1) self._fd.set_size(int(self._scale[i] * Pango.SCALE)) pl.set_font_description(self._fd) w = pl.get_size()[0] / Pango.SCALE j -= 1 if self._x_pos[i] is not None: x = int(self.rect[0] + self._x_pos[i]) elif self._horiz_align[i] == "center": x = int(self.rect[0] + self._margins[0] + (my_width - w) / 2) elif self._horiz_align[i] == 'left': x = int(self.rect[0] + self._margins[0]) else: # right x = int(self.rect[0] + self.rect[2] - w - self._margins[2]) h = pl.get_size()[1] / Pango.SCALE if self._y_pos[i] is not None: y = int(self.rect[1] + self._y_pos[i]) elif self._vert_align[i] == "middle": y = int(self.rect[1] + self._margins[1] + (my_height - h) / 2) elif self._vert_align[i] == "top": y = int(self.rect[1] + self._margins[1]) else: # bottom y = int(self.rect[1] + self.rect[3] - h - self._margins[3]) cr.save() cr.translate(x, y) cr.set_source_rgb(self._colors[i][0], self._colors[i][1], self._colors[i][2]) PangoCairo.update_layout(cr, pl) PangoCairo.show_layout(cr, pl) cr.restore()
def show_text(cr, fd, label, size, x, y, page_width, page_height): fd.set_size(int(size * Pango.SCALE)) # TODO: RTL support # Pango doesn't like nulls if type(label) == str or type(label) == unicode: text = label.replace('\0', ' ').rstrip() else: text = str(label).rstrip() top = y left = x right = page_width - LEFT_MARGIN bottom = page_height - TOP_MARGIN * 2 sentences = text.split('\n') for s, sentence in enumerate(sentences): pl = PangoCairo.create_layout(cr) pl.set_font_description(fd) words = sentence.split(' ') for w, word in enumerate(words): pl.set_text(word + ' ', -1) width, height = pl.get_size() width /= Pango.SCALE height /= Pango.SCALE if x + width > right: x = LEFT_MARGIN y += size * 1.5 if y > bottom and \ (w < len(words) - 1 or s < len(sentences) - 1): cr.show_page() pl = PangoCairo.create_layout(cr) pl.set_font_description(fd) y = TOP_MARGIN x = LEFT_MARGIN pl.set_text(word + ' ', -1) cr.save() cr.translate(x, y) PangoCairo.update_layout(cr, pl) PangoCairo.show_layout(cr, pl) cr.restore() x += width x = LEFT_MARGIN y += size * 1.5 if y > bottom and s < len(sentences) - 1: cr.show_page() pl = PangoCairo.create_layout(cr) pl.set_font_description(fd) y = TOP_MARGIN x = LEFT_MARGIN
def show_text(cr, fd, label, size, x, y): pl = PangoCairo.create_layout(cr) fd.set_size(int(size * Pango.SCALE)) pl.set_font_description(fd) if isinstance(label, str) or isinstance(label, unicode): pl.set_text(label.replace('\0', ' '), -1) else: pl.set_text(str(label), -1) pl.set_width((PAGE_WIDTH - LEFT_MARGIN * 2) * Pango.SCALE) cr.save() cr.translate(x, y) PangoCairo.update_layout(cr, pl) PangoCairo.show_layout(cr, pl) cr.restore()