summaryrefslogtreecommitdiff
path: root/src/vector/root.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-25 21:47:58 -0300
committerGabriel Schneider <[email protected]>2026-09-25 21:48:16 -0300
commit9c33714591a2147f7e5879e77212ca9886d0c967 (patch)
treed250be9d9bc365d7894d49455c0f6376966b58be /src/vector/root.zig
downloadsketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.tar.gz
sketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.zip
Put the game first: lib/sketchbook, sketches/vecanim
The root is the game. The platform library moves to lib/sketchbook (with serve_web.py, which serves any sketch's web build, as a tool of it), and vecanim to sketches/vecanim, still its own package on the same library.
Diffstat (limited to 'src/vector/root.zig')
-rw-r--r--src/vector/root.zig144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/vector/root.zig b/src/vector/root.zig
new file mode 100644
index 0000000..7556948
--- /dev/null
+++ b/src/vector/root.zig
@@ -0,0 +1,144 @@
+//! Vector graphics for sketches: SVG in, cubic geometry as data, and two
+//! interchangeable renderers. The CPU backend computes exact coverage and
+//! runs anywhere (it is also the reference in tests); the GPU backend
+//! describes stencil-then-cover passes for the host to run. The backend is
+//! a comptime choice: `vector.Renderer(.gpu)` or `vector.Renderer(.cpu)`.
+const std = @import("std");
+const abi = @import("abi");
+pub const geom = @import("geom.zig");
+pub const Drawing = @import("Drawing.zig");
+pub const svg = @import("svg.zig");
+pub const Polygons = @import("Polygons.zig");
+pub const Paint = @import("Paint.zig");
+pub const Cpu = @import("Cpu.zig");
+pub const Gpu = @import("Gpu.zig");
+pub const layouts = @import("vector_layouts");
+pub const morph = @import("morph.zig");
+
+pub const Backend = enum { cpu, gpu };
+
+pub const Options = struct {
+ /// Multiplies everything the shape draws.
+ alpha: f32 = 1,
+ fill: bool = true,
+ stroke: bool = true,
+ /// Also outline the shape in its fill color, `width` device pixels wide
+ /// (manim's Write draws glyph borders before filling them).
+ outline: ?struct { width: f32, alpha: f32 = 1 } = null,
+ /// The drawn fraction of strokes and outlines, along their length.
+ trim: f32 = 1,
+};
+
+/// Draws drawings into an image of `width` x `height` pixels. `host` is a
+/// `sketch.Host`; `f` a `sketch.Frame`. Both are duck-typed, so the library
+/// depends on nothing but the ABI.
+pub fn Renderer(comptime backend: Backend) type {
+ return struct {
+ const Self = @This();
+ pub const kind = backend;
+
+ target: switch (backend) {
+ .cpu => Cpu,
+ .gpu => Gpu,
+ },
+ polys: Polygons,
+ image: abi.Image,
+
+ pub fn init(gpa: std.mem.Allocator, host: anytype, w: u32, h: u32) !Self {
+ const image = host.newImage(w, h);
+ return .{
+ .target = switch (backend) {
+ .cpu => try .init(gpa, w, h),
+ .gpu => .{ .width = w, .height = h, .image = image },
+ },
+ .polys = .init(gpa),
+ .image = image,
+ };
+ }
+
+ pub fn deinit(r: *Self, host: anytype) void {
+ if (backend == .cpu) r.target.deinit();
+ r.polys.deinit();
+ host.freeImage(r.image);
+ }
+
+ pub fn width(r: *const Self) u32 {
+ return r.target.width;
+ }
+ pub fn height(r: *const Self) u32 {
+ return r.target.height;
+ }
+
+ /// Starts a picture. `f.arena` holds what the GPU pass needs until
+ /// the frame is consumed.
+ pub fn begin(r: *Self, f: anytype, clear: Drawing.Color) void {
+ switch (backend) {
+ .cpu => r.target.clear(clear),
+ .gpu => r.target.begin(f.arena, clear),
+ }
+ }
+
+ /// All shapes of `d`; `m` maps its user space to pixels.
+ pub fn draw(r: *Self, d: Drawing, m: geom.Affine, alpha: f32) !void {
+ for (d.shapes) |shape| try r.drawShape(d, shape, m, .{ .alpha = alpha });
+ }
+
+ pub fn drawShape(r: *Self, d: Drawing, shape: Drawing.Shape, user_to_device: geom.Affine, o: Options) !void {
+ const m = user_to_device.mul(shape.transform);
+ const s = shape.style;
+ const clip: ?[4]geom.Vec = if (shape.clip) |q| .{ user_to_device.apply(q[0]), user_to_device.apply(q[1]), user_to_device.apply(q[2]), user_to_device.apply(q[3]) } else null;
+ if (o.fill and s.fill != .none) {
+ r.polys.clear();
+ try r.polys.fill(d, shape, m);
+ if (clip) |q| try r.polys.clipConvex(q);
+ r.fill(s.fill_rule, &Paint.of(d, shape, s.fill, m), o.alpha * s.opacity * s.fill_opacity);
+ }
+ if (o.stroke and s.stroke != .none and s.stroke_width > 0) {
+ var st: Polygons.Stroke = .of(s, m);
+ st.dashes = s.dashes[0..s.dash_count];
+ st.trim = o.trim;
+ r.polys.clear();
+ try r.polys.stroke(d, shape, m, st);
+ if (clip) |q| try r.polys.clipConvex(q);
+ r.fill(.nonzero, &Paint.of(d, shape, s.stroke, m), o.alpha * s.opacity * s.stroke_opacity);
+ }
+ if (o.outline) |outline| if (s.fill != .none) {
+ r.polys.clear();
+ try r.polys.stroke(d, shape, m, .{ .width = outline.width, .cap = .round, .join = .round, .trim = o.trim, .space = .identity });
+ r.fill(.nonzero, &Paint.of(d, shape, s.fill, m), o.alpha * s.opacity * outline.alpha);
+ };
+ }
+
+ /// Fills whatever is in `r.polys`.
+ pub fn fill(r: *Self, rule: Drawing.FillRule, paint: *const Paint, alpha: f32) void {
+ r.target.fill(&r.polys, rule, paint, alpha);
+ }
+
+ /// Hands the picture to the host and draws it at `dst`.
+ pub fn present(r: *Self, f: anytype, dst: anytype) void {
+ switch (backend) {
+ .cpu => f.pixels(r.image, r.target.width, r.target.height, r.target.pixels, .{ .premultiplied = true }),
+ .gpu => f.gpu(r.target.pass()),
+ }
+ f.image(r.image, dst, .{});
+ }
+ };
+}
+
+/// The transform placing a drawing's view box into `dst`, centered and
+/// scaled to fit (SVG's default `xMidYMid meet`).
+pub fn fit(d: Drawing, dst: geom.Rect) geom.Affine {
+ const v = d.view;
+ if (v.w <= 0 or v.h <= 0) return .translate(dst.x, dst.y);
+ const s = @min(dst.w / v.w, dst.h / v.h);
+ return geom.Affine.translate(dst.x + (dst.w - v.w * s) / 2, dst.y + (dst.h - v.h * s) / 2).mul(.scale(s, s)).mul(.translate(-v.x, -v.y));
+}
+
+test {
+ _ = morph;
+ _ = geom;
+ _ = svg;
+ _ = Polygons;
+ _ = Cpu;
+ _ = Gpu;
+}