Skip to content

Commit

Permalink
Context: new managed interface
Browse files Browse the repository at this point in the history
This commit is the final piece of the current API overhaul - the managed
Context.

This is now the place where all managed drawing operations are expected
to take place. It pulls together all of our umnanaged components -
surfaces, patterns, paths, and painter operations, and provides a
managed frontend to these operations, doing things such as holding an
allocator for said unmanaged operations (that need one), and providing
synchronization of settings such as transformation matrices and
tolerance.

Helpers have been added to get and set every setting within the context;
it's now considered unsupported behavior for a consumer to set a field
directly on a context (or need to set one directly, for that matter,
which should help steer further updates).

If someone requires functionality that the Context is incapable of
providing or would be considered unsuitable for the context, the
recommendation is to now use the individual components in an unmanaged
way. For this, the Context serves as an example for how this can be
done.
  • Loading branch information
vancluever committed Nov 8, 2024
1 parent 79614d1 commit 8f7c448
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 8f7c448

Please sign in to comment.