summaryrefslogtreecommitdiff
path: root/src/anim/typst.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:27:39 -0300
committerGabriel Schneider <[email protected]>2026-11-12 13:22:02 -0300
commit71e534631bf68f33f802d64794340870374b9f69 (patch)
tree2a45229ce238a4e2ce355683f0e75a7725d4851a /src/anim/typst.zig
parent00605f7a109845a99f16621b267305a8b995dddc (diff)
downloadsketchbook-71e534631bf68f33f802d64794340870374b9f69.tar.gz
sketchbook-71e534631bf68f33f802d64794340870374b9f69.zip
sketchbook: anim, manim-style scenes as a module (from the game's vecanim), with objects a scene draws itself
The player takes its canvas from the room it has and fits the scene's frame into it, plays through and quits when exported, and draws vector objects in runs around custom ones. The renderers' resvg comparison and the timings come along as `zig build diff` and `zig build bench`; the comparison's reference is padded to whole blocks rather than squeezed, which had put Typst text over the limit. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/anim/typst.zig')
-rw-r--r--src/anim/typst.zig66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/anim/typst.zig b/src/anim/typst.zig
new file mode 100644
index 0000000..d9df734
--- /dev/null
+++ b/src/anim/typst.zig
@@ -0,0 +1,66 @@
+//! Build-time Typst for a scene file. `sb.addTypst` builds a small generator
+//! around this file that imports the scene, whose `construct` has already
+//! run at comptime and listed its Typst objects (`scene.typst_sources`),
+//! compiles each with the `typst` CLI after the scene's `typst_preamble`,
+//! and writes a module the player uses to find the SVGs by source:
+//!
+//! typst.find(source) ?[]const u8
+const std = @import("std");
+
+/// Used when the scene declares no `typst_preamble`.
+pub const default_preamble =
+ \\#set page(width: auto, height: auto, margin: 3pt, fill: none)
+;
+
+pub fn generate(comptime SceneFile: type, comptime sources: []const []const u8, gpa: std.mem.Allocator, io: std.Io, out_path: []const u8, cache_path: []const u8) !void {
+ const preamble: []const u8 = if (@hasDecl(SceneFile, "typst_preamble")) SceneFile.typst_preamble else default_preamble;
+ const cwd = std.Io.Dir.cwd();
+ var out = try cwd.createDirPathOpen(io, out_path, .{});
+ defer out.close(io);
+ var cache = try cwd.createDirPathOpen(io, cache_path, .{});
+ defer cache.close(io);
+
+ var table: std.ArrayList(u8) = .empty;
+ var seen: std.AutoHashMapUnmanaged(u64, void) = .empty;
+ for (sources) |source| {
+ const key = std.hash.Wyhash.hash(0, source);
+ if ((try seen.getOrPut(gpa, key)).found_existing) continue;
+ const name = try std.fmt.allocPrint(gpa, "{x:0>16}.svg", .{key});
+ // Cached by preamble and source together.
+ const cache_name = try std.fmt.allocPrint(gpa, "{x:0>16}-{s}", .{ std.hash.Wyhash.hash(0, preamble), name });
+ const svg = cache.readFileAlloc(io, cache_name, gpa, .limited(64 << 20)) catch try compile(gpa, io, cache, cache_name, preamble, source);
+ try out.writeFile(io, .{ .sub_path = name, .data = svg });
+ try table.print(gpa, " .{{ 0x{x:0>16}, @embedFile(\"{s}\") }},\n", .{ key, name });
+ }
+ const module = try std.fmt.allocPrint(gpa,
+ \\//! Generated by sketchbook's src/anim/typst.zig: the scene's Typst objects, as SVG.
+ \\const std = @import("std");
+ \\
+ \\const table = [_]struct {{ u64, []const u8 }}{{
+ \\{s}}};
+ \\
+ \\/// The SVG compiled from Typst `source`, if the scene declared it.
+ \\pub fn find(source: []const u8) ?[]const u8 {{
+ \\ const key = std.hash.Wyhash.hash(0, source);
+ \\ for (table) |entry| if (entry[0] == key) return entry[1];
+ \\ return null;
+ \\}}
+ \\
+ , .{table.items});
+ try out.writeFile(io, .{ .sub_path = "typst.zig", .data = module });
+}
+
+/// Runs `typst compile` on `preamble ++ source` and caches the SVG.
+fn compile(gpa: std.mem.Allocator, io: std.Io, cache: std.Io.Dir, name: []const u8, preamble: []const u8, source: []const u8) ![]const u8 {
+ const typ = try std.fmt.allocPrint(gpa, "{s}.typ", .{name});
+ try cache.writeFile(io, .{ .sub_path = typ, .data = try std.mem.concat(gpa, u8, &.{ preamble, "\n", source, "\n" }) });
+ const result = try std.process.run(gpa, io, .{
+ .argv = &.{ "typst", "compile", "--format", "svg", typ, name },
+ .cwd = .{ .dir = cache },
+ });
+ if (result.term != .exited or result.term.exited != 0) {
+ std.debug.print("typst failed on:\n{s}\n{s}\n", .{ source, result.stderr });
+ return error.TypstFailed;
+ }
+ return cache.readFileAlloc(io, name, gpa, .limited(64 << 20));
+}