From ca65808361ca232b62768a0e3adbd5acaeae57e8 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Tue, 9 Jul 2024 21:07:20 -0600 Subject: [PATCH] zig update 0.13.0 --- .github/workflows/ci.yml | 4 +- .gitignore | 2 +- build.zig | 88 +++++++++++++++-------------- build.zig.zon | 17 ++++-- src/binding/binding.zig | 4 +- src/lib/builder.zig | 40 ++++++------- src/lib/{data => }/ground-truth.zig | 4 +- src/lib/parsing.zig | 52 ++++++++--------- src/lib/rendering.zig | 36 ++++++------ src/lib/svg.zig | 30 +++++----- src/lib/text.zig | 64 ++++++++++----------- src/tools/render.zig | 30 +++++----- 12 files changed, 193 insertions(+), 178 deletions(-) rename src/lib/{data => }/ground-truth.zig (99%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aceb578..f06dff4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,9 +21,9 @@ jobs: submodules: 'recursive' - name: Install Zig - uses: goto-bus-stop/setup-zig@v2 + uses: mlugg/setup-zig@v1 with: - version: v0.11.0 + version: 0.13.0 - name: Install dotnet sdk uses: actions/setup-dotnet@v4 diff --git a/.gitignore b/.gitignore index 146786d..729563c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -zig-cache/ +.zig-cache/ zig-out/ dev-thingies/ kcov-output/ diff --git a/build.zig b/build.zig index c098b1e..f1c40f7 100644 --- a/build.zig +++ b/build.zig @@ -1,8 +1,8 @@ const std = @import("std"); -fn initNativeLibrary(lib: *std.build.CompileStep, tvg: *std.Build.Module) void { - lib.addModule("tvg", tvg); - lib.addIncludePath(.{ .path = "src/binding/include" }); +fn initNativeLibrary(lib: *std.Build.Step.Compile, tvg: *std.Build.Module) void { + lib.root_module.addImport("tvg", tvg); + lib.addIncludePath(lib.step.owner.path("src/binding/include")); lib.bundle_compiler_rt = true; } @@ -12,13 +12,13 @@ pub fn build(b: *std.Build) !void { // TinyVG package const tvg = b.addModule("tvg", .{ - .source_file = .{ .path = "src/lib/tinyvg.zig" }, - .dependencies = &.{.{ .name = "ptk", .module = ptk }}, + .root_source_file = b.path("src/lib/tinyvg.zig"), + .imports = &.{ .{ .name = "ptk", .module = ptk }}, }); const args_dep = b.dependency("args", .{}); const args = args_dep.module("args"); - const www_folder = std.build.InstallDir{ .custom = "www" }; + const www_folder = std.Build.InstallDir{ .custom = "www" }; const install_include = b.option(bool, "install-include", "Installs the include directory") orelse true; const install_www = b.option(bool, "install-www", "Installs the www directory (polyfill)") orelse true; @@ -30,7 +30,7 @@ pub fn build(b: *std.Build) !void { const static_native_lib = b.addStaticLibrary(.{ .name = "tinyvg", - .root_source_file = .{ .path = "src/binding/binding.zig" }, + .root_source_file = b.path("src/binding/binding.zig"), .target = target, .optimize = optimize, }); @@ -39,14 +39,14 @@ pub fn build(b: *std.Build) !void { b.installArtifact(static_native_lib); } - const dynamic_lib_name = if (target.isWindows()) + const dynamic_lib_name = if (target.result.os.tag == .windows) "tinyvg.dll" else "tinyvg"; const dynamic_native_lib = b.addSharedLibrary(.{ .name = dynamic_lib_name, - .root_source_file = .{ .path = "src/binding/binding.zig" }, + .root_source_file = b.path("src/binding/binding.zig"), .target = target, .optimize = optimize, }); @@ -56,47 +56,47 @@ pub fn build(b: *std.Build) !void { } if (install_include) { - const install_header = b.addInstallFileWithDir(.{ .path = "src/binding/include/tinyvg.h" }, .header, "tinyvg.h"); + const install_header = b.addInstallFileWithDir(b.path("src/binding/include/tinyvg.h"), .header, "tinyvg.h"); b.getInstallStep().dependOn(&install_header.step); } const render = b.addExecutable(.{ .name = "tvg-render", - .root_source_file = .{ .path = "src/tools/render.zig" }, + .root_source_file = b.path("src/tools/render.zig"), .target = target, .optimize = optimize, }); - render.addModule("tvg", tvg); - render.addModule("args", args); + render.root_module.addImport("tvg", tvg); + render.root_module.addImport("args", args); if (install_bin) { b.installArtifact(render); } const text = b.addExecutable(.{ .name = "tvg-text", - .root_source_file = .{ .path = "src/tools/text.zig" }, + .root_source_file = b.path("src/tools/text.zig"), .target = target, .optimize = optimize, }); - text.addModule("tvg", tvg); - text.addModule("args", args); - text.addModule("ptk", ptk); + text.root_module.addImport("tvg", tvg); + text.root_module.addImport("args", args); + text.root_module.addImport("ptk", ptk); if (install_bin) { b.installArtifact(text); } const ground_truth_generator = b.addExecutable(.{ .name = "ground-truth-generator", - .root_source_file = .{ .path = "src/lib/data/ground-truth.zig" }, - .main_pkg_path = .{ .path = "src/lib" }, + .root_source_file = b.path("src/lib/ground-truth.zig"), + .target = b.host, .optimize = optimize, }); - for (tvg.dependencies.keys(), tvg.dependencies.values()) |name, mod| { - ground_truth_generator.addModule(name, mod); + for (tvg.import_table.keys(), tvg.import_table.values()) |name, mod| { + ground_truth_generator.root_module.addImport(name, mod); } 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."); @@ -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); @@ -127,37 +127,41 @@ pub fn build(b: *std.Build) !void { { const tvg_tests = b.addTest(.{ - .root_source_file = tvg.source_file, + .root_source_file = tvg.root_source_file.?, .optimize = optimize, - .main_pkg_path = .{ .path = "src" }, }); - for (tvg.dependencies.keys(), tvg.dependencies.values()) |name, mod| { - tvg_tests.addModule(name, mod); + for (tvg.import_table.keys(), tvg.import_table.values()) |name, mod| { + tvg_tests.root_module.addImport(name, mod); } const static_binding_test = b.addExecutable(.{ .name = "static-native-binding", + .target = target, .optimize = optimize, }); static_binding_test.linkLibC(); - static_binding_test.addIncludePath(.{ .path = "src/binding/include" }); - static_binding_test.addCSourceFile(.{ .file = .{ .path = "examples/usage.c" }, .flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" } }); + static_binding_test.addIncludePath(b.path("src/binding/include")); + static_binding_test.addCSourceFile(.{ + .file = b.path("examples/usage.c"), + .flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" }, + }); static_binding_test.linkLibrary(static_native_lib); const dynamic_binding_test = b.addExecutable(.{ .name = "dynamic-native-binding", .optimize = optimize, + .target = target, }); dynamic_binding_test.linkLibC(); - dynamic_binding_test.addIncludePath(.{ .path = "src/binding/include" }); - dynamic_binding_test.addCSourceFile(.{ .file = .{ .path = "examples/usage.c" }, .flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" } }); + dynamic_binding_test.addIncludePath(b.path("src/binding/include")); + dynamic_binding_test.addCSourceFile(.{ .file = b.path("examples/usage.c"), .flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" } }); 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); @@ -165,18 +169,20 @@ pub fn build(b: *std.Build) !void { test_step.dependOn(&dynamic_binding_test_run.step); } - const polyfill = b.addSharedLibrary(.{ + const polyfill = b.addExecutable(.{ .name = "tinyvg", - .root_source_file = .{ .path = "src/polyfill/tinyvg.zig" }, - .target = .{ + .root_source_file = b.path("src/polyfill/tinyvg.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .cpu_model = .baseline, .os_tag = .freestanding, - }, + }), .optimize = optimize, }); - polyfill.strip = (optimize != .Debug); - polyfill.addModule("tvg", tvg); + polyfill.entry = .disabled; + polyfill.root_module.export_symbol_names = &.{ "convertToSvg" }; + polyfill.root_module.strip = (optimize != .Debug); + polyfill.root_module.addImport("tvg", tvg); if (install_www) { var artifact_install = b.addInstallArtifact(polyfill, .{}); @@ -184,7 +190,7 @@ pub fn build(b: *std.Build) !void { b.getInstallStep().dependOn(&artifact_install.step); } - const copy_stuff = b.addInstallFileWithDir(.{ .path = "src/polyfill/tinyvg.js" }, www_folder, "tinyvg.js"); + const copy_stuff = b.addInstallFileWithDir(b.path("src/polyfill/tinyvg.js"), www_folder, "tinyvg.js"); if (install_www) { b.getInstallStep().dependOn(©_stuff.step); diff --git a/build.zig.zon b/build.zig.zon index b822428..2e409b7 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,12 +3,21 @@ .version = "0.0.1", .dependencies = .{ .args = .{ - .url = "https://github.com/MasterQ32/zig-args/archive/91d1e89fb89a4d01dec7c9aec95b0a324080ebcc.tar.gz", - .hash = "12203d04cafc97f952d74cdb077e74c0ab3414f9f6b5fbd159112c62bfa584a0dbed", + .url = "https://github.com/ikskuh/zig-args/archive/03af1b6c5bfda9646a562c861055024daed5b238.tar.gz", + .hash = "1220904d2fdcd970dd0d216211d092eb3ef6da01117163cc9393ab845a1d66c029d9", }, .ptk = .{ - .url = "https://github.com/MasterQ32/parser-toolkit/archive/6238b7c6893582fb56f39676a090b1af1226fe1a.tar.gz", - .hash = "1220cf55c10add71a9cd2591dbe118ffa9a9198e21069e440fae1f6e3eef6f274733", + .url = "https://github.com/ikskuh/parser-toolkit/archive/9349d087a3216ebea27cc99b5b4bd8affe234816.tar.gz", + .hash = "12206f0ade18be8db985f6a688ca7ec9ffdbb95f033c8db91760b0d13cfe27618b61", }, }, + .paths = .{ + "build.zig", + "build.zig.zon", + "docs", + "examples", + "LICENSE", + "README.md", + "src", + }, } diff --git a/src/binding/binding.zig b/src/binding/binding.zig index d8ef382..30cc59b 100644 --- a/src/binding/binding.zig +++ b/src/binding/binding.zig @@ -16,7 +16,7 @@ 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 } @@ -24,7 +24,7 @@ fn renderBitmap(data: []const u8, src_anti_alias: c.tinyvg_AntiAlias, width: u32 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(), diff --git a/src/lib/builder.zig b/src/lib/builder.zig index 93143da..7a1e529 100644 --- a/src/lib/builder.zig +++ b/src/lib/builder.zig @@ -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); }, } @@ -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, @@ -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); }, } } @@ -405,7 +405,7 @@ const ReducedCount = enum(u6) { _, }; -const ground_truth = @import("data/ground-truth.zig"); +const ground_truth = @import("ground-truth.zig"); test "encode shield (default range, scale 1/256)" { var buffer: [1024]u8 = undefined; diff --git a/src/lib/data/ground-truth.zig b/src/lib/ground-truth.zig similarity index 99% rename from src/lib/data/ground-truth.zig rename to src/lib/ground-truth.zig index a3c5157..786799b 100644 --- a/src/lib/data/ground-truth.zig +++ b/src/lib/ground-truth.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const tvg = @import("../tinyvg.zig"); +const tvg = @import("tinyvg.zig"); // pub const everything_16 = blk: { // @setEvalBranchQuota(100_000); @@ -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), diff --git a/src/lib/parsing.zig b/src/lib/parsing.zig index 810eb05..6c4678d 100644 --- a/src/lib/parsing.zig +++ b/src/lib/parsing.zig @@ -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(); @@ -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, @@ -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, }; @@ -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; } @@ -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]))), }; @@ -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(); } @@ -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; @@ -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, @@ -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(), @@ -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); } @@ -601,7 +601,7 @@ 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); } }; } @@ -666,7 +666,7 @@ test "coverage test" { var source_buf: [2048]u8 = undefined; var stream = std.io.fixedBufferStream(&source_buf); - @import("data/ground-truth.zig").writeEverything(stream.writer(), .default) catch unreachable; + @import("ground-truth.zig").writeEverything(stream.writer(), .default) catch unreachable; try stream.seekTo(0); diff --git a/src/lib/rendering.zig b/src/lib/rendering.zig index 93c9147..5ed351e 100644 --- a/src/lib/rendering.zig +++ b/src/lib/rendering.zig @@ -89,7 +89,7 @@ pub fn renderStream( try renderCommand(&framebuffer, parser.header, parser.color_table, cmd, temporary_allocator); } - var image = Image{ + const image = Image{ .pixels = try image_allocator.alloc(Color8, target_pixel_count), .width = target_size.width, .height = target_size.height, @@ -260,9 +260,9 @@ pub fn isFramebuffer(comptime T: type) bool { std.meta.Child(T) else T; - return std.meta.trait.hasFn("setPixel")(FbType) and - std.meta.trait.hasField("width")(FbType) and - std.meta.trait.hasField("height")(FbType); + return std.meta.hasFn(FbType, "setPixel") and + @hasField(FbType, "width") and + @hasField(FbType, "height"); } const Point = tvg.Point; @@ -412,10 +412,10 @@ pub fn renderCommand( .outline_fill_rectangles => |data| { for (data.rectangles) |rect| { painter.fillRectangle(framebuffer, rect.x, rect.y, rect.width, rect.height, color_table, data.fill_style); - var tl = Point{ .x = rect.x, .y = rect.y }; - var tr = Point{ .x = rect.x + rect.width, .y = rect.y }; - var bl = Point{ .x = rect.x, .y = rect.y + rect.height }; - var br = Point{ .x = rect.x + rect.width, .y = rect.y + rect.height }; + const tl = Point{ .x = rect.x, .y = rect.y }; + const tr = Point{ .x = rect.x + rect.width, .y = rect.y }; + const bl = Point{ .x = rect.x, .y = rect.y + rect.height }; + const br = Point{ .x = rect.x + rect.width, .y = rect.y + rect.height }; painter.drawLine(framebuffer, color_table, data.line_style, data.line_width, data.line_width, .{ .start = tl, .end = tr }); painter.drawLine(framebuffer, color_table, data.line_style, data.line_width, data.line_width, .{ .start = tr, .end = br }); painter.drawLine(framebuffer, color_table, data.line_style, data.line_width, data.line_width, .{ .start = br, .end = bl }); @@ -527,7 +527,7 @@ pub fn renderPath( .horiz => |x| try point_store.append(Point{ .x = x.data, .y = point_store.back().y }, x.line_width orelse last_width), .vert => |y| try point_store.append(Point{ .x = point_store.back().x, .y = y.data }, y.line_width orelse last_width), .bezier => |bezier| { - var previous = point_store.back(); + const previous = point_store.back(); const oct0_x = [4]f32{ previous.x, bezier.data.c0.x, bezier.data.c1.x, bezier.data.p1.x }; const oct0_y = [4]f32{ previous.y, bezier.data.c0.y, bezier.data.c1.y, bezier.data.p1.y }; @@ -544,7 +544,7 @@ pub fn renderPath( try point_store.append(bezier.data.p1, new_width); }, .quadratic_bezier => |bezier| { - var previous = point_store.back(); + const previous = point_store.back(); const oct0_x = [3]f32{ previous.x, bezier.data.c.x, bezier.data.p1.x }; const oct0_y = [3]f32{ previous.y, bezier.data.c.y, bezier.data.p1.y }; @@ -628,7 +628,7 @@ inline fn sqrt(val: anytype) @TypeOf(val) { return @sqrt(val); } inline fn abs(val: anytype) @TypeOf(val) { - return @fabs(val); + return @abs(val); } pub fn renderEllipse( @@ -745,7 +745,7 @@ fn renderCircle( const angle = std.math.asin(std.math.clamp(sqrt(len_squared) / r, -1.0, 1.0)) * 2; const arc = if (large_arc) (std.math.tau - angle) else angle; - var pos = sub(p0, center); + const pos = sub(p0, center); for (0..circle_divs - 1) |i| { const step_mat = rotationMat(@as(f32, @floatFromInt(i)) * (if (turn_left) -arc else arc) / circle_divs); const point = add(applyMat(step_mat, pos), center); @@ -857,9 +857,9 @@ fn distance(p1: Point, p2: Point) f32 { } fn getProjectedPointOnLine(v1: Point, v2: Point, p: Point) Point { - var l1 = sub(v2, v1); - var l2 = sub(p, v1); - var proj = dot(l1, l2) / length2(l1); + const l1 = sub(v2, v1); + const l2 = sub(p, v1); + const proj = dot(l1, l2) / length2(l1); return add(v1, scale(l1, proj)); } @@ -904,7 +904,7 @@ const Painter = struct { while (x <= max_x) : (x += 1) { // compute "center" of the pixel - var p = self.mapPointToImage(pointFromInts(x, y)); + const p = self.mapPointToImage(pointFromInts(x, y)); var inside_count: usize = 0; for (points_lists) |points| { @@ -958,7 +958,7 @@ const Painter = struct { //----------- - q.x = @fabs(q.x); + q.x = @abs(q.x); const b = ra - rb; const c = tvg.point(@sqrt(h - b * b), b); @@ -1030,7 +1030,7 @@ const Painter = struct { while (x <= max_x) : (x += 1) { // compute "center" of the pixel - var p = self.mapPointToImage(pointFromInts(x, y)); + const p = self.mapPointToImage(pointFromInts(x, y)); const dist = sdUnevenCapsule( p, diff --git a/src/lib/svg.zig b/src/lib/svg.zig index 974aa3c..8cd5bbc 100644 --- a/src/lib/svg.zig +++ b/src/lib/svg.zig @@ -148,24 +148,24 @@ pub fn renderStream(allocator: std.mem.Allocator, parser: anytype, writer: anyty }, .draw_line_path => |data| { - var style = svgStyle(&cache, null, data.style, data.line_width); - var path = SvgPath{ .path = data.path }; + const style = svgStyle(&cache, null, data.style, data.line_width); + const path = SvgPath{ .path = data.path }; try writer.print( \\ , .{ style, path }); }, .fill_path => |data| { - var style = svgStyle(&cache, data.style, null, null); - var path = SvgPath{ .path = data.path }; + const style = svgStyle(&cache, data.style, null, null); + const path = SvgPath{ .path = data.path }; try writer.print( \\ , .{ style, path }); }, .outline_fill_path => |data| { - var style = svgStyle(&cache, data.fill_style, data.line_style, data.line_width); - var path = SvgPath{ .path = data.path }; + const style = svgStyle(&cache, data.fill_style, data.line_style, data.line_width); + const path = SvgPath{ .path = data.path }; try writer.print( \\ @@ -194,9 +194,9 @@ pub fn renderStream(allocator: std.mem.Allocator, parser: anytype, writer: anyty try writer.writeAll(""); }, .radial => |grad| { - var dx = grad.point_1.x - grad.point_0.x; - var dy = grad.point_1.y - grad.point_0.y; - var r = std.math.sqrt(dx * dx + dy * dy); + const dx = grad.point_1.x - grad.point_0.x; + const dy = grad.point_1.y - grad.point_0.y; + const r = std.math.sqrt(dx * dx + dy * dy); try writer.print( \\ @@ -328,9 +328,9 @@ const SvgStyleCache = struct { } const color = self.color_table[i]; - var r: u8 = @intFromFloat(std.math.clamp(255.0 * color.r, 0.0, 255.0)); - var g = @as(u8, @intFromFloat(std.math.clamp(255.0 * color.g, 0.0, 255.0))); - var b = @as(u8, @intFromFloat(std.math.clamp(255.0 * color.b, 0.0, 255.0))); + const r: u8 = @intFromFloat(std.math.clamp(255.0 * color.r, 0.0, 255.0)); + const g = @as(u8, @intFromFloat(std.math.clamp(255.0 * color.g, 0.0, 255.0))); + const b = @as(u8, @intFromFloat(std.math.clamp(255.0 * color.b, 0.0, 255.0))); try writer.print("{s}:#{X:0>2}{X:0>2}{X:0>2};", .{ prefix, r, g, b }); if (color.a != 1.0) { try writer.print("{s}-opacity:{d};", .{ prefix, color.a }); @@ -343,9 +343,9 @@ const SvgStyleCache = struct { } const color = self.color_table[i]; - var r: u8 = @intFromFloat(std.math.clamp(255.0 * color.r, 0.0, 255.0)); - var g: u8 = @intFromFloat(std.math.clamp(255.0 * color.g, 0.0, 255.0)); - var b = @as(u8, @intFromFloat(std.math.clamp(255.0 * color.b, 0.0, 255.0))); + const r: u8 = @intFromFloat(std.math.clamp(255.0 * color.r, 0.0, 255.0)); + const g: u8 = @intFromFloat(std.math.clamp(255.0 * color.g, 0.0, 255.0)); + const b = @as(u8, @intFromFloat(std.math.clamp(255.0 * color.b, 0.0, 255.0))); try writer.print("{s}#{X:0>2}{X:0>2}{X:0>2}{s}", .{ prefix, r, g, b, postfix }); } }; diff --git a/src/lib/text.zig b/src/lib/text.zig index f927329..dbd0fc2 100644 --- a/src/lib/text.zig +++ b/src/lib/text.zig @@ -483,10 +483,10 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) if (item == .end) break; - var x = try self.parseNumber(); - var y = try self.parseNumber(); - var width = try self.parseNumber(); - var height = try self.parseNumber(); + const x = try self.parseNumber(); + const y = try self.parseNumber(); + const width = try self.parseNumber(); + const height = try self.parseNumber(); try self.expectEnd(); @@ -509,8 +509,8 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) if (item == .end) break; - var p0 = try self.parsePoint(); - var p1 = try self.parsePoint(); + const p0 = try self.parsePoint(); + const p1 = try self.parsePoint(); try self.expectEnd(); @@ -533,8 +533,8 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) if (item == .end) break; - var x = try self.parseNumber(); - var y = try self.parseNumber(); + const x = try self.parseNumber(); + const y = try self.parseNumber(); try self.expectEnd(); @@ -649,11 +649,11 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) if ((try self.parseInteger(u8)) != 1) return error.UnsupportedVersion; - var header = try self.parseHeader(); + const header = try self.parseHeader(); try self.builder.writeHeader(header.width, header.height, header.scale, header.color_encoding, header.coordinate_range); - var color_table = try self.parseColorTable(); + const color_table = try self.parseColorTable(); defer self.allocator.free(color_table); try self.builder.writeColorTable(color_table); @@ -668,7 +668,7 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) const command = try self.parseEnum(tvg.Command); switch (command) { .fill_polygon => { - var style = try self.parseStyle(); + const style = try self.parseStyle(); var points = try self.readPoints(); defer points.deinit(); @@ -677,7 +677,7 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) }, .fill_rectangles => { - var style = try self.parseStyle(); + const style = try self.parseStyle(); var rects = try self.readRectangles(); defer rects.deinit(); @@ -685,7 +685,7 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) }, .fill_path => { - var style = try self.parseStyle(); + const style = try self.parseStyle(); var path = try self.readPath(); defer path.deinit(); @@ -694,8 +694,8 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) }, .draw_lines => { - var style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const style = try self.parseStyle(); + const line_width = try self.parseNumber(); var lines = try self.readLines(); defer lines.deinit(); @@ -704,8 +704,8 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) }, .draw_line_loop => { - var style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const style = try self.parseStyle(); + const line_width = try self.parseNumber(); var points = try self.readPoints(); defer points.deinit(); @@ -714,8 +714,8 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) }, .draw_line_strip => { - var style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const style = try self.parseStyle(); + const line_width = try self.parseNumber(); var points = try self.readPoints(); defer points.deinit(); @@ -723,18 +723,18 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) try self.builder.writeDrawLineStrip(style, line_width, points.items); }, .draw_line_path => { - var style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const style = try self.parseStyle(); + const line_width = try self.parseNumber(); - var path = try self.readPath(); + const path = try self.readPath(); try self.builder.writeDrawPath(style, line_width, path.segments); }, .outline_fill_polygon => { - var fill_style = try self.parseStyle(); - var line_style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const fill_style = try self.parseStyle(); + const line_style = try self.parseStyle(); + const line_width = try self.parseNumber(); var points = try self.readPoints(); defer points.deinit(); @@ -743,9 +743,9 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) }, .outline_fill_rectangles => { - var fill_style = try self.parseStyle(); - var line_style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const fill_style = try self.parseStyle(); + const line_style = try self.parseStyle(); + const line_width = try self.parseNumber(); var rects = try self.readRectangles(); defer rects.deinit(); @@ -753,11 +753,11 @@ pub fn parse(allocator: std.mem.Allocator, source: []const u8, writer: anytype) try self.builder.writeOutlineFillRectangles(fill_style, line_style, line_width, rects.items); }, .outline_fill_path => { - var fill_style = try self.parseStyle(); - var line_style = try self.parseStyle(); - var line_width = try self.parseNumber(); + const fill_style = try self.parseStyle(); + const line_style = try self.parseStyle(); + const line_width = try self.parseNumber(); - var path = try self.readPath(); + const path = try self.readPath(); try self.builder.writeOutlineFillPath(fill_style, line_style, line_width, path.segments); }, diff --git a/src/tools/render.zig b/src/tools/render.zig index 5757bd0..177285a 100644 --- a/src/tools/render.zig +++ b/src/tools/render.zig @@ -6,7 +6,7 @@ fn printUsage(stream: anytype) !void { try stream.writeAll( \\tvg-render [-o ] [-g ] [-a] [-s ] \\ - \\Renders a TinyVG vector graphic into a TGA file. + \\Renders a TinyVG vector graphic into a TGA file. \\ \\Options: \\ -h, --help Prints this text. @@ -136,7 +136,7 @@ pub fn main() !u8 { var dest_file: std.fs.File = if (write_stdout) std.io.getStdIn() else blk: { - var out_name = cli.options.output orelse try std.mem.concat(allocator, u8, &[_][]const u8{ + const out_name = cli.options.output orelse try std.mem.concat(allocator, u8, &[_][]const u8{ cli.positionals[0][0..(cli.positionals[0].len - std.fs.path.extension(cli.positionals[0]).len)], ".tga", }); @@ -146,7 +146,7 @@ pub fn main() !u8 { defer if (!read_stdin) dest_file.close(); - var writer = dest_file.writer(); + const writer = dest_file.writer(); try dumpTga(writer, width, height, image.pixels); } @@ -161,20 +161,20 @@ fn dumpTga(src_writer: anytype, width: u16, height: u16, pixels: []const tvg.ren const image_id = "Hello, TGA!"; - try writer.writeIntLittle(u8, @as(u8, @intCast(image_id.len))); - try writer.writeIntLittle(u8, 0); // color map type = no color map - try writer.writeIntLittle(u8, 2); // image type = uncompressed true-color image + try writer.writeInt(u8, @as(u8, @intCast(image_id.len)), .little); + try writer.writeInt(u8, 0, .little); // color map type = no color map + try writer.writeInt(u8, 2, .little); // image type = uncompressed true-color image // color map spec - try writer.writeIntLittle(u16, 0); // first index - try writer.writeIntLittle(u16, 0); // length - try writer.writeIntLittle(u8, 0); // number of bits per pixel + try writer.writeInt(u16, 0, .little); // first index + try writer.writeInt(u16, 0, .little); // length + try writer.writeInt(u8, 0, .little); // number of bits per pixel // image spec - try writer.writeIntLittle(u16, 0); // x origin - try writer.writeIntLittle(u16, 0); // y origin - try writer.writeIntLittle(u16, width); // width - try writer.writeIntLittle(u16, height); // height - try writer.writeIntLittle(u8, 32); // bits per pixel - try writer.writeIntLittle(u8, 8 | 0x20); // 0…3 => alpha channel depth = 8, 4…7 => direction=top left + try writer.writeInt(u16, 0, .little); // x origin + try writer.writeInt(u16, 0, .little); // y origin + try writer.writeInt(u16, width, .little); // width + try writer.writeInt(u16, height, .little); // height + try writer.writeInt(u8, 32, .little); // bits per pixel + try writer.writeInt(u8, 8 | 0x20, .little); // 0…3 => alpha channel depth = 8, 4…7 => direction=top left try writer.writeAll(image_id); try writer.writeAll(""); // color map data \o/