freetype: implement some functions binding

This commit is contained in:
Ali Chraghi 2022-05-25 17:46:30 +04:30 committed by Stephen Gutekanst
parent 1a082ef5a7
commit 7a8aee8665
5 changed files with 71 additions and 24 deletions

View file

@ -8,15 +8,6 @@ const convertError = @import("error.zig").convertError;
const Glyph = @This();
pub const BBox = c.FT_BBox;
pub const RenderMode = enum(u3) {
normal = c.FT_RENDER_MODE_NORMAL,
light = c.FT_RENDER_MODE_LIGHT,
mono = c.FT_RENDER_MODE_MONO,
lcd = c.FT_RENDER_MODE_LCD,
lcd_v = c.FT_RENDER_MODE_LCD_V,
sdf = c.FT_RENDER_MODE_SDF,
};
pub const BBoxMode = enum(u2) {
// https://freetype.org/freetype2/docs/reference/ft2-glyph_management.html#ft_glyph_bbox_mode
// both `unscaled` and `subpixel` constants are set to 0
@ -25,6 +16,15 @@ pub const BBoxMode = enum(u2) {
truncate = c.FT_GLYPH_BBOX_TRUNCATE,
pixels = c.FT_GLYPH_BBOX_PIXELS,
};
pub const GlyphMetrics = c.FT_Glyph_Metrics;
pub const GlyphFormat = enum(u32) {
none = c.FT_GLYPH_FORMAT_NONE,
composite = c.FT_GLYPH_FORMAT_COMPOSITE,
bitmap = c.FT_GLYPH_FORMAT_BITMAP,
outline = c.FT_GLYPH_FORMAT_OUTLINE,
plotter = c.FT_GLYPH_FORMAT_PLOTTER,
svg = c.FT_GLYPH_FORMAT_SVG,
};
handle: c.FT_Glyph,
@ -48,13 +48,13 @@ pub fn transform(self: Glyph, matrix: ?types.Matrix, delta: ?types.Vector) Error
try convertError(c.FT_Glyph_Transform(self.handle, &m, &d));
}
pub fn getCBox(self: Glyph, bbox_mode: BBoxMode) BBox {
var res = std.mem.zeroes(BBox);
pub fn getCBox(self: Glyph, bbox_mode: BBoxMode) types.BBox {
var res = std.mem.zeroes(types.BBox);
c.FT_Glyph_Get_CBox(self.handle, @enumToInt(bbox_mode), &res);
return res;
}
pub fn toBitmap(self: Glyph, render_mode: RenderMode, origin: ?types.Vector) Error!BitmapGlyph {
pub fn toBitmap(self: Glyph, render_mode: types.RenderMode, origin: ?types.Vector) Error!BitmapGlyph {
var res = self.handle;
var o = origin orelse std.mem.zeroes(types.Vector);
try convertError(c.FT_Glyph_To_Bitmap(&res, @enumToInt(render_mode), &o, 0));
@ -73,6 +73,10 @@ pub fn strokeBorder(self: Glyph, stroker: Stroker, inside: bool) Error!Glyph {
return Glyph.init(res);
}
pub fn format(self: Glyph) GlyphFormat {
return @intToEnum(GlyphFormat, self.handle.*.format);
}
pub fn advanceX(self: Glyph) isize {
return self.handle.*.advance.x;
}

View file

@ -9,7 +9,6 @@ const convertError = @import("error.zig").convertError;
const GlyphSlot = @This();
pub const GlyphMetrics = c.FT_Glyph_Metrics;
pub const SubGlyphInfo = struct {
index: i32,
flags: u32,
@ -24,7 +23,7 @@ pub fn init(handle: c.FT_GlyphSlot) GlyphSlot {
return GlyphSlot{ .handle = handle };
}
pub fn render(self: GlyphSlot, render_mode: Glyph.RenderMode) Error!void {
pub fn render(self: GlyphSlot, render_mode: types.RenderMode) Error!void {
return convertError(c.FT_Render_Glyph(self.handle, @enumToInt(render_mode)));
}
@ -41,11 +40,8 @@ pub fn glyph(self: GlyphSlot) Error!Glyph {
}
pub fn outline(self: GlyphSlot) ?Outline {
const out = self.handle.*.outline;
const format = self.handle.*.format;
return if (format == c.FT_GLYPH_FORMAT_OUTLINE)
Outline.init(out)
return if (self.format() == .outline)
Outline.init(&self.handle.*.outline)
else
null;
}
@ -74,6 +70,10 @@ pub fn advance(self: GlyphSlot) types.Vector {
return self.handle.*.advance;
}
pub fn metrics(self: GlyphSlot) GlyphMetrics {
pub fn format(self: GlyphSlot) Glyph.GlyphFormat {
return @intToEnum(Glyph.GlyphFormat, self.handle.*.format);
}
pub fn metrics(self: GlyphSlot) Glyph.GlyphMetrics {
return self.handle.*.metrics;
}

View file

@ -1,16 +1,28 @@
const std = @import("std");
const c = @import("c.zig");
const types = @import("types.zig");
const Glyph = @import("Glyph.zig");
const Error = @import("error.zig").Error;
const convertError = @import("error.zig").convertError;
const Outline = @This();
handle: c.FT_Outline,
handle: *c.FT_Outline,
pub fn init(handle: c.FT_Outline) Outline {
pub fn init(handle: *c.FT_Outline) Outline {
return Outline{ .handle = handle };
}
pub fn numPoints(self: Outline) u15 {
return @intCast(u15, self.handle.*.n_points);
}
pub fn numContours(self: Outline) u15 {
return @intCast(u15, self.handle.*.n_contours);
}
pub fn points(self: Outline) []const types.Vector {
return self.handle.points[0..@intCast(u15, self.handle.n_points)];
return self.handle.*.points[0..self.numPoints()];
}
pub fn tags(self: Outline) []const u8 {
@ -18,5 +30,24 @@ pub fn tags(self: Outline) []const u8 {
}
pub fn contours(self: Outline) []const i16 {
return self.handle.contours[0..@intCast(u15, self.handle.n_contours)];
return self.handle.*.contours[0..self.numContours()];
}
pub fn check(self: Outline) Error!void {
try convertError(c.FT_Outline_Check(self.handle));
}
pub fn transform(self: Outline, matrix: ?types.Matrix) void {
var m = matrix orelse std.mem.zeroes(types.Matrix);
c.FT_Outline_Transform(self.handle, &m);
}
pub fn bbox(self: Outline) Error!types.BBox {
var res = std.mem.zeroes(types.BBox);
try convertError(c.FT_Outline_Get_BBox(self.handle, &res));
return res;
}
pub fn decompose(self: Outline, callbacks_ctx: anytype, callbacks: *c.FT_Outline_Funcs) Error!void {
try convertError(c.FT_Outline_Decompose(self.handle, callbacks, @ptrCast(?*anyopaque, callbacks_ctx)));
}

View file

@ -1,5 +1,6 @@
pub usingnamespace @cImport({
@cInclude("freetype/freetype.h");
@cInclude("freetype/ftbbox.h");
@cInclude("freetype/ftlcdfil.h");
@cInclude("freetype/ftmodapi.h");
@cInclude("freetype/ftstroke.h");

View file

@ -4,6 +4,17 @@ const utils = @import("utils.zig");
pub const Vector = c.FT_Vector;
pub const Matrix = c.FT_Matrix;
pub const Pos = c.FT_Pos;
pub const BBox = c.FT_BBox;
pub const RenderMode = enum(u3) {
normal = c.FT_RENDER_MODE_NORMAL,
light = c.FT_RENDER_MODE_LIGHT,
mono = c.FT_RENDER_MODE_MONO,
lcd = c.FT_RENDER_MODE_LCD,
lcd_v = c.FT_RENDER_MODE_LCD_V,
sdf = c.FT_RENDER_MODE_SDF,
};
pub const OpenFlags = packed struct {
memory: bool = false,