def __init__(self, antialias): G15Text.__init__(self, antialias) pangocairo.context_set_font_options(pango_context, self._create_font_options()) self.__pango_cairo_context = None self.__layout = None self.valign = pango.ALIGN_CENTER self.__layout = pango.Layout(pango_context)
def create_pango_context(self): pango_context = self.pango_fontmap.create_context() options = cairo.FontOptions() options.set_hint_metrics(cairo.HINT_METRICS_OFF) pangocairo.context_set_font_options(pango_context, options) pangocairo.context_set_resolution(pango_context, 72) return pango_context
def draw(self, cr): try: layout = self.layout except AttributeError: layout = cr.create_layout() # set font options # see http://lists.freedesktop.org/archives/cairo/2007-February/009688.html context = layout.get_context() fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_DEFAULT) fo.set_hint_style(cairo.HINT_STYLE_NONE) fo.set_hint_metrics(cairo.HINT_METRICS_OFF) pangocairo.context_set_font_options(context, fo) # set font font = pango.FontDescription() font.set_family(self.pen.fontname) font.set_absolute_size(self.pen.fontsize * pango.SCALE) layout.set_font_description(font) # set text layout.set_text(self.t) # cache it self.layout = layout else: cr.update_layout(layout) width, height = layout.get_size() width = float(width) / pango.SCALE height = float(height) / pango.SCALE cr.move_to(self.x - self.w / 2, self.y) if self.j == self.LEFT: x = self.x elif self.j == self.CENTER: x = self.x - 0.5 * width elif self.j == self.RIGHT: x = self.x - width else: assert 0 y = self.y - height cr.move_to(x, y) cr.set_source_rgba(*self.pen.color) cr.show_layout(layout)
def draw(self, cr): try: layout = self.layout except AttributeError: layout = cr.create_layout() # set font options # See http://lists.freedesktop.org/archives/cairo/2007-February/009688.html context = layout.get_context() fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_DEFAULT) fo.set_hint_style(cairo.HINT_STYLE_NONE) fo.set_hint_metrics(cairo.HINT_METRICS_OFF) pangocairo.context_set_font_options(context, fo) # set font font = pango.FontDescription() font.set_family(self.pen.fontname) font.set_absolute_size(self.pen.fontsize*pango.SCALE) layout.set_font_description(font) # set text layout.set_text(self.t) # cache it self.layout = layout else: cr.update_layout(layout) width, height = layout.get_size() width = float(width)/pango.SCALE height = float(height)/pango.SCALE cr.move_to(self.x - self.w/2, self.y) if self.j == LEFT: x = self.x elif self.j == CENTER: x = self.x - 0.5*width elif self.j == RIGHT: x = self.x - width else: assert 0 y = self.y - height cr.move_to(x, y) cr.set_source_rgba(*self.pen.color) cr.show_layout(layout)
def cairo_text_bbox(text, font_params, spacing=0, scale=1.0): surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8) ctx = cairo.Context(surf) # The scaling must match the final context. # If not there can be a mismatch between the computed extents here # and those generated for the final render. ctx.scale(scale, scale) font = cairo_font(font_params) if use_pygobject: status, attrs, plain_text, _ = pango.parse_markup(text, len(text), '\0') layout = pangocairo.create_layout(ctx) pctx = layout.get_context() fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL) pangocairo.context_set_font_options(pctx, fo) layout.set_font_description(font) layout.set_spacing(spacing * pango.SCALE) layout.set_text(plain_text, len(plain_text)) layout.set_attributes(attrs) li = layout.get_iter() # Get first line of text baseline = li.get_baseline() / pango.SCALE re = layout.get_pixel_extents()[1] # Get logical extents extents = (re.x, re.y, re.x + re.width, re.y + re.height) else: # pyGtk attrs, plain_text, _ = pango.parse_markup(text) pctx = pangocairo.CairoContext(ctx) pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL) layout = pctx.create_layout() layout.set_font_description(font) layout.set_spacing(spacing * pango.SCALE) layout.set_text(plain_text) layout.set_attributes(attrs) li = layout.get_iter() # Get first line of text baseline = li.get_baseline() / pango.SCALE #print('@@ EXTENTS:', layout.get_pixel_extents()[1], spacing) extents = layout.get_pixel_extents()[1] # Get logical extents return [extents[0], extents[1], extents[2], extents[3], baseline]
def create_layout (self, text, markup=True): cr = self.cr layout = cr.create_layout () font_options = cairo.FontOptions () font_options.set_hint_metrics (cairo.HINT_METRICS_OFF) pangocairo.context_set_font_options (layout.get_context (), font_options) if markup: layout.set_markup (text) else: layout.set_text (text) return layout
def main(): if len(sys.argv) != 3: usage() sys.exit(1) version = sys.argv[1] imgpath = sys.argv[2] surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 140,24) ctx = cairo.Context(surface) c = pangocairo.CairoContext(ctx) l = c.create_layout() font_desc = pango.FontDescription('Grixel Acme 7 Wide Bold 14') l.set_font_description(font_desc) fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_NONE) c.set_font_options(fo) pangocairo.context_set_font_options (l.get_context(), fo) l.set_text('theory') set_context_color(ctx,'#333333') ctx.move_to(4,-8) c.show_layout(l) c.update_layout(l) l = c.create_layout() font_desc = pango.FontDescription('Grixel Acme 7 Wide 7') attr = pango.AttrList() attr.insert(pango.AttrLetterSpacing(1200,0,100)) l.set_attributes(attr) l.set_font_description(font_desc) fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_NONE) pangocairo.context_set_font_options (l.get_context(), fo) l.set_text(version) ctx.move_to(105,8) ctx.set_source_rgb (.33,.33,.33) c.show_layout(l) c.update_layout(l) surface.write_to_png(imgpath)
def draw_text(x, y, text, font, text_color, spacing, c): c.save() c.set_source_rgba(*rgb_to_cairo(text_color)) font = cairo_font(font) c.translate(x, y) if use_pygobject: status, attrs, plain_text, _ = pango.parse_markup( text, len(text), '\0') layout = pangocairo.create_layout(c) pctx = layout.get_context() fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL) pangocairo.context_set_font_options(pctx, fo) layout.set_font_description(font) layout.set_spacing(spacing * pango.SCALE) layout.set_text(plain_text, len(plain_text)) layout.set_attributes(attrs) pangocairo.update_layout(c, layout) pangocairo.show_layout(c, layout) else: # pyGtk attrs, plain_text, _ = pango.parse_markup(text) pctx = pangocairo.CairoContext(c) pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL) layout = pctx.create_layout() layout.set_font_description(font) layout.set_spacing(spacing * pango.SCALE) layout.set_text(plain_text) layout.set_attributes(attrs) pctx.update_layout(layout) pctx.show_layout(layout) c.restore()
"background=\"\\1\" style=\"", text ) text = re.sub( "style=\"\"", "", text ) text = text.strip() # First pass, find image size to hold the text. mode = { "grey" : -1, "bilevel" : cairo.ANTIALIAS_NONE, "subpixel" : cairo.ANTIALIAS_SUBPIXEL }[ args.mode ] pangocairo.cairo_font_map_get_default().set_resolution( RESOLUTION ) surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, 0, 0 ) context = pangocairo.CairoContext( cairo.Context( surface ) ) layout = context.create_layout() options = cairo.FontOptions() options.set_antialias( mode ) pangocairo.context_set_font_options( layout.get_context(), options ) layout.set_font_description( pango.FontDescription( args.font ) ) layout.set_markup( text ) width = max( layout.get_pixel_size()[ 0 ] + args.pad * 2, args.width ) height = max( layout.get_pixel_size()[ 1 ] + args.pad * 2, args.height ) # Second pass, render actual image and save it. surface = cairo.ImageSurface( cairo.FORMAT_ARGB32, width, height ) context = pangocairo.CairoContext( cairo.Context( surface ) ) layout = context.create_layout() options = cairo.FontOptions() options.set_antialias( mode ) pangocairo.context_set_font_options( layout.get_context(), options ) layout.set_font_description( pango.FontDescription( args.font ) ) layout.set_markup( text )
def __init__(self, width = 80, height = 24, scale = True): # Widget super class initialization gtk.DrawingArea.__init__(self) # By default a GTK DrawingArea is not focussable, so we # cannot get key-pressed-events. Make it work. self.set_flags(gtk.CAN_FOCUS) # Cairo surface for Immunity CANVAS logo self.logo = cairo.ImageSurface.create_from_png( os.path.join( os.path.dirname(__file__), "pixmaps/canvas_spray_trans.png" ) ) # Flag that controls terminal resizing. If set, the # height and width of the terminal will be adjusted dependent # on the allocated terminal size. self.scale_flag = scale # Initialize terminal width and height self.data = None if not self.scale_flag: self.resize(width, height) # Cursor starts at position (x, y) = (0, 0) self.cursor_pos = [0, 0] # Connect keyboard events self.connect('key-press-event', self.key_press_event) # Initialize pango environment -- we do this immediatly, # as in order to determine the space needed for this widget # we need to know the font metrics that we will be using. self.pango_ctx = self.get_pango_context() # Base direction for bidirectional fonts is left to right. self.pango_ctx.set_base_dir(pango.DIRECTION_LTR) # If necessary set up options for our font context if pangocairo.context_get_font_options(self.pango_ctx) == None: opts = cairo.FontOptions() pangocairo.context_set_font_options(self.pango_ctx, opts) # Create the pango layout object using the context self.font_info = ImmTermFontInfo(self.pango_ctx) # Set up the default font self.set_font() # Global terminal attributes. These will be used for new # text that is added to the terminal. self.attributes = ImmTermCharAttributes() # Initialize the event handler dictionary. self.event_handlers = { "newline-input-event": None, } # Input buffer used to handle newline events. We track # input events for the newline-input-event handler here. self.input_buffer = "" self.input_pos = 0
def draw(self, cr, highlight=False): try: layout = self.layout except AttributeError: layout = cr.create_layout() # set font options # see http://lists.freedesktop.org/archives/cairo/2007-February/009688.html context = layout.get_context() fo = cairo.FontOptions() fo.set_antialias(cairo.ANTIALIAS_DEFAULT) fo.set_hint_style(cairo.HINT_STYLE_NONE) fo.set_hint_metrics(cairo.HINT_METRICS_OFF) pangocairo.context_set_font_options(context, fo) # set font font = pango.FontDescription() font.set_family(self.pen.fontname) font.set_absolute_size(self.pen.fontsize * pango.SCALE) layout.set_font_description(font) # set text layout.set_text(self.t) # cache it self.layout = layout else: cr.update_layout(layout) descent = 2 # XXX get descender from font metrics width, height = layout.get_size() width = float(width) / pango.SCALE height = float(height) / pango.SCALE # we know the width that dot thinks this text should have # we do not necessarily have a font with the same metrics # scale it so that the text fits inside its box if width > self.w: f = self.w / width width = self.w # equivalent to width *= f height *= f descent *= f else: f = 1.0 if self.j == self.LEFT: x = self.x elif self.j == self.CENTER: x = self.x - 0.5 * width elif self.j == self.RIGHT: x = self.x - width else: assert 0 y = self.y - height + descent cr.move_to(x, y) cr.save() cr.scale(f, f) cr.set_source_rgba(*self.select_pen(highlight).color) cr.show_layout(layout) cr.restore() if 0: # DEBUG # show where dot thinks the text should appear cr.set_source_rgba(1, 0, 0, .9) if self.j == self.LEFT: x = self.x elif self.j == self.CENTER: x = self.x - 0.5 * self.w elif self.j == self.RIGHT: x = self.x - self.w cr.move_to(x, self.y) cr.line_to(x + self.w, self.y) cr.stroke()
errmsg = "%s\n%s" % (_("Could not create %s") % filename, msg) raise Errors.ReportError(errmsg) except: LOG.warn(sys.exc_info()[0:2]) raise Errors.ReportError(_("Could not create %s") % filename) surface.set_fallback_resolution(300, 300) cr = pangocairo.CairoContext(cairo.Context(surface)) fontmap = pangocairo.cairo_font_map_get_default() saved_resolution = fontmap.get_resolution() fontmap.set_resolution(DPI) pango_context = fontmap.create_context() options = cairo.FontOptions() options.set_hint_metrics(cairo.HINT_METRICS_OFF) pangocairo.context_set_font_options(pango_context, options) layout = pango.Layout(pango_context) cr.update_context(pango_context) # paginate the document self.paginate_document(layout, page_width, page_height, DPI, DPI) body_pages = self._pages # build the table of contents and alphabetical index toc_page = None index_page = None toc = [] index = {} for page_nr, page in enumerate(body_pages): if page.has_toc(): toc_page = page_nr
def regenerate(self): # padding is the transparent space around the notification, which # the blurred shadow will bleed into padding = self.t.value('dimensions', 'padding') if self.t.value('image', 'visible'): self.try_load_image() # dimensions of the notification width, height = self.resize_window_as_needed(padding) # dimensions of the window (e.g. including padding) win_width = 2 * padding + width win_height = 2 * padding + height # if no location was specified, do our own thing logging.debug('figuring out location') location = self.n.location if location[0] == -1 or self.t.value('location', 'force'): d = { 'screen_x': self.root['x'], 'screen_y': self.root['y'], 'screen_w': self.root['w'], 'screen_h': self.root['h'], 'win_w': win_width, 'win_h': win_height } # loc_data is [gravity, x_expr, y_expr] loc_data = (self.t.value('location', 'x'), self.t.value('location', 'y')) location = (eval(loc_data[0], d), eval(loc_data[1], d)) self.set_gravity(self.t.value('location', 'gravity')) screen = self.get_screen() self.move(location[0], location[1]) new_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, win_width, win_height) text_spacing = self.t.value('dimensions', 'text-spacing') text_offset = padding + text_spacing text_width = (width - 2 * text_spacing) if self.image != None: text_offset += self.t.value('image', 'size') + text_spacing text_width -= text_spacing text_width -= self.t.value('image', 'size') try: # base transparent window cr = pangocairo.CairoContext(cairo.Context(new_surface)) cr.set_operator(cairo.OPERATOR_SOURCE) cr.set_source_rgba(0, 0, 0, 0) cr.paint() bk = 'urgency-{0}'.format(self.n.urgency) while 'same-as' in self.t.value(bk): bk = self.t.value(bk, 'same-as') shad_w = self.t.value('shadow', 'width') # urgency-based drop shadow cr.set_source_rgba(*self.t.value(bk, 'shadow-color')) cnote.util.cairo_rounded_rect(cr, 1.0, padding - shad_w, padding - shad_w, self.t.value('dimensions', 'corner-radius'), width + 2 * shad_w, height + 2 * shad_w) cr.fill() cnote.cairo_blur.gaussian_blur(new_surface, self.t.value('shadow', 'blur')) # background cr.set_source_rgba(*self.t.value(bk, 'background-color')) cnote.util.cairo_rounded_rect(cr, 1.0, padding, padding, self.t.value('dimensions', 'corner-radius'), width, height) cr.fill() # summary cr.set_line_width(1.0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.set_source_rgba(*self.t.value(bk, 'title-text', 'color')) cr.new_path() cr.move_to(text_offset, padding + text_spacing) layout = cr.create_layout() layout.set_text(self.n.summary) layout.set_width(pango.SCALE * text_width) layout.set_font_description(self.get_font(self.t.value( bk, 'title-text', 'font'))) pangocairo.context_set_font_options(layout.get_context(), screen.get_font_options()) cr.update_layout(layout) cr.show_layout(layout) # body cr.set_source_rgba(*self.t.value(bk, 'body-text', 'color')) cr.new_path() cr.move_to(text_offset, padding + layout.get_pixel_size()[1] + int(text_spacing * 1.5)) if self.t.value('miscellaneous', 'strip-markup'): layout.set_text(cnote.util.strip_markup(self.n.body)) else: layout.set_markup(self.n.body) layout.set_width(pango.SCALE * text_width) layout.set_font_description(self.get_font(self.t.value(bk, 'body-text', 'font'))) pangocairo.context_set_font_options(layout.get_context(), screen.get_font_options()) cr.update_layout(layout) cr.show_layout(layout) # image, if available if self.image != None: cr.set_operator(cairo.OPERATOR_OVER) gdkcr = gtk.gdk.CairoContext(cr) gdkcr.set_source_pixbuf(self.image, padding + text_spacing, padding + text_spacing) gdkcr.paint_with_alpha(self.t.value('image', 'opacity')) self.surface = new_surface except Exception as ex: logging.error(ex)