Skip to content

Commit

Permalink
Update Zig files to build with 0.12.0-dev.1830+779b8e259
Browse files Browse the repository at this point in the history
  • Loading branch information
theacodes committed Dec 18, 2023
1 parent d912330 commit 24b2218
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 185 deletions.
3 changes: 2 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def build_pages():


def copy_native():
src = NATIVE / "zig-out" / "lib" / "gingerbread.wasm"
src = NATIVE / "zig-out" / "bin" / "gingerbread.wasm"
dest = BUILD / "native" / "gingerbread.wasm"
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copyfile(src, dest)
Expand All @@ -64,6 +64,7 @@ def watch():
server = livereload.Server()
server.watch(WEB, func=build, delay="forever")
server.watch(BUILD)
server.watch(NATIVE, func=build)
server.serve(root="build")


Expand Down
139 changes: 81 additions & 58 deletions native/build.zig
Original file line number Diff line number Diff line change
@@ -1,63 +1,86 @@
const std = @import("std");
const Builder = std.build.Builder;

pub fn build(b: *Builder) void {
const rel_opts = b.standardReleaseOptions();
pub fn build(b: *std.Build) void {
const target: std.zig.CrossTarget = .{ .cpu_arch = .wasm32, .os_tag = .wasi };
//const target = b.standardTargetOptions(.{});

const libpotrace = b.addStaticLibrary("potrace", null);
const libpotrace_flags = [_][]const u8{"-std=gnu17", "-DHAVE_CONFIG_H"};
libpotrace.setTarget(target);
libpotrace.setBuildMode(rel_opts);
libpotrace.linkSystemLibrary("c");
libpotrace.addIncludePath("lib/potrace-1.16/src");
libpotrace.addIncludePath("lib/potrace-config");
libpotrace.addCSourceFile("lib/potrace-1.16/src/curve.c", &libpotrace_flags);
libpotrace.addCSourceFile("lib/potrace-1.16/src/trace.c", &libpotrace_flags);
libpotrace.addCSourceFile("lib/potrace-1.16/src/decompose.c", &libpotrace_flags);
libpotrace.addCSourceFile("lib/potrace-1.16/src/potracelib.c", &libpotrace_flags);

const libclipper2 = b.addStaticLibrary("clipper2", null);
const libclipper2_flags = [_][]const u8{"-std=gnu++17", "-fno-exceptions", "-Dthrow=abort"};
libclipper2.setTarget(target);
libclipper2.setBuildMode(rel_opts);
const optimize = b.standardOptimizeOption(.{});

const libpotrace = b.addStaticLibrary(.{
.name = "potrace",
.target = target,
.optimize = optimize,
});
const libpotrace_flags = .{ "-std=gnu17", "-DHAVE_CONFIG_H" };
libpotrace.linkLibC();
libpotrace.addIncludePath(.{ .path = "lib/potrace-1.16/src" });
libpotrace.addIncludePath(.{ .path = "lib/potrace-config" });
libpotrace.addCSourceFile(.{ .file = .{ .path = "lib/potrace-1.16/src/curve.c" }, .flags = &libpotrace_flags });
libpotrace.addCSourceFile(.{ .file = .{ .path = "lib/potrace-1.16/src/trace.c" }, .flags = &libpotrace_flags });
libpotrace.addCSourceFile(.{ .file = .{ .path = "lib/potrace-1.16/src/decompose.c" }, .flags = &libpotrace_flags });
libpotrace.addCSourceFile(.{ .file = .{ .path = "lib/potrace-1.16/src/potracelib.c" }, .flags = &libpotrace_flags });

const libclipper2 = b.addStaticLibrary(.{
.name = "clipper2",
.target = target,
.optimize = optimize,
.link_libc = true,
});
const libclipper2_flags = .{ "-std=gnu++17", "-fno-exceptions", "-Dthrow=abort" };
libclipper2.linkLibC();
libclipper2.linkSystemLibrary("c++");
libclipper2.addIncludePath("lib/clipper2/CPP/Clipper2Lib");
libclipper2.addIncludePath("src");
libclipper2.addCSourceFile("lib/clipper2/CPP/Clipper2Lib/clipper.engine.cpp", &libclipper2_flags);
libclipper2.addCSourceFile("lib/clipper2/CPP/Clipper2Lib/clipper.offset.cpp", &libclipper2_flags);
libclipper2.addCSourceFile("src/clipperwrapper.cpp", &libclipper2_flags);

if (target.cpu_arch != null and target.cpu_arch.? == .wasm32) {
const libgingerbread = b.addSharedLibrary("gingerbread", "src/gingerbread.zig", b.version(1, 0, 0));
libgingerbread.wasi_exec_model = std.builtin.WasiExecModel.reactor;
libgingerbread.setBuildMode(rel_opts);
libgingerbread.setTarget(target);
libgingerbread.linkSystemLibrary("c");
libgingerbread.linkLibrary(libpotrace);
libgingerbread.linkLibrary(libclipper2);
libgingerbread.addIncludePath("src");
libgingerbread.addIncludePath("lib/potrace-1.16/src");

libgingerbread.install();
}

const main = b.addTest("src/tests.zig");
main.setBuildMode(rel_opts);
main.setTarget(target);
main.linkSystemLibrary("c");
main.linkLibrary(libpotrace);
main.linkLibrary(libclipper2);
main.addIncludePath("src/");
main.addIncludePath("lib/potrace-1.16/src");
main.addIncludePath("lib/potrace-config");
main.addIncludePath("lib/stb");
main.addCSourceFile("src/load_image.c", &[_][]const u8{"-std=gnu17",});

//main.install();

const test_step = b.step("test", "Test the program");
test_step.dependOn(&main.step);
b.default_step.dependOn(test_step);
libclipper2.addIncludePath(.{ .path = "lib/clipper2/CPP/Clipper2Lib" });
libclipper2.addIncludePath(.{ .path = "src" });
libclipper2.addCSourceFile(.{
.file = .{ .path = "lib/clipper2/CPP/Clipper2Lib/clipper.engine.cpp" },
.flags = &libclipper2_flags,
});
libclipper2.addCSourceFile(.{
.file = .{ .path = "lib/clipper2/CPP/Clipper2Lib/clipper.offset.cpp" },
.flags = &libclipper2_flags,
});
libclipper2.addCSourceFile(.{
.file = .{ .path = "src/clipperwrapper.cpp" },
.flags = &libclipper2_flags,
});

const libgingerbread = b.addExecutable(.{
.name = "gingerbread",
.root_source_file = .{ .path = "src/gingerbread.zig" },
.version = .{ .major = 1, .minor = 0, .patch = 0 },
.target = target,
.optimize = optimize,
});
libgingerbread.entry = .disabled;
libgingerbread.rdynamic = true;
libgingerbread.wasi_exec_model = std.builtin.WasiExecModel.reactor;
libgingerbread.strip = false;
libgingerbread.linkLibC();
libgingerbread.linkLibrary(libpotrace);
libgingerbread.linkLibrary(libclipper2);
libgingerbread.addIncludePath(.{ .path = "src" });
libgingerbread.addIncludePath(.{ .path = "lib/potrace-1.16/src" });

b.installArtifact(libgingerbread);

// const main = b.addTest(.{
// .name = "main",
// .root_source_file = .{ .path = "src/tests.zig" },
// .target = target,
// .optimize = optimize,
// .link_libc = true,
// });
// main.linkLibrary(libpotrace);
// main.linkLibrary(libclipper2);
// main.addIncludePath(.{ .path = "src/" });
// main.addIncludePath(.{ .path = "lib/potrace-1.16/src" });
// main.addIncludePath(.{ .path = "lib/potrace-config" });
// main.addIncludePath(.{ .path = "lib/stb" });
// main.addCSourceFile(.{ .file = .{ .path = "src/load_image.c" }, .flags = &.{
// "-std=gnu17",
// } });

// //main.install();

// const test_step = b.step("test", "Test the program");
// test_step.dependOn(&main.step);
// b.default_step.dependOn(test_step);
}
82 changes: 50 additions & 32 deletions native/src/clipper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const geometry = @import("geometry.zig");
const Point = geometry.Point;
const Poly = geometry.Poly;


const PathD = struct {
ptr: *c.pathd_t,

Expand All @@ -31,7 +30,7 @@ const PathD = struct {

pub fn get(self: PathD, index: usize) Point {
const val = c.PathD_at(self.ptr, index);
return .{.x = val.x, .y = val.y};
return .{ .x = val.x, .y = val.y };
}

pub const Iterator = struct {
Expand Down Expand Up @@ -76,8 +75,8 @@ const PathD = struct {
pub fn svg_path(self: PathD) void {
var it = self.iterator();
while (it.next()) |pt| {
var letter = if (it.n == 1) "M" else "L";
print("{s} {d:3.3},{d:3.3} ", .{letter, pt.x, pt.y});
const letter = if (it.n == 1) "M" else "L";
print("{s} {d:3.3},{d:3.3} ", .{ letter, pt.x, pt.y });
}
print("\n", .{});
}
Expand Down Expand Up @@ -124,7 +123,7 @@ const PathList = struct {

pub fn get(self: PathList, index: usize) PathD {
const val = c.PathsD_at(self.ptr, index).?;
return .{.ptr = val};
return .{ .ptr = val };
}

pub const Iterator = struct {
Expand Down Expand Up @@ -170,7 +169,7 @@ const PathList = struct {
}

pub fn to_poly(self: PathList, allocator: std.mem.Allocator) !Poly {
var outline = try self.get(0).to_points(allocator);
const outline = try self.get(0).to_points(allocator);
var holes = try std.ArrayList([]Point).initCapacity(allocator, self.len() - 1);

// The first path is the outline, subsequent paths are holes, so skip
Expand All @@ -184,7 +183,7 @@ const PathList = struct {
return .{
.allocator = allocator,
.outline = outline,
.holes = holes.toOwnedSlice(),
.holes = try holes.toOwnedSlice(),
};
}
};
Expand All @@ -205,15 +204,15 @@ pub const FillRule = enum(u8) {
};

pub fn boolean_op(clip_type: ClipType, fill_rule: FillRule, subjects: PathList, clips: PathList, decimal_precision: i32) PathList {
var solution_ptr = c.clipper2_boolean_op(
@enumToInt(clip_type),
@enumToInt(fill_rule),
const solution_ptr = c.clipper2_boolean_op(
@intFromEnum(clip_type),
@intFromEnum(fill_rule),
subjects.ptr,
clips.ptr,
decimal_precision,
).?;

return .{.ptr = solution_ptr};
return .{ .ptr = solution_ptr };
}

pub const Options = struct {
Expand Down Expand Up @@ -258,30 +257,29 @@ pub fn simplify_poly(allocator: std.mem.Allocator, poly: Poly) !Poly {
return try result.to_poly(allocator);
}


test "PathD" {
var pathd = PathD.init();
defer pathd.deinit();

try testing.expect(pathd.len() == 0);

pathd.append(Point {.x = 1, .y = 2});
pathd.append(Point {.x = 3, .y = 4});
pathd.append(Point{ .x = 1, .y = 2 });
pathd.append(Point{ .x = 3, .y = 4 });

try testing.expect(pathd.len() == 2);

var it = pathd.iterator();
try testing.expect(std.meta.eql(it.next().?, Point {.x = 1, .y = 2}));
try testing.expect(std.meta.eql(it.next().?, Point {.x = 3, .y = 4}));
try testing.expect(std.meta.eql(it.next().?, Point{ .x = 1, .y = 2 }));
try testing.expect(std.meta.eql(it.next().?, Point{ .x = 3, .y = 4 }));
try testing.expect(it.next() == null);

// Converting to and from points
var points = try pathd.to_points(std.testing.allocator);
const points = try pathd.to_points(std.testing.allocator);
defer std.testing.allocator.free(points);

try testing.expect(points.len == 2);
try testing.expect(std.meta.eql(points[0], Point {.x = 1, .y = 2}));
try testing.expect(std.meta.eql(points[1], Point {.x = 3, .y = 4}));
try testing.expect(std.meta.eql(points[0], Point{ .x = 1, .y = 2 }));
try testing.expect(std.meta.eql(points[1], Point{ .x = 3, .y = 4 }));

var pathd2 = PathD.from_points(points);
defer pathd2.deinit();
Expand All @@ -304,7 +302,7 @@ test "PathList" {
try testing.expect(pathsd.len() == 0);

var pathd = PathD.init();
pathd.append(Point {.x = 1, .y = 2});
pathd.append(Point{ .x = 1, .y = 2 });
pathsd.append(pathd);
pathd.deinit();

Expand All @@ -321,12 +319,12 @@ test "PathList to Poly" {
defer pathsd.deinit();

var outline = PathD.init();
outline.append(Point {.x = 1, .y = 2});
outline.append(Point{ .x = 1, .y = 2 });
pathsd.append(outline);
outline.deinit();

var hole = PathD.init();
hole.append(Point {.x = 3, .y = 4});
hole.append(Point{ .x = 3, .y = 4 });
pathsd.append(hole);
hole.deinit();

Expand All @@ -343,13 +341,23 @@ test "PathList to Poly" {
test "boolean op" {
var subjects = PathList.init();
defer subjects.deinit();
var subject = PathD.from_points(&[_]Point{.{.x = 0, .y = 0}, .{.x = 100, .y = 0}, .{.x = 100, .y = 100}, .{.x = 0, .y = 100},});
var subject = PathD.from_points(&[_]Point{
.{ .x = 0, .y = 0 },
.{ .x = 100, .y = 0 },
.{ .x = 100, .y = 100 },
.{ .x = 0, .y = 100 },
});
subjects.append(subject);
subject.deinit();

var clips = PathList.init();
defer clips.deinit();
var clip = PathD.from_points(&[_]Point{.{.x = 25, .y = 25}, .{.x = 75, .y = 25}, .{.x = 75, .y = 75}, .{.x = 25, .y = 75},});
var clip = PathD.from_points(&[_]Point{
.{ .x = 25, .y = 25 },
.{ .x = 75, .y = 25 },
.{ .x = 75, .y = 75 },
.{ .x = 25, .y = 75 },
});
clips.append(clip);
clip.deinit();

Expand All @@ -367,14 +375,24 @@ test "simplify poly" {
var hole = std.ArrayList(Point).init(a);
var holes = std.ArrayList([]Point).init(a);

try outline.appendSlice(&[_]Point{.{.x = 0, .y = 0}, .{.x = 100, .y = 0}, .{.x = 100, .y = 100}, .{.x = 0, .y = 100},});
try hole.appendSlice(&[_]Point{.{.x = 25, .y = 25}, .{.x = 75, .y = 25}, .{.x = 75, .y = 75}, .{.x = 25, .y = 75},});
try holes.append(hole.toOwnedSlice());

var poly = Poly {
try outline.appendSlice(&[_]Point{
.{ .x = 0, .y = 0 },
.{ .x = 100, .y = 0 },
.{ .x = 100, .y = 100 },
.{ .x = 0, .y = 100 },
});
try hole.appendSlice(&[_]Point{
.{ .x = 25, .y = 25 },
.{ .x = 75, .y = 25 },
.{ .x = 75, .y = 75 },
.{ .x = 25, .y = 75 },
});
try holes.append(try hole.toOwnedSlice());

var poly = Poly{
.allocator = a,
.outline = outline.toOwnedSlice(),
.holes = holes.toOwnedSlice(),
.outline = try outline.toOwnedSlice(),
.holes = try holes.toOwnedSlice(),
};

defer poly.deinit();
Expand All @@ -394,7 +412,7 @@ test "simplify poly" {

print("Result: {?}\n", .{poly});

for (poly.outline) |_, i| {
for (poly.outline, 0..) |_, i| {
try testing.expect(std.meta.eql(poly.outline[i], simplified.outline[i]));
}
}
Loading

0 comments on commit 24b2218

Please sign in to comment.