Skip to content

Commit

Permalink
Update for recent Zig 0.12, use .NET 6.0
Browse files Browse the repository at this point in the history
This commit updates the repo to build with more recent versions of Zig
0.12, namely updating for semantic changes, e.g. enforcing const when
values are not mutated, build API changes, generalization of readInt
and writeInt, etc. This also updates the zig-args and parser-toolkit
dependencies to accommodate.

Additionally, this updates svg2tvgt to build with .NET 6.0 versus 5.0,
which is EOL and has been removed out of nixpkgs. This built without any
other changes necessary.
  • Loading branch information
vancluever committed Dec 23, 2023
1 parent 9d120c7 commit ee1ece4
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 151 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ jobs:
- name: Install Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: v0.11.0
version: master

- name: Install dotnet sdk
uses: actions/setup-dotnet@v1
with:
dotnet-version: '5.0.x'
dotnet-version: '6.0.x'

- name: Compile Tools
run: zig build install
Expand Down
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ This SDK enables you to work with the [TinyVG](https://tinyvg.tech/) vector grap

## Building

The SDK is implemented with [Zig](https://ziglang.org/) 0.11.0 and [dotnet 5](https://dotnet.microsoft.com/en-us/).
The SDK is implemented with [Zig](https://ziglang.org/) 0.12.0 and [dotnet 6](https://dotnet.microsoft.com/en-us/).

To build the SDK (except `svg2tvgt`), do this:

```sh-session
[user@host sdk]$ cd src/tools/svg2tvgt/
[user@host sdk]$ zig build
[user@host sdk]$
```
Expand All @@ -26,21 +27,18 @@ This will then produce the folders `zig-cache` (for temporary files) and `zig-ou
To build `svg2tvgt`, go into the folder `src/tools/svg2tvgt` and do this:

```sh-session
[user@host sdk]$ cd src/tools/svg2tvgt/
[user@host svg2tvgt]$ dotnet build
Microsoft (R) Build Engine version 16.11.1+3e40a09f8 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

MSBuild version 17.3.2+561848881 for .NET
Determining projects to restore...
All projects are up-to-date for restore.
svg2tvgt -> /mnt/src/tools/svg2tvgt/bin/Debug/net5.0/svg2tvgt.dll
svg2tvgt -> /mnt/src/tools/svg2tvgt/bin/Debug/net6.0/svg2tvgt.dll

Build succeeded.
0 Warning(s)
0 Error(s)

Time Elapsed 00:00:01.59
Time Elapsed 00:00:00.69
[user@host svg2tvgt]$
```

This will then produce `src/tools/svg2tvgt/bin/Debug/net5.0/svg2tvgt` (or `.exe` if you are on windows).
This will then produce `src/tools/svg2tvgt/bin/Debug/net6.0/svg2tvgt` (or `.exe` if you are on windows).
10 changes: 5 additions & 5 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn build(b: *std.Build) !void {
}

const generate_ground_truth = b.addRunArtifact(ground_truth_generator);
generate_ground_truth.cwd = b.cache_root.path;
generate_ground_truth.cwd = .{ .cwd_relative = b.cache_root.path.? };

const gen_gt_step = b.step("generate", "Regenerates the ground truth data.");

Expand All @@ -111,14 +111,14 @@ pub fn build(b: *std.Build) !void {
tvg_conversion.addArg("2");
tvg_conversion.addArg("--output");
tvg_conversion.addArg(file[0 .. file.len - 3] ++ "tga");
tvg_conversion.cwd = b.cache_root.path;
tvg_conversion.cwd = .{ .cwd_relative = b.cache_root.path.? };
tvg_conversion.step.dependOn(&generate_ground_truth.step);

const tvgt_conversion = b.addRunArtifact(text);
tvgt_conversion.addArg(file);
tvgt_conversion.addArg("--output");
tvgt_conversion.addArg(file[0 .. file.len - 3] ++ "tvgt");
tvgt_conversion.cwd = b.cache_root.path;
tvgt_conversion.cwd = .{ .cwd_relative = b.cache_root.path.? };
tvgt_conversion.step.dependOn(&generate_ground_truth.step);

gen_gt_step.dependOn(&tvgt_conversion.step);
Expand Down Expand Up @@ -154,10 +154,10 @@ pub fn build(b: *std.Build) !void {
dynamic_binding_test.linkLibrary(dynamic_native_lib);

const static_binding_test_run = b.addRunArtifact(static_binding_test);
static_binding_test_run.cwd = b.cache_root.path;
static_binding_test_run.cwd = .{ .cwd_relative = b.cache_root.path.? };

const dynamic_binding_test_run = b.addRunArtifact(dynamic_binding_test);
dynamic_binding_test_run.cwd = b.cache_root.path;
dynamic_binding_test_run.cwd = .{ .cwd_relative = b.cache_root.path.? };

const test_step = b.step("test", "Runs all tests");
test_step.dependOn(&b.addRunArtifact(tvg_tests).step);
Expand Down
9 changes: 5 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
.{
.name = "tinyvg",
.version = "0.0.1",
.paths = .{""},
.dependencies = .{
.args = .{
.url = "https://github.com/MasterQ32/zig-args/archive/91d1e89fb89a4d01dec7c9aec95b0a324080ebcc.tar.gz",
.hash = "12203d04cafc97f952d74cdb077e74c0ab3414f9f6b5fbd159112c62bfa584a0dbed",
.url = "https://github.com/MasterQ32/zig-args/archive/3797c1473b48e445d721d40c8fbbe9a7422095ae.tar.gz",
.hash = "122012fa395ee8ac94592f3a535ed3899a4485e96ee3d4ec8f82c761bf304b34b870",
},
.ptk = .{
.url = "https://github.com/MasterQ32/parser-toolkit/archive/6238b7c6893582fb56f39676a090b1af1226fe1a.tar.gz",
.hash = "1220cf55c10add71a9cd2591dbe118ffa9a9198e21069e440fae1f6e3eef6f274733",
.url = "https://github.com/MasterQ32/parser-toolkit/archive/5416f1ff721cbfbf91397e557fc400e004804361.tar.gz",
.hash = "12207450343aa027d9478b85b67b2f390360851d17dbf00e95376d133140276479c2",
},
},
}
4 changes: 2 additions & 2 deletions src/binding/binding.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ fn renderBitmap(data: []const u8, src_anti_alias: c.tinyvg_AntiAlias, width: u32
var temp_mem = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer temp_mem.deinit();

var size_hint: tvg.rendering.SizeHint = if (width == 0 and height == 0)
const size_hint: tvg.rendering.SizeHint = if (width == 0 and height == 0)
.inherit
else if (width == 0)
tvg.rendering.SizeHint{ .height = height }
else if (height == 0)
tvg.rendering.SizeHint{ .width = width }
else
tvg.rendering.SizeHint{ .size = .{ .width = width, .height = height } };
var anti_alias: tvg.rendering.AntiAliasing = @enumFromInt(src_anti_alias);
const anti_alias: tvg.rendering.AntiAliasing = @enumFromInt(src_anti_alias);

var image = try tvg.rendering.renderBuffer(
temp_mem.allocator(),
Expand Down
38 changes: 19 additions & 19 deletions src/lib/builder.zig
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ pub fn Builder(comptime Writer: type) type {
const rwidth = mapSizeToType(u8, width) catch return error.OutOfRange;
const rheight = mapSizeToType(u8, height) catch return error.OutOfRange;

try self.writer.writeIntLittle(u8, rwidth);
try self.writer.writeIntLittle(u8, rheight);
try self.writer.writeInt(u8, rwidth, .little);
try self.writer.writeInt(u8, rheight, .little);
},

.default => {
const rwidth = mapSizeToType(u16, width) catch return error.OutOfRange;
const rheight = mapSizeToType(u16, height) catch return error.OutOfRange;

try self.writer.writeIntLittle(u16, rwidth);
try self.writer.writeIntLittle(u16, rheight);
try self.writer.writeInt(u16, rwidth, .little);
try self.writer.writeInt(u16, rheight, .little);
},

.enhanced => {
try self.writer.writeIntLittle(u32, width);
try self.writer.writeIntLittle(u32, height);
try self.writer.writeInt(u32, width, .little);
try self.writer.writeInt(u32, height, .little);
},
}

Expand All @@ -93,21 +93,21 @@ pub fn Builder(comptime Writer: type) type {
(@as(u16, ((rgb8[1] >> 2) & 0x2F)) << 5) |
(@as(u16, ((rgb8[2] >> 3) & 0x1F)) << 11);

try self.writer.writeIntLittle(u16, value);
try self.writer.writeInt(u16, value, .little);
},

.u8888 => for (colors) |c| {
var rgba = c.toRgba8();
try self.writer.writeIntLittle(u8, rgba[0]);
try self.writer.writeIntLittle(u8, rgba[1]);
try self.writer.writeIntLittle(u8, rgba[2]);
try self.writer.writeIntLittle(u8, rgba[3]);
const rgba = c.toRgba8();
try self.writer.writeInt(u8, rgba[0], .little);
try self.writer.writeInt(u8, rgba[1], .little);
try self.writer.writeInt(u8, rgba[2], .little);
try self.writer.writeInt(u8, rgba[3], .little);
},
.f32 => for (colors) |c| {
try self.writer.writeIntLittle(u32, @as(u32, @bitCast(c.r)));
try self.writer.writeIntLittle(u32, @as(u32, @bitCast(c.g)));
try self.writer.writeIntLittle(u32, @as(u32, @bitCast(c.b)));
try self.writer.writeIntLittle(u32, @as(u32, @bitCast(c.a)));
try self.writer.writeInt(u32, @as(u32, @bitCast(c.r)), .little);
try self.writer.writeInt(u32, @as(u32, @bitCast(c.g)), .little);
try self.writer.writeInt(u32, @as(u32, @bitCast(c.b)), .little);
try self.writer.writeInt(u32, @as(u32, @bitCast(c.a)), .little);
},

.custom => return error.UnsupportedColorEncoding,
Expand Down Expand Up @@ -357,14 +357,14 @@ pub fn Builder(comptime Writer: type) type {
switch (self.range) {
.reduced => {
const reduced_val = std.math.cast(i8, val) orelse return error.OutOfRange;
try self.writer.writeIntLittle(i8, reduced_val);
try self.writer.writeInt(i8, reduced_val, .little);
},
.default => {
const reduced_val = std.math.cast(i16, val) orelse return error.OutOfRange;
try self.writer.writeIntLittle(i16, reduced_val);
try self.writer.writeInt(i16, reduced_val, .little);
},
.enhanced => {
try self.writer.writeIntLittle(i32, val);
try self.writer.writeInt(i32, val, .little);
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/data/ground-truth.zig
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ pub fn writeEverything(src_writer: anytype, range: tvg.Range) !void {
Emitter.emitOutlineFillPath,
};

var style_base = [_]tvg.Style{
const style_base = [_]tvg.Style{
tvg.Style{ .flat = 0 },
tvg.Style{ .linear = .{
.point_0 = tvg.point(0, 0),
Expand Down
51 changes: 25 additions & 26 deletions src/lib/parsing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ pub fn Parser(comptime Reader: type) type {
const range: tvg.Range = @enumFromInt(scale_and_flags.coordinate_range);

const width: u32 = switch (range) {
.reduced => mapZeroToMax(try reader.readIntLittle(u8)),
.default => mapZeroToMax(try reader.readIntLittle(u16)),
.enhanced => std.math.cast(u32, mapZeroToMax(try reader.readIntLittle(u32))) orelse return error.InvalidData,
.reduced => mapZeroToMax(try reader.readInt(u8, .little)),
.default => mapZeroToMax(try reader.readInt(u16, .little)),
.enhanced => std.math.cast(u32, mapZeroToMax(try reader.readInt(u32, .little))) orelse return error.InvalidData,
};
const height: u32 = switch (range) {
.reduced => mapZeroToMax(try reader.readIntLittle(u8)),
.default => mapZeroToMax(try reader.readIntLittle(u16)),
.enhanced => std.math.cast(u32, mapZeroToMax(try reader.readIntLittle(u32))) orelse return error.InvalidData,
.reduced => mapZeroToMax(try reader.readInt(u8, .little)),
.default => mapZeroToMax(try reader.readInt(u16, .little)),
.enhanced => std.math.cast(u32, mapZeroToMax(try reader.readInt(u32, .little))) orelse return error.InvalidData,
};

const color_count = try self.readUInt();
Expand All @@ -159,13 +159,13 @@ pub fn Parser(comptime Reader: type) type {
for (self.color_table) |*c| {
c.* = switch (color_encoding) {
.u8888 => tvg.Color{
.r = @as(f32, @floatFromInt(try reader.readIntLittle(u8))) / 255.0,
.g = @as(f32, @floatFromInt(try reader.readIntLittle(u8))) / 255.0,
.b = @as(f32, @floatFromInt(try reader.readIntLittle(u8))) / 255.0,
.a = @as(f32, @floatFromInt(try reader.readIntLittle(u8))) / 255.0,
.r = @as(f32, @floatFromInt(try reader.readInt(u8, .little))) / 255.0,
.g = @as(f32, @floatFromInt(try reader.readInt(u8, .little))) / 255.0,
.b = @as(f32, @floatFromInt(try reader.readInt(u8, .little))) / 255.0,
.a = @as(f32, @floatFromInt(try reader.readInt(u8, .little))) / 255.0,
},
.u565 => blk: {
const rgb = try reader.readIntLittle(u16);
const rgb = try reader.readInt(u16, .little);
break :blk tvg.Color{
.r = @as(f32, @floatFromInt((rgb & 0x001F) >> 0)) / 31.0,
.g = @as(f32, @floatFromInt((rgb & 0x07E0) >> 5)) / 63.0,
Expand All @@ -175,10 +175,10 @@ pub fn Parser(comptime Reader: type) type {
},
.f32 => tvg.Color{
// TODO: Verify if this is platform independently correct:
.r = @as(f32, @bitCast(try reader.readIntLittle(u32))),
.g = @as(f32, @bitCast(try reader.readIntLittle(u32))),
.b = @as(f32, @bitCast(try reader.readIntLittle(u32))),
.a = @as(f32, @bitCast(try reader.readIntLittle(u32))),
.r = @as(f32, @bitCast(try reader.readInt(u32, .little))),
.g = @as(f32, @bitCast(try reader.readInt(u32, .little))),
.b = @as(f32, @bitCast(try reader.readInt(u32, .little))),
.a = @as(f32, @bitCast(try reader.readInt(u32, .little))),
},
.custom => return error.UnsupportedColorFormat,
};
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn Parser(comptime Reader: type) type {
// alignment here
try self.temp_buffer.resize(@sizeOf(T) * length);

var items = std.mem.bytesAsSlice(T, self.temp_buffer.items[0..(@sizeOf(T) * length)]);
const items = std.mem.bytesAsSlice(T, self.temp_buffer.items[0..(@sizeOf(T) * length)]);
std.debug.assert(items.len == length);
return items;
}
Expand All @@ -229,7 +229,7 @@ pub fn Parser(comptime Reader: type) type {
// T2 alignment could be larger than T1
const offset = std.mem.alignForward(usize, @sizeOf(T1) * length1, @alignOf(T2));

var result = .{
const result = .{
.first = std.mem.bytesAsSlice(T1, self.temp_buffer.items[0 .. @sizeOf(T1) * length1]),
.second = @as([]T2, @alignCast(std.mem.bytesAsSlice(T2, self.temp_buffer.items[offset..][0 .. @sizeOf(T2) * length2]))),
};
Expand Down Expand Up @@ -447,7 +447,7 @@ pub fn Parser(comptime Reader: type) type {
segment.start.x = try self.readUnit();
segment.start.y = try self.readUnit();

var commands = buffers.second[segment_start..][0..segment_len];
const commands = buffers.second[segment_start..][0..segment_len];
for (commands) |*node| {
node.* = try self.readNode();
}
Expand All @@ -472,7 +472,7 @@ pub fn Parser(comptime Reader: type) type {
};
const tag: Tag = @bitCast(try self.readByte());

var line_width: ?f32 = if (tag.has_line_width)
const line_width: ?f32 = if (tag.has_line_width)
try self.readUnit()
else
null;
Expand Down Expand Up @@ -501,7 +501,7 @@ pub fn Parser(comptime Reader: type) type {
},
}) },
.arc_circle => blk: {
var flags = try self.readByte();
const flags = try self.readByte();
break :blk PathNode{ .arc_circle = PathNode.NodeData(PathNode.ArcCircle).init(line_width, PathNode.ArcCircle{
.radius = try self.readUnit(),
.large_arc = (flags & 1) != 0,
Expand All @@ -513,7 +513,7 @@ pub fn Parser(comptime Reader: type) type {
}) };
},
.arc_ellipse => blk: {
var flags = try self.readByte();
const flags = try self.readByte();
break :blk PathNode{ .arc_ellipse = PathNode.NodeData(PathNode.ArcEllipse).init(line_width, PathNode.ArcEllipse{
.radius_x = try self.readUnit(),
.radius_y = try self.readUnit(),
Expand Down Expand Up @@ -589,9 +589,9 @@ pub fn Parser(comptime Reader: type) type {

fn readUnit(self: *const Self) !f32 {
const unit: tvg.Unit = switch (self.header.coordinate_range) {
.reduced => @enumFromInt(try self.reader.readIntLittle(i8)),
.default => @enumFromInt(try self.reader.readIntLittle(i16)),
.enhanced => @enumFromInt(try self.reader.readIntLittle(i32)),
.reduced => @enumFromInt(try self.reader.readInt(i8, .little)),
.default => @enumFromInt(try self.reader.readInt(i16, .little)),
.enhanced => @enumFromInt(try self.reader.readInt(i32, .little)),
};
return unit.toFloat(self.header.scale);
}
Expand All @@ -601,11 +601,10 @@ pub fn Parser(comptime Reader: type) type {
}

fn readU16(self: *Self) !u16 {
return try self.reader.readIntLittle(u16);
return try self.reader.readInt(u16, .little);
}
};
}

const CountAndStyleTag = packed struct {
const Self = @This();
raw_count: u6,
Expand Down
Loading

0 comments on commit ee1ece4

Please sign in to comment.