diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/sketch/draw.zig | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/src/sketch/draw.zig b/src/sketch/draw.zig index c151ab2..e479db5 100644 --- a/src/sketch/draw.zig +++ b/src/sketch/draw.zig @@ -200,6 +200,48 @@ pub const Tessellation = struct { return t.stroke(gpa, points, style, xf); } + /// `points` as an open path of dashes: `pattern[0]` units drawn, then + /// `pattern[1]` skipped, measured along the path from its start, so a + /// dash turns a corner whole (with `style`'s join). For a closed + /// outline, give the first point again at the end. + pub fn strokeDashes(t: *Tessellation, gpa: Allocator, points: []const Vec, pattern: [2]f32, style: Stroke, xf: Affine) Error!void { + if (points.len < 2 or !(pattern[0] > 0) or !(pattern[1] >= 0)) return; + var open = style; + open.closed = false; + var piece: std.ArrayList(Vec) = .empty; + defer piece.deinit(gpa); + try piece.append(gpa, points[0]); + var on = true; + var left = pattern[0]; + for (points[0 .. points.len - 1], points[1..]) |a, b| { + var rest = math.length(b - a); + if (rest == 0) continue; + const dir = math.scale(b - a, 1 / rest); + var at = a; + while (rest > left) { + at += math.scale(dir, left); + rest -= left; + if (on) { + try piece.append(gpa, at); + try t.stroke(gpa, piece.items, open, xf); + } + piece.clearRetainingCapacity(); + on = !on; + if (on) try piece.append(gpa, at); + left = pattern[@intFromBool(!on)]; + // A pattern with no gap is one dash after another. + if (left == 0) { + on = true; + left = pattern[0]; + try piece.append(gpa, at); + } + } + left -= rest; + if (on) try piece.append(gpa, b); + } + if (on and piece.items.len > 1) try t.stroke(gpa, piece.items, open, xf); + } + /// The rect's area. pub fn fillRect(t: *Tessellation, gpa: Allocator, r: math.Rect, xf: Affine) Error!void { if (!(r.w > 0) or !(r.h > 0)) return; @@ -711,6 +753,16 @@ pub fn point(f: *root.Frame, at: Vec, pen: Pen) void { f.shape(&t, color); } +/// `polyline` in dashes, `pattern[0]` units drawn and `pattern[1]` skipped +/// along the path, in the stroke colour (no fill): a freed block's outline, +/// a link on its way out. Give the first point again to close the outline. +pub fn dashed(f: *root.Frame, points: []const Vec, pattern: [2]f32, pen: Pen) void { + const color = pen.stroke orelse return; + var t: Tessellation = .empty; + t.strokeDashes(f.scratch, points, pattern, pen.outline(false), pen.xf) catch return dropped(f); + f.shape(&t, color); +} + fn path(f: *root.Frame, points: []const Vec, pen: Pen, closed: bool) void { if (pen.fill) |color| { var t: Tessellation = .empty; @@ -941,6 +993,35 @@ test "strokes: lines with round ends, acute zig-zags without overlap" { try testing.expectEqual(@as(usize, 2), t.ends.items.len); } +test "dashes: measured along the path, whole round a corner" { + const gpa = testing.allocator; + var t: Tessellation = .empty; + defer t.deinit(gpa); + // 100 long, 12 on and 8 off: five dashes, the last ending at 92. + try t.strokeDashes(gpa, &.{ .{ 0, 0 }, .{ 100, 0 } }, .{ 12, 8 }, .{ .width = 4 }, .identity); + try expectValid(&t); + try testing.expectEqual(@as(usize, 5), t.ends.items.len); + var reach: f32 = 0; + for (t.points.items) |p| reach = @max(reach, p[0]); + try testing.expectEqual(@as(f32, 92), reach); + + // A dash that meets a corner goes round it as one piece: 30 of path, + // 20 on from the start, so it ends 10 down the second side. + t.clearRetainingCapacity(); + try t.strokeDashes(gpa, &.{ .{ 0, 0 }, .{ 10, 0 }, .{ 10, 20 } }, .{ 20, 100 }, .{ .width = 2 }, .identity); + try expectValid(&t); + try testing.expectEqual(@as(usize, 1), t.ends.items.len); + var down: f32 = 0; + for (t.points.items) |p| down = @max(down, p[1]); + try testing.expectEqual(@as(f32, 10), down); + + // Nothing to dash. + t.clearRetainingCapacity(); + try t.strokeDashes(gpa, &.{.{ 0, 0 }}, .{ 12, 8 }, .{ .width = 4 }, .identity); + try t.strokeDashes(gpa, &.{ .{ 0, 0 }, .{ 9, 0 } }, .{ 0, 8 }, .{ .width = 4 }, .identity); + try testing.expectEqual(@as(usize, 0), t.indices.items.len); +} + test "nothing to draw draws nothing" { const gpa = testing.allocator; var t: Tessellation = .empty; |
