Skip to content

Commit cbdc372

Browse files
committed
Cleanup
1 parent 94ebdc1 commit cbdc372

5 files changed

Lines changed: 94 additions & 80 deletions

File tree

examples/image_pattern.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const cairo = @import("cairo");
44
const setBackground = @import("utils.zig").setBackground;
55

66
/// https://www.cairographics.org/samples/imagepattern/
7-
fn imagePattern(cr: *cairo.Context) !void {
7+
fn imagePattern(allocator: *std.mem.Allocator, cr: *cairo.Context) !void {
88
var image = try cairo.Surface.createFromPng("data/romedalen.png");
99
defer image.destroy();
1010

@@ -26,7 +26,9 @@ fn imagePattern(cr: *cairo.Context) !void {
2626

2727
const sx = @intToFloat(f64, w) / 256.0 * 5.0;
2828
const sy = @intToFloat(f64, h) / 256.0 * 5.0;
29-
var matrix = try cairo.Matrix.initScale(&arena.allocator, sx, sy);
29+
30+
var matrix = try cairo.Matrix.initScale(allocator, sx, sy);
31+
defer matrix.destroy();
3032

3133
pattern.setMatrix(&matrix);
3234

@@ -47,6 +49,6 @@ pub fn main() !void {
4749
defer cr.destroy();
4850

4951
setBackground(&cr);
50-
try imagePattern(&cr);
52+
try imagePattern(std.testing.allocator, &cr);
5153
_ = surface.writeToPng("examples/generated/image_pattern.png");
5254
}

src/drawing/context.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ pub const Context = struct {
6060
}
6161

6262
/// https://cairographics.org/manual/cairo-cairo-t.html#cairo-create
63-
pub fn create(cs: *Surface) !Self {
64-
var c_ptr = c.cairo_create(cs.surface);
63+
pub fn create(surface: *Surface) !Self {
64+
var c_ptr = c.cairo_create(surface.c_ptr);
6565
if (c_ptr == null) return Error.NullPointer;
6666
try checkStatus(c_ptr);
6767
return Self{ .c_ptr = c_ptr.? };
@@ -99,7 +99,7 @@ pub const Context = struct {
9999

100100
/// https://cairographics.org/manual/cairo-cairo-t.html#cairo-mask
101101
pub fn mask(self: *Self, pattern: *Pattern) void {
102-
c.cairo_mask(self.c_ptr, pattern.pattern);
102+
c.cairo_mask(self.c_ptr, pattern.c_ptr);
103103
}
104104

105105
/// https://cairographics.org/manual/cairo-Paths.html#cairo-move-to
@@ -216,7 +216,7 @@ pub const Context = struct {
216216

217217
/// https://cairographics.org/manual/cairo-cairo-t.html#cairo-set-source
218218
pub fn setSource(self: *Self, source: *Pattern) void {
219-
c.cairo_set_source(self.c_ptr, source.pattern);
219+
c.cairo_set_source(self.c_ptr, source.c_ptr);
220220
}
221221

222222
/// https://cairographics.org/manual/cairo-cairo-t.html#cairo-set-source-rgb
@@ -230,8 +230,8 @@ pub const Context = struct {
230230
}
231231

232232
/// https://cairographics.org/manual/cairo-cairo-t.html#cairo-set-source-surface
233-
pub fn setSourceSurface(self: *Self, cs: *Surface, x: f64, y: f64) void {
234-
c.cairo_set_source_surface(self.c_ptr, cs.surface, x, y);
233+
pub fn setSourceSurface(self: *Self, surface: *Surface, x: f64, y: f64) void {
234+
c.cairo_set_source_surface(self.c_ptr, surface.c_ptr, x, y);
235235
}
236236

237237
/// https://cairographics.org/manual/cairo-cairo-t.html#cairo-set-tolerance

src/drawing/pattern.zig

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const enums = @import("../enums.zig");
66
const Error = @import("../errors.zig").Error;
77
const Surface = @import("../surfaces/surface.zig").Surface;
88
const Matrix = @import("../utilities/matrix.zig").Matrix;
9-
const MatrixT = @import("../utilities/matrix.zig").MatrixT;
109

1110
/// Possible return values for cairo_pattern_status ()
1211
/// https://www.cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-status
@@ -29,79 +28,78 @@ const PatternType = enum {
2928
};
3029

3130
pub const Pattern = struct {
32-
pattern: *c.struct__cairo_pattern,
31+
c_ptr: *c.struct__cairo_pattern,
3332

3433
const Self = @This();
3534

3635
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-linear
3736
pub fn createLinear(x0: f64, y0: f64, x1: f64, y1: f64) !Self {
38-
var pattern = c.cairo_pattern_create_linear(x0, y0, x1, y1);
39-
try checkStatus(pattern);
40-
return Self{ .pattern = pattern.? };
37+
var c_ptr = c.cairo_pattern_create_linear(x0, y0, x1, y1);
38+
try checkStatus(c_ptr);
39+
return Self{ .c_ptr = c_ptr.? };
4140
}
4241

4342
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-radial
4443
pub fn createRadial(cx0: f64, cy0: f64, radius0: f64, cx1: f64, cy1: f64, radius1: f64) !Self {
45-
var pattern = c.cairo_pattern_create_radial(cx0, cy0, radius0, cx1, cy1, radius1);
46-
try checkStatus(pattern);
47-
return Self{ .pattern = pattern.? };
44+
var c_ptr = c.cairo_pattern_create_radial(cx0, cy0, radius0, cx1, cy1, radius1);
45+
try checkStatus(c_ptr);
46+
return Self{ .c_ptr = c_ptr.? };
4847
}
4948

5049
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-rgb
5150
pub fn createRgb(r: f64, g: f64, b: f64) !Self {
52-
var pattern = c.cairo_pattern_create_rgb(r, g, b);
53-
try checkStatus(pattern);
54-
return Self{ .pattern = pattern.? };
51+
var c_ptr = c.cairo_pattern_create_rgb(r, g, b);
52+
try checkStatus(c_ptr);
53+
return Self{ .c_ptr = c_ptr.? };
5554
}
5655

5756
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-rgba
5857
pub fn createRgba(r: f64, g: f64, b: f64, alpha: f64) !Self {
59-
var pattern = c.cairo_pattern_create_rgba(r, g, b, alpha);
60-
try checkStatus(pattern);
61-
return Self{ .pattern = pattern.? };
58+
var c_ptr = c.cairo_pattern_create_rgba(r, g, b, alpha);
59+
try checkStatus(c_ptr);
60+
return Self{ .c_ptr = c_ptr.? };
6261
}
6362

6463
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-create-for-surface
6564
pub fn createForSurface(surface: *Surface) !Self {
66-
var pattern = c.cairo_pattern_create_for_surface(surface.surface);
67-
try checkStatus(pattern);
68-
return Self{ .pattern = pattern.? };
65+
var c_ptr = c.cairo_pattern_create_for_surface(surface.c_ptr);
66+
try checkStatus(c_ptr);
67+
return Self{ .c_ptr = c_ptr.? };
6968
}
7069

7170
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-set-extend
7271
pub fn setExtend(self: *Self, extend: enums.Extend) void {
73-
c.cairo_pattern_set_extend(self.pattern, extend.toCairoEnum());
72+
c.cairo_pattern_set_extend(self.c_ptr, extend.toCairoEnum());
7473
}
7574

7675
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-set-matrix
7776
pub fn setMatrix(self: *Self, matrix: *Matrix) void {
78-
c.cairo_pattern_set_matrix(self.pattern, matrix.matrix);
77+
c.cairo_pattern_set_matrix(self.c_ptr, matrix.c_ptr);
7978
}
8079

8180
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-destroy
8281
pub fn destroy(self: *Self) void {
83-
c.cairo_pattern_destroy(self.pattern);
84-
// std.debug.print("cairo.Pattern {} destroyed\n", .{self});
82+
c.cairo_pattern_destroy(self.c_ptr);
8583
}
8684

8785
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-add-color-stop-rgb
8886
pub fn addColorStopRgb(self: *Self, offset: f64, r: f64, g: f64, b: f64) void {
89-
c.cairo_pattern_add_color_stop_rgb(self.pattern, offset, r, g, b);
87+
c.cairo_pattern_add_color_stop_rgb(self.c_ptr, offset, r, g, b);
9088
}
9189

9290
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-add-color-stop-rgba
9391
pub fn addColorStopRgba(self: *Self, offset: f64, r: f64, g: f64, b: f64, alpha: f64) void {
94-
c.cairo_pattern_add_color_stop_rgba(self.pattern, offset, r, g, b, alpha);
92+
c.cairo_pattern_add_color_stop_rgba(self.c_ptr, offset, r, g, b, alpha);
9593
}
9694
};
9795

9896
/// Check whether an error has previously occurred for this pattern.
9997
/// https://cairographics.org/manual/cairo-cairo-pattern-t.html#cairo-pattern-status
100-
fn checkStatus(cairo_pattern: ?*c.struct__cairo_pattern) !void {
101-
if (cairo_pattern == null) {
98+
fn checkStatus(c_ptr: ?*c.struct__cairo_pattern) !void {
99+
if (c_ptr == null) {
102100
return Error.NoMemory;
103101
} else {
104-
const c_enum = c.cairo_pattern_status(cairo_pattern);
102+
const c_enum = c.cairo_pattern_status(c_ptr);
105103
const c_integer = @enumToInt(c_enum);
106104
return switch (c_integer) {
107105
c.CAIRO_STATUS_SUCCESS => {},
@@ -123,7 +121,7 @@ test "checkStatus() returns no error" {
123121
defer pat.destroy();
124122

125123
var errored = false;
126-
_ = checkStatus(pat.pattern) catch |err| {
124+
_ = checkStatus(pat.c_ptr) catch |err| {
127125
errored = true;
128126
};
129127
expectEqual(false, errored);

src/surfaces/surface.zig

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,28 @@ pub const SurfaceType = enum {
5353
};
5454

5555
pub const Surface = struct {
56-
surface: *c.struct__cairo_surface,
56+
c_ptr: *c.struct__cairo_surface,
5757

5858
const Self = @This();
5959

6060
pub fn getType(self: *Self) SurfaceType {
61-
const c_enum = c.cairo_surface_get_type(self.surface);
61+
const c_enum = c.cairo_surface_get_type(self.c_ptr);
6262
const c_integer = @enumToInt(c_enum);
6363
return @intToEnum(SurfaceType, @intCast(u5, c_integer));
6464
}
6565

6666
/// https://cairographics.org/manual/cairo-Image-Surfaces.html#cairo-image-surface-create
6767
pub fn image(width: u16, height: u16) !Surface {
68-
var surface = try image_surface.create(Format.Argb32, width, height);
69-
try checkStatus(surface);
70-
return Self{ .surface = surface };
68+
var c_ptr = try image_surface.create(Format.Argb32, width, height);
69+
try checkStatus(c_ptr);
70+
return Self{ .c_ptr = c_ptr };
7171
}
7272

7373
pub fn setSize(self: *Self, width: f64, height: f64) void {
7474
const st = self.getType();
7575
switch (st) {
76-
SurfaceType.Xcb => xcb_surface.setSize(self.surface, @floatToInt(u16, width), @floatToInt(u16, height)),
77-
SurfaceType.Pdf => pdf_surface.setSize(self.surface, width, height),
76+
SurfaceType.Xcb => xcb_surface.setSize(self.c_ptr, @floatToInt(u16, width), @floatToInt(u16, height)),
77+
SurfaceType.Pdf => pdf_surface.setSize(self.c_ptr, width, height),
7878
else => std.debug.print("`setSize` not implemented for {}\n", .{st}),
7979
}
8080
}
@@ -85,20 +85,20 @@ pub const Surface = struct {
8585
if (st != SurfaceType.Svg) {
8686
return Error.SurfaceTypeMismatch;
8787
} else {
88-
return svg_surface.getDocumentUnit(self.surface);
88+
return svg_surface.getDocumentUnit(self.c_ptr);
8989
}
9090
}
9191

9292
pub fn pdf(comptime filename: [*]const u8, width_pt: f64, height_pt: f64) !Surface {
93-
var surface = try pdf_surface.create(filename, width_pt, height_pt);
94-
try checkStatus(surface);
95-
return Self{ .surface = surface };
93+
var c_ptr = try pdf_surface.create(filename, width_pt, height_pt);
94+
try checkStatus(c_ptr);
95+
return Self{ .c_ptr = c_ptr };
9696
}
9797

9898
pub fn createFromPng(filename: [*]const u8) !Surface {
99-
var surface = try png_surface.create(filename);
100-
try checkStatus(surface);
101-
return Self{ .surface = surface };
99+
var c_ptr = try png_surface.create(filename);
100+
try checkStatus(c_ptr);
101+
return Self{ .c_ptr = c_ptr };
102102
}
103103

104104
/// https://cairographics.org/manual/cairo-Image-Surfaces.html#cairo-image-surface-get-width
@@ -108,7 +108,7 @@ pub const Surface = struct {
108108
std.debug.print("`getWidth` not implemented for {}\n", .{st});
109109
return Error.SurfaceTypeMismatch;
110110
} else {
111-
return @intCast(u16, c.cairo_image_surface_get_width(self.surface));
111+
return @intCast(u16, c.cairo_image_surface_get_width(self.c_ptr));
112112
}
113113
}
114114

@@ -119,15 +119,14 @@ pub const Surface = struct {
119119
std.debug.print("`getHeight` not implemented for {}\n", .{st});
120120
return Error.SurfaceTypeMismatch;
121121
} else {
122-
return @intCast(u16, c.cairo_image_surface_get_height(self.surface));
122+
return @intCast(u16, c.cairo_image_surface_get_height(self.c_ptr));
123123
}
124124
}
125125

126126
pub fn svg(comptime filename: [*]const u8, width_pt: f64, height_pt: f64) !Surface {
127-
var surface = try svg_surface.create(filename, width_pt, height_pt);
128-
try checkStatus(surface);
129-
// std.debug.assert(SurfaceStatus.Success == surfaceStatusAsEnum(surface));
130-
return Self{ .surface = surface };
127+
var c_ptr = try svg_surface.create(filename, width_pt, height_pt);
128+
try checkStatus(c_ptr);
129+
return Self{ .c_ptr = c_ptr };
131130
}
132131

133132
/// https://www.cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surface-get-device
@@ -139,35 +138,34 @@ pub const Surface = struct {
139138
}
140139

141140
pub fn xcb(conn: ?*c.struct_xcb_connection_t, drawable: u32, visual: ?*c.struct_xcb_visualtype_t, width: u16, height: u16) !Surface {
142-
var surface = try xcb_surface.create(conn, drawable, visual, width, height);
143-
try checkStatus(surface);
144-
var device = try Surface.getDevice(surface);
145-
return Self{ .surface = surface };
141+
var c_ptr = try xcb_surface.create(conn, drawable, visual, width, height);
142+
try checkStatus(c_ptr);
143+
var device = try Surface.getDevice(c_ptr);
144+
return Self{ .c_ptr = c_ptr };
146145
}
147146

148147
/// Decrease the reference count on surface by one.
149148
/// https://www.cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surface-destroy
150149
pub fn destroy(self: *Self) void {
151-
c.cairo_surface_destroy(self.surface);
152-
// std.debug.print("cairo.Surface {} destroyed\n", .{self});
150+
c.cairo_surface_destroy(self.c_ptr);
153151
}
154152

155153
/// Do any pending drawing for the surface and also restore any temporary modifications cairo has made to the surface's state.
156154
/// https://www.cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surface-flush/
157155
pub fn flush(self: *Self) void {
158-
c.cairo_surface_flush(self.surface);
156+
c.cairo_surface_flush(self.c_ptr);
159157
}
160158

161159
/// https://www.cairographics.org/manual/cairo-cairo-surface-t.html#cairo-surface-mark-dirty
162160
pub fn markDirty(self: *Self) void {
163-
c.cairo_surface_mark_dirty(self.surface);
161+
c.cairo_surface_mark_dirty(self.c_ptr);
164162
}
165163

166164
/// Write the contents of surface to a new file filename as a PNG image.
167165
/// https://cairographics.org/manual/cairo-PNG-Support.html#cairo-surface-write-to-png
168166
/// TODO: cast C string
169167
pub fn writeToPng(self: *Self, filename: [*]const u8) c.enum__cairo_status {
170-
const s = c.cairo_surface_write_to_png(self.surface, filename);
168+
const s = c.cairo_surface_write_to_png(self.c_ptr, filename);
171169
return s;
172170
}
173171
};
@@ -221,7 +219,7 @@ test "checkStatus() returns no error" {
221219
var surface_image = try Surface.image(320, 240);
222220
defer surface_image.destroy();
223221
var errored = false;
224-
_ = checkStatus(surface_image.surface) catch |err| {
222+
_ = checkStatus(surface_image.c_ptr) catch |err| {
225223
errored = true;
226224
};
227225
expectEqual(false, errored);
@@ -246,7 +244,7 @@ test "Surface.getDevice() returns NullPointer when the surface does not have an
246244
var surface_image = try Surface.image(320, 240);
247245
defer surface_image.destroy();
248246
var caught = false;
249-
_ = Surface.getDevice(surface_image.surface) catch |err| {
247+
_ = Surface.getDevice(surface_image.c_ptr) catch |err| {
250248
expectEqual(Error.NullPointer, err);
251249
caught = true;
252250
};

0 commit comments

Comments
 (0)