freetype: support compiling with stage2 (-fno-stage1)

This commit is contained in:
PiergiorgioZagaria 2022-07-06 11:59:28 +02:00 committed by Stephen Gutekanst
parent d3b03901fb
commit b4ac18ec57
10 changed files with 61 additions and 29 deletions

View file

@ -67,11 +67,20 @@ pub fn build(b: *std.build.Builder) !void {
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
main_tests.addPackage(c_pkg); main_tests.addPackage(c_pkg);
// Remove once the stage2 compiler fixes pkg std not found
main_tests.addPackage(utils_pkg);
main_tests.addPackage(pkg); main_tests.addPackage(pkg);
link(b, main_tests, .{ .freetype = .{ link(b, main_tests, .{ .freetype = .{
.ft_config_path = "./test/ft", .ft_config_path = "./test/ft",
.brotli = true, .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"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&freetype_tests.step); test_step.dependOn(&freetype_tests.step);
@ -86,6 +95,10 @@ pub fn build(b: *std.build.Builder) !void {
example_exe.setBuildMode(mode); example_exe.setBuildMode(mode);
example_exe.setTarget(target); example_exe.setTarget(target);
example_exe.addPackage(pkg); example_exe.addPackage(pkg);
// Remove once the stage2 compiler fixes pkg std not found
example_exe.addPackage(utils_pkg);
link(b, example_exe, .{}); link(b, example_exe, .{});
example_exe.install(); example_exe.install();

View file

@ -1,6 +1,11 @@
const std = @import("std"); const std = @import("std");
const freetype = @import("freetype"); const freetype = @import("freetype");
// Remove once the stage2 compiler fixes pkg std not found
comptime {
_ = @import("utils");
}
const OutlinePrinter = struct { const OutlinePrinter = struct {
library: freetype.Library, library: freetype.Library,
face: freetype.Face, face: freetype.Face,
@ -14,13 +19,14 @@ const OutlinePrinter = struct {
const Self = @This(); const Self = @This();
var buf = [_]u8{0} ** (1024 * 10);
pub fn init(file: std.fs.File) freetype.Error!Self { pub fn init(file: std.fs.File) freetype.Error!Self {
var lib = try freetype.Library.init(); var lib = try freetype.Library.init();
return Self{ return Self{
.library = lib, .library = lib,
.face = try lib.newFace("upstream/assets/FiraSans-Regular.ttf", 0), .face = try lib.newFace("upstream/assets/FiraSans-Regular.ttf", 0),
.output_file = file, .output_file = file,
.path_stream = std.io.fixedBufferStream(&std.mem.zeroes([1024 * 10]u8)), .path_stream = std.io.fixedBufferStream(&buf),
.xMin = 0, .xMin = 0,
.yMin = 0, .yMin = 0,
.width = 0, .width = 0,

View file

@ -2,6 +2,11 @@
const std = @import("std"); const std = @import("std");
const freetype = @import("freetype"); const freetype = @import("freetype");
// Remove once the stage2 compiler fixes pkg std not found
comptime {
_ = @import("utils");
}
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();

View file

@ -123,7 +123,7 @@ pub fn getCharIndex(self: Face, char: u32) ?u32 {
} }
pub fn getNameIndex(self: Face, name: [:0]const u8) ?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; 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 { pub fn getPostscriptName(self: Face) ?[:0]const u8 {
return if (c.FT_Get_Postscript_Name(self.handle)) |face_name| 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 else
null; null;
} }
@ -185,21 +185,21 @@ pub fn getCharVariantIsDefault(self: Face, char: u32, variant_selector: u32) ?bo
pub fn getVariantSelectors(self: Face) ?[]u32 { pub fn getVariantSelectors(self: Face) ?[]u32 {
return if (c.FT_Face_GetVariantSelectors(self.handle)) |chars| 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 else
null; null;
} }
pub fn getVariantsOfChar(self: Face, char: u32) ?[]u32 { pub fn getVariantsOfChar(self: Face, char: u32) ?[]u32 {
return if (c.FT_Face_GetVariantsOfChar(self.handle, char)) |variants| 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 else
null; null;
} }
pub fn getCharsOfVariant(self: Face, variant_selector: u32) ?[]u32 { pub fn getCharsOfVariant(self: Face, variant_selector: u32) ?[]u32 {
return if (c.FT_Face_GetCharsOfVariant(self.handle, variant_selector)) |chars| 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 else
null; null;
} }
@ -288,14 +288,14 @@ pub fn numGlyphs(self: Face) u32 {
pub fn familyName(self: Face) ?[:0]const u8 { pub fn familyName(self: Face) ?[:0]const u8 {
return if (self.handle.*.family_name) |family| return if (self.handle.*.family_name) |family|
std.mem.span(family) std.mem.span(@ptrCast([*:0]const u8, family))
else else
null; null;
} }
pub fn styleName(self: Face) ?[:0]const u8 { pub fn styleName(self: Face) ?[:0]const u8 {
return if (self.handle.*.style_name) |style_name| return if (self.handle.*.style_name) |style_name|
std.mem.span(style_name) std.mem.span(@ptrCast([*:0]const u8, style_name))
else else
null; null;
} }
@ -316,7 +316,7 @@ pub fn numCharmaps(self: Face) u32 {
} }
pub fn charmaps(self: Face) []const CharMap { 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 { pub fn bbox(self: Face) BBox {

View file

@ -1,4 +1,5 @@
const c = @import("c"); const c = @import("c");
const builtin = @import("builtin");
const intToError = @import("error.zig").intToError; const intToError = @import("error.zig").intToError;
const errorToInt = @import("error.zig").errorToInt; const errorToInt = @import("error.zig").errorToInt;
const Error = @import("error.zig").Error; const Error = @import("error.zig").Error;
@ -46,10 +47,10 @@ pub fn bbox(self: Outline) Error!BBox {
pub fn Funcs(comptime Context: type) type { pub fn Funcs(comptime Context: type) type {
return struct { return struct {
move_to: fn (ctx: Context, 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: 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: fn (ctx: Context, control: Vector, 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: fn (ctx: Context, control_0: Vector, control_1: 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, shift: i32,
delta: 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 }; var wrapper = FuncsWrapper(@TypeOf(ctx)){ .ctx = ctx, .callbacks = callbacks };
try intToError(c.FT_Outline_Decompose( try intToError(c.FT_Outline_Decompose(
self.handle, self.handle,
&.{ &c.FT_Outline_Funcs{
.move_to = @TypeOf(wrapper).move_to, .move_to = @TypeOf(wrapper).move_to,
.line_to = @TypeOf(wrapper).line_to, .line_to = @TypeOf(wrapper).line_to,
.conic_to = @TypeOf(wrapper).conic_to, .conic_to = @TypeOf(wrapper).conic_to,

View file

@ -333,7 +333,7 @@ pub const Buffer = struct {
pub fn getGlyphPositions(self: Buffer) ?[]Position { pub fn getGlyphPositions(self: Buffer) ?[]Position {
return if (hb_buffer_get_glyph_positions(self.handle, null)) |positions| return if (hb_buffer_get_glyph_positions(self.handle, null)) |positions|
positions[0..self.getLength()] @ptrCast([*]Position, positions)[0..self.getLength()]
else else
null; null;
} }

View file

@ -13,7 +13,7 @@ pub const Direction = enum(u3) {
} }
pub fn toString(self: Direction) [:0]const u8 { 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 { 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 { pub fn getDefault() Language {

View file

@ -11,8 +11,9 @@ pub const ListShapers = struct {
pub fn next(self: *ListShapers) ?[:0]const u8 { pub fn next(self: *ListShapers) ?[:0]const u8 {
self.index += 1; self.index += 1;
return std.mem.span( return std.mem.span(@ptrCast(
?[*:0]const u8,
self.list[self.index - 1] orelse return null, self.list[self.index - 1] orelse return null,
); ));
} }
}; };

View file

@ -68,7 +68,7 @@ pub const ShapePlan = struct {
} }
pub fn getShaper(self: ShapePlan) [:0]const u8 { 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)));
} }
}; };

View file

@ -1,17 +1,23 @@
const testing = @import("std").testing; const testing = @import("std").testing;
const freetype = @import("freetype"); const freetype = @import("freetype");
const firasnas_font_path = "upstream/assets/FiraSans-Regular.ttf"; const test_option = @import("test_option");
const firasnas_font_data = @embedFile("../upstream/assets/FiraSans-Regular.ttf"); 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" { test "new face from file" {
const lib = try freetype.Library.init(); 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" { test "new face from memory" {
const lib = try freetype.Library.init(); const lib = try freetype.Library.init();
_ = try lib.newFaceMemory(firasnas_font_data, 0); _ = try lib.newFaceMemory(firasans_font_data, 0);
} }
test "new stroker" { test "new stroker" {
@ -30,7 +36,7 @@ test "set lcd filter" {
test "load glyph" { test "load glyph" {
const lib = try freetype.Library.init(); 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.setPixelSizes(100, 100);
try face.setCharSize(10 * 10, 0, 72, 0); try face.setCharSize(10 * 10, 0, 72, 0);
@ -50,13 +56,13 @@ test "attach file" {
test "attach from memory" { test "attach from memory" {
const lib = try freetype.Library.init(); const lib = try freetype.Library.init();
const face = try lib.newFace("upstream/assets/DejaVuSans.pfb", 0); 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); try face.attachMemory(file);
} }
test "charmap iterator" { test "charmap iterator" {
const lib = try freetype.Library.init(); 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 iterator = face.getCharmapIterator();
var old_char: usize = 0; var old_char: usize = 0;
while (iterator.next()) |c| { while (iterator.next()) |c| {
@ -67,13 +73,13 @@ test "charmap iterator" {
test "get name index" { test "get name index" {
const lib = try freetype.Library.init(); 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").?); try testing.expectEqual(@as(u32, 1120), face.getNameIndex("summation").?);
} }
test "get index name" { test "get index name" {
const lib = try freetype.Library.init(); 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; var name_buf: [30]u8 = undefined;
try testing.expectEqualStrings("summation", try face.getGlyphName(1120, &name_buf)); try testing.expectEqualStrings("summation", try face.getGlyphName(1120, &name_buf));
} }