font/sprite: fix z2d StaticPath accounting + undefined use

Annotate the node count of all uses of z2d `StaticPath` to verify
correctness, adjusted the size of a couple which were oversized, and
changed all painter calls that take node slices from `StaticPath`s to
use the slice from the wrapped `ArrayList` so that we don't include any
potentially `undefined` nodes at the end of the list, which I think was
causing a crash before.
This commit is contained in:
Qwerasd
2024-12-23 04:21:33 -05:00
parent 98651ab0e5
commit 9f4d9dc36e
2 changed files with 43 additions and 43 deletions

View File

@ -2515,19 +2515,19 @@ fn draw_smooth_mosaic(
const right: f64 = @floatFromInt(self.metrics.cell_width);
var path: z2d.StaticPath(12) = .{};
path.init();
path.init(); // nodes.len = 0
if (mosaic.tl) path.lineTo(left, top);
if (mosaic.ul) path.lineTo(left, upper);
if (mosaic.ll) path.lineTo(left, lower);
if (mosaic.bl) path.lineTo(left, bottom);
if (mosaic.bc) path.lineTo(center, bottom);
if (mosaic.br) path.lineTo(right, bottom);
if (mosaic.lr) path.lineTo(right, lower);
if (mosaic.ur) path.lineTo(right, upper);
if (mosaic.tr) path.lineTo(right, top);
if (mosaic.tc) path.lineTo(center, top);
path.close();
if (mosaic.tl) path.lineTo(left, top); // +1, nodes.len = 1
if (mosaic.ul) path.lineTo(left, upper); // +1, nodes.len = 2
if (mosaic.ll) path.lineTo(left, lower); // +1, nodes.len = 3
if (mosaic.bl) path.lineTo(left, bottom); // +1, nodes.len = 4
if (mosaic.bc) path.lineTo(center, bottom); // +1, nodes.len = 5
if (mosaic.br) path.lineTo(right, bottom); // +1, nodes.len = 6
if (mosaic.lr) path.lineTo(right, lower); // +1, nodes.len = 7
if (mosaic.ur) path.lineTo(right, upper); // +1, nodes.len = 8
if (mosaic.tr) path.lineTo(right, top); // +1, nodes.len = 9
if (mosaic.tc) path.lineTo(center, top); // +1, nodes.len = 10
path.close(); // +2, nodes.len = 12
try z2d.painter.fill(
canvas.alloc,
@ -2535,7 +2535,7 @@ fn draw_smooth_mosaic(
&.{ .opaque_pattern = .{
.pixel = .{ .alpha8 = .{ .a = @intFromEnum(Shade.on) } },
} },
&path.nodes,
path.wrapped_path.nodes.items,
.{},
);
}
@ -2560,12 +2560,12 @@ fn draw_edge_triangle(
};
var path: z2d.StaticPath(5) = .{};
path.init();
path.init(); // nodes.len = 0
path.moveTo(center, middle);
path.lineTo(x0, y0);
path.lineTo(x1, y1);
path.close();
path.moveTo(center, middle); // +1, nodes.len = 1
path.lineTo(x0, y0); // +1, nodes.len = 2
path.lineTo(x1, y1); // +1, nodes.len = 3
path.close(); // +2, nodes.len = 5
try z2d.painter.fill(
canvas.alloc,
@ -2573,7 +2573,7 @@ fn draw_edge_triangle(
&.{ .opaque_pattern = .{
.pixel = .{ .alpha8 = .{ .a = @intFromEnum(Shade.on) } },
} },
&path.nodes,
path.wrapped_path.nodes.items,
.{},
);
}

View File

@ -184,13 +184,13 @@ pub const Canvas = struct {
/// Draw and fill a quad.
pub fn quad(self: *Canvas, q: Quad(f64), color: Color) !void {
var path: z2d.StaticPath(6) = .{};
path.init();
path.init(); // nodes.len = 0
path.moveTo(q.p0.x, q.p0.y);
path.lineTo(q.p1.x, q.p1.y);
path.lineTo(q.p2.x, q.p2.y);
path.lineTo(q.p3.x, q.p3.y);
path.close();
path.moveTo(q.p0.x, q.p0.y); // +1, nodes.len = 1
path.lineTo(q.p1.x, q.p1.y); // +1, nodes.len = 2
path.lineTo(q.p2.x, q.p2.y); // +1, nodes.len = 3
path.lineTo(q.p3.x, q.p3.y); // +1, nodes.len = 4
path.close(); // +2, nodes.len = 6
try z2d.painter.fill(
self.alloc,
@ -198,7 +198,7 @@ pub const Canvas = struct {
&.{ .opaque_pattern = .{
.pixel = .{ .alpha8 = .{ .a = @intFromEnum(color) } },
} },
&path.nodes,
path.wrapped_path.nodes.items,
.{},
);
}
@ -206,12 +206,12 @@ pub const Canvas = struct {
/// Draw and fill a triangle.
pub fn triangle(self: *Canvas, t: Triangle(f64), color: Color) !void {
var path: z2d.StaticPath(5) = .{};
path.init();
path.init(); // nodes.len = 0
path.moveTo(t.p0.x, t.p0.y);
path.lineTo(t.p1.x, t.p1.y);
path.lineTo(t.p2.x, t.p2.y);
path.close();
path.moveTo(t.p0.x, t.p0.y); // +1, nodes.len = 1
path.lineTo(t.p1.x, t.p1.y); // +1, nodes.len = 2
path.lineTo(t.p2.x, t.p2.y); // +1, nodes.len = 3
path.close(); // +2, nodes.len = 5
try z2d.painter.fill(
self.alloc,
@ -219,18 +219,18 @@ pub const Canvas = struct {
&.{ .opaque_pattern = .{
.pixel = .{ .alpha8 = .{ .a = @intFromEnum(color) } },
} },
&path.nodes,
path.wrapped_path.nodes.items,
.{},
);
}
pub fn triangle_outline(self: *Canvas, t: Triangle(f64), thickness: f64, color: Color) !void {
var path: z2d.StaticPath(5) = .{};
path.init();
var path: z2d.StaticPath(3) = .{};
path.init(); // nodes.len = 0
path.moveTo(t.p0.x, t.p0.y);
path.lineTo(t.p1.x, t.p1.y);
path.lineTo(t.p2.x, t.p2.y);
path.moveTo(t.p0.x, t.p0.y); // +1, nodes.len = 1
path.lineTo(t.p1.x, t.p1.y); // +1, nodes.len = 2
path.lineTo(t.p2.x, t.p2.y); // +1, nodes.len = 3
try z2d.painter.stroke(
self.alloc,
@ -238,7 +238,7 @@ pub const Canvas = struct {
&.{ .opaque_pattern = .{
.pixel = .{ .alpha8 = .{ .a = @intFromEnum(color) } },
} },
&path.nodes,
path.wrapped_path.nodes.items,
.{
.line_cap_mode = .round,
.line_width = thickness,
@ -248,11 +248,11 @@ pub const Canvas = struct {
/// Stroke a line.
pub fn line(self: *Canvas, l: Line(f64), thickness: f64, color: Color) !void {
var path: z2d.StaticPath(3) = .{};
path.init();
var path: z2d.StaticPath(2) = .{};
path.init(); // nodes.len = 0
path.moveTo(l.p0.x, l.p0.y);
path.lineTo(l.p1.x, l.p1.y);
path.moveTo(l.p0.x, l.p0.y); // +1, nodes.len = 1
path.lineTo(l.p1.x, l.p1.y); // +1, nodes.len = 2
try z2d.painter.stroke(
self.alloc,
@ -260,7 +260,7 @@ pub const Canvas = struct {
&.{ .opaque_pattern = .{
.pixel = .{ .alpha8 = .{ .a = @intFromEnum(color) } },
} },
&path.nodes,
path.wrapped_path.nodes.items,
.{
.line_cap_mode = .round,
.line_width = thickness,