From b4ac18ec5717869e403b8247b250713b6892870e Mon Sep 17 00:00:00 2001 From: PiergiorgioZagaria Date: Wed, 6 Jul 2022 11:59:28 +0200 Subject: [PATCH] freetype: support compiling with stage2 (-fno-stage1) --- freetype/build.zig | 13 +++++++++++++ freetype/examples/glyph-to-svg.zig | 8 +++++++- freetype/examples/single-glyph.zig | 5 +++++ freetype/src/freetype/Face.zig | 16 ++++++++-------- freetype/src/freetype/Outline.zig | 11 ++++++----- freetype/src/harfbuzz/buffer.zig | 2 +- freetype/src/harfbuzz/common.zig | 4 ++-- freetype/src/harfbuzz/shape.zig | 5 +++-- freetype/src/harfbuzz/shape_plan.zig | 2 +- freetype/test/main.zig | 24 +++++++++++++++--------- 10 files changed, 61 insertions(+), 29 deletions(-) diff --git a/freetype/build.zig b/freetype/build.zig index d7bf51da..fa53a9ea 100644 --- a/freetype/build.zig +++ b/freetype/build.zig @@ -67,11 +67,20 @@ pub fn build(b: *std.build.Builder) !void { main_tests.setBuildMode(mode); main_tests.setTarget(target); main_tests.addPackage(c_pkg); + + // Remove once the stage2 compiler fixes pkg std not found + main_tests.addPackage(utils_pkg); + main_tests.addPackage(pkg); link(b, main_tests, .{ .freetype = .{ .ft_config_path = "./test/ft", .brotli = true, } }); + var font_option = b.addOptions(); + + font_option.addOption([]const u8, "font", try std.fs.cwd().readFileAlloc(b.allocator, "upstream/assets/FiraSans-Regular.ttf", std.math.maxInt(u32))); + font_option.addOption([]const u8, "file", try std.fs.cwd().readFileAlloc(b.allocator, "upstream/assets/DejaVuSans.pfm", std.math.maxInt(u32))); + main_tests.addOptions("test_option", font_option); const test_step = b.step("test", "Run library tests"); test_step.dependOn(&freetype_tests.step); @@ -86,6 +95,10 @@ pub fn build(b: *std.build.Builder) !void { example_exe.setBuildMode(mode); example_exe.setTarget(target); example_exe.addPackage(pkg); + + // Remove once the stage2 compiler fixes pkg std not found + example_exe.addPackage(utils_pkg); + link(b, example_exe, .{}); example_exe.install(); diff --git a/freetype/examples/glyph-to-svg.zig b/freetype/examples/glyph-to-svg.zig index 6bb9e002..fe3fbf28 100644 --- a/freetype/examples/glyph-to-svg.zig +++ b/freetype/examples/glyph-to-svg.zig @@ -1,6 +1,11 @@ const std = @import("std"); const freetype = @import("freetype"); +// Remove once the stage2 compiler fixes pkg std not found +comptime { + _ = @import("utils"); +} + const OutlinePrinter = struct { library: freetype.Library, face: freetype.Face, @@ -14,13 +19,14 @@ const OutlinePrinter = struct { const Self = @This(); + var buf = [_]u8{0} ** (1024 * 10); pub fn init(file: std.fs.File) freetype.Error!Self { var lib = try freetype.Library.init(); return Self{ .library = lib, .face = try lib.newFace("upstream/assets/FiraSans-Regular.ttf", 0), .output_file = file, - .path_stream = std.io.fixedBufferStream(&std.mem.zeroes([1024 * 10]u8)), + .path_stream = std.io.fixedBufferStream(&buf), .xMin = 0, .yMin = 0, .width = 0, diff --git a/freetype/examples/single-glyph.zig b/freetype/examples/single-glyph.zig index 5a581d5b..ac82384d 100644 --- a/freetype/examples/single-glyph.zig +++ b/freetype/examples/single-glyph.zig @@ -2,6 +2,11 @@ const std = @import("std"); const freetype = @import("freetype"); +// Remove once the stage2 compiler fixes pkg std not found +comptime { + _ = @import("utils"); +} + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); diff --git a/freetype/src/freetype/Face.zig b/freetype/src/freetype/Face.zig index 004943d5..ba80a68b 100644 --- a/freetype/src/freetype/Face.zig +++ b/freetype/src/freetype/Face.zig @@ -123,7 +123,7 @@ pub fn getCharIndex(self: Face, char: u32) ?u32 { } pub fn getNameIndex(self: Face, name: [:0]const u8) ?u32 { - const i = c.FT_Get_Name_Index(self.handle, name); + const i = c.FT_Get_Name_Index(self.handle, name.ptr); return if (i == 0) null else i; } @@ -146,7 +146,7 @@ pub fn getGlyphName(self: Face, index: u32, name_buf: *[30]u8) Error![]const u8 pub fn getPostscriptName(self: Face) ?[:0]const u8 { return if (c.FT_Get_Postscript_Name(self.handle)) |face_name| - std.mem.span(face_name) + std.mem.span(@ptrCast([*:0]const u8, face_name)) else null; } @@ -185,21 +185,21 @@ pub fn getCharVariantIsDefault(self: Face, char: u32, variant_selector: u32) ?bo pub fn getVariantSelectors(self: Face) ?[]u32 { return if (c.FT_Face_GetVariantSelectors(self.handle)) |chars| - @ptrCast([]u32, std.mem.sliceTo(chars, 0)) + @ptrCast([]u32, std.mem.sliceTo(@ptrCast([*:0]u32, chars), 0)) else null; } pub fn getVariantsOfChar(self: Face, char: u32) ?[]u32 { return if (c.FT_Face_GetVariantsOfChar(self.handle, char)) |variants| - @ptrCast([]u32, std.mem.sliceTo(variants, 0)) + @ptrCast([]u32, std.mem.sliceTo(@ptrCast([*:0]u32, variants), 0)) else null; } pub fn getCharsOfVariant(self: Face, variant_selector: u32) ?[]u32 { return if (c.FT_Face_GetCharsOfVariant(self.handle, variant_selector)) |chars| - @ptrCast([]u32, std.mem.sliceTo(chars, 0)) + @ptrCast([]u32, std.mem.sliceTo(@ptrCast([*:0]u32, chars), 0)) else null; } @@ -288,14 +288,14 @@ pub fn numGlyphs(self: Face) u32 { pub fn familyName(self: Face) ?[:0]const u8 { return if (self.handle.*.family_name) |family| - std.mem.span(family) + std.mem.span(@ptrCast([*:0]const u8, family)) else null; } pub fn styleName(self: Face) ?[:0]const u8 { return if (self.handle.*.style_name) |style_name| - std.mem.span(style_name) + std.mem.span(@ptrCast([*:0]const u8, style_name)) else null; } @@ -316,7 +316,7 @@ pub fn numCharmaps(self: Face) u32 { } pub fn charmaps(self: Face) []const CharMap { - return @ptrCast([]const CharMap, self.handle.*.charmaps[0..self.numCharmaps()]); + return @ptrCast([*]const CharMap, self.handle.*.charmaps)[0..self.numCharmaps()]; } pub fn bbox(self: Face) BBox { diff --git a/freetype/src/freetype/Outline.zig b/freetype/src/freetype/Outline.zig index b24af084..0ba53540 100644 --- a/freetype/src/freetype/Outline.zig +++ b/freetype/src/freetype/Outline.zig @@ -1,4 +1,5 @@ const c = @import("c"); +const builtin = @import("builtin"); const intToError = @import("error.zig").intToError; const errorToInt = @import("error.zig").errorToInt; const Error = @import("error.zig").Error; @@ -46,10 +47,10 @@ pub fn bbox(self: Outline) Error!BBox { pub fn Funcs(comptime Context: type) type { return struct { - move_to: fn (ctx: Context, to: Vector) Error!void, - line_to: fn (ctx: Context, to: Vector) Error!void, - conic_to: fn (ctx: Context, control: Vector, to: Vector) Error!void, - cubic_to: fn (ctx: Context, control_0: Vector, control_1: Vector, to: Vector) Error!void, + move_to: if (builtin.zig_backend == .stage1 or builtin.zig_backend == .other) fn (ctx: Context, to: Vector) Error!void else *const fn (ctx: Context, to: Vector) Error!void, + line_to: if (builtin.zig_backend == .stage1 or builtin.zig_backend == .other) fn (ctx: Context, to: Vector) Error!void else *const fn (ctx: Context, to: Vector) Error!void, + conic_to: if (builtin.zig_backend == .stage1 or builtin.zig_backend == .other) fn (ctx: Context, control: Vector, to: Vector) Error!void else *const fn (ctx: Context, control: Vector, to: Vector) Error!void, + cubic_to: if (builtin.zig_backend == .stage1 or builtin.zig_backend == .other) fn (ctx: Context, control_0: Vector, control_1: Vector, to: Vector) Error!void else *const fn (ctx: Context, control_0: Vector, control_1: Vector, to: Vector) Error!void, shift: i32, delta: i32, }; @@ -121,7 +122,7 @@ pub fn decompose(self: Outline, ctx: anytype, callbacks: Funcs(@TypeOf(ctx))) Er var wrapper = FuncsWrapper(@TypeOf(ctx)){ .ctx = ctx, .callbacks = callbacks }; try intToError(c.FT_Outline_Decompose( self.handle, - &.{ + &c.FT_Outline_Funcs{ .move_to = @TypeOf(wrapper).move_to, .line_to = @TypeOf(wrapper).line_to, .conic_to = @TypeOf(wrapper).conic_to, diff --git a/freetype/src/harfbuzz/buffer.zig b/freetype/src/harfbuzz/buffer.zig index d69ab65f..4a58ea0d 100644 --- a/freetype/src/harfbuzz/buffer.zig +++ b/freetype/src/harfbuzz/buffer.zig @@ -333,7 +333,7 @@ pub const Buffer = struct { pub fn getGlyphPositions(self: Buffer) ?[]Position { return if (hb_buffer_get_glyph_positions(self.handle, null)) |positions| - positions[0..self.getLength()] + @ptrCast([*]Position, positions)[0..self.getLength()] else null; } diff --git a/freetype/src/harfbuzz/common.zig b/freetype/src/harfbuzz/common.zig index 3d86a605..f04092a2 100644 --- a/freetype/src/harfbuzz/common.zig +++ b/freetype/src/harfbuzz/common.zig @@ -13,7 +13,7 @@ pub const Direction = enum(u3) { } pub fn toString(self: Direction) [:0]const u8 { - return std.mem.span(c.hb_direction_to_string(@enumToInt(self))); + return std.mem.span(@ptrCast([*:0]const u8, c.hb_direction_to_string(@enumToInt(self)))); } }; @@ -210,7 +210,7 @@ pub const Language = struct { } pub fn toString(self: Language) [:0]const u8 { - return std.mem.span(c.hb_language_to_string(self.handle)); + return std.mem.span(@ptrCast([*:0]const u8, c.hb_language_to_string(self.handle))); } pub fn getDefault() Language { diff --git a/freetype/src/harfbuzz/shape.zig b/freetype/src/harfbuzz/shape.zig index e35d4409..c098c9cc 100644 --- a/freetype/src/harfbuzz/shape.zig +++ b/freetype/src/harfbuzz/shape.zig @@ -11,8 +11,9 @@ pub const ListShapers = struct { pub fn next(self: *ListShapers) ?[:0]const u8 { self.index += 1; - return std.mem.span( + return std.mem.span(@ptrCast( + ?[*:0]const u8, self.list[self.index - 1] orelse return null, - ); + )); } }; diff --git a/freetype/src/harfbuzz/shape_plan.zig b/freetype/src/harfbuzz/shape_plan.zig index 22ee682b..47bbd7ce 100644 --- a/freetype/src/harfbuzz/shape_plan.zig +++ b/freetype/src/harfbuzz/shape_plan.zig @@ -68,7 +68,7 @@ pub const ShapePlan = struct { } pub fn getShaper(self: ShapePlan) [:0]const u8 { - return std.mem.span(c.hb_shape_plan_get_shaper(self.handle)); + return std.mem.span(@ptrCast([*:0]const u8, c.hb_shape_plan_get_shaper(self.handle))); } }; diff --git a/freetype/test/main.zig b/freetype/test/main.zig index b92d5a6b..bc8be31d 100644 --- a/freetype/test/main.zig +++ b/freetype/test/main.zig @@ -1,17 +1,23 @@ const testing = @import("std").testing; const freetype = @import("freetype"); -const firasnas_font_path = "upstream/assets/FiraSans-Regular.ttf"; -const firasnas_font_data = @embedFile("../upstream/assets/FiraSans-Regular.ttf"); +const test_option = @import("test_option"); +const firasans_font_path = "upstream/assets/FiraSans-Regular.ttf"; +const firasans_font_data = test_option.font; + +// Remove once the stage2 compiler fixes pkg std not found +comptime { + _ = @import("utils"); +} test "new face from file" { const lib = try freetype.Library.init(); - _ = try lib.newFace(firasnas_font_path, 0); + _ = try lib.newFace(firasans_font_path, 0); } test "new face from memory" { const lib = try freetype.Library.init(); - _ = try lib.newFaceMemory(firasnas_font_data, 0); + _ = try lib.newFaceMemory(firasans_font_data, 0); } test "new stroker" { @@ -30,7 +36,7 @@ test "set lcd filter" { test "load glyph" { const lib = try freetype.Library.init(); - const face = try lib.newFace(firasnas_font_path, 0); + const face = try lib.newFace(firasans_font_path, 0); try face.setPixelSizes(100, 100); try face.setCharSize(10 * 10, 0, 72, 0); @@ -50,13 +56,13 @@ test "attach file" { test "attach from memory" { const lib = try freetype.Library.init(); const face = try lib.newFace("upstream/assets/DejaVuSans.pfb", 0); - const file = @embedFile("../upstream/assets/DejaVuSans.pfm"); + const file = test_option.file; try face.attachMemory(file); } test "charmap iterator" { const lib = try freetype.Library.init(); - const face = try lib.newFace(firasnas_font_path, 0); + const face = try lib.newFace(firasans_font_path, 0); var iterator = face.getCharmapIterator(); var old_char: usize = 0; while (iterator.next()) |c| { @@ -67,13 +73,13 @@ test "charmap iterator" { test "get name index" { const lib = try freetype.Library.init(); - const face = try lib.newFace(firasnas_font_path, 0); + const face = try lib.newFace(firasans_font_path, 0); try testing.expectEqual(@as(u32, 1120), face.getNameIndex("summation").?); } test "get index name" { const lib = try freetype.Library.init(); - const face = try lib.newFace(firasnas_font_path, 0); + const face = try lib.newFace(firasans_font_path, 0); var name_buf: [30]u8 = undefined; try testing.expectEqualStrings("summation", try face.getGlyphName(1120, &name_buf)); }