freetype: ~99% Core-API Coverage

- breaking structure changes
- optimazed examples
- more tests
This commit is contained in:
Ali Chraghi 2022-06-07 00:12:29 +04:30 committed by Stephen Gutekanst
parent db377459c4
commit 04a0a79ef6
21 changed files with 1176 additions and 549 deletions

View file

@ -1,101 +1,59 @@
const std = @import("std");
const c = @import("c.zig");
const types = @import("types.zig");
const GlyphSlot = @import("GlyphSlot.zig");
const Library = @import("Library.zig");
const Error = @import("error.zig").Error;
const intToError = @import("error.zig").intToError;
const utils = @import("utils.zig");
const intToError = @import("error.zig").intToError;
const Error = @import("error.zig").Error;
const GlyphSlot = @import("freetype.zig").GlyphSlot;
const LoadFlags = @import("freetype.zig").LoadFlags;
const FaceFlags = @import("freetype.zig").FaceFlags;
const StyleFlags = @import("freetype.zig").StyleFlags;
const FSType = @import("freetype.zig").FSType;
const OpenArgs = @import("freetype.zig").OpenArgs;
const KerningMode = @import("freetype.zig").KerningMode;
const Encoding = @import("freetype.zig").Encoding;
const CharMap = @import("freetype.zig").CharMap;
const Size = @import("freetype.zig").Size;
const SizeRequest = @import("freetype.zig").SizeRequest;
const BitmapSize = @import("freetype.zig").BitmapSize;
const Matrix = @import("types.zig").Matrix;
const BBox = @import("types.zig").BBox;
const Vector = @import("image.zig").Vector;
const RootTransform = @import("color.zig").RootTransform;
const PaintFormat = @import("color.zig").PaintFormat;
const Color = @import("color.zig").Color;
const ClipBox = @import("color.zig").ClipBox;
const OpaquePaint = @import("color.zig").OpaquePaint;
const Paint = @import("color.zig").Paint;
const PaletteData = @import("color.zig").PaletteData;
const GlyphLayersIterator = @import("color.zig").GlyphLayersIterator;
const Face = @This();
pub const SizeMetrics = extern struct {
x_ppem: u16,
y_ppem: u16,
x_scale: c_long,
y_scale: c_long,
ascender: c_long,
descender: c_long,
height: c_long,
max_advance: c_long,
};
pub const KerningMode = enum(u2) {
default = c.FT_KERNING_DEFAULT,
unfitted = c.FT_KERNING_UNFITTED,
unscaled = c.FT_KERNING_UNSCALED,
};
pub const LoadFlags = packed struct {
no_scale: bool = false,
no_hinting: bool = false,
render: bool = false,
no_bitmap: bool = false,
vertical_layout: bool = false,
force_autohint: bool = false,
crop_bitmap: bool = false,
pedantic: bool = false,
ignore_global_advance_with: bool = false,
no_recurse: bool = false,
ignore_transform: bool = false,
monochrome: bool = false,
linear_design: bool = false,
no_autohint: bool = false,
target_normal: bool = false,
target_light: bool = false,
target_mono: bool = false,
target_lcd: bool = false,
target_lcd_v: bool = false,
color: bool = false,
pub const CharmapIterator = struct {
face: Face,
index: u32,
charcode: u32,
pub const Flag = enum(u21) {
no_scale = c.FT_LOAD_NO_SCALE,
no_hinting = c.FT_LOAD_NO_HINTING,
render = c.FT_LOAD_RENDER,
no_bitmap = c.FT_LOAD_NO_BITMAP,
vertical_layout = c.FT_LOAD_VERTICAL_LAYOUT,
force_autohint = c.FT_LOAD_FORCE_AUTOHINT,
crop_bitmap = c.FT_LOAD_CROP_BITMAP,
pedantic = c.FT_LOAD_PEDANTIC,
ignore_global_advance_with = c.FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH,
no_recurse = c.FT_LOAD_NO_RECURSE,
ignore_transform = c.FT_LOAD_IGNORE_TRANSFORM,
monochrome = c.FT_LOAD_MONOCHROME,
linear_design = c.FT_LOAD_LINEAR_DESIGN,
no_autohint = c.FT_LOAD_NO_AUTOHINT,
target_normal = c.FT_LOAD_TARGET_NORMAL,
target_light = c.FT_LOAD_TARGET_LIGHT,
target_mono = c.FT_LOAD_TARGET_MONO,
target_lcd = c.FT_LOAD_TARGET_LCD,
target_lcd_v = c.FT_LOAD_TARGET_LCD_V,
color = c.FT_LOAD_COLOR,
};
pub fn toBitFields(flags: LoadFlags) u21 {
return utils.structToBitFields(u21, Flag, flags);
pub fn init(face: Face) CharmapIterator {
var i: u32 = 0;
const cc = c.FT_Get_First_Char(face.handle, &i);
return .{
.face = face,
.index = i,
.charcode = @intCast(u32, cc),
};
}
};
pub const StyleFlags = packed struct {
bold: bool = false,
italic: bool = false,
pub const Flag = enum(u2) {
bold = c.FT_STYLE_FLAG_BOLD,
italic = c.FT_STYLE_FLAG_ITALIC,
};
pub fn toBitFields(flags: StyleFlags) u2 {
return utils.structToBitFields(u2, StyleFlags, Flag, flags);
pub fn next(self: *CharmapIterator) ?u32 {
self.charcode = @intCast(u32, c.FT_Get_Next_Char(self.face.handle, self.charcode, &self.index));
return if (self.index != 0)
self.charcode
else
null;
}
};
handle: c.FT_Face,
glyph: GlyphSlot,
pub fn init(handle: c.FT_Face) Face {
return Face{
.handle = handle,
.glyph = GlyphSlot.init(handle.*.glyph),
};
}
pub fn deinit(self: Face) void {
intToError(c.FT_Done_Face(self.handle)) catch |err| {
@ -117,8 +75,16 @@ pub fn attachMemory(self: Face, bytes: []const u8) Error!void {
});
}
pub fn attachStream(self: Face, args: types.OpenArgs) Error!void {
return intToError(c.FT_Attach_Stream(self.handle, &args.toCInterface()));
pub fn attachStream(self: Face, args: OpenArgs) Error!void {
return intToError(c.FT_Attach_Stream(self.handle, &args.cast()));
}
pub fn loadGlyph(self: Face, index: u32, flags: LoadFlags) Error!void {
return intToError(c.FT_Load_Glyph(self.handle, index, flags.cast()));
}
pub fn loadChar(self: Face, char: u32, flags: LoadFlags) Error!void {
return intToError(c.FT_Load_Char(self.handle, char, flags.cast()));
}
pub fn setCharSize(self: Face, pt_width: i32, pt_height: i32, horz_resolution: u16, vert_resolution: u16) Error!void {
@ -129,29 +95,277 @@ pub fn setPixelSizes(self: Face, pixel_width: u32, pixel_height: u32) Error!void
return intToError(c.FT_Set_Pixel_Sizes(self.handle, pixel_width, pixel_height));
}
pub fn loadGlyph(self: Face, index: u32, flags: LoadFlags) Error!void {
return intToError(c.FT_Load_Glyph(self.handle, index, flags.toBitFields()));
pub fn requestSize(self: Face, req: SizeRequest) Error!void {
var req_mut = req;
return intToError(c.FT_Request_Size(self.handle, &req_mut));
}
pub fn loadChar(self: Face, char: u32, flags: LoadFlags) Error!void {
return intToError(c.FT_Load_Char(self.handle, char, flags.toBitFields()));
pub fn selectSize(self: Face, strike_index: i32) Error!void {
return intToError(c.FT_Select_Size(self.handle, strike_index));
}
pub fn setTransform(self: Face, matrix: ?types.Matrix, delta: ?types.Vector) Error!void {
var m = matrix orelse std.mem.zeroes(types.Matrix);
var d = delta orelse std.mem.zeroes(types.Vector);
return c.FT_Set_Transform(self.handle, @ptrCast(*c.FT_Matrix, &m), @ptrCast(*c.FT_Vector, &d));
pub fn setTransform(self: Face, matrix: ?Matrix, delta: ?Vector) Error!void {
var matrix_mut = matrix;
var delta_mut = delta;
return c.FT_Set_Transform(self.handle, if (matrix_mut) |*m| m else null, if (delta_mut) |*d| d else null);
}
pub fn getCharIndex(self: Face, index: u32) ?u32 {
const i = c.FT_Get_Char_Index(self.handle, index);
pub fn getTransform(self: Face) std.meta.Tuple(&.{ Matrix, Vector }) {
var matrix: Matrix = undefined;
var delta: Vector = undefined;
c.FT_Get_Transform(self.handle, &matrix, &delta);
return .{ matrix, delta };
}
pub fn getCharIndex(self: Face, char: u32) ?u32 {
const i = c.FT_Get_Char_Index(self.handle, char);
return if (i == 0) null else i;
}
pub fn getKerning(self: Face, left_char_index: u32, right_char_index: u32, mode: KerningMode) Error!types.Vector {
var vec = std.mem.zeroes(types.Vector);
try intToError(c.FT_Get_Kerning(self.handle, left_char_index, right_char_index, @enumToInt(mode), @ptrCast(*c.FT_Vector, &vec)));
return vec;
pub fn getNameIndex(self: Face, name: [:0]const u8) ?u32 {
const i = c.FT_Get_Name_Index(self.handle, name);
return if (i == 0) null else i;
}
pub fn getKerning(self: Face, left_char_index: u32, right_char_index: u32, mode: KerningMode) Error!Vector {
var kerning: Vector = undefined;
try intToError(c.FT_Get_Kerning(self.handle, left_char_index, right_char_index, @enumToInt(mode), &kerning));
return kerning;
}
pub fn getTrackKerning(self: Face, point_size: i32, degree: i32) Error!i32 {
var kerning: i32 = 0;
try intToError(c.FT_Get_Track_Kerning(self.handle, point_size, degree, &@intCast(c_long, kerning)));
return kerning;
}
pub fn getGlyphName(self: Face, index: u32) Error![]const u8 {
var name_buf: [30]u8 = undefined;
try intToError(c.FT_Get_Glyph_Name(self.handle, index, &name_buf, 30));
return std.mem.sliceTo(&name_buf, 0);
}
pub fn getPostscriptName(self: Face) ?[:0]const u8 {
return if (c.FT_Get_Postscript_Name(self.handle)) |face_name|
std.mem.span(face_name)
else
null;
}
pub fn getCharmapIterator(self: Face) CharmapIterator {
return CharmapIterator.init(self);
}
pub fn selectCharmap(self: Face, encoding: Encoding) Error!void {
return intToError(c.FT_Select_Charmap(self.handle, @enumToInt(encoding)));
}
pub fn setCharmap(self: Face, char_map: *CharMap) Error!void {
return intToError(c.FT_Set_Charmap(self.handle, char_map));
}
pub fn getFSTypeFlags(self: Face) FSType {
return FSType.from(@intCast(u10, c.FT_Get_FSType_Flags(self.handle)));
}
pub fn getCharVariantIndex(self: Face, char: u32, variant_selector: u32) ?u32 {
return switch (c.FT_Face_GetCharVariantIndex(self.handle, char, variant_selector)) {
0 => null,
else => |i| i,
};
}
pub fn getCharVariantIsDefault(self: Face, char: u32, variant_selector: u32) ?bool {
return switch (c.FT_Face_GetCharVariantIsDefault(self.handle, char, variant_selector)) {
-1 => null,
0 => false,
1 => true,
else => unreachable,
};
}
pub fn getVariantSelectors(self: Face) ?[]u32 {
return if (c.FT_Face_GetVariantSelectors(self.handle)) |chars|
@ptrCast([]u32, std.mem.sliceTo(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))
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))
else
null;
}
pub fn getPaletteData(self: Face) Error!PaletteData {
var p: c.FT_Palette_Data = undefined;
try intToError(c.FT_Palette_Data_Get(self.handle, &p));
return PaletteData{ .handle = p };
}
fn selectPalette(self: Face, index: u16) Error!?[]const Color {
var color: [*:0]Color = undefined;
try intToError(c.FT_Palette_Select(self.handle, index, &color));
const pd = try getPaletteData();
return self.color[0..pd.numPaletteEntries()];
}
pub fn setPaletteForegroundColor(self: Face, color: Color) Error!void {
try intToError(c.FT_Palette_Set_Foreground_Color(self.handle, color));
}
pub fn getGlyphLayersIterator(self: Face, glyph_index: u32) GlyphLayersIterator {
return GlyphLayersIterator.init(self, glyph_index);
}
pub fn getColorGlyphPaint(self: Face, base_glyph: u32, root_transform: RootTransform) ?Paint {
var opaque_paint: OpaquePaint = undefined;
if (c.FT_Get_Color_Glyph_Paint(self.handle, base_glyph, @enumToInt(root_transform), &opaque_paint) == 0)
return null;
return self.getPaint(opaque_paint);
}
pub fn getColorGlyphClibBox(self: Face, base_glyph: u32) ?ClipBox {
var clib_box: ClipBox = undefined;
if (c.FT_Get_Color_Glyph_ClipBox(self.handle, base_glyph, &clib_box) == 0)
return null;
return clib_box;
}
pub fn getPaint(self: Face, opaque_paint: OpaquePaint) ?Paint {
var p: c.FT_COLR_Paint = undefined;
if (c.FT_Get_Paint(self.handle, opaque_paint, &p) == 0)
return null;
return switch (@intToEnum(PaintFormat, p.format)) {
.color_layers => Paint{ .color_layers = p.u.colr_layers },
.glyph => Paint{ .glyph = p.u.glyph },
.solid => Paint{ .solid = p.u.solid },
.linear_gradient => Paint{ .linear_gradient = p.u.linear_gradient },
.radial_gradient => Paint{ .radial_gradient = p.u.radial_gradient },
.sweep_gradient => Paint{ .sweep_gradient = p.u.sweep_gradient },
.transform => Paint{ .transform = p.u.transform },
.translate => Paint{ .translate = p.u.translate },
.scale => Paint{ .scale = p.u.scale },
.rotate => Paint{ .rotate = p.u.rotate },
.skew => Paint{ .skew = p.u.skew },
.composite => Paint{ .composite = p.u.composite },
.color_glyph => Paint{ .color_glyph = p.u.colr_glyph },
};
}
pub fn newSize(self: Face) Error!Size {
var s: c.FT_Size = undefined;
try intToError(c.FT_New_Size(self.handle, &s));
return Size{ .handle = s };
}
pub fn numFaces(self: Face) u32 {
return @intCast(u32, self.handle.*.num_faces);
}
pub fn faceIndex(self: Face) u32 {
return @intCast(u32, self.handle.*.face_index);
}
pub fn faceFlags(self: Face) FaceFlags {
return FaceFlags.from(@intCast(u19, self.handle.*.face_flags));
}
pub fn styleFlags(self: Face) StyleFlags {
return StyleFlags.from(@intCast(u2, self.handle.*.style_flags));
}
pub fn numGlyphs(self: Face) u32 {
return @intCast(u32, self.handle.*.num_glyphs);
}
pub fn familyName(self: Face) ?[:0]const u8 {
return if (self.handle.*.family_name) |family|
std.mem.span(family)
else
null;
}
pub fn styleName(self: Face) ?[:0]const u8 {
return if (self.handle.*.style_name) |style_name|
std.mem.span(style_name)
else
null;
}
pub fn numFixedSizes(self: Face) u32 {
return @intCast(u32, self.handle.*.num_fixed_sizes);
}
pub fn availableSizes(self: Face) ?BitmapSize {
return if (self.handle.*.available_sizes != null)
self.handle.*.available_sizes.*
else
null;
}
pub fn numCharmaps(self: Face) u32 {
return @intCast(u32, self.handle.*.num_charmaps);
}
pub fn charmaps(self: Face) []const CharMap {
return @ptrCast([]const CharMap, self.handle.*.charmaps[0..self.numCharmaps()]);
}
pub fn bbox(self: Face) BBox {
return self.handle.*.bbox;
}
pub fn unitsPerEM(self: Face) u16 {
return self.handle.*.units_per_EM;
}
pub fn ascender(self: Face) i16 {
return self.handle.*.ascender;
}
pub fn descender(self: Face) i16 {
return self.handle.*.descender;
}
pub fn height(self: Face) i16 {
return self.handle.*.height;
}
pub fn maxAdvanceWidth(self: Face) i16 {
return self.handle.*.max_advance_width;
}
pub fn maxAdvanceHeight(self: Face) i16 {
return self.handle.*.max_advance_height;
}
pub fn underlinePosition(self: Face) i16 {
return self.handle.*.underline_position;
}
pub fn underlineThickness(self: Face) i16 {
return self.handle.*.underline_thickness;
}
pub fn glyph(self: Face) GlyphSlot {
return .{ .handle = self.handle.*.glyph };
}
pub fn size(self: Face) Size {
return Size{ .handle = self.handle.*.size };
}
pub fn charmap(self: Face) CharMap {
return self.handle.*.charmap.*;
}
pub fn hasHorizontal(self: Face) bool {
@ -197,76 +411,3 @@ pub fn isCidKeyed(self: Face) bool {
pub fn isTricky(self: Face) bool {
return c.FT_IS_TRICKY(self.handle);
}
pub fn ascender(self: Face) i16 {
return self.handle.*.ascender;
}
pub fn descender(self: Face) i16 {
return self.handle.*.descender;
}
pub fn emSize(self: Face) u16 {
return self.handle.*.units_per_EM;
}
pub fn height(self: Face) i16 {
return self.handle.*.height;
}
pub fn maxAdvanceWidth(self: Face) i16 {
return self.handle.*.max_advance_width;
}
pub fn maxAdvanceHeight(self: Face) i16 {
return self.handle.*.max_advance_height;
}
pub fn underlinePosition(self: Face) i16 {
return self.handle.*.underline_position;
}
pub fn underlineThickness(self: Face) i16 {
return self.handle.*.underline_thickness;
}
pub fn numFaces(self: Face) i64 {
return self.handle.*.num_faces;
}
pub fn numGlyphs(self: Face) i64 {
return self.handle.*.num_glyphs;
}
pub fn familyName(self: Face) ?[:0]const u8 {
return if (self.handle.*.family_name) |family|
std.mem.span(family)
else
null;
}
pub fn styleName(self: Face) ?[:0]const u8 {
return if (self.handle.*.style_name) |style_name|
std.mem.span(style_name)
else
null;
}
pub fn styleFlags(self: Face) StyleFlags {
const flags = self.handle.*.style_flags;
return utils.bitFieldsToStruct(StyleFlags, StyleFlags.Flag, flags);
}
pub fn sizeMetrics(self: Face) ?SizeMetrics {
return if (self.handle.*.size) |size|
@ptrCast(*SizeMetrics, &size.*.metrics).*
else
null;
}
pub fn postscriptName(self: Face) ?[:0]const u8 {
return if (c.FT_Get_Postscript_Name(self.handle)) |face_name|
std.mem.span(face_name)
else
null;
}