Python cairo.Context() Examples
The following are 30
code examples of cairo.Context().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
cairo
, or try the search function
.
Example #1
Source File: drawing.py From xy with MIT License | 7 votes |
def render(self, scale=96/25.4, margin=10, line_width=0.5): import cairo x1, y1, x2, y2 = self.bounds width = int(scale * self.width + margin * 2) height = int(scale * self.height + margin * 2) surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height) dc = cairo.Context(surface) dc.set_line_cap(cairo.LINE_CAP_ROUND) dc.translate(margin, height - margin) dc.scale(scale, -scale) dc.translate(-x1, -y1) dc.set_line_width(line_width) dc.set_source_rgb(1, 1, 1) dc.paint() # dc.arc(0, 0, 3.0 / scale, 0, 2 * math.pi) # dc.set_source_rgb(1, 0, 0) # dc.fill() dc.set_source_rgb(0, 0, 0) for path in self.paths: dc.move_to(*path[0]) for x, y in path: dc.line_to(x, y) dc.stroke() return surface
Example #2
Source File: strokectrls.py From sk1-wx with GNU General Public License v3.0 | 7 votes |
def generate_dash_bitmap(dash=None): if not dash: dash = [] w, h = DASH_BITMAP_SIZE surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(surface) y = round(h / 2.0) ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0) ctx.set_line_width(DASH_LINE_WIDTH) cdash = [] for item in dash: cdash.append(item * DASH_LINE_WIDTH) ctx.set_dash(cdash) ctx.move_to(0, y) ctx.line_to(w, y) ctx.stroke() return wal.copy_surface_to_bitmap(surface)
Example #3
Source File: grid.py From IEEE-802.11ah-ns-3 with GNU General Public License v2.0 | 6 votes |
def layout(self, width): self.__width = width surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1) ctx = cairo.Context(surface) line_height = 0 total_height = self.__padding line_used = self.__padding for legend in self.__legends: (t_width, t_height) = ctx.text_extents(legend)[2:4] item_width = self.__padding + self.__padding + t_width + self.__padding item_height = t_height + self.__padding if item_height > line_height: line_height = item_height if line_used + item_width > self.__width: line_used = self.__padding + item_width total_height += line_height else: line_used += item_width x = line_used - item_width total_height += line_height self.__height = total_height
Example #4
Source File: prefs_ruler.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def load_font(self, font_size, color): fntdir = 'ruler-font%dpx' % font_size fntdir = os.path.join(config.resource_dir, 'fonts', fntdir) def get_colored_surface(filename, clr): filename = fsutils.get_sys_path(filename) surface = cairo.ImageSurface.create_from_png(filename) w, h = surface.get_width(), surface.get_height() res = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) cr = cairo.Context(res) cr.set_source_rgb(*clr) cr.mask_surface(surface, 0, 0) cr.fill() return w, res for char in '.,-0123456789': file_name = 'hdot.png' if char in '.,' else 'h%s.png' % char file_name = os.path.join(fntdir, file_name) self.font[char] = get_colored_surface(file_name, color)
Example #5
Source File: quarter_circles.py From generative-art with MIT License | 6 votes |
def main(filename="output.png", img_width=2000, img_height=2000, rows=10, columns=10, palette=random.choice(palettes.PALETTES), fill=True): ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, img_width, img_height) ims.set_fallback_resolution(300.0, 300.0) ctx = cairo.Context(ims) ctx.rectangle(0, 0, img_width, img_height) ctx.set_source_rgb(*palettes.hex_to_tuple(palette['background'])) ctx.fill() col_width = img_width // columns row_height = img_height // rows for r in range(rows): for c in range(columns): hex_colors = random.choices(palette['colors'], k=2) color1 = palettes.hex_to_tuple(hex_colors[0]) color2 = palettes.hex_to_tuple(hex_colors[1]) tile(ctx, c * col_width, r * row_height, col_width, row_height, color1, color2, fill) ims.write_to_png(filename)
Example #6
Source File: canvas.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def paint(self): if not self.matrix: self._fit_to_page() self.renderer.set_colorspace(self.printer.colorspace) self._keep_center() w, h = self.get_size() surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) self.ctx = cairo.Context(surface) self.ctx.set_source_rgb(*wal.wxcolor_to_dec(wal.GRAY)) self.ctx.paint() self.ctx.set_matrix(self.matrix) self._draw_page() self._render_page() self._draw_page_border() self.draw_bitmap(wal.copy_surface_to_bitmap(surface))
Example #7
Source File: ruler.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def paint(self): w, h = self.get_size() if self.surface is None or self.width != w or self.height != h: self.surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) self.width, self.height = w, h self.ctx = cairo.Context(self.surface) self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)) self.ctx.set_source_rgb(*config.ruler_bg) self.ctx.paint() self.ctx.set_antialias(cairo.ANTIALIAS_NONE) self.ctx.set_line_width(1.0) self.ctx.set_dash([]) self.ctx.set_source_rgb(*config.ruler_fg) if self.horizontal: self.hrender(w, h) else: self.vrender(w, h) self.draw_bitmap(wal.copy_surface_to_bitmap(self.surface))
Example #8
Source File: random_lines.py From generative-art with MIT License | 6 votes |
def main(filename="output.png", palettes=palettes.PALETTES, rows=15, columns=19): ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, IMG_WIDTH, IMG_HEIGHT) ims.set_fallback_resolution(300.0, 300.0) ctx = cairo.Context(ims) # Black background ctx.rectangle(0, 0, IMG_WIDTH, IMG_HEIGHT) ctx.set_source_rgb(0, 0, 0) ctx.fill() cell_width = IMG_WIDTH // columns cell_height = IMG_HEIGHT // rows for y in range(0, IMG_HEIGHT, cell_height): for x in range(0, IMG_WIDTH, cell_width): cell(ctx, x, y, cell_width, cell_height, random.choice(palettes)) ims.write_to_png(filename)
Example #9
Source File: colorctrls.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def draw_gradient(self): w, h = self.get_size() gradient = self.fill[2] surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) ctx = cairo.Context(surface) self.draw_cairo_background(ctx) if gradient[0] == sk2const.GRADIENT_LINEAR: grd = cairo.LinearGradient(0.0, h / 2.0, w, h / 2.0) else: grd = cairo.RadialGradient(w / 2.0, h / 2.0, 0, w / 2.0, h / 2.0, w / 2.0) for stop in gradient[2]: grd.add_color_stop_rgba(stop[0], *self.get_cairo_color(stop[1])) ctx.set_source(grd) ctx.rectangle(0, 0, w, h) ctx.fill() self.gc_draw_bitmap(wal.copy_surface_to_bitmap(surface), 0, 0)
Example #10
Source File: strokectrls.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def generate_arrow_bitmap(arrow=0, end=False): w, h = ARROW_BITMAP_SIZE surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(surface) y = round(h / 2.0) x = w / 2 ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0) ctx.set_line_width(ARROW_LINE_WIDTH) x = x - 15 if end else x - 15 ctx.move_to(x, y) x2 = w if end else 0 ctx.line_to(x2, y) ctx.stroke() if arrow: ctx.new_path() trafo = [-4, 0.0, 0.0, 4, x, y] if end else [4, 0.0, 0.0, 4, x, y] ctx.append_path(arrows.get_arrow_cpath(arrow-1, trafo)) ctx.fill() return wal.copy_surface_to_bitmap(surface)
Example #11
Source File: fontctrl.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def generate_fontsample_cache(fonts): w = config.font_preview_width fontsize = config.font_preview_size color = cms.val_255(config.font_preview_color) text = config.font_preview_text.decode('utf-8') for item in fonts: h = libpango.get_sample_size(text, item, fontsize)[1] if not h: h = 10 LOG.warn('Incorrect font <%s>: zero font height', item) surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w, h) ctx = cairo.Context(surface) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.paint() matrix = cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) ctx.set_matrix(matrix) ctx.set_source_rgb(1.0, 1.0, 1.0) ctx.set_antialias(cairo.ANTIALIAS_DEFAULT) libpango.render_sample(ctx, text, item, fontsize) ctx.fill() bmp = wal.copy_surface_to_bitmap(surface) FONTSAMPLE_CACHE.append(wal.invert_text_bitmap(bmp, color))
Example #12
Source File: libcairodoc.py From gprime with GNU General Public License v2.0 | 6 votes |
def draw(self, cairo_context, pango_layout, width, dpi_x, dpi_y): """Draw itself onto a cairo surface. @param cairo_context: context to draw on @param type: cairo.Context class @param pango_layout: pango layout to write on @param type: Pango.Layout class @param width: width of available space for this element @param type: device points @param dpi_x: the horizontal resolution @param type: dots per inch @param dpi_y: the vertical resolution @param type: dots per inch @return: height of the element @rtype: device points """ raise NotImplementedError
Example #13
Source File: renderer.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def cdc_draw_move_frame(self, trafo): bbox = self.presenter.selection.bbox if bbox: cpath = libcairo.convert_bbox_to_cpath(bbox) libcairo.apply_trafo(cpath, trafo) libcairo.apply_trafo(cpath, self.canvas.trafo) bbox = self.cdc_to_int(*libcairo.get_cpath_bbox(cpath)) frame = self.cdc_bbox_to_frame(bbox) if self.frame and frame == self.frame: return if not self.frame: self.frame = frame bbox2 = self.cdc_frame_to_bbox(self.frame) frame_sum = self.cdc_bbox_to_frame(libgeom.sum_bbox(bbox, bbox2)) x, y, w, h = self.cdc_normalize_rect(*frame_sum) self.frame = frame surface = cairo.ImageSurface(cairo.FORMAT_RGB24, w + 2, h + 2) ctx = cairo.Context(surface) ctx.set_source_surface(self.surface, -x + 1, -y + 1) ctx.paint() ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, -x + 1, -y + 1)) self._cdc_draw_cpath(ctx, cpath) self.canvas.dc.put_surface(surface, x - 1, y - 1, False) self.cdc_reflect_snapping()
Example #14
Source File: ruler.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def load_font(color=(0, 0, 0)): fntdir = 'ruler-font%dpx' % config.ruler_font_size fntdir = os.path.join(config.resource_dir, 'fonts', fntdir) def get_colored_surface(file_name, color, vertical=False): file_name = fsutils.get_sys_path(file_name) surface = cairo.ImageSurface.create_from_png(file_name) w, h = surface.get_width(), surface.get_height() res = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) cr = cairo.Context(res) cr.set_source_rgb(*color) cr.mask_surface(surface, 0, 0) cr.fill() return h if vertical else w, res for char in '.,-0123456789': file_name = 'hdot.png' if char in '.,' else 'h%s.png' % char file_name = os.path.join(fntdir, file_name) HFONT[char] = get_colored_surface(file_name, color) file_name = 'vdot.png' if char in '.,' else 'v%s.png' % char file_name = os.path.join(fntdir, file_name) VFONT[char] = get_colored_surface(file_name, color, True)
Example #15
Source File: ruler.py From sk1-wx with GNU General Public License v3.0 | 6 votes |
def paint(self): if self.presenter is None: return w, h = self.dc.get_size() fmt = cairo.FORMAT_RGB24 if self.surface is None or self.width != w or self.height != h: self.surface = cairo.ImageSurface(fmt, w, h) self.width, self.height = w, h self.ctx = cairo.Context(self.surface) self.ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.0)) self.ctx.set_source_rgb(*config.ruler_bg) self.ctx.paint() self.ctx.set_antialias(cairo.ANTIALIAS_NONE) self.ctx.set_line_width(1.0) self.ctx.set_dash([]) self.ctx.set_source_rgb(*config.ruler_fg) if self.vertical: self.vrender(w, h) else: self.hrender(w, h) self.dc.draw_surface(self.surface, 0, 0)
Example #16
Source File: grid.py From ns3-rdma with GNU General Public License v2.0 | 6 votes |
def layout(self, width): self.__width = width surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1) ctx = cairo.Context(surface) line_height = 0 total_height = self.__padding line_used = self.__padding for legend in self.__legends: (t_width, t_height) = ctx.text_extents(legend)[2:4] item_width = self.__padding + self.__padding + t_width + self.__padding item_height = t_height + self.__padding if item_height > line_height: line_height = item_height if line_used + item_width > self.__width: line_used = self.__padding + item_width total_height += line_height else: line_used += item_width x = line_used - item_width total_height += line_height self.__height = total_height
Example #17
Source File: grid.py From ns3-load-balance with GNU General Public License v2.0 | 6 votes |
def layout(self, width): self.__width = width surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1) ctx = cairo.Context(surface) line_height = 0 total_height = self.__padding line_used = self.__padding for legend in self.__legends: (t_width, t_height) = ctx.text_extents(legend)[2:4] item_width = self.__padding + self.__padding + t_width + self.__padding item_height = t_height + self.__padding if item_height > line_height: line_height = item_height if line_used + item_width > self.__width: line_used = self.__padding + item_width total_height += line_height else: line_used += item_width x = line_used - item_width total_height += line_height self.__height = total_height
Example #18
Source File: thumbshot.py From epoptes with GNU General Public License v3.0 | 6 votes |
def thumbshot(width, height): """Return a thumbshot of the current screen as bytes.""" root = Gdk.get_default_root_window() if root is None: raise RuntimeError('Cannot find the root window, is xorg running?') geometry = root.get_geometry() surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height) ctx = cairo.Context(surface) # TODO: check if this actually does client-size resizing ctx.scale(float(width) / geometry.width, float(height) / geometry.height) Gdk.cairo_set_source_window(ctx, root, 0, 0) ctx.paint() # TODO: is a pixbuf necessary, or can we get the bytes from the surface? pixbuf = Gdk.pixbuf_get_from_surface(surface, 0, 0, width, height) rowst = pixbuf.get_rowstride() pixels = pixbuf.get_pixels() return (b"%i\n%ix%i\n" % (rowst, width, height) + pixels # TODO: the last padding isn't included, so do it manually + b"\0"*(rowst*height - len(pixels)))
Example #19
Source File: grid.py From ntu-dsi-dcn with GNU General Public License v2.0 | 6 votes |
def layout(self, width): self.__width = width surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1) ctx = cairo.Context(surface) line_height = 0 total_height = self.__padding line_used = self.__padding for legend in self.__legends: (t_width, t_height) = ctx.text_extents(legend)[2:4] item_width = self.__padding + self.__padding + t_width + self.__padding item_height = t_height + self.__padding if item_height > line_height: line_height = item_height if line_used + item_width > self.__width: line_used = self.__padding + item_width total_height += line_height else: line_used += item_width x = line_used - item_width total_height += line_height self.__height = total_height
Example #20
Source File: grid.py From 802.11ah-ns3 with GNU General Public License v2.0 | 6 votes |
def layout(self, width): self.__width = width surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1) ctx = cairo.Context(surface) line_height = 0 total_height = self.__padding line_used = self.__padding for legend in self.__legends: (t_width, t_height) = ctx.text_extents(legend)[2:4] item_width = self.__padding + self.__padding + t_width + self.__padding item_height = t_height + self.__padding if item_height > line_height: line_height = item_height if line_used + item_width > self.__width: line_used = self.__padding + item_width total_height += line_height else: line_used += item_width x = line_used - item_width total_height += line_height self.__height = total_height
Example #21
Source File: un_deux_trois.py From generative-art with MIT License | 6 votes |
def main(filename="output.png", palette=random.choice(palettes.PALETTES), num_columns=12, num_rows=12, num_steps=[1, 2, 3], line_width=4): ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, IMG_WIDTH, IMG_HEIGHT) ims.set_fallback_resolution(300.0, 300.0) ctx = cairo.Context(ims) # Background ctx.rectangle(0, 0, IMG_WIDTH, IMG_HEIGHT) ctx.set_source_rgb(*palettes.hex_to_tuple(palette['background'])) ctx.fill() column_size = IMG_WIDTH // num_columns row_size = IMG_HEIGHT // num_rows for c in range(num_columns): for r in range(num_rows): if r < num_rows // 3: current_num_steps = num_steps[0] elif r < 2 * num_rows // 3: current_num_steps = num_steps[1] else: current_num_steps = num_steps[2] #p = random.choice(palettes.PALETTES) lines(ctx, palette['colors'], c * column_size, r * row_size, column_size, row_size, current_num_steps, line_width) ims.write_to_png(filename)
Example #22
Source File: grid.py From ns3-load-balance with GNU General Public License v2.0 | 5 votes |
def layout(self, width): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 1, 1) ctx = cairo.Context(surface) # calculate scale delta data_delta = self.__hi - self.__lo closest = 1 while (closest*10) < data_delta: closest *= 10 if (data_delta / closest) == 0: delta = closest elif(data_delta / closest) == 1: delta = closest / 10 else: delta = closest start = self.__lo - (self.__lo % delta) + delta end = self.__hi - (self.__hi % delta) self.__delta = delta self.__width = width # calculate text height max_text_height = ctx.text_extents("ABCDEFGHIJKLMNOPQRSTUVWXYZabcedefghijklmnopqrstuvwxyz0123456789")[3] self.max_text_height = max_text_height height = max_text_height + 10 self.__height = height
Example #23
Source File: ft.py From addons-source with GNU General Public License v2.0 | 5 votes |
def draw_file(p, fn, writer): global ctx surface = writer.start(fn, 10, 10) ctx = cairo.Context(surface) draw_tree(p) (w, h) = (p.get('w'), p.get('h')) surface = writer.start(fn, w, h) ctx = cairo.Context(surface) draw_tree(p) ctx.show_page() writer.finish()
Example #24
Source File: backend_cairo.py From matplotlib-4-abaqus with MIT License | 5 votes |
def set_ctx_from_surface (self, surface): self.gc.ctx = cairo.Context (surface)
Example #25
Source File: hyphae.py From hyphae with MIT License | 5 votes |
def __init__(self,n): sur = cairo.ImageSurface(cairo.FORMAT_ARGB32,n,n) ctx = cairo.Context(sur) ctx.scale(n,n) ctx.set_source_rgb(BACK,BACK,BACK) ctx.rectangle(0,0,1,1) ctx.fill() self.sur = sur self.ctx = ctx self.colors = ((0,0,0)) self.ncolors = 1
Example #26
Source File: main.py From pywonderland with MIT License | 5 votes |
def main(hexagon_size, imgsize): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, imgsize, imgsize) ctx = cairo.Context(surface) # we will put the center of the hexagon at the origin a, b, c = hexagon_size ctx.translate(imgsize / 2.0, imgsize / 2.0) extent = max(c, a * HALFSQRT3, b * HALFSQRT3) + 1 ctx.scale(imgsize / (extent * 2.0), -imgsize / (extent * 2.0)) ctx.translate(-b * HALFSQRT3, -c / 2.0) # paint background ctx.set_source_rgb(1, 1, 1) ctx.paint() ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.set_line_join(cairo.LINE_JOIN_ROUND) T = LozengeTiling(hexagon_size) sample = run_cftp(T) for key, val in T.get_tiles(sample).items(): for verts in val: A, B, C, D = square_to_hex(verts) ctx.move_to(A[0], A[1]) ctx.line_to(B[0], B[1]) ctx.line_to(C[0], C[1]) ctx.line_to(D[0], D[1]) ctx.close_path() if key == "T": ctx.set_source_rgb(*TOP_COLOR) elif key == "L": ctx.set_source_rgb(*LEFT_COLOR) else: ctx.set_source_rgb(*RIGHT_COLOR) ctx.fill_preserve() ctx.set_line_width(LINE_WIDTH) ctx.set_source_rgb(*EDGE_COLOR) ctx.stroke() surface.write_to_png("random_lozenge_tiling.png")
Example #27
Source File: fractaltree.py From pywonderland with MIT License | 5 votes |
def main(): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context(surface) ctx.set_line_cap(cairo.LINE_CAP_ROUND) ctx.set_line_join(cairo.LINE_JOIN_ROUND) ctx.set_source_rgb(1, 1, 1) ctx.paint() fractal_tree(ctx, ITERATIONS, ROOT, TRUNK_LEN, RATIO, THETA, ANGLE, PERTURB) surface.write_to_png("random_fractal_tree.png")
Example #28
Source File: backend_cairo.py From matplotlib-4-abaqus with MIT License | 5 votes |
def __init__(self, dpi): """ """ if _debug: print('%s.%s()' % (self.__class__.__name__, _fn_name())) self.dpi = dpi self.gc = GraphicsContextCairo (renderer=self) self.text_ctx = cairo.Context ( cairo.ImageSurface (cairo.FORMAT_ARGB32,1,1)) self.mathtext_parser = MathTextParser('Cairo') RendererBase.__init__(self)
Example #29
Source File: grid.py From ns3-load-balance with GNU General Public License v2.0 | 5 votes |
def output_png(self, filename): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, self.__data.get_width(), self.__data.get_height()) ctx = cairo.Context(self.__buffer_surface) self.__data.draw(ctx) surface.write_to_png(filename)
Example #30
Source File: DescendantsLines.py From addons-source with GNU General Public License v2.0 | 5 votes |
def draw_file(p, fn, writer, title, footer, header_coeff): """ called by write_report to generate the chart Uses the tree of person & family records, "p", created by load_gramps """ global ctx # 1st pass is just to get size of chart surface = writer.start(None, 10, 10) ctx = cairo.Context(surface) draw_tree(p) (w, h) = (p.get('w'), p.get('h')) log.debug('### End of 1st Pass. Surface w=%d, h=%d', w, h) surface = writer.start(fn, w + MARGIN_LEFT + MARGIN_RIGHT, h + MARGIN_HEADER + MARGIN_FOOTER) ctx = cairo.Context(surface) draw_header_footer(w / 2, MARGIN_HEADER / 2, title, header_coeff) draw_tree(p) draw_header_footer(w / 2, h + MARGIN_HEADER + MARGIN_FOOTER / 2, footer, header_coeff) ctx.show_page() writer.finish() #------------------------------------------------------------------------ # # DescendantsLines # #------------------------------------------------------------------------