def render_ustr(self, ustr): self._buffer[:] = self._nullbuffer[:] self._layout.set_text(ustr.encode("utf8"), -1) _, logical = self._layout.get_pixel_extents() x0, y0 = logical.x, logical.y assert x0 >= 0 assert y0 >= 0 x1, y1 = x0 + logical.width, y0 + logical.height self._cairo.set_source_rgba(0, 0, 0, 1) PangoCairo.show_layout(self._cairo, self._layout) # each pixel is one byte, let's slice it width = logical.width stride = width new_buffer = bytearray(width * logical.height) for dstrow, srcrow in enumerate(range(y0, y1)): dsti = dstrow*stride srci = x0+srcrow*self.WIDTH new_buffer[dsti:dsti+width] = \ self._buffer[srci:srci+width] baseline = int(self._layout.get_baseline() / Pango.SCALE) return width, logical.height, baseline, new_buffer
def sync_layout(self, size): """ Sync layout changes and calculate intrinsic size based on the parent's size. """ super(Text, self).sync_layout(size) width, height = self.size if self.__intrinsic_size_param == (width, height, self.text, self.font.name, self.font.size): self.intrinsic_size = self.__intrinsic_size_cache return self.__intrinsic_size_cache cr = create_cairo_context() layout = PangoCairo.create_layout(cr) layout.set_width(width * Pango.SCALE) layout.set_height(height * Pango.SCALE) layout.set_ellipsize(Pango.EllipsizeMode.END) layout.set_wrap(Pango.WrapMode.WORD_CHAR) layout.set_font_description(self.font.get_font_description()) layout.set_text(self.text, -1) PangoCairo.show_layout(cr, layout) self.__intrinsic_size_cache = \ int(min(width, Pango.units_to_double(layout.get_size()[0]))), \ int(min(height, Pango.units_to_double(layout.get_size()[1]))) self.__intrinsic_size_param = (width, height, self.text, self.font.name, self.font.size) self.intrinsic_size = self.__intrinsic_size_cache return self.__intrinsic_size_cache
def draw_cb(self, widget, cr): alloc = self.get_allocation() width = alloc.width height = alloc.height style = self.get_style() c = style.bg[gtk.STATE_NORMAL] c_rgb = [float(x)/65535 for x in (c.red, c.green, c.blue)] cr.set_source_rgb(*c_rgb) cr.rectangle(0, 0, width, height) cr.fill() c = style.text[gtk.STATE_NORMAL] c = [float(x)/65535 for x in (c.red, c.green, c.blue)] cr.set_source_rgb(*c_rgb) layout = self.lay_out_group_names(width) leading = style.font_desc.get_size() / 6 vmargin = leading // pango.SCALE layout.set_spacing(leading) cr.move_to(0, self.VERTICAL_MARGIN) if pygtkcompat.USE_GTK3: PangoCairo.show_layout(cr, layout) else: cr.show_layout(layout) # Catch reflows, which maybe result in more or fewer rows. self.set_size_request(-1, -1) # "ask again, give me my natural size" self.queue_resize_no_redraw() self.layout = layout
def draw_label(self, context, lod): layout, rect, cursor_rect, layout_pos = self._calc_layout_params() cursor_width = cursor_rect.h * 0.075 cursor_width = max(cursor_width, 1.0) label_rgba = self.get_label_color() context.save() context.rectangle(*rect) context.clip() # draw text context.set_source_rgba(*label_rgba) context.move_to(*layout_pos) PangoCairo.show_layout(context, layout) context.restore() # don't clip the caret # draw caret context.move_to(cursor_rect.x, cursor_rect.y) context.rel_line_to(0, cursor_rect.h) context.set_source_rgba(*label_rgba) context.set_line_width(cursor_width) context.stroke() # reset attributes; layout is reused by all keys due to memory leak layout.set_attributes(Pango.AttrList())
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 draw(self, canvas, cr): if self.sex == Person.MALE: label = '\u2642 ' + self.name bg_color = (0.72, 0.81, 0.90) elif self.sex == Person.FEMALE: label = '\u2640 ' + self.name bg_color = (1, 0.80, 0.94) else: label = '\u2650 ' + self.name bg_color = (0.95, 0.86, 0.71) layout = canvas.create_pango_layout(label) font = Pango.FontDescription('Sans') layout.set_font_description(font) width, height = layout.get_size() self.width = width / 1024 cr.set_source_rgb(*bg_color) cr.rectangle(self.x + 1, self.y + 1, self.width - 2, self.height - 2) cr.fill() cr.set_source_rgb(0.50, 0.50, 0.50) cr.move_to(self.x, self.y) cr.line_to(self.x + self.width, self.y) cr.stroke() cr.move_to(self.x, self.y + self.height) cr.line_to(self.x + self.width, self.y + self.height) cr.stroke() cr.set_source_rgb(0, 0, 0) cr.move_to(self.x, self.y) PangoCairo.show_layout(cr, layout)
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_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(self, cr, layout_info, hide_if_empty=False): if not hide_if_empty or self.obj.text: layout = create_layout(cr, self.obj.id_str() + " " + self.obj.text, layout_info, 6) xpos, ypos = show_layout(cr, layout, layout_info) cr.move_to(self.obj.x, self.obj.y) cr.line_to(xpos, ypos) cr.set_line_width(LINE_WIDTH) set_rgb_from_color_cycle(cr) cr.stroke() cr.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) cr.set_source_rgba(0.0, 0.0, 1.0, 0.5) cr.set_line_width(LINE_WIDTH) self.draw_box(cr) text = str(self.obj.id[-1]) layout = PangoCairo.create_layout(cr) layout.set_text(text, len(text)) # Dont recreate the description all the time? font = Pango.FontDescription(layout_info['boxfont']) layout.set_font_description(font) cr.move_to(self.obj.x + LINE_WIDTH, self.obj.y + LINE_WIDTH) PangoCairo.show_layout(cr, layout) cr.new_path()
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 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 draw_string(self, label, x, y, offsets=None): '''Draw a string at the specified point. offsets is an optional tuple specifying where the string will be drawn relative to the coordinates passed in; for instance, if offsets are (-1, -1) the string will be drawn with the bottom right edge at the given x, y. ''' fontname = "Sans Italic 14" # fontname = "Sans Italic 14" layout = PangoCairo.create_layout(self.ctx) desc = Pango.font_description_from_string(fontname) layout.set_font_description( desc) layout.set_text(label, -1) if offsets: width, height = layout.get_pixel_size() # # pango draws text with the upper left corner at x, y. # # So that's an offset of (1, 1). Adjust if offsets are different. # # XXX Cairo may do things differently. # xbearing, ybearing, width, height, xadvance, yadvance = \ # self.ctx.text_extents(label) if offsets[0] == 0: x -= int(width/2) elif offsets[0] != 1: x += int(width * offsets[0]) if offsets[1] != 1: y += int(height * offsets[1] - height/2) self.ctx.move_to(x, y) PangoCairo.show_layout (self.ctx, layout)
def render_paragraph(cr, unit_scale, y, text, font = "Serif 6.5", align_top = False): x = INSET + SIDE_TITLE_WIDTH + SIDE_GAP cr.save() cr.move_to(x, y) # Remove the mm scale cr.scale(1.0 / unit_scale, 1.0 / unit_scale) layout = PangoCairo.create_layout(cr) m = re.match(r'(.*?)([0-9]+(\.[0-9]*)?)$', font) font_size = float(m.group(2)) font_size *= unit_scale / POINTS_PER_MM font = m.group(1) + str(font_size) fd = Pango.FontDescription.from_string(font) layout.set_font_description(fd) layout.set_width((CARD_WIDTH - x - INSET) * unit_scale * Pango.SCALE) layout.set_text(text, -1) (ink_rect, logical_rect) = layout.get_pixel_extents() if align_top: cr.rel_move_to(0, -logical_rect.height) PangoCairo.show_layout(cr, layout) cr.restore() return logical_rect.height / unit_scale
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(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 _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_graph(): """Funkcja rysujaca wykres. Funkcja rysuje osie wykresu, slupki na osi OX dla zadanych wartosci a takze umieszcza jezyki na osi OY. """ ctx.set_source_rgba(0, 0, 0, 1) # ustawienie koloru ctx.set_line_width(2) # ustawienie grubosci linii ctx.move_to(130, 600) ctx.line_to(130, 250) ctx.move_to(130, 600) ctx.line_to(580, 600) # narysowanie osi wykresu ctx.stroke_preserve() count = 0 # ustawienie wartosci licznika na 0 k = 125 # przyjeta skala, sluzy do rysowania slupkow na podstawie danej ilosci ksiazek for length in get_values(): ctx.set_source_rgba(0, 0, 0, 1) # ustawienie koloru ctx.set_line_width(2) # ustawienie grubosci linii ctx.rectangle(132, 570 - 32 * count, length / k, 20) # rysowanie slupka dla zadanej dlugosci ctx.stroke_preserve() ctx.set_source_rgba(0, 128, 128, 1) # ustawienie koloru ctx.fill() # wypelnienie slupka kolorem count += 1 # inkrementacja licznika count = 0 # wyzerowanie licznika for name in get_names(): # dla kazdej nazwy kraju ctx.set_source_rgba(0, 0, 255, 1) # ustawienie koloru layout.set_text(name, -1) # umieszczenie nazwy kraju na osi OY ctx.move_to(0, 570 - 32 * count) PangoCairo.show_layout(ctx, layout) # wyswietlenie zmian count += 1
def draw(self, cc): if self.text: cc.save() x_scale = cc.get_matrix()[0] x_trans = cc.get_matrix()[4] cc.identity_matrix() #layout = cc.create_layout() layout = PangoCairo.create_layout(cc) layout.set_markup(self.text) layout.set_font_description(Pango.FontDescription(self.font)) txw, txh = layout.get_size() if self.conf['text_position'] in [POSITION_SW, POSITION_NW]: x_trans = x_trans - txw / Pango.SCALE - x_scale * self.conf['border'] layout.set_alignment(Pango.Alignment.RIGHT) else: x_trans = x_trans + x_scale * (1 + self.conf['border']) layout.set_alignment(Pango.Alignment.LEFT) if self.conf['text_position'] in [POSITION_NE, POSITION_NW]: y_trans = x_scale * self.conf['border'] / 2 else: y_trans = x_scale * (1 - self.conf['border'] / 2) - txh / Pango.SCALE cc.translate(x_trans, y_trans) # Draw text shadow cc.translate(1,1) cc.set_source_rgba(self.conf['text_shadow_color_r'], self.conf['text_shadow_color_g'], self.conf['text_shadow_color_b'], self.conf['text_shadow_color_a']) PangoCairo.show_layout(cc, layout) # Draw text cc.translate(-1,-1) cc.set_source_rgba(self.conf['text_color_r'], self.conf['text_color_g'], self.conf['text_color_b'], self.conf['text_color_a']) PangoCairo.show_layout(cc, layout) cc.restore()
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 _render_hits(self, surface): """Add the number of hits to a Cairo surface""" if not self.creature or not self.creature.hits: return ctx = cairo.Context(surface) ctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL) layout = PangoCairo.create_layout(ctx) layout.set_alignment(Pango.Alignment.CENTER) # TODO Vary font size with scale desc = Pango.FontDescription("monospace 20") layout.set_font_description(desc) layout.set_text(str(self.creature.hits), -1) size = surface.get_width() layout.set_width(size) ctx.set_source_rgb(1, 0, 0) x = 0.5 * size y = 0.2 * size ctx.set_source_rgb(1, 1, 1) ctx.set_line_width(1) width, height = layout.get_pixel_size() ctx.rectangle(x - 0.5 * width, y, 0.9 * width, 0.8 * height) ctx.fill() ctx.set_source_rgb(1, 0, 0) ctx.move_to(x, y) PangoCairo.show_layout(ctx, layout)
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 draw_text_box_centered(ctx, widget, w_width, w_height, text, font_desc=None, add_progress=None): style_context = widget.get_style_context() text_color = style_context.get_color(Gtk.StateFlags.PRELIGHT) if font_desc is None: font_desc = style_context.get_font(Gtk.StateFlags.NORMAL) font_desc.set_size(14 * Pango.SCALE) pango_context = widget.create_pango_context() layout = Pango.Layout(pango_context) layout.set_font_description(font_desc) layout.set_text(text, -1) width, height = layout.get_pixel_size() ctx.move_to(w_width / 2 - width / 2, w_height / 2 - height / 2) ctx.set_source_rgba(text_color.red, text_color.green, text_color.blue, 0.5) PangoCairo.show_layout(ctx, layout) # Draw an optional progress bar below the text (same width) if add_progress is not None: bar_height = 10 ctx.set_source_rgba(*text_color) ctx.set_line_width(1.) rounded_rectangle(ctx, w_width / 2 - width / 2 - .5, w_height / 2 + height - .5, width + 1, bar_height + 1) ctx.stroke() rounded_rectangle(ctx, w_width / 2 - width / 2, w_height / 2 + height, int(width * add_progress) + .5, bar_height) ctx.fill()
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 draw_cb(self, widget, cr): alloc = self.get_allocation() width = alloc.width height = alloc.height fg, bg = self._text_colors["normal"] #cr.set_source_rgb(*bg.get_rgb()) #cr.paint() cr.set_source_rgb(*fg.get_rgb()) layout = self.lay_out_group_names(width) vmargin = self._leading // pango.SCALE layout.set_spacing(self._leading) cr.move_to(0, self.VERTICAL_MARGIN) if pygtkcompat.USE_GTK3: PangoCairo.show_layout(cr, layout) else: cr.show_layout(layout) # Catch reflows, which maybe result in more or fewer rows. self.set_size_request(-1, -1) # "ask again, give me my natural size" self.queue_resize_no_redraw() self.layout = layout
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 border(self, title=None, clear=None): x1 = self.fontwidth / 2 y1 = self.fontheight / 2 x2 = x1 + (self.width_chars - 1) * self.fontwidth y2 = y1 + (self.height_chars - 1) * self.fontheight ctx = cairo.Context(self._surface) ctx.set_source_rgb(*colours[self.colour.foreground]) self._rect(ctx, self.fontwidth, x1, x2, y1, y2) ctx.stroke() if title: layout = PangoCairo.create_layout(ctx) layout.set_text(title, -1) layout.set_font_description(self.font) width, height = layout.get_pixel_size() ctx.set_source_rgb(*colours[self.colour.background]) x = self.fontwidth + 4 ctx.rectangle(x, 0, width, self.fontheight) ctx.fill() ctx.set_source_rgb(*colours[self.colour.foreground]) ctx.move_to(x, 0) PangoCairo.show_layout(ctx, layout) if clear: layout = PangoCairo.create_layout(ctx) layout.set_text(clear, -1) layout.set_font_description(self.font) width, height = layout.get_pixel_size() ctx.set_source_rgb(*colours[self.colour.background]) x = self.width - self.fontwidth - width - 4 y = self.height - self.fontheight ctx.rectangle(x, y, width, self.fontheight) ctx.fill() ctx.set_source_rgb(*colours[self.colour.foreground]) ctx.move_to(x, y) PangoCairo.show_layout(ctx, layout) self.damage(0, 0, self.height, self.width)
def do_draw(self, gpsmap, ctx): """ Draw all the messages """ ctx.save() font_size = "%s %d" % (self.font, self.size) font = Pango.FontDescription(font_size) descr = Pango.font_description_from_string(self.font) descr.set_size(self.size * Pango.SCALE) color = Gdk.color_parse(self.color) ctx.set_source_rgba(float(color.red / 65535.0), float(color.green / 65535.0), float(color.blue / 65535.0), 0.9) # transparency d_width = gpsmap.get_allocation().width d_width -= 100 ctx.restore() ctx.save() ctx.move_to(100, 5) layout = PangoCairo.create_layout(ctx) layout.set_font_description(descr) layout.set_indent(Pango.SCALE * 0) layout.set_alignment(Pango.Alignment.LEFT) layout.set_wrap(Pango.WrapMode.WORD_CHAR) layout.set_spacing(Pango.SCALE * 3) layout.set_width(d_width * Pango.SCALE) layout.set_text(self.message, -1) PangoCairo.show_layout(ctx, layout) ctx.restore() ctx.stroke()
def __on_expose_event( self, widget, context ): """ Draws the text on the widget. This should be called indirectly by using self.queue_draw() """ #if self.__graphics_context is None: # self.__graphics_context = self.get_window().new_gc() #self.get_window().draw_layout(_context, # int(self.__x_offset), 0, self.__pango_layout, # self._fg) #context.show_text("test") if not self.__graphics_context: self.__graphics_context = context self.__graphics_context.set_source_rgb(1.0, 1.0, 1.0) #self.__graphics_context.set_source_rgb(self._fg) self.__pango_layout = PangoCairo.create_layout(self.__graphics_context) #self.__reset_widget() self.__pango_layout.set_markup(self.text, -1) PangoCairo.show_layout(self.__graphics_context, self.__pango_layout) lbl_x, lbl_y = self.__pango_layout.get_pixel_size() #self.set_size_request( -1, lbl_y ) print lbl_x, lbl_y self.__graphics_context.move_to(0, 0)
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 + 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 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 rotate_text(ctx, markup, cx, cy, angle): ctx.save() layout = PangoCairo.create_layout(ctx) layout.set_font_description(BASE_FONT) layout.set_alignment(Pango.Alignment.CENTER) layout.set_markup(markup, -1) _, extents = layout.get_pixel_extents() tw, th = extents.width, extents.height ctx.translate(cx, cy) ctx.rotate(angle * math.pi / 180) ctx.move_to(-tw, -th) ctx.line_to(0, 0) ctx.stroke() PangoCairo.show_layout(ctx, layout) ctx.restore()
def draw_label(self, ctx): ctx.move_to(self.label_pos[0], self.label_pos[1]) if self.hover: ctx.set_source_rgba(self.label_color_hover[0], self.label_color_hover[1], self.label_color_hover[2], 1.0) else: ctx.set_source_rgba(self.label_color_normal[0], self.label_color_normal[1], self.label_color_normal[2], 1.0) ctx.move_to(self.label_pos[0], self.label_pos[1]) PangoCairo.show_layout(ctx, self.label_layout)
def on_spectrum_draw(self, drawing_area, ctx): ctx.paint() n_bars = self.spectrum_magnitudes.size if n_bars > 0: width = drawing_area.get_allocation().width height = drawing_area.get_allocation().height style = drawing_area.get_style_context() bar_height = self.spectrum_magnitudes * height x = np.linspace(0, width, n_bars) y = height - bar_height color = style.lookup_color('theme_selected_bg_color')[1] ctx.set_source_rgba(color.red, color.green, color.blue, 1.0) ctx.set_line_width(1.1) for i in range(len(x) - 1): ctx.move_to(x[i], y[i]) ctx.line_to(x[i + 1], y[i + 1]) ctx.stroke() if self.draw_guideline: guideline_h = int(self.guideline_position * height) ctx.move_to(0, guideline_h) ctx.line_to(width, guideline_h) ctx.set_source_rgba(1.0, 0.0, 0.0, 1.0) ctx.set_line_width(1.1) ctx.stroke() if self.mouse_inside: label = str(self.mouse_freq) + ' Hz, ' label += str(self.mouse_intensity) + ' dB' layout = PangoCairo.create_layout(ctx) layout.set_text(label, -1) layout.set_font_description(self.font_description) text_width, text_height = layout.get_pixel_size() ctx.move_to(width - text_width, 0) 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 = int(CONFIG.image_size[1] + CONFIG.image_border_width) 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() # bar with halo ctx.set_line_width(0) ctx.set_source_rgb(*self.color[1]) ctx.rectangle(CONFIG.image_border_width + 1.8, h - 3.2 - CONFIG.image_border_width, w - 2 * (CONFIG.image_border_width + 1.8), 3.4) ctx.fill() ctx.set_source_rgb(*self.color[0]) ctx.rectangle(CONFIG.image_border_width + 2, h - 3 - CONFIG.image_border_width, w - 2 * (CONFIG.image_border_width + 2), 3) ctx.fill() # border ctx.rectangle(0, 0, w, h) ctx.set_line_width(CONFIG.image_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 - layout.get_iter().get_baseline() / Pango.SCALE) / 2.0 - 3) PangoCairo.show_layout(ctx, layout) ctx.show_page()
def __expose(self, widget, context): """Create canvas hint message at start """ thought_count = len(self._main_area.thoughts) if thought_count > 0: return False pango_context = self._main_area.pango_context layout = Pango.Layout(pango_context) context.set_source_rgb(0.6, 0.6, 0.6) context.set_line_width(4.0) context.set_dash([10.0, 5.0], 0.0) geom = list(self._main_area.window.get_geometry()) geom[3] = geom[3] - ((self.get_window().get_geometry()[3] - geom[3]) / 2) # Make sure initial thought is "above the fold" if geom[2] < geom[3]: xf = 2 yf = 4 else: xf = 4 yf = 2 layout.set_alignment(Pango.Alignment.CENTER) text = _('Click to add\ncentral thought') layout.set_text(text, len(text)) width, height = layout.get_pixel_size() context.rectangle(geom[2] / xf - (width / 2), geom[3] / yf - (height / 2), layout.get_width(), layout.get_height()) PangoCairo.show_layout(context, layout) round = 40 ul = (geom[2] / xf - (width / 2) - round, geom[3] / yf - (height / 2) - round) lr = (geom[2] / xf + (width / 2) + round, geom[3] / yf + (height / 2) + round) context.move_to(ul[0], ul[1] + round) context.line_to(ul[0], lr[1] - round) context.curve_to(ul[0], lr[1], ul[0], lr[1], ul[0] + round, lr[1]) context.line_to(lr[0] - round, lr[1]) context.curve_to(lr[0], lr[1], lr[0], lr[1], lr[0], lr[1] - round) context.line_to(lr[0], ul[1] + round) context.curve_to(lr[0], ul[1], lr[0], ul[1], lr[0] - round, ul[1]) context.line_to(ul[0] + round, ul[1]) context.curve_to(ul[0], ul[1], ul[0], ul[1], ul[0], ul[1] + round) context.stroke() return False
def draw_label(self, cr): ''' Draw the label based on its attributes ''' my_width = self.rect.width - self._margins[0] - self._margins[2] if my_width < 0: my_width = 0 my_height = self.rect.height - self._margins[1] - self._margins[3] for i in range(len(self.labels)): pl = PangoCairo.create_layout(cr) text = str(self.labels[i]) pl.set_text(text, len(text)) 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): text = "…" + self.labels[i][len(self.labels[i]) - j:] pl.set_text(text, len(text)) 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._horiz_align[i] == "center": x = int(self.rect.x + self._margins[0] + (my_width - w) / 2) elif self._horiz_align[i] == 'left': x = int(self.rect.x + self._margins[0]) else: # right x = int(self.rect.x + self.rect.width - w - self._margins[2]) h = pl.get_size()[1] / Pango.SCALE if self._vert_align[i] == "middle": y = int(self.rect.y + self._margins[1] + (my_height - h) / 2) elif self._vert_align[i] == "top": y = int(self.rect.y + self._margins[1]) else: # bottom y = int(self.rect.y + self.rect.height - h - self._margins[3]) cr.save() cr.translate(x, y) cr.set_source_rgb(self._color[0], self._color[1], self._color[2]) PangoCairo.update_layout(cr, pl) PangoCairo.show_layout(cr, pl) cr.restore()
def _marker(self, color, txt, lat, lon, ctx, dpi): marker_path = os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', 'images', 'marker.svg')) fp = open(marker_path, 'r') data = fp.read() fp.close() if color[0] != '#': c = Color(color) color = c.hex_l data = data.replace('#000000', color) rsvg = Rsvg.Handle() svg = rsvg.new_from_data(data.encode()) x, y = self._latlon2xy(lat, lon, dpi) scale = (50.0 / svg.props.height) * (dpi / 72.0) x -= svg.props.width * scale / 2 y -= svg.props.height * scale ctx.save() ctx.translate(x, y) ctx.scale(scale, scale) svg.render_cairo(ctx) pc = PangoCairo.create_context(ctx) layout = PangoCairo.create_layout(ctx) fd = Pango.FontDescription('Droid Sans') fd.set_size(Pango.SCALE) layout.set_font_description(fd) layout.set_text(txt, -1) draw_utils.adjust_font_size(layout, fd, svg.props.width / 3, svg.props.width / 3) ink, logical = layout.get_extents() ctx.translate(svg.props.width / 2 - logical.width / svg.props.height, svg.props.height / 5) PangoCairo.update_layout(ctx, layout) PangoCairo.show_layout(ctx, layout) ctx.restore()
def draw(self, canvas, cr): cr.set_source_rgb(0.83, 0.83, 0.83) cr.rectangle(self.x, self.y, self.width, self.height) cr.fill() cr.set_source_rgb(1, 1, 1) cr.rectangle(self.x, self.y, self.width, self.height) cr.stroke() layout = canvas.create_pango_layout('F') font = Pango.FontDescription('Sans') layout.set_font_description(font) width, height = layout.get_size() cr.set_source_rgb(0, 0, 0) cr.move_to(self.x, self.y) PangoCairo.show_layout(cr, layout)
def _draw_text(self, label, x, y, size): pl = PangoCairo.create_layout(self._cr) fd = Pango.FontDescription('Sans') fd.set_size(int(size) * Pango.SCALE) pl.set_font_description(fd) if type(label) == str or type(label) == unicode: pl.set_text(label.replace('\0', ' '), -1) elif type(label) == float or type(label) == int: pl.set_text(str(label), -1) else: pl.set_text(str(label), -1) self._cr.save() self._cr.translate(x, y) self._cr.set_source_rgb(1, 1, 1) PangoCairo.update_layout(self._cr, pl) PangoCairo.show_layout(self._cr, pl) self._cr.restore()
def __call__(self, text): self.create_params() self.layout.set_text(text, -1) stroke_rect, _ = self.layout.get_pixel_extents() # Applies a fit-height variation. font_desc = self.layout.get_font_description() font_desc.set_size(int(font_desc.get_size() * 0.8 * self.height/stroke_rect.height ) ) self.layout.set_font_description(font_desc) stroke_rect, _ = self.layout.get_pixel_extents() PangoCairo.show_layout(self.context, self.layout) data = self.surface.get_data() #data = np.frombuffer(data, dtype=np.uint8).reshape(( targetH, self.width ))[:, :stroke_rect.width] data = np.frombuffer(data, dtype=np.uint8).reshape((self.height, self.width))[:, :stroke_rect.width] data = np.invert(data) return data
def drawShadowText(self, text, x, y, ctx, playout): playout.set_text(text, -1) ctx.set_source_rgb(1.0, 1.0, 1.0) for dx, dy in ( (-1, -1), (1, -1), (-1, 1), (1, 1), ): ctx.move_to(x + dx, y + dy) pangocairo.show_layout(ctx, playout) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.move_to(x, y) pangocairo.show_layout(ctx, playout)
def _draw_text(self, cr, txt, x, y, centered=False, tline_x=-1, w=-1): layout = PangoCairo.create_layout(cr) layout.set_text(txt, -1) desc = Pango.FontDescription("Sans 8") layout.set_font_description(desc) lw, lh = layout.get_pixel_size() if centered == True: x = w/2 - lw/2 + tline_x if lw > w: return cr.move_to(x, y) cr.set_source_rgb(*TEXT_COLOR) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout)
def do_render(self, context, widget, background_area, cell_area, flags): if not self.data: return text, widthfrac, goodness = self.data if widthfrac: paintGraph(context, widthfrac, stoplightColor(goodness), cell_area) if text: layout = PangoCairo.create_layout(context) layout.set_text(text, -1) fd = Pango.font_description_from_string("Sans 10") layout.set_font_description(fd) w, h = layout.get_pixel_size() context.move_to(cell_area.x, cell_area.y) context.rel_move_to(70 - w, (height - h) / 2) PangoCairo.show_layout(context, layout)
def _draw_symbol(self, context, symbol, color, transparency=0.): c = context.cairo cairo_context = c if isinstance(c, CairoBoundingBoxContext): cairo_context = c._cairo width = self.width height = self.height # c.set_antialias(Antialias.GOOD) layout = PangoCairo.create_layout(cairo_context) font_name = constants.ICON_FONT def set_font_description(): layout.set_markup('<span font_desc="%s %s">&#x%s;</span>' % (font_name, font_size, symbol)) if symbol in self.__symbol_size_cache and \ self.__symbol_size_cache[symbol]['width'] == width and \ self.__symbol_size_cache[symbol]['height'] == height: font_size = self.__symbol_size_cache[symbol]['size'] set_font_description() else: font_size = 30 set_font_description() pango_size = (width * SCALE, height * SCALE) while layout.get_size()[0] > pango_size[0] or layout.get_size( )[1] > pango_size[1]: font_size *= 0.9 set_font_description() self.__symbol_size_cache[symbol] = { 'width': width, 'height': height, 'size': font_size } c.move_to(width / 2. - layout.get_size()[0] / float(SCALE) / 2., height / 2. - layout.get_size()[1] / float(SCALE) / 2.) c.set_source_rgba(*gap_draw_helper.get_col_rgba(color, transparency)) PangoCairo.update_layout(cairo_context, layout) PangoCairo.show_layout(cairo_context, layout)
def draw_text(self, ctx, exclude_extents=True): if not self.display_text: return ctx.save() if self.use_text_surface: text_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(self.width), int(self.height)) text_ctx = cairo.Context(text_surface) else: text_ctx = ctx layout, x, y = self.get_text_layout(text_ctx, exclude_extents=exclude_extents) text_ctx.move_to(x, y) text_rect = layout.get_pixel_extents()[0] if text_rect.width > 0 and text_rect.height > 0: PangoCairo.show_layout(text_ctx, layout) if self.use_text_surface: ctx.set_source_surface(text_surface) ctx.paint() """ ctx.save() ctx.translate(x, y) ctx.scale(1./Pango.SCALE, 1./Pango.SCALE) for i in xrange(len(self.display_text)): rect = layout.index_to_pos(i) ctx.rectangle(rect.x, rect.y, rect.width, rect.height) #ctx.rectangle(rect.x*1./Pango.SCALE, rect.y*1./Pango.SCALE, rect.width*1./Pango.SCALE, rect.height*1./Pango.SCALE) ctx.restore() draw_stroke(ctx, 1, "FF0000") """ """@Debugging ctx.new_path() ctx.translate(x, y) ctx.rectangle(0,0,text_width, text_height) draw_stroke(ctx, 1, "FF0000") ctx.translate(-text_left, -text_top) ctx.rectangle(0,0,text_width, text_height) draw_stroke(ctx, 1, "000000") """ ctx.restore()
def draw_layout(self, cr, x, y, rotation, xscale, yscale): cr.save() layout = PangoCairo.create_layout(cr) layout.set_text(self.text, -1) layout.set_font_description(self.font_desc) layout.set_alignment(self.alignment) self.pixel_size = layout.get_pixel_size() # Shadow if self.shadow_on: cr.save() r, g, b = self.shadow_color_rgb a = self.shadow_opacity / 100.0 cr.set_source_rgba(r, g, b, a) cr.move_to(x + self.shadow_xoff, y + self.shadow_yoff) cr.scale(xscale, yscale) cr.rotate(rotation) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) cr.restore() # Text if self.fill_on: cr.set_source_rgba(*self.color_rgba) cr.move_to(x, y) cr.scale(xscale, yscale) cr.rotate(rotation) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) # Outline if self.outline_on: if self.fill_on == False: # case when user only wants outline we need to transform here cr.move_to(x, y) cr.scale(xscale, yscale) cr.rotate(rotation) PangoCairo.layout_path(cr, layout) cr.set_source_rgba(*self.outline_color_rgba) cr.set_line_width(self.outline_width) cr.stroke() cr.restore()
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_overlay(self, context, width, height): context = cairo_context_from_gi(context) if self.scroll_enable and self.message: #print str(width) + " x " + str(height) #context.scale(width, height) #context.scale(width / 100, height / 100) #context.scale(100, 100) #context.set_source_rgb(1, 0, 0) #context.paint_with_alpha(1) #context.select_font_face("Helvetica") #context.set_font_face(None) #context.set_font_size(0.05) #context.move_to(0.1, 0.1) #context.show_text("Hello World") #context.rectangle(0, height * 0.60, width, 30) #context.rectangle(0, 0.60, 1, 0.1) context.set_source_rgb(1, 0, 0) context.rectangle(0, 0.55 * height, width, 0.15 * height) context.fill() #context.scale(1.0 / width, 1.0 / height) #context.translate(0, height * 0.60) layout = PangoCairo.create_layout(context) #font = Pango.FontDescription("Arial " + str(0.090 * height)) #font.set_family("Sans") #font.set_size(0.090 * height) #font.set_size(25) #font.set_stretch(Pango.Stretch.ULTRA_CONDENSED) font = Pango.font_description_from_string("Sans Condensed " + str(0.090 * height)) layout.set_font_description(font) layout.set_text(self.message, -1) context.save() (layout_width, layout_height) = layout.get_pixel_size() self.scroll_wrap = 1.0 + (float(layout_width) / float(width)) pos = (self.scroll_pos * width) - layout_width context.set_source_rgb(1, 1, 1) context.translate(pos, 0.55 * height) PangoCairo.update_layout(context, layout) PangoCairo.show_layout(context, layout) context.restore()
def draw_cursor(self, cr): cr.set_source_rgb(0.7, 0.7, 0.7) cr.rectangle(ep.cursor_position[0]-3, ep.cursor_position[1]-3, 6, 6) cr.stroke() cr.identity_matrix() cr.set_source_rgb(0.5, 0.5, 0.5) cr.move_to(ep.cursor_position[0]-6, ep.cursor_position[1]) cr.line_to(ep.cursor_position[0]+6, ep.cursor_position[1]) cr.stroke() cr.move_to(ep.cursor_position[0], ep.cursor_position[1]-6) cr.line_to(ep.cursor_position[0], ep.cursor_position[1]+6) cr.stroke() cr.identity_matrix() self.pangolayout.set_markup('%.3f, %.3f'%(ep.pointer_position[0], ep.pointer_position[1]), -1) cr.move_to(ep.cursor_position[0]+12, ep.cursor_position[1]-6) PangoCairo.show_layout(cr, self.pangolayout) cr.identity_matrix()
def addstr(self, y, x, text, colour=None): if not colour: colour = self.colour ctx = cairo.Context(self._surface) # Draw the background ctx.set_source_rgb(*colours[colour.background]) ctx.rectangle(x * self.fontwidth, y * self.fontheight, len(text) * self.fontwidth, self.fontheight) ctx.fill() layout = PangoCairo.create_layout(ctx) layout.set_text(text, -1) layout.set_font_description(self.monospace) ctx.set_source_rgb(*colours[colour.foreground]) ctx.move_to(x * self.fontwidth, y * self.fontheight) PangoCairo.show_layout(ctx, layout) self.damage(y * self.fontheight, x * self.fontwidth, self.fontheight, len(text) * self.fontwidth) self.move(y, x + len(text))
def _draw_time(self): markup = _('<markup>\ <span lang="en" font_desc="Sans,Monospace Bold 12">\ <span foreground="#E6000A">%s</span></span></markup>') cr = self.get_property('window').cairo_create() cr.set_source_rgba(*style.Color(self._COLOR_E).get_rgba()) pango_layout = pangocairo.create_layout(cr) d = int(self._center_y + self._scale + 20) markup_f = markup % "Physical Emotional Intellectual" pango_layout.set_markup(markup_f) 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 + 5) pangocairo.show_layout(cr, pango_layout)
def draw_layout(self, cr, x, y, rotation, xscale, yscale): cr.save() layout = PangoCairo.create_layout(cr) layout.set_text(self.text, -1) layout.set_font_description(self.font_desc) layout.set_alignment(self.alignment) self.pixel_size = layout.get_pixel_size() cr.set_source_rgba(*self.color_rgba) cr.move_to(x, y) cr.scale(xscale, yscale) cr.rotate(rotation) PangoCairo.update_layout(cr, layout) PangoCairo.show_layout(cr, layout) cr.restore()
def text_along_circle(ctx, markup, angle, radius): ctx.save() ctx.translate(SIZE / 2, SIZE / 2) # TODO refactor this standard setup ctx.set_antialias(cairo.ANTIALIAS_BEST) layout = PangoCairo.create_layout(ctx) layout.set_font_description(BASE_FONT) layout.set_alignment(Pango.Alignment.CENTER) layout.set_markup(markup, -1) _, extents = layout.get_pixel_extents() tw, th = extents.width, extents.height # TODO flip text when angle exceeds...something ctx.rotate(angle * 2 * math.pi) ctx.move_to(-tw / 2, -radius - (th / 2)) PangoCairo.show_layout(ctx, layout) ctx.restore()
def render(self, response): """Render widget.""" configure_fontconfig() surface = cairo.ImageSurface.create_from_png(self.get_filename()) height = surface.get_height() ctx = cairo.Context(surface) columns = self.get_columns() column_width = self.get_column_width(surface, columns) fonts = self.get_column_fonts() for i, column in enumerate(columns): offset = self.offset for row, text in enumerate(column): layout = PangoCairo.create_layout(ctx) layout.set_font_description(fonts[row]) # Set color and position ctx.move_to(self.column_offset + column_width * i, offset) ctx.set_source_rgb(*COLOR_DATA[self.color]) # Add text layout.set_markup(text) layout.set_alignment(Pango.Alignment.CENTER) layout.set_width(column_width * Pango.SCALE) offset += layout.get_pixel_size().height # Render to cairo context PangoCairo.show_layout(ctx, layout) # Render column separators if self.lines and i > 0: ctx.new_path() ctx.set_source_rgb(*COLOR_DATA[self.color]) ctx.set_line_width(0.5) ctx.move_to(column_width * i, self.offset) ctx.line_to(column_width * i, height - self.offset) ctx.stroke() self.render_additional(ctx) surface.write_to_png(response)
def draw_hints(self, context): extent = 25.0 context.save() context.new_path() context.rectangle(self.x - extent, self.y - extent, extent, extent) context.set_source_rgba(130 / 255.0, 130 / 255.0, 250 / 255.0, 0.25) context.fill_preserve() context.set_line_width(1) context.set_source_rgb(130 / 255.0, 130 / 255.0, 250 / 255.0) context.stroke() #context = pangocairo.CairoContext(context) #layout = pangocairo.CairoContext.create_layout(context) layout = PangoCairo.create_layout(context) if platform.system() == 'Windows': fontname = 'Sans' else: fontname = 'Ubuntu' text = str(int(self.z)) length = len(text) if length > 3: size = 6 text = "..." + text[length - 1:4] elif length > 2: size = 8 elif length > 1: size = 10 else: size = 12 description = '%s Bold %d' % (fontname, size) font = Pango.FontDescription(description) layout.set_justify(True) layout.set_font_description(font) layout.set_text(text, len(text)) context.set_source_rgb(0, 0, 0) width, height = layout.get_size() width /= Pango.SCALE height /= Pango.SCALE context.move_to(self.x - (extent + width) / 2, self.y - (extent + height) / 2) PangoCairo.show_layout(context, layout) context.set_antialias(cairo.ANTIALIAS_DEFAULT) context.restore()
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 __paint_txt(self, pdf_surface, pdf_size, pdf_context, page): if not PANGO_AVAILABLE: return img = page.img scale_factor_x = pdf_size[0] / img.size[0] scale_factor_y = pdf_size[1] / img.size[1] scale_factor = min(scale_factor_x, scale_factor_y) for line in page.boxes: for word in line.word_boxes: box_size = ( (word.position[1][0] - word.position[0][0]) * scale_factor, (word.position[1][1] - word.position[0][1]) * scale_factor ) layout = PangoCairo.create_layout(pdf_context) layout.set_text(word.content, -1) txt_size = layout.get_size() if 0 in txt_size or 0 in box_size: continue txt_factors = ( float(box_size[0]) * Pango.SCALE / txt_size[0], float(box_size[1]) * Pango.SCALE / txt_size[1], ) pdf_context.save() try: pdf_context.set_source_rgb(0, 0, 0) pdf_context.translate( word.position[0][0] * scale_factor, word.position[0][1] * scale_factor ) # make the text use the whole box space pdf_context.scale(txt_factors[0], txt_factors[1]) PangoCairo.update_layout(pdf_context, layout) PangoCairo.show_layout(pdf_context, layout) finally: pdf_context.restore()
def render(self, text, img, isDay): print "updating" self.surface = cairo.ImageSurface.create_from_png(img) ctx = cairo.Context(self.surface) ctx.scale(1.0, 1.0) ctx.set_source_surface(self.surface, 0, 0) ctx.paint() width = 136 height = 72 radius = 10 degrees = 0.017453293 x = (self.width - width)/2 y = (self.height - height)/2 if not isDay: ctx.set_source_rgba(0.0, 0.0, 0.0, 0.7) else: ctx.set_source_rgba(1.0, 1.0, 1.0, 0.7) ctx.arc(x + width - radius, y + radius, radius, -90 * degrees, 0 * degrees); ctx.arc(x + width - radius, y + height - radius, radius, 0 * degrees, 90 * degrees); ctx.arc(x + radius, y + height - radius, radius, 90 * degrees, 180 * degrees); ctx.arc(x + radius, y + radius, radius, 180 * degrees, 270 * degrees); ctx.close_path() ctx.fill() self.pango_layout = self.create_pango_layout(text) self.pango_layout.set_markup ("<span size='xx-large'><b>%s</b></span>"%text, -1) if not isDay: ctx.set_source_rgb(1.0, 1.0, 1.0) else: ctx.set_source_rgb(0.0, 0.0, 0.0) text_width, text_height = self.pango_layout.get_pixel_size() ctx.move_to(x + (width - text_width)/2, y + (height - text_height)/2) PangoCairo.show_layout(ctx, self.pango_layout) pixbuf = Gdk.pixbuf_get_from_surface(self.surface, 0, 0, self.width, self.height) self.pixbuf = pixbuf return self.pixbuf
def set_player_name(self, position, name=None): """Sets the name of the player at position. @param position: the position of the player. @param name: the name of the player, or None for no player. """ self.playernames[position] = name id = ('playername', position) # Identifier for player name item. # If no name specified, show hand at position as translucent. handid = ('hand', position) if handid in self.items: opacity = 1 if name else 0.5 self.update_item(handid, opacity=opacity) layout = Pango.Layout(self.create_pango_context()) layout.set_font_description(self.font_description) if name is None: layout.set_text(DIRECTION_NAMES[position], -1) else: layout.set_text(DIRECTION_NAMES[position] + ': ' + name, -1) # Create an ImageSurface respective to dimensions of text. width, height = layout.get_pixel_size() width += 8 height += 4 surface, context = self.new_surface(width, height) # Draw background box, text to ImageSurface. context.set_line_width(4) context.rectangle(0, 0, width, height) context.set_source_rgb(0, 0.5, 0) context.fill_preserve() context.set_source_rgb(0, 0.25, 0) context.stroke() context.move_to(4, 2) context.set_source_rgb(1, 1, 1) PangoCairo.show_layout(context, layout) if id in self.items: self.update_item(id, source=surface) else: self.add_item(id, surface, self.player_xy[position], z_index=2)