diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/scene_compiled.script | 13 | ||||
| -rw-r--r-- | test/scene_compiled.zig | 34 | ||||
| -rw-r--r-- | test/scene_live.script | 37 | ||||
| -rw-r--r-- | test/scene_live.zig | 38 | ||||
| -rw-r--r-- | test/svg/dashes.svg | 6 | ||||
| -rw-r--r-- | test/svg/fill-rules.svg | 6 | ||||
| -rw-r--r-- | test/svg/gradients.svg | 16 | ||||
| -rw-r--r-- | test/svg/opacity.svg | 7 | ||||
| -rw-r--r-- | test/svg/paths.svg | 8 | ||||
| -rw-r--r-- | test/svg/shapes.svg | 8 | ||||
| -rw-r--r-- | test/svg/strokes.svg | 11 | ||||
| -rw-r--r-- | test/svg/transforms.svg | 13 | ||||
| -rw-r--r-- | test/svg/typst-equation.typ | 3 | ||||
| -rw-r--r-- | test/svg/typst-shapes.typ | 8 | ||||
| -rw-r--r-- | test/svg/typst-text.typ | 5 | ||||
| -rw-r--r-- | test/svg/use.svg | 11 | ||||
| -rw-r--r-- | test/svgview.zig | 78 |
17 files changed, 302 insertions, 0 deletions
diff --git a/test/scene_compiled.script b/test/scene_compiled.script new file mode 100644 index 0000000..3c9ef44 --- /dev/null +++ b/test/scene_compiled.script @@ -0,0 +1,13 @@ +# The compiled test scene, through the player, and exported ({frames}: +# 30 fps, 640x360, so half size, played once without the player's controls). +frames 30 +screenshot .zig-cache/anim/compiled-written.png +console set t 1.5 +frames 1 +screenshot .zig-cache/anim/compiled-morph.png +# Frame 68 is 2.27 s in: the dot over the panel's corner, at half size. +expect-pixel {frames}/000068.png 400 240 88 196 221 255 1 +expect-pixel {frames}/000068.png 250 235 42 42 48 255 1 +# No bar of controls along the bottom, and the last frame is the end, 3 s. +expect-pixel {frames}/000068.png 8 352 28 28 31 255 +expect-pixel {frames}/000090.png 0 0 28 28 31 255 diff --git a/test/scene_compiled.zig b/test/scene_compiled.zig new file mode 100644 index 0000000..f4a917c --- /dev/null +++ b/test/scene_compiled.zig @@ -0,0 +1,34 @@ +//! The anim module's test scene, compiled: Typst written and morphed, and +//! a custom object whose data is a constant, shown as far as it is `drawn`. +const anim = @import("anim"); +const sk = @import("sketch"); + +pub const typst_preamble = + \\#set page(width: auto, height: auto, margin: 3pt, fill: none) + \\#set text(fill: white, size: 28pt) +; +pub const scene = anim.Scene(construct); + +/// Lines of code in a panel of 320x120 units around its origin. +const Code = struct { + lines: []const []const u8, + + pub fn draw(c: *const Code, f: *sk.Frame, at: anim.Placed) void { + sk.draw.rect(f, .{ .x = -160, .y = -60, .w = 320, .h = 120 }, at.pen(.{ .fill = .hex(0x2a2a30) })); + const count: f32 = @floatFromInt(c.lines.len); + const shown: usize = @intFromFloat(at.pose.drawn * count); + for (c.lines[0..shown], 0..) |line, i| at.text(f, -150, -50 + 24 * @as(f32, @floatFromInt(i)), 1.5, line, .white); + } +}; +const code: Code = .{ .lines = &.{ "mov rax, 1", "add rax, rbx", "ret" } }; + +fn construct(s: *anim.Builder) void { + const pythagoras = s.typst("$ a^2 + b^2 = c^2 $", .{ .y = -200 }); + const solved = s.typst("$ c = sqrt(a^2 + b^2) $", .{ .y = -200 }); + const panel = s.custom(&code, .{ .y = 60 }); + const dot = s.circle(.{ .r = 20, .x = 160, .y = 120, .fill = anim.blue }); + s.play(.{ pythagoras.write(), panel.write() }, .{ .seconds = 1 }); + s.play(.{ pythagoras.transformInto(solved), dot.fadeIn() }, .{ .seconds = 1 }); + s.wait(0.5); + s.play(.{anim.camera(.{ .zoom = 1.5 })}, .{ .seconds = 0.5 }); +} diff --git a/test/scene_live.script b/test/scene_live.script new file mode 100644 index 0000000..30b2087 --- /dev/null +++ b/test/scene_live.script @@ -0,0 +1,37 @@ +# The live test scene, through the player. It holds at the pause. +frames 150 +expect t 2 +screenshot .zig-cache/anim/live.png +# The custom panel is over the circle added before it, +expect-pixel .zig-cache/anim/live.png 600 330 32 64 96 255 1 +expect-pixel .zig-cache/anim/live.png 450 360 252 98 85 255 1 +# under the square added after it, +expect-pixel .zig-cache/anim/live.png 725 400 131 193 103 255 1 +# and its bar is as full as the value the timeline moved to 0.5. +expect-pixel .zig-cache/anim/live.png 600 390 240 172 95 255 1 +expect-pixel .zig-cache/anim/live.png 680 390 32 64 96 255 1 +# Half way through its move it is half way. +console set playing false +console set t 1.5 +screenshot .zig-cache/anim/live-moving.png +expect-pixel .zig-cache/anim/live-moving.png 400 330 32 64 96 255 1 +expect-pixel .zig-cache/anim/live-moving.png 600 330 252 98 85 255 1 +# The CPU renderer layers the same way. +console set t 2 +key b +expect backend cpu +screenshot .zig-cache/anim/live-cpu.png +expect-pixel .zig-cache/anim/live-cpu.png 600 330 32 64 96 255 1 +expect-pixel .zig-cache/anim/live-cpu.png 725 400 131 193 103 255 1 +key b +# A click continues: the camera zooms to 2 and the panel fades to half, +# over the background where it now reaches. +click 640 360 +expect playing true +frames 70 +screenshot .zig-cache/anim/live-zoom.png +expect-pixel .zig-cache/anim/live-zoom.png 800 300 30 46 64 255 2 +# Guides draw over everything. +key g +expect guides true +screenshot .zig-cache/anim/live-guides.png diff --git a/test/scene_live.zig b/test/scene_live.zig new file mode 100644 index 0000000..cb21b9b --- /dev/null +++ b/test/scene_live.zig @@ -0,0 +1,38 @@ +//! The anim module's test scene, live: a custom object drawn with 2D +//! commands between two vector objects, on the same timeline and camera. +const anim = @import("anim"); +const sk = @import("sketch"); + +pub const typst = struct { + pub const title = "#text(fill: white, size: 30pt)[registers]"; +}; +pub const scene = anim.Live(frame, typst); + +/// A panel of 200x100 units around its origin, with a label and a bar. +const Panel = struct { + label: []const u8, + /// How much of the bar is full, 0 to 1. + level: f32, + + pub fn draw(p: *const Panel, f: *sk.Frame, at: anim.Placed) void { + sk.draw.rect(f, .{ .x = -100, .y = -50, .w = 200, .h = 100 }, at.pen(.{ .fill = .hex(0x204060) })); + sk.draw.rect(f, .{ .x = -100, .y = 10, .w = 200 * p.level, .h = 40 }, at.pen(.{ .fill = .hex(0xf0ac5f) })); + at.text(f, -92, -42, 1.5, p.label, .white); + } +}; + +fn frame(s: *anim.Frame) void { + const title = s.typst(typst.title, .{ .y = -250 }); + // Added in this order, shown in this order: circle, panel, square. + const under = s.circle(.{ .r = 120, .x = -100, .fill = anim.red }); + const panel = s.new(Panel{ .label = "rax", .level = 0 }); + const table = s.custom(panel, .{ .x = -300 }); + const over = s.rect(.{ .w = 60, .h = 60, .x = 100, .y = 50, .fill = anim.green }); + const level = s.value(0); + s.play(.{ title.fadeIn(), under.fadeIn(), table.fadeIn(), over.fadeIn() }, .{ .seconds = 1 }); + s.play(.{ table.move(.{ .x = 0 }), level.to(0.5) }, .{ .seconds = 1 }); + s.pause(); + s.play(.{ anim.camera(.{ .zoom = 2 }), table.move(.{ .opacity = 0.5 }) }, .{ .seconds = 1 }); + // After the plays: the value as it stands now. + panel.level = s.get(level); +} diff --git a/test/svg/dashes.svg b/test/svg/dashes.svg new file mode 100644 index 0000000..ebd9ce6 --- /dev/null +++ b/test/svg/dashes.svg @@ -0,0 +1,6 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 120" width="200" height="120"> + <line x1="10" y1="20" x2="190" y2="20" stroke="#58c4dd" stroke-width="6" stroke-dasharray="20 10"/> + <line x1="10" y1="45" x2="190" y2="45" stroke="#83c167" stroke-width="6" stroke-dasharray="5" stroke-dashoffset="3"/> + <circle cx="60" cy="85" r="28" fill="none" stroke="#fc6255" stroke-width="5" stroke-dasharray="12 6 2 6"/> + <rect x="120" y="60" width="60" height="50" fill="none" stroke="#f7d96f" stroke-width="4" stroke-dasharray="15 5" stroke-linecap="round"/> +</svg> diff --git a/test/svg/fill-rules.svg b/test/svg/fill-rules.svg new file mode 100644 index 0000000..af0e3b8 --- /dev/null +++ b/test/svg/fill-rules.svg @@ -0,0 +1,6 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 220 110" width="220" height="110"> + <!-- The same self-intersecting star and nested squares under both rules. --> + <path d="M55 5 L85 95 L10 40 L100 40 L25 95 Z" fill="#fc6255" fill-rule="nonzero"/> + <path d="M165 5 L195 95 L120 40 L210 40 L135 95 Z" fill="#fc6255" fill-rule="evenodd"/> + <path d="M5 100 h40 v8 h-40z M15 102 h20 v4 h-20z" fill="#58c4dd" fill-rule="evenodd"/> +</svg> diff --git a/test/svg/gradients.svg b/test/svg/gradients.svg new file mode 100644 index 0000000..a5f2c81 --- /dev/null +++ b/test/svg/gradients.svg @@ -0,0 +1,16 @@ +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 240 160" width="240" height="160"> + <defs> + <linearGradient id="lin"><stop offset="0" stop-color="#58c4dd"/><stop offset="1" stop-color="#fc6255"/></linearGradient> + <linearGradient id="diag" x1="0" y1="0" x2="1" y2="1"><stop offset="0%" stop-color="#f7d96f"/><stop offset="50%" stop-color="#83c167"/><stop offset="100%" stop-color="#1c1c1f"/></linearGradient> + <linearGradient id="user" gradientUnits="userSpaceOnUse" x1="10" y1="0" x2="60" y2="0" spreadMethod="reflect"><stop offset="0" stop-color="#ece6e2"/><stop offset="1" stop-color="#9a72ac"/></linearGradient> + <linearGradient id="derived" xlink:href="#lin" gradientTransform="rotate(90 0.5 0.5)"/> + <radialGradient id="rad"><stop offset="0" stop-color="#ffffff"/><stop offset="1" stop-color="#58c4dd" stop-opacity="0.2"/></radialGradient> + <radialGradient id="focus" cx="0.5" cy="0.5" r="0.5" fx="0.3" fy="0.3"><stop offset="0" stop-color="#f7d96f"/><stop offset="1" stop-color="#c55f73"/></radialGradient> + </defs> + <rect x="10" y="10" width="100" height="40" fill="url(#lin)"/> + <rect x="130" y="10" width="100" height="60" fill="url(#diag)"/> + <rect x="10" y="60" width="110" height="30" fill="url(#user)"/> + <rect x="10" y="100" width="50" height="50" fill="url(#derived)"/> + <circle cx="110" cy="125" r="30" fill="url(#rad)"/> + <ellipse cx="190" cy="115" rx="40" ry="35" fill="url(#focus)" stroke="url(#lin)" stroke-width="4"/> +</svg> diff --git a/test/svg/opacity.svg b/test/svg/opacity.svg new file mode 100644 index 0000000..c0d0b8a --- /dev/null +++ b/test/svg/opacity.svg @@ -0,0 +1,7 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 120" width="200" height="120"> + <rect x="10" y="10" width="80" height="80" fill="#58c4dd"/> + <circle cx="90" cy="60" r="40" fill="#fc6255" fill-opacity="0.5"/> + <circle cx="150" cy="60" r="40" fill="#83c167" opacity="0.4" stroke="#ece6e2" stroke-width="8" stroke-opacity="0.8"/> + <g opacity="0.6"><rect x="20" y="95" width="160" height="15" fill="#f7d96f"/></g> + <rect x="120" y="20" width="60" height="20" style="fill: rgb(154, 114, 172); fill-opacity: 70%"/> +</svg> diff --git a/test/svg/paths.svg b/test/svg/paths.svg new file mode 100644 index 0000000..173ec9e --- /dev/null +++ b/test/svg/paths.svg @@ -0,0 +1,8 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 120" width="200" height="120"> + <!-- Every path command, absolute and relative, with implicit repeats. --> + <path d="M10 10 L60 10 l20 20 H120 v30 h-40 Z" fill="#58c4dd"/> + <path d="M140 10 C160 0 190 30 180 50 S150 80 140 60 Q130 40 150 30 T170 20" fill="none" stroke="#fc6255" stroke-width="3"/> + <path d="M20 80 a30 20 0 1 0 60 0 a30 20 0 1 0 -60 0z" fill="#83c167"/> + <path d="M100 110A40 40 0 0 1 180 110" fill="none" stroke="#f7d96f" stroke-width="4"/> + <path d="M100,70 l10-10 10,10-10,10z" fill="#9a72ac"/> +</svg> diff --git a/test/svg/shapes.svg b/test/svg/shapes.svg new file mode 100644 index 0000000..07aa01a --- /dev/null +++ b/test/svg/shapes.svg @@ -0,0 +1,8 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 140" width="240" height="140"> + <rect x="10" y="10" width="60" height="40" fill="#58c4dd"/> + <rect x="80" y="10" width="60" height="40" rx="12" fill="#83c167" stroke="#ece6e2" stroke-width="3"/> + <rect x="150" y="10" width="80" height="40" rx="30" ry="10" fill="#9a72ac"/> + <circle cx="40" cy="95" r="30" fill="#fc6255"/> + <ellipse cx="120" cy="95" rx="40" ry="20" fill="#f7d96f"/> + <polygon points="180,70 230,90 210,130 170,120" fill="#5cd0b3" stroke="#1c1c1f" stroke-width="2"/> +</svg> diff --git a/test/svg/strokes.svg b/test/svg/strokes.svg new file mode 100644 index 0000000..6a25a16 --- /dev/null +++ b/test/svg/strokes.svg @@ -0,0 +1,11 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 160" width="240" height="160"> + <!-- Joins, caps, miter limits and thin lines. --> + <polyline points="10,50 40,10 70,50" fill="none" stroke="#58c4dd" stroke-width="10" stroke-linejoin="miter"/> + <polyline points="90,50 120,10 150,50" fill="none" stroke="#83c167" stroke-width="10" stroke-linejoin="round"/> + <polyline points="170,50 200,10 230,50" fill="none" stroke="#f7d96f" stroke-width="10" stroke-linejoin="bevel"/> + <line x1="20" y1="80" x2="220" y2="80" stroke="#fc6255" stroke-width="12" stroke-linecap="butt"/> + <line x1="20" y1="105" x2="220" y2="105" stroke="#fc6255" stroke-width="12" stroke-linecap="round"/> + <line x1="20" y1="130" x2="220" y2="130" stroke="#fc6255" stroke-width="12" stroke-linecap="square"/> + <polyline points="10,150 120,145 230,150" fill="none" stroke="#ece6e2" stroke-width="6" stroke-miterlimit="1"/> + <line x1="10" y1="157" x2="230" y2="157" stroke="#ece6e2" stroke-width="0.5"/> +</svg> diff --git a/test/svg/transforms.svg b/test/svg/transforms.svg new file mode 100644 index 0000000..0eb7756 --- /dev/null +++ b/test/svg/transforms.svg @@ -0,0 +1,13 @@ +<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 150" width="200" height="150"> + <g transform="translate(50 50)"> + <rect x="-20" y="-20" width="40" height="40" fill="#58c4dd" transform="rotate(30)"/> + </g> + <g transform="translate(140 50) scale(1.5 0.7)"> + <circle r="25" fill="#83c167" stroke="#ece6e2" stroke-width="4"/> + </g> + <rect x="20" y="100" width="40" height="30" fill="#fc6255" transform="skewX(30)"/> + <g transform="matrix(0.8 0.3 -0.3 0.8 130 95)"> + <path d="M0 0 h40 v30 h-40z" fill="#f7d96f"/> + </g> + <rect x="150" y="100" width="30" height="30" fill="#9a72ac" transform="rotate(45 165 115)"/> +</svg> diff --git a/test/svg/typst-equation.typ b/test/svg/typst-equation.typ new file mode 100644 index 0000000..da0c34c --- /dev/null +++ b/test/svg/typst-equation.typ @@ -0,0 +1,3 @@ +#set page(width: auto, height: auto, margin: 6pt, fill: none) +#set text(fill: rgb("#ece6e2"), size: 28pt) +$ integral_0^infinity e^(-x^2) dif x = sqrt(pi) / 2 $ diff --git a/test/svg/typst-shapes.typ b/test/svg/typst-shapes.typ new file mode 100644 index 0000000..b9bcdff --- /dev/null +++ b/test/svg/typst-shapes.typ @@ -0,0 +1,8 @@ +#set page(width: auto, height: auto, margin: 6pt, fill: none) +#stack(dir: ltr, spacing: 8pt, + rect(width: 60pt, height: 30pt, stroke: 2pt + red, fill: blue, radius: 5pt), + circle(radius: 16pt, fill: gradient.linear(red, blue)), + circle(radius: 16pt, fill: gradient.radial(yellow, purple)), + line(length: 60pt, stroke: (paint: green, thickness: 3pt, dash: "dashed")), + polygon(fill: teal, stroke: 1pt + white, (0pt, 20pt), (20pt, 0pt), (40pt, 20pt)), +) diff --git a/test/svg/typst-text.typ b/test/svg/typst-text.typ new file mode 100644 index 0000000..ce6281d --- /dev/null +++ b/test/svg/typst-text.typ @@ -0,0 +1,5 @@ +#set page(width: 300pt, height: auto, margin: 6pt, fill: none) +#set text(fill: rgb("#ece6e2"), size: 12pt) += Vector sketches +The quick brown fox jumps over the lazy dog. *Bold*, _italic_, `mono`, and +Unicode: é ß ñ — “quotes” — 1 ≤ 2 → ∞. diff --git a/test/svg/use.svg b/test/svg/use.svg new file mode 100644 index 0000000..5336f01 --- /dev/null +++ b/test/svg/use.svg @@ -0,0 +1,11 @@ +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 200 100" width="200" height="100"> + <!-- Symbols and uses, with style inherited from the use element. --> + <defs> + <symbol id="leaf"><path d="M0 0 C20 -20 40 -20 50 0 C40 20 20 20 0 0Z"/></symbol> + <circle id="dot" r="8"/> + </defs> + <use xlink:href="#leaf" x="10" y="30" fill="#83c167"/> + <use href="#leaf" x="70" y="30" fill="#5cd0b3" transform="rotate(20 95 30)"/> + <g fill="#fc6255"><use href="#dot" x="150" y="30"/><use href="#dot" x="175" y="30" fill="#f7d96f"/></g> + <g color="#58c4dd"><rect x="20" y="60" width="160" height="25" fill="currentColor"/></g> +</svg> diff --git a/test/svgview.zig b/test/svgview.zig new file mode 100644 index 0000000..0675c83 --- /dev/null +++ b/test/svgview.zig @@ -0,0 +1,78 @@ +//! svgview: shows one SVG from a list, fitted to its own canvas size, with +//! either renderer. The differential tests drive it headless: set `index` +//! and `backend` from the console, screenshot, compare with resvg. +//! +//! The list is `$SVGVIEW_LIST` (default `svgview.txt`): one `path width +//! height` per line. +const std = @import("std"); +const sk = @import("sketch"); +const vector = @import("vector"); + +pub const name = "svgview"; +pub const width = 512; +pub const height = 512; + +pub const State = struct { + index: u32 = 0, + backend: vector.Backend = .gpu, +}; + +pub const Session = struct { + gpu: ?vector.Renderer(.gpu) = null, + cpu: ?vector.Renderer(.cpu) = null, +}; + +const Entry = struct { path: []const u8, w: u32, h: u32 }; + +fn entry(host: sk.Host, index: u32) ?Entry { + const list = host.readFile(host.env("SVGVIEW_LIST") orelse "svgview.txt"); + var lines = std.mem.tokenizeScalar(u8, list, '\n'); + var i: u32 = 0; + while (lines.next()) |line| : (i += 1) { + if (i != index) continue; + var words = std.mem.tokenizeScalar(u8, line, ' '); + const path = words.next() orelse return null; + const w = std.fmt.parseInt(u32, words.next() orelse return null, 10) catch return null; + const h = std.fmt.parseInt(u32, words.next() orelse return null, 10) catch return null; + return .{ .path = path, .w = w, .h = h }; + } + return null; +} + +pub fn draw(s: *const State, ss: *Session, f: *sk.Frame) void { + const e = entry(f.host, s.index) orelse { + f.clear(0x400000); + f.text(8, 8, 2, "no such entry", 0xffffff); + return; + }; + f.canvas(e.w, e.h); + show(s, ss, f, e) catch |err| { + f.clear(0x400000); + f.print(8, 8, 2, 0xffffff, "{s}: {t}", .{ e.path, err }); + }; +} + +fn show(s: *const State, ss: *Session, f: *sk.Frame, e: Entry) !void { + const d = try vector.svg.parse(f.scratch, f.host.readFile(e.path)); + // As resvg -w does: scale to the width, from the top-left corner (the + // height is rounded, so fitting both would drift by a fraction). + const s_ = @as(f32, @floatFromInt(e.w)) / d.view.w; + const place = vector.geom.Affine.scale(s_, s_).mul(.translate(-d.view.x, -d.view.y)); + switch (s.backend) { + .gpu => try render(&ss.gpu, f, d, place, e), + .cpu => try render(&ss.cpu, f, d, place, e), + } +} + +fn render(slot: anytype, f: *sk.Frame, d: vector.Drawing, place: vector.geom.Affine, e: Entry) !void { + if (slot.*) |*r| if (r.width() != e.w or r.height() != e.h) { + r.deinit(f.host); + slot.* = null; + }; + if (slot.* == null) slot.* = try .init(f.session, f.host, e.w, e.h); + const r = &slot.*.?; + // resvg's `--background '#808080'`. + r.begin(f, .{ 128.0 / 255.0, 128.0 / 255.0, 128.0 / 255.0, 1 }); + try r.draw(d, place, 1); + r.present(f, sk.Rect{ .w = @floatFromInt(e.w), .h = @floatFromInt(e.h) }); +} |
