except NotImplementedError: raise SystemExit("For python 3.x, you need pycairo >= 1.11+ (from https://github.com/pygobject/pycairo)") h = L.get_height() w = L.get_width() if h < H and w < W: x0 = W//2 + (random.uniform()-.1)*50 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)
face.load_char('😀', freetype.FT_LOAD_COLOR) bitmap = face.glyph.bitmap width = face.glyph.bitmap.width rows = face.glyph.bitmap.rows # The line below depends on this assumption. Check. if ( face.glyph.bitmap.pitch != width * 4 ): raise RuntimeError('pitch != width * 4 for color bitmap: Please report this.') bitmap = np.array(bitmap.buffer, dtype=np.uint8).reshape((bitmap.rows,bitmap.width,4)) I = ImageSurface(FORMAT_ARGB32, width, rows) try: ndI = np.ndarray(shape=(rows,width), buffer=I.get_data(), dtype=np.uint32, order='C', strides=[I.get_stride(), 4]) except NotImplementedError: raise SystemExit("For python 3.x, you need pycairo >= 1.11+ (from https://github.com/pygobject/pycairo)") # Although both are 32-bit, cairo is host-order while # freetype is small endian. ndI[:,:] = bitmap[:,:,3] * 2**24 + bitmap[:,:,2] * 2**16 + bitmap[:,:,1] * 2**8 + bitmap[:,:,0] I.mark_dirty() surface = ImageSurface(FORMAT_ARGB32, 2*width, rows) ctx = Context(surface) ctx.set_source_surface(I, 0, 0) ctx.paint() ctx.set_source_surface(I, width/2, 0)
def draw_triangle(target: cairo.ImageSurface, triangle, attributes, texture: Texture): # drop z coordinate p0, p1, p2 = [p[:2] for p in triangle] # compute area area = edge(p0, p1, p2) if area == 0: return width, height = resolution(target) xmin = int(max(min(p0[0], p1[0], p2[0]), 0)) xmax = int(min(max(p0[0], p1[0], p2[0]), width)) ymin = int(max(min(p0[1], p1[1], p2[1]), 0)) ymax = int(min(max(p0[1], p1[1], p2[1]), height)) x, y = np.meshgrid(range(xmin, xmax), range(ymin, ymax), indexing='xy') p = np.vstack([x.ravel(), y.ravel()]).T # Barycentric coordinates are calculated as the areas of the three sub-triangles divided # by the area of the whole triangle. barycentric = np.vstack( [edge(p1, p2, p), edge(p2, p0, p), edge(p0, p1, p)]).T / area # Find all indices of rows where all columns are positive is_inside = np.all(barycentric >= 0, axis=-1) # Compute indices of all points inside triangle stride = np.array([4, target.get_stride()]) indices = np.dot(p[is_inside], stride) # Interpolate vertex attributes attrs = np.dot(barycentric[is_inside], attributes) # Fill pixels data = target.get_data() for index, (u, v) in zip(indices, attrs): r, g, b = texture(u, v) data[index + 0] = r data[index + 1] = g data[index + 2] = b
dtype=ubyte, order='C', offset=1, strides=[pitch, 3]) ndB = ndarray(shape=(rows, width), buffer=copybuffer, dtype=ubyte, order='C', offset=2, strides=[pitch, 3]) try: ndI = ndarray(shape=(rows, width), buffer=I.get_data(), dtype=uint32, order='C', strides=[I.get_stride(), 4]) 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