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()
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) scalematrix = Matrix() scalematrix.scale(1.0/scale,1.0/scale) scalematrix.translate(-(600.0 - widthZ *scale /2.0 ), -50)
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()
def draw(widget: Gtk.Widget, cr: cairo.Context): if not world.map: return player_x = world.player.x player_y = world.player.y allocation = widget.get_allocation() tile_width = allocation.width / world.map_width tile_height = allocation.height / world.map_height # clear drawing cr.set_source_rgb(1, 1, 1) cr.paint() # draw shadow shadow_surface = cairo.ImageSurface(cairo.Format.ARGB32, allocation.width, allocation.height) scr = cairo.Context(shadow_surface) scr.set_line_width(1) scr.set_source_rgb(0, 0, 0) for y, row in enumerate(world.map): for x, tile in enumerate(row): if tile == "#": left = x * tile_width top = y * tile_height right = left + tile_width bottom = top + tile_height shadow_points = [] for dest_x, dest_y in itertools.product([left, right], [top, bottom]): other_x = {left: right, right: left}[dest_x] other_y = {top: bottom, bottom: top}[dest_y] if line_goes_through_border(player_x, player_y, dest_x, dest_y, other_x, top, bottom) \ or line_goes_through_border(player_y, player_x, dest_y, dest_x, other_y, left, right): continue shadow_points.append((dest_x, dest_y)) if len(shadow_points) == 3: for i in range(len(shadow_points)): if shadow_points[i] == (shadow_points[(i - 1) % 3][0], shadow_points[(i + 1) % 3][1]) \ or shadow_points[i] == (shadow_points[(i + 1) % 3][0], shadow_points[(i - 1) % 3][1]): del shadow_points[i] break elif len(shadow_points) == 4: continue for i, shadow_point in enumerate( extend_shadow_points(shadow_points, allocation)): if i == 0: scr.move_to(*shadow_point) else: scr.line_to(*shadow_point) scr.fill() cr.set_source_surface(shadow_surface) cr.mask(cairo.SolidPattern(0, 0, 0, .6)) cr.fill() # draw tiles cr.set_line_width(1) for y, row in enumerate(world.map): for x, tile in enumerate(row): if tile == "#": cr.set_source_rgb(.9, 0, 0) cr.rectangle(x * tile_width, y * tile_height, tile_width, tile_height) cr.fill_preserve() cr.set_source_rgba(0, 0, 0) cr.stroke() for player in world.players.values(): collides = False for y, row in enumerate(world.map): for x, tile in enumerate(row): if tile == "#": left = x * tile_width top = y * tile_height right = left + tile_width bottom = top + tile_height if collision_rect_line(Rectangle(left, right, top, bottom), player_x, player_y, player.x, player.y): collides = True break if collides: break if not collides: draw_player(cr, player) draw_player( cr, Player(player_x, player_y, world.player.rotation, world.player.health)) for bullet in world.bullets: draw_bullet(cr, bullet)
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) scalematrix = Matrix() scalematrix.scale(1.0 / scale, 1.0 / scale) scalematrix.translate(-(600.0 - widthZ * scale / 2.0), -50)
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()