def new_render_layer(self, color=None, mirror=False): size_in_pixels = self.scale_point(self.size_in_inch) m = self._xform_matrix matrix = cairo.Matrix(m.xx, m.yx, m.xy, m.yy, m.x0, m.y0) layer = cairo.SVGSurface(None, size_in_pixels[0], size_in_pixels[1]) ctx = cairo.Context(layer) if self.invert: ctx.set_source_rgba(0.0, 0.0, 0.0, 1.0) ctx.set_operator(cairo.OPERATOR_OVER) ctx.paint() if mirror: matrix.xx = -1.0 matrix.x0 = self.origin_in_pixels[0] + self.size_in_pixels[0] self.ctx = ctx self.ctx.set_matrix(matrix) self.active_layer = layer self.active_matrix = matrix
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))
def paint(self): imgpat = cairo.SurfacePattern(self.img) scaler = cairo.Matrix() x, y, w, h = self.get_allocation() width = float(self.img.get_width()) height = float(self.img.get_height()) ws = float(width / float(w)) hs = float(height / float(h)) #1 = 100% ; 2 = 50% ; 0.5 = 200% scaler.scale(ws, hs) imgpat.set_matrix(scaler) imgpat.set_filter(cairo.FILTER_BEST) cr = self.window.cairo_create() cr.set_source(imgpat) cr.paint()
def ext_joinarc_to(ctx, a, b, c): prev_mx = ctx.get_matrix() mx_xx = b.x - c.x mx_yx = b.y - c.y mx = cairo.Matrix(xx=mx_xx, yx=mx_yx, x0=a.x - mx_xx, xy=b.x - a.x, yy=b.y - a.y, y0=a.y - mx_yx) ctx.transform(mx) ctx.arc( 0.0, 0.0, 1.0, # center x and y, radius. 0.0, 0.5 * math.pi) # angle begin and end. ctx.set_matrix(prev_mx)
def __init__(self, canvas=None): self._matrix = cairo.Matrix() self._painter = DefaultPainter(self) self._bounding_box_painter = BoundingBoxPainter(ItemPainter(self), self) # Handling selections. # TODO: Move this to a context? self._selected_items = set() self._focused_item = None self._hovered_item = None self._dropzone_item = None self._qtree = Quadtree() self._bounds = Rectangle(0, 0, 0, 0) self._canvas = None if canvas: self._set_canvas(canvas)
def _gen_cairo_context(self): # 利用cairo产生画布 surface = cairo.ImageSurface.create_for_data( self._frame_array, cairo.FORMAT_ARGB32, self._frame_width, self._frame_height ) ctx = cairo.Context(surface) ctx.scale(self._frame_width, self._frame_height) ctx.transform( cairo.Matrix( self._frame_height / self._frame_width * (1.0 / self._scale), 0, 0, -1.0 / self._scale, 0.5, 0.5, ) ) return ctx
def generate_preview(presenter, renderer_cls, size=(100, 100), transparent=False, img_format='PNG', encoded=True): wp, hp = size surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(wp), int(hp)) ctx = cairo.Context(surface) if not transparent: ctx.set_source_rgb(1.0, 1.0, 1.0) ctx.paint() # ---rendering mthds = presenter.methods layers = mthds.get_visible_layers(mthds.get_page()) bbox = mthds.count_bbox(layers) if bbox: x, y, x1, y1 = bbox w = abs(x1 - x) or 1.0 h = abs(y1 - y) or 1.0 coef = min(wp / w, hp / h) * 0.99 trafo0 = [1.0, 0.0, 0.0, 1.0, -x - w / 2.0, -y - h / 2.0] trafo1 = [coef, 0.0, 0.0, -coef, 0.0, 0.0] trafo2 = [1.0, 0.0, 0.0, 1.0, wp / 2.0, hp / 2.0] trafo = libgeom.multiply_trafo(trafo0, trafo1) trafo = libgeom.multiply_trafo(trafo, trafo2) ctx.set_matrix(cairo.Matrix(*trafo)) rend = renderer_cls(presenter.cms) rend.antialias_flag = True for item in layers: rend.render(ctx, item.childs) # ---rendering image_stream = StringIO() surface.write_to_png(image_stream) if not img_format == 'PNG': image_stream.seek(0, 0) image = Image.open(image_stream) image.load() image_stream = StringIO() image.save(image_stream, format=img_format) image_str = image_stream.getvalue() return b64encode(image_str) if encoded else image_str
def _make_icon_frame(icon_surface, art_size=None, scale=1, default_icon=False): icon_w = icon_surface.get_width() icon_h = icon_surface.get_height() ratio = icon_h / icon_w # Scale down the image according to the biggest axis if ratio > 1: w = int(art_size.width / ratio) h = art_size.height else: w = art_size.width h = int(art_size.height * ratio) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w * scale, h * scale) surface.set_device_scale(scale, scale) ctx = cairo.Context(surface) matrix = cairo.Matrix() line_width = 0.6 ctx.new_sub_path() ctx.arc(w / 2, h / 2, (w / 2) - line_width, 0, 2 * pi) ctx.set_source_rgba(0, 0, 0, 0.7) ctx.set_line_width(line_width) ctx.stroke_preserve() if default_icon: ctx.set_source_rgb(1, 1, 1) ctx.fill() ctx.set_source_rgba(0, 0, 0, 0.3) ctx.mask_surface(icon_surface, w / 3, h / 3) ctx.fill() else: matrix.scale(icon_w / (w * scale), icon_h / (h * scale)) ctx.set_source_surface(icon_surface, 0, 0) pattern = ctx.get_source() pattern.set_matrix(matrix) ctx.fill() ctx.arc(w / 2, h / 2, w / 2, 0, 2 * pi) ctx.clip() return surface
def get_cairo_context(self, pixel_array): """Returns the cairo context for a pixel array after caching it to self.pixel_array_to_cairo_context If that array has already been cached, it returns the cached version instead. Parameters ---------- pixel_array : np.array The Pixel array to get the cairo context of. Returns ------- cairo.Context The cairo context of the pixel array. """ cached_ctx = self.get_cached_cairo_context(pixel_array) if cached_ctx: return cached_ctx pw = self.pixel_width ph = self.pixel_height fw = self.frame_width fh = self.frame_height fc = self.frame_center surface = cairo.ImageSurface.create_for_data( pixel_array, cairo.FORMAT_ARGB32, pw, ph, ) ctx = cairo.Context(surface) ctx.scale(pw, ph) ctx.set_matrix( cairo.Matrix( (pw / fw), 0, 0, -(ph / fh), (pw / 2) - fc[0] * (pw / fw), (ph / 2) + fc[1] * (ph / fh), ), ) self.cache_cairo_context(pixel_array, ctx) return ctx
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_ARGB32, w, h) ctx = cairo.Context(surface) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.set_matrix(cairo.Matrix(1.0, 0.0, 0.0, 1.0, 0.0, 0.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(bmp)
def on_print_page(self, page): page_obj = self.get_print_pages()[page - 1] dc = self.GetDC() w, h = dc.GetSizeTuple() pw, ph = self.GetPageSizeMM() pw *= uc2const.mm_to_pt ph *= uc2const.mm_to_pt trafo = (w / pw, 0, 0, -h / ph, w / 2.0 - self.shifts[0], h / 2.0 - self.shifts[1]) matrix = cairo.Matrix(*trafo) surface = cairo.Win32PrintingSurface(dc.GetHDC()) ctx = cairo.Context(surface) ctx.set_matrix(matrix) for group in page_obj.childs: self.renderer.render(ctx, group.childs)
def OnPaint(self, event): # # gSVG = rsvg.Handle(file=sys.argv[1]) # # gDimensionData = gSVG.get_dimension_data() scaledSize = (self.gDimensionData[0] * SCALE, self.gDimensionData[1] * SCALE) surface = cairo.SVGSurface(None, scaledSize[0], scaledSize[1]) ctx = cairo.Context(surface) if self.gSVG != None: matrix = cairo.Matrix(xx=SCALE, yx=0, xy=0, yy=SCALE, x0=0, y0=0) ctx.transform(matrix) self.gSVG.render_cairo(ctx) surface.write_to_png(gTmpPngPath) surface.finish() svgBmp = wx.Bitmap(gTmpPngPath, wx.BITMAP_TYPE_PNG) dc = wx.PaintDC(self) dc.DrawBitmap(svgBmp, 0, 0)
def get_matrix(self, segment_index): xmin, xmax = self.samples.get_x_min_max() ymin, ymax = self.samples.get_y_min_max() ydiff = ymax - ymin segments = self.samples.get_segment_count() matrix = cairo.Matrix() widget_height = 1.0 * self.drawing_area.get_allocated_height() widget_width = 1.0 * self.drawing_area.get_allocated_width() matrix.scale(float(widget_width) / (xmax - xmin), widget_height) matrix.scale(self.x_mult, self.y_mult) matrix.translate(-self.x_shift, -self.y_shift) if segment_index >= 0: ybase = self.samples.get_y_base() matrix.scale(1, 1 / (ydiff * segments)) matrix.translate(0, ymax - ybase + segment_index * ydiff) matrix.scale(1, -1) return matrix
def on_drag(self, sprite, event): matrix = cairo.Matrix() # the pivot point change causes the sprite to be at different location after # rotation so we are compensating that # this is bit lame as i could not figure out how to properly # transform the matrix so that it would give me back the new delta matrix.translate(self.x + self.rotator.x, self.y + self.rotator.y) matrix.rotate(self.rotation) matrix.translate(-self.rotator.x, -self.rotator.y) new_x, new_y = matrix.transform_point(0,0) prev_x, prev_y = self.get_matrix().transform_point(0,0) self.x -= new_x - prev_x self.y -= new_y - prev_y # setting the pivot point self.pivot_x, self.pivot_y = self.rotator.x, self.rotator.y
def fset(self, path): self._graphic_path = path if path: self._graphic_surface = cairo.ImageSurface.create_from_png( self._graphic_path) self._graphic_pattern = cairo.SurfacePattern( self._graphic_surface) self._graphic_scaler = cairo.Matrix() self._graphic_pattern.set_filter(cairo.FILTER_BEST) self._graphic_dimension = max( self._graphic_surface.get_width(), self._graphic_surface.get_height()) self._scale = 1.0 else: self._graphic_surface = None self._graphic_pattern = None self._graphic_scaler = None self._graphic_dimension = 0 self._scale = 0.0
def addPanel(ctx, label, filename, x, y, w, h, config, panelConfig): ctx.save() #ctx.new_path() im1 = cairo.ImageSurface.create_from_png(file(filename, 'r')) # Set origin for this layer ctx.translate(x, y) imgpat = cairo.SurfacePattern(im1) imh = im1.get_height() imw = im1.get_width() scale_w = imw / w scale_h = imh / h compromise_scale = max(scale_w, scale_h) # Scale source image scaler = cairo.Matrix() scaler.scale(compromise_scale, compromise_scale) imgpat.set_matrix(scaler) imgpat.set_filter(cairo.FILTER_BEST) #ctx.set_source_surface(im1, 0, 0) # draw image at specific position (in context-relative units) ctx.set_source(imgpat) #ctx.move_to(245, 245) ctx.rectangle(0, 0, w, h) #ctx.scale(0.2, 0.2) ctx.fill() #ctx.paint() #ctx.save() ctx.restore() ctx.save() ctx.translate(x, y - 0.02) ctx.set_source_rgb(0.0, 0.0, 0.0) ctx.select_font_face(annotationsFont) ctx.set_font_size(50) ctx.show_text(label) ctx.restore()
def set_cursor(self): pointer, x, y = self.pointer.get_position() w, h = self.size self.pixbuffer = Gdk.pixbuf_get_from_window( Gdk.get_default_root_window(), x - (w // 2), y - (h // 2), w, h) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h) ctx = cairo.Context(surface) trafo = (self.zoom, 0, 0, self.zoom, -(self.zoom - 1) * w // 2, -(self.zoom - 1) * h // 2) ctx.set_matrix(cairo.Matrix(*trafo)) Gdk.cairo_set_source_pixbuf(ctx, self.pixbuffer, 0, 0) ctx.get_source().set_filter(cairo.FILTER_NEAREST) ctx.paint() ctx.set_antialias(cairo.ANTIALIAS_NONE) ctx.set_matrix(NORMAL_TRAFO) ctx.set_source_rgb(0, 0, 0) ctx.set_line_width(1) ctx.rectangle(1, 1, w - 1, h - 1) ctx.stroke() size = 10 x0, y0 = w // 2 + 1, h // 2 + 1 ctx.rectangle(x0 - size / 2, y0 - size / 2, size, size) ctx.set_source_rgb(1, 0, 0) ctx.stroke() ctx.rectangle(x0 - size / 2 - 1, y0 - size / 2 - 1, size + 2, size + 2) ctx.set_source_rgb(1, 1, 1) ctx.stroke() cursor_pixbuffer = Gdk.pixbuf_get_from_surface(surface, 0, 0, w, h) cursor = Gdk.Cursor.new_from_pixbuf(self.w.get_screen().get_display(), cursor_pixbuffer, w // 2, h // 2) self.pointer.grab( self.w.get_window(), Gdk.GrabOwnership.APPLICATION, True, (Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.SCROLL_MASK), cursor, Gdk.CURRENT_TIME)
def get_glyphs(ctx, layout_data, text, width, text_style, markup): glyphs = [] i = -1 for item in text: i += 1 if item in NONPRINTING_CHARS: res = False if markup and not item == '\n': for mrk in markup: if 'u' in mrk[0] or 's' in mrk[0]: if i >= mrk[1][0] and i < mrk[1][1]: res = True break if not res: glyphs.append(None) continue ctx.new_path() ctx.move_to(0, 0) layout = core.create_layout(ctx) text_range = [i, i + len(item)] vpos = core.set_glyph_layout(item, width, text_style, markup, text_range, True, layout) if vpos: for index in range(*text_range): x, y, w, h, base_line, byte_index = layout_data[index] dh = (y - base_line) * vpos layout_data[index] = (x, y + dh, w, h, base_line + dh, byte_index) core.layout_path(ctx, layout) cpath = ctx.copy_path() m00 = 1.0 m11 = -1.0 if os.name == 'nt': m00 *= 0.1 m11 *= 0.1 matrix = cairo.Matrix(m00, 0.0, 0.0, m11, layout_data[i][0], layout_data[i][1]) libcairo.apply_cmatrix(cpath, matrix) glyphs.append(cpath) return glyphs
def _finishWaveform(self): surfaces = [] surface = cairo.ImageSurface(cairo.FORMAT_A8, self.base_width, self.theight) cr = cairo.Context(surface) self._plotWaveform(cr, self.base_width) self.audioSink.reset() for width in [25, 100, 200]: scaled = cairo.ImageSurface(cairo.FORMAT_A8, width, self.theight) cr = cairo.Context(scaled) matrix = cairo.Matrix() matrix.scale(self.base_width / width, 1.0) cr.set_source_surface(surface) cr.get_source().set_matrix(matrix) cr.rectangle(0, 0, width, self.theight) cr.fill() surfaces.append(scaled) surfaces.append(surface) gobject.idle_add(self._finishThumbnail, surfaces, self._audio_cur)
def drawclt(clt, name): """Create a pdf file 'name'.pdf in the subfolder 'examples' with a pictographic representation of the CLT 'clt'. This function is not used anywhere in the code and kept for debugging purposes only. """ scale = 100 w = max(clt.top, clt.bot) h = max([1]+[abs(i[1]-i[0]) for i in clt.arcs_top()]+\ [abs(i[1]-i[0]) for i in clt.arcs_bot()]+\ [abs(i[1]-i[0]-clt.top) for i in clt.arcs_mix()]) surface = cairo.PDFSurface("examples/" + name + '.pdf', w * scale, (h + 1) * scale) ctx = cairo.Context(surface) matrix = cairo.Matrix(scale, 0, 0, scale, 0.5 * scale, 0.5 * scale) ctx.set_matrix(matrix) ctx.set_line_cap(cairo.LINE_CAP_ROUND) # Drawing code draw_tangle_ends(0, 0, clt, h, ctx) draw_arcs(0, 0, clt, h, ctx, (0, 0, 0))
def on_draw(self, wd, cr: cairo.Context): cr.identity_matrix() mat = cairo.Matrix(self.width, 0, 0, -self.height, 0, self.height) cr.set_matrix(mat) # Background cr.rectangle(0, 0, 1, 1) cr.set_source_rgb(*hex2rgb(colors[-1])) cr.fill() # Set line colors cr.set_line_width(5e-3) self.x, self.y = 0, 0.05 if self.delta is None: cr.set_source_rgb(*self.line_color) for i, c in zip(linspace(1, 0, 4), colors[:4]): self.x, self.y = 0, 0.05 cr.set_source_rgba(*hex2rgb(c)) self.turtle_draw(cr, delta=i) else: self.movie(cr)
def png_saver(sk2_doc, filename=None, fileptr=None, translate=True, cnf=None, **kw): cnf = merge_cnf(cnf, kw) if filename and not fileptr: fileptr = get_fileptr(filename, True) page = sk2_doc.methods.get_page() w, h = page.page_format[1] trafo = (1.0, 0, 0, -1.0, w / 2.0, h / 2.0) if 'scale-png-output' in cnf.keys(): scale = float(cnf['scale-png-output']) if scale > 0: w = int(w * scale) h = int(h * scale) trafo = (scale, 0, 0, -scale, w / 2.0, h / 2.0) canvas_matrix = cairo.Matrix(*trafo) surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(w), int(h)) ctx = cairo.Context(surface) ctx.set_matrix(canvas_matrix) rend = CairoRenderer(sk2_doc.cms) antialias_flag = True if 'antialiasing' in cnf.keys(): if not cnf['antialiasing'] in ('True', '1'): antialias_flag = False rend.antialias_flag = antialias_flag layers = sk2_doc.methods.get_visible_layers(page) for item in layers: if not item.properties[3] and antialias_flag: rend.antialias_flag = False rend.render(ctx, item.childs) if not item.properties[3] and antialias_flag: rend.antialias_flag = True surface.write_to_png(fileptr) fileptr.close()
def resize(file_in, file_out, width=1189, height=1682): document = Poppler.Document.new_from_file('file://' + file_in, None) number_of_pages = document.get_n_pages() width = float(width) height = float(height) horizontal = (width > height) pdfsurface = cairo.PDFSurface(file_out, width, height) context = cairo.Context(pdfsurface) for i in range(0, number_of_pages): current_page = document.get_page(i) widthi, heighti = current_page.get_size() horizontali = (widthi > heighti) if horizontal != horizontali: sw = width / heighti sh = height / widthi if sw < sh: scale = sw else: scale = sh context.save() mtr = cairo.Matrix() mtr.rotate(ROTATE_270 / 180.0 * math.pi) context.transform(mtr) context.scale(scale, scale) context.translate(-widthi, 0.0) current_page.render(context) context.restore() else: sw = width / widthi sh = height / heighti if sw < sh: scale = sw else: scale = sh context.save() context.scale(scale, scale) current_page.render(context) context.restore() context.show_page() pdfsurface.flush() pdfsurface.finish()
def StrokePath(ctx, page, i): eonum = page.curfg ctx.save() if eonum == 0x80000008 or page.mfobjs[eonum].style == 5: ctx.set_line_width(0) ctx.stroke() else: eo = page.mfobjs[eonum] style = eo.style & 0xF if eo.width < 2.1 or (style > 0 and style < 5): eo.width = 1. / (page.scale * page.zoom) matrix = cairo.Matrix(1. / page.width, 0, 0, 1. / page.height, 0, 0) ctx.transform(matrix) ctx.set_line_width(eo.width) lcap, ljoin = 0, 0 if (eo.style & 0xF00) >> 8 < 3: lcap = capjoin[(eo.style & 0xF00) >> 8] if (eo.style & 0xF000) >> 12 < 3: ljoin = capjoin[(eo.style & 0xF000) >> 12] ctx.set_line_cap(lcap) ctx.set_line_join(ljoin) if style > 5: style = 0 if style > 0 and page.bkmode == 2: ctx.set_dash(dashes[0], len(dashes[0]) / 2) r = page.bkcolor.r g = page.bkcolor.g b = page.bkcolor.b ctx.set_source_rgba(r / 255., g / 255., b / 255., 1) ctx.stroke_preserve() ctx.set_dash(dashes[style], len(dashes[style]) / 2) if eo.flag != 1: r, g, b = eo.clr.r, eo.clr.g, eo.clr.b else: clr = page.palette[ eo.clr.r] ## FIXME! have to be 2 byte made of R and G. r, g, b = ord(clr.r), ord(clr.g), ord(clr.b) ctx.set_source_rgba(r / 255., g / 255., b / 255., 1.) ctx.stroke() ctx.restore()
def get_rtl_glyphs(ctx, layout_data, log_layout_data, byte_dict, rtl_regs, text, width, text_style, markup): glyphs = [] for item in layout_data: try: index = byte_dict[item[5]] txt = text[index] if is_item_in_rtl(index, rtl_regs): text_range = [index - len(txt) + 1, index + 1] else: text_range = [index, index + len(txt)] except: continue if txt in NONPRINTING_CHARS: glyphs.append(None) continue ctx.new_path() ctx.move_to(0, 0) layout = core.create_layout(ctx) vpos = core.set_glyph_layout(txt, width, text_style, markup, text_range, True, layout) if vpos: for index in range(*text_range): x, y, w, h, base_line, byte_index = log_layout_data[index] dh = (y - base_line) * vpos log_layout_data[index] = (x, y + dh, w, h, base_line + dh, byte_index) core.layout_path(ctx, layout) cpath = ctx.copy_path() m00 = 1.0 m11 = -1.0 if os.name == 'nt': m00 *= 0.1 m11 *= 0.1 matrix = cairo.Matrix(m00, 0.0, 0.0, m11, item[0], item[1]) libcairo.apply_cmatrix(cpath, matrix) glyphs.append(cpath) return glyphs
def paint(self): w, h = self.get_size() shift = 0 if wal.is_msw(): shift = 1 fmt = cairo.FORMAT_RGB24 self.surface = cairo.ImageSurface(fmt, w - shift, h - shift) 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(*self.prefs.bg_btn.get_value()) 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(*self.prefs.fg_btn.get_value()) self.ctx.move_to(0, h) self.ctx.line_to(w, h) small_l = self.prefs.ruler_small_tick.get_value() for item in SMALL_TICKS: self.ctx.move_to(item, h - small_l) self.ctx.line_to(item, h - 1) large_l = self.prefs.ruler_large_tick.get_value() for pos, txt in TEXT_TICKS: self.ctx.move_to(pos, h - large_l) self.ctx.line_to(pos, h - 1) self.ctx.stroke() vshift = self.prefs.ruler_text_vshift.get_value() hshift = self.prefs.ruler_text_hshift.get_value() for pos, txt in TEXT_TICKS: for character in txt: data = self.font[character] position = int(pos) + hshift self.ctx.set_source_surface(data[1], position, vshift) self.ctx.paint() pos += data[0] self.draw_surface(self.surface)
def StretchDIBits(ctx, page, i): ctx.save() matrix = cairo.Matrix(1. / page.width, 0, 0, 1. / page.height, 0, 0) ctx.transform(matrix) y = page.cmds[i].args[0] ##xDest x = page.cmds[i].args[1] ##yDest x, y = convcoords(page, ctx, x, y) dx = page.cmds[i].args[2] ##cxSrc dy = page.cmds[i].args[3] ##cySrc bmpsize = struct.pack('<I', page.cmds[i].args[4]) ##nSize-header+14 bmpshift = struct.pack( '<I', page.cmds[i].args[5] ) ##calculated shift to the bitmap data (stored after palette, if any palette exists) dxdst = page.cmds[i].args[6] ##cxDest dydst = page.cmds[i].args[7] ##cyDest dwROP = page.cmds[i].args[8] ##dwRop bmpdata = page.cmds[i].args[9] ##data bmp = '\x42\x4d' + bmpsize + '\x00\x00\x00\x00' + bmpshift + bmpdata pixbufloader = gtk.gdk.PixbufLoader() pixbufloader.write(bmp) pixbufloader.close() pixbuf = pixbufloader.get_pixbuf() ctx.save() ctx.translate(x, y) ctx.scale(dxdst * 1. / dx, dydst * 1. / dy) if dwROP == 0xee0086: ## SRCPAINT cssize = max(abs(dx), abs(dy)) cs = cairo.ImageSurface(0, cssize, cssize) ct = cairo.Context(cs) ct2 = gtk.gdk.CairoContext(ct) ct2.set_source_pixbuf(pixbuf, 0, 0) ct2.paint() page.mask = cs if dwROP == 0x8800c6: ##SRCAND ctx.set_source_pixbuf(pixbuf, 0, 0) ctx.mask_surface(page.mask) if dwROP == 0xcc0020: ## SRCCOPY ctx.set_source_pixbuf(pixbuf, 0, 0) ctx.paint() ctx.restore() ctx.restore()
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)
def set_context_size(self, width, height, viewbox): """Set the Cairo context size, set the SVG viewport size.""" if viewbox: x, y, x_size, y_size = viewbox self.context_width, self.context_height = x_size, y_size x_ratio, y_ratio = width / x_size, height / y_size matrix = cairo.Matrix() if x_ratio > y_ratio: matrix.translate((width - x_size * y_ratio) / 2, 0) matrix.scale(y_ratio, y_ratio) matrix.translate(-x, -y / y_ratio * x_ratio) elif x_ratio < y_ratio: matrix.translate(0, (height - y_size * x_ratio) / 2) matrix.scale(x_ratio, x_ratio) matrix.translate(-x / x_ratio * y_ratio, -y) else: matrix.scale(x_ratio, y_ratio) matrix.translate(-x, -y) apply_matrix_transform(self, matrix) else: self.context_width, self.context_height = width, height
def draw_snake_graph_with_cairo(self, dp, graph): if graph is None: return False cr = dp.cr cr.save() cr.transform(cairo.Matrix(dp.l, 0, 0, dp.l, dp.l / 2, dp.l / 2)) cr.set_source_rgba(*dp.rgba_text) cr.select_font_face(dp.fn, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) cr.set_font_size(dp.fs) for x, y in np.transpose(graph.nonzero()): dist = graph[x, y] extent = cr.text_extents(str(dist)) cr.move_to(x - extent.width / 2, y + extent.height / 2) cr.show_text(str(dist)) cr.restore()