Skip to content

Commit

Permalink
Merge pull request #56 from vancluever/managed-context
Browse files Browse the repository at this point in the history
Context: new managed interface
  • Loading branch information
vancluever authored Nov 8, 2024
2 parents 79614d1 + ac2b5ea commit ec4848f
Show file tree
Hide file tree
Showing 49 changed files with 1,522 additions and 1,597 deletions.
29 changes: 10 additions & 19 deletions spec/003_fill_triangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,19 @@ pub const filename = "003_fill_triangle";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);
const margin = 10;
try path.moveTo(alloc, 0 + margin, 0 + margin);
try path.lineTo(alloc, width - margin - 1, 0 + margin);
try path.lineTo(alloc, width / 2 - 1, height - margin - 1);
try path.close(alloc);
try context.moveTo(0 + margin, 0 + margin);
try context.lineTo(width - margin - 1, 0 + margin);
try context.lineTo(width / 2 - 1, height - margin - 1);
try context.close();

try context.fill(alloc, path);
try context.fill();

return sfc;
}
31 changes: 11 additions & 20 deletions spec/004_fill_square.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,20 @@ pub const filename = "004_fill_square";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);
const margin = 50;
try path.moveTo(alloc, 0 + margin, 0 + margin);
try path.lineTo(alloc, width - margin - 1, 0 + margin);
try path.lineTo(alloc, width - margin - 1, height - margin - 1);
try path.lineTo(alloc, 0 + margin, height - margin - 1);
try path.close(alloc);
try context.moveTo(0 + margin, 0 + margin);
try context.lineTo(width - margin - 1, 0 + margin);
try context.lineTo(width - margin - 1, height - margin - 1);
try context.lineTo(0 + margin, height - margin - 1);
try context.close();

try context.fill(alloc, path);
try context.fill();

return sfc;
}
30 changes: 11 additions & 19 deletions spec/005_fill_trapezoid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,23 @@ pub const filename = "005_fill_trapezoid";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);

const margin_top = 89;
const margin_bottom = 50;
const margin_y = 100;
try path.moveTo(alloc, 0 + margin_top, 0 + margin_y);
try path.lineTo(alloc, width - margin_top - 1, 0 + margin_y);
try path.lineTo(alloc, width - margin_bottom - 1, height - margin_y - 1);
try path.lineTo(alloc, 0 + margin_bottom, height - margin_y - 1);
try path.close(alloc);
try context.moveTo(0 + margin_top, 0 + margin_y);
try context.lineTo(width - margin_top - 1, 0 + margin_y);
try context.lineTo(width - margin_bottom - 1, height - margin_y - 1);
try context.lineTo(0 + margin_bottom, height - margin_y - 1);
try context.close();

try context.fill(alloc, path);
try context.fill();

return sfc;
}
38 changes: 15 additions & 23 deletions spec/006_fill_star_even_odd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,27 @@ pub const filename = "006_fill_star_even_odd";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.fill_rule = .even_odd,
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);
context.setFillRule(.even_odd);

const margin = 20;
const x_scale = 3;
const y_scale = 5;
// With all 5 points numbered 1-5 clockwise, we draw odds first (1, 3, 5),
// then evens (4, 2), with the close connecting 4 and 1.
try path.moveTo(alloc, width / 2, 0 + margin); // 1
try path.lineTo(alloc, width - margin * x_scale - 1, height - margin - 1); // 3
try path.lineTo(alloc, 0 + margin, 0 + margin * y_scale); // 5
try path.lineTo(alloc, width - margin - 1, 0 + margin * y_scale); // 2
try path.lineTo(alloc, 0 + margin * x_scale, height - margin - 1); // 4
try path.close(alloc);

try context.fill(alloc, path);
try context.moveTo(width / 2, 0 + margin); // 1
try context.lineTo(width - margin * x_scale - 1, height - margin - 1); // 3
try context.lineTo(0 + margin, 0 + margin * y_scale); // 5
try context.lineTo(width - margin - 1, 0 + margin * y_scale); // 2
try context.lineTo(0 + margin * x_scale, height - margin - 1); // 4
try context.close();

try context.fill();

return sfc;
}
32 changes: 12 additions & 20 deletions spec/007_fill_bezier.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,18 @@ pub const filename = "007_fill_bezier";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);

try path.moveTo(alloc, 19, 249);
try path.curveTo(alloc, 89, 49, 209, 49, 279, 249);
try path.close(alloc);

try context.fill(alloc, path);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);

try context.moveTo(19, 249);
try context.curveTo(89, 49, 209, 49, 279, 249);
try context.close();

try context.fill();

