def on_adjustment_changed(self, adj): """ Change the transformation matrix of the view to reflect the value of the x/y adjustment (scrollbar). """ if adj.value == 0.0: return # Can not use self._matrix.translate( - adj.value , 0) here, since # the translate method effectively does a m * self._matrix, which # will result in the translation being multiplied by the orig. matrix m = Matrix() if adj is self._hadjustment: m.translate(-adj.value, 0) elif adj is self._vadjustment: m.translate(0, -adj.value) self._matrix *= m # Force recalculation of the bounding boxes: self.request_update((), self._canvas.get_all_items()) self.queue_draw_refresh()
def on_adjustment_changed(self, adj): """ Change the transformation matrix of the view to reflect the value of the x/y adjustment (scrollbar). """ if adj.value == 0.0: return # Can not use self._matrix.translate( - adj.value , 0) here, since # the translate method effectively does a m * self._matrix, which # will result in the translation being multiplied by the orig. matrix m = Matrix() if adj is self._hadjustment: m.translate( - adj.value, 0) elif adj is self._vadjustment: m.translate(0, - adj.value) self._matrix *= m # Force recalculation of the bounding boxes: self.request_update((), self._canvas.get_all_items()) self.queue_draw_refresh()
y0 = H//2 + (random.uniform()-.1)*50 for dx,dy in spiral(): c = .25+.75*random.random() x = int(x0+dx) y = int(y0+dy) checked = False I.flush() if not (x <= w//2 or y <= h//2 or x >= (W-w//2) or y >= (H-h//2)): ndI = ndarray(shape=(h,w), buffer=I.get_data(), dtype=ubyte, order='C', offset=(x-w//2) + I.get_stride() * (y-h//2), strides=[I.get_stride(), 1]) ndL = ndarray(shape=(h,w), buffer=L.get_data(), dtype=ubyte, order='C', strides=[L.get_stride(), 1]) if ((ndI * ndL).sum() == 0): checked = True new_region = RectangleInt(x-w//2, y-h//2, w, h) if (checked or ( drawn_regions.contains_rectangle(new_region) == REGION_OVERLAP_OUT )): ctxI.set_source_surface(L, 0, 0) pattern = ctxI.get_source() scalematrix = Matrix() scalematrix.scale(1.0,1.0) scalematrix.translate(w//2 - x, h//2 - y) pattern.set_matrix(scalematrix) ctxI.set_source_rgba(c,c,c,c) ctxI.mask(pattern) drawn_regions.union(new_region) break I.flush() I.write_to_png("wordle-cairo.png") Image.open("wordle-cairo.png").show()
"For python 3.x, you need pycairo >= 1.11+ (from https://github.com/pygobject/pycairo)" ) # 255 * 2**24 = opaque ndI[:, :] = 255 * 2**24 + ndR[:, :] * 2**16 + ndG[:, :] * 2**8 + ndB[:, :] I.mark_dirty() surface = ImageSurface(FORMAT_ARGB32, 800, 600) ctx = Context(surface) ctx.set_source_surface(I, 0, 0) pattern = ctx.get_source() SurfacePattern.set_filter(pattern, FILTER_BEST) scale = 480.0 / rows scalematrix = Matrix() scalematrix.scale(1.0 / scale, 1.0 / scale) scalematrix.translate(-(400.0 - width * scale / 2.0) + 200, -60) pattern.set_matrix(scalematrix) ctx.paint() # we need this later for shifting the taller LCD_V glyph up. rows_old = rows # LCD_V face.load_char('S', FT_LOAD_RENDER | FT_LOAD_TARGET_LCD_V) bitmap = face.glyph.bitmap width = face.glyph.bitmap.width rows = face.glyph.bitmap.rows // 3 pitch = face.glyph.bitmap.pitch copybuffer = (c_ubyte * (pitch * face.glyph.bitmap.rows))() memmove(pointer(copybuffer), bitmap._FT_Bitmap.buffer, pitch * face.glyph.bitmap.rows)
except NotImplementedError: raise SystemExit("For python 3.x, you need pycairo >= 1.11+ (from https://github.com/pygobject/pycairo)") # 255 * 2**24 = opaque ndI[:,:] = 255 * 2**24 + ndR[:,:] * 2**16 + ndG[:,:] * 2**8 + ndB[:,:] I.mark_dirty() surface = ImageSurface(FORMAT_ARGB32, 800, 600) ctx = Context(surface) ctx.set_source_surface(I, 0, 0) pattern = ctx.get_source() SurfacePattern.set_filter(pattern, FILTER_BEST) scale = 480.0 / rows scalematrix = Matrix() scalematrix.scale(1.0/scale,1.0/scale) scalematrix.translate(-(400.0 - width *scale /2.0 )+200, -60) pattern.set_matrix(scalematrix) ctx.paint() # we need this later for shifting the taller LCD_V glyph up. rows_old = rows # LCD_V face.load_char('S', FT_LOAD_RENDER | FT_LOAD_TARGET_LCD_V ) bitmap = face.glyph.bitmap width = face.glyph.bitmap.width rows = face.glyph.bitmap.rows//3 pitch = face.glyph.bitmap.pitch copybuffer = (c_ubyte * (pitch * face.glyph.bitmap.rows))() memmove(pointer(copybuffer), bitmap._FT_Bitmap.buffer,
# fill background as gray ctx.rectangle(0,0,1200,500) ctx.set_source_rgb (0.5 , 0.5, 0.5) ctx.fill() # use the stroked font's size as scale, as it is likely slightly larger scale = 400.0 / rowsZ # draw bitmap first ctx.set_source_surface(F, 0, 0) patternF = ctx.get_source() SurfacePattern.set_filter(patternF, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0/scale,1.0/scale) scalematrix.translate(-(600.0 - widthF *scale /2.0 ), -50) patternF.set_matrix(scalematrix) ctx.set_source_rgb (1 , 1, 0) ctx.mask(patternF) ctx.fill() scalematrix.translate(+400,0) patternF.set_matrix(scalematrix) ctx.mask(patternF) ctx.fill() # stroke on top ctx.set_source_surface(Z, 0, 0) patternZ = ctx.get_source() SurfacePattern.set_filter(patternZ, FILTER_BEST)
VERTS[i + 1][0], VERTS[i + 1][1]) i += 2 elif (CODES[i] == CURVE4): ctx.curve_to(VERTS[i][0], VERTS[i][1], VERTS[i + 1][0], VERTS[i + 1][1], VERTS[i + 2][0], VERTS[i + 2][1]) i += 3 ctx.fill_preserve() ctx.set_source_rgb(0, 0, 0) ctx.set_line_width(6) ctx.stroke() ctx.restore() scale2 = (height_s - 2.0 * MARGIN) / rows ctx.set_source_surface(Z, 0, 0) pattern = ctx.get_source() SurfacePattern.set_filter(pattern, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0 / scale2, 1.0 / scale2) scalematrix.translate(-(width_s / 2.0 - width * scale2 / 2.0), -MARGIN) pattern.set_matrix(scalematrix) ctx.set_source_rgba(0, 0, 0, 0.7) ctx.mask(pattern) ctx.fill() surface.flush() surface.write_to_png("glyph-vector-2-cairo.png") surface.finish() Image.open("glyph-vector-2-cairo.png").show()
# fill background as gray ctx.rectangle(0, 0, 1200, 500) ctx.set_source_rgb(0.5, 0.5, 0.5) ctx.fill() # use the stroked font's size as scale, as it is likely slightly larger scale = 400.0 / rowsZ # draw bitmap first ctx.set_source_surface(F, 0, 0) patternF = ctx.get_source() SurfacePattern.set_filter(patternF, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0 / scale, 1.0 / scale) scalematrix.translate(-(600.0 - widthF * scale / 2.0), -50) patternF.set_matrix(scalematrix) ctx.set_source_rgb(1, 1, 0) ctx.mask(patternF) ctx.fill() scalematrix.translate(+400, 0) patternF.set_matrix(scalematrix) ctx.mask(patternF) ctx.fill() # stroke on top ctx.set_source_surface(Z, 0, 0) patternZ = ctx.get_source() SurfacePattern.set_filter(patternZ, FILTER_BEST)
face.load_char('S', FT_LOAD_RENDER | FT_LOAD_TARGET_MONO) bitmap = face.glyph.bitmap width = face.glyph.bitmap.width rows = face.glyph.bitmap.rows pitch = face.glyph.bitmap.pitch glyph_surface = make_image_surface(face.glyph.bitmap) surface = ImageSurface(FORMAT_ARGB32, 800, 600) ctx = Context(surface) ctx.rectangle(0, 0, 800, 600) ctx.set_line_width(0) ctx.set_source_rgb(0.5, 0.5, 0.5) ctx.fill() # scale = 480.0 / rows ctx.set_source_surface(glyph_surface, 0, 0) pattern = ctx.get_source() SurfacePattern.set_filter(pattern, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0 / scale, 1.0 / scale) scalematrix.translate(-(400.0 - width * scale / 2.0), -60) pattern.set_matrix(scalematrix) ctx.set_source_rgb(0, 0, 1) ctx.mask(pattern) ctx.fill() surface.flush() surface.write_to_png("glyph-mono+alpha-cairo.png") Image.open("glyph-mono+alpha-cairo.png").show()
FT_LOAD_TARGET_MONO ) bitmap = face.glyph.bitmap width = face.glyph.bitmap.width rows = face.glyph.bitmap.rows pitch = face.glyph.bitmap.pitch glyph_surface = make_image_surface(face.glyph.bitmap) surface = ImageSurface(FORMAT_ARGB32, 800, 600) ctx = Context(surface) ctx.rectangle(0,0,800,600) ctx.set_line_width(0) ctx.set_source_rgb (0.5 , 0.5, 0.5) ctx.fill() # scale = 480.0 / rows ctx.set_source_surface(glyph_surface, 0, 0) pattern = ctx.get_source() SurfacePattern.set_filter(pattern, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0/scale,1.0/scale) scalematrix.translate(-(400.0 - width *scale /2.0 ), -60) pattern.set_matrix(scalematrix) ctx.set_source_rgb (0 , 0, 1) ctx.mask(pattern) ctx.fill() surface.flush() surface.write_to_png("glyph-mono+alpha-cairo.png") Image.open("glyph-mono+alpha-cairo.png").show()
face = Face('./Vera.ttf') face.set_char_size( 4*48*64 ) flags = FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP face.load_char('S', flags ) slot = face.glyph glyph = slot.get_glyph() stroker = Stroker( ) stroker.set(64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0 ) glyph.stroke( stroker , True ) blyph = glyph.to_bitmap(FT_RENDER_MODE_NORMAL, Vector(0,0), True ) bitmap = blyph.bitmap width, rows, pitch = bitmap.width, bitmap.rows, bitmap.pitch surface = ImageSurface(FORMAT_ARGB32, 600, 800) ctx = Context(surface) Z = make_image_surface(bitmap) ctx.set_source_surface(Z, 0, 0) scale = 640.0 / rows patternZ = ctx.get_source() SurfacePattern.set_filter(patternZ, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0/scale,1.0/scale) scalematrix.translate(-(300.0 - width *scale /2.0 ), -80) patternZ.set_matrix(scalematrix) ctx.set_source_rgb (0 , 0, 1) ctx.mask(patternZ) ctx.fill() surface.flush() surface.write_to_png("glyph-outline-cairo.png") Image.open("glyph-outline-cairo.png").show()
i += 2 elif (CODES[i] == CURVE4): ctx.curve_to(VERTS[i][0],VERTS[i][1], VERTS[i+1][0],VERTS[i+1][1], VERTS[i+2][0],VERTS[i+2][1]) i += 3 ctx.fill_preserve() ctx.set_source_rgb(0,0,0) ctx.set_line_width(6) ctx.stroke() ctx.restore() scale2 = (height_s - 2.0 * MARGIN)/rows ctx.set_source_surface(Z, 0, 0) pattern = ctx.get_source() SurfacePattern.set_filter(pattern, FILTER_BEST) scalematrix = Matrix() scalematrix.scale(1.0/scale2, 1.0/scale2) scalematrix.translate(-( width_s/2.0 - width *scale2 /2.0 ), -MARGIN) pattern.set_matrix(scalematrix) ctx.set_source_rgba (0, 0, 0, 0.7) ctx.mask(pattern) ctx.fill() surface.flush() surface.write_to_png("glyph-vector-2-cairo.png") surface.finish() Image.open("glyph-vector-2-cairo.png").show()