return sfc;
}
28 changes: 10 additions & 18 deletions spec/008_stroke_triangle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,20 @@ pub const filename = "008_stroke_triangle";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);

const margin = 10;
try path.moveTo(alloc, 0 + margin, 0 + margin);
try path.lineTo(alloc, width - margin - 1, 0 + margin);
try path.lineTo(alloc, width / 2 - 1, height - margin - 1);
try path.close(alloc);
try context.moveTo(0 + margin, 0 + margin);
try context.lineTo(width - margin - 1, 0 + margin);
try context.lineTo(width / 2 - 1, height - margin - 1);
try context.close();

try context.stroke(alloc, path);
try context.stroke();

return sfc;
}
30 changes: 11 additions & 19 deletions spec/009_stroke_square.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,21 @@ pub const filename = "009_stroke_square";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);

const margin = 50;
try path.moveTo(alloc, 0 + margin, 0 + margin);
try path.lineTo(alloc, width - margin - 1, 0 + margin);
try path.lineTo(alloc, width - margin - 1, height - margin - 1);
try path.lineTo(alloc, 0 + margin, height - margin - 1);
try path.close(alloc);
try context.moveTo(0 + margin, 0 + margin);
try context.lineTo(width - margin - 1, 0 + margin);
try context.lineTo(width - margin - 1, height - margin - 1);
try context.lineTo(0 + margin, height - margin - 1);
try context.close();

try context.stroke(alloc, path);
try context.stroke();

return sfc;
}
30 changes: 11 additions & 19 deletions spec/010_stroke_trapezoid.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,23 @@ pub const filename = "010_stroke_trapezoid";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);

const margin_top = 89;
const margin_bottom = 50;
const margin_y = 100;
try path.moveTo(alloc, 0 + margin_top, 0 + margin_y);
try path.lineTo(alloc, width - margin_top - 1, 0 + margin_y);
try path.lineTo(alloc, width - margin_bottom - 1, height - margin_y - 1);
try path.lineTo(alloc, 0 + margin_bottom, height - margin_y - 1);
try path.close(alloc);
try context.moveTo(0 + margin_top, 0 + margin_y);
try context.lineTo(width - margin_top - 1, 0 + margin_y);
try context.lineTo(width - margin_bottom - 1, height - margin_y - 1);
try context.lineTo(0 + margin_bottom, height - margin_y - 1);
try context.close();

try context.stroke(alloc, path);
try context.stroke();

return sfc;
}
38 changes: 15 additions & 23 deletions spec/011_stroke_star.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,27 @@ pub const filename = "011_stroke_star";
pub fn render(alloc: mem.Allocator, aa_mode: z2d.options.AntiAliasMode) !z2d.Surface {
const width = 300;
const height = 300;
const sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context: z2d.Context = .{
.surface = sfc,
.pattern = .{
.opaque_pattern = .{
.pixel = .{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } }, // White on black
},
},
.line_width = 6,
.anti_aliasing_mode = aa_mode,
};

var path = try z2d.Path.initCapacity(alloc, 0);
defer path.deinit(alloc);
var sfc = try z2d.Surface.init(.image_surface_rgb, alloc, width, height);

var context = try z2d.Context.init(alloc, &sfc);
defer context.deinit();
context.setSource(.{ .rgb = .{ .r = 0xFF, .g = 0xFF, .b = 0xFF } });
context.setAntiAliasingMode(aa_mode);
context.setLineWidth(6);

const margin = 20;
const x_scale = 3;
const y_scale = 5;
// With all 5 points numbered 1-5 clockwise, we draw odds first (1, 3, 5),
// then evens (4, 2), with the close connecting 4 and 1.
try path.moveTo(alloc, width / 2, 0 + margin); // 1
try path.lineTo(alloc, width - margin * x_scale - 1, height - margin - 1); // 3
try path.lineTo(alloc, 0 + margin, 0 + margin * y_scale); // 5
try path.lineTo(alloc, width - margin - 1, 0 + margin * y_scale); // 2
try path.lineTo(alloc, 0 + margin * x_scale, height - margin - 1); // 4
try path.close(alloc);

try context.stroke(alloc, path);
try context.moveTo(width / 2, 0 + margin); // 1
try context.lineTo(width - margin * x_scale - 1, height - margin - 1); // 3
try context.lineTo(0 + margin, 0 + margin * y_scale); // 5
try context.lineTo(width - margin - 1, 0 + margin * y_scale); // 2
try context.lineTo(0 + margin * x_scale, height - margin - 1); // 4
try context.close();

try context.stroke();

return sfc;
}
Loading

0 comments on commit ec4848f

Please sign in to comment.