diff --git a/packages/media/src/stbtt.zig b/packages/media/src/stbtt.zig index e3e5f37..5dd0df3 100644 --- a/packages/media/src/stbtt.zig +++ b/packages/media/src/stbtt.zig @@ -137,19 +137,30 @@ pub const Font = opaque { stbtt.allocator.destroy(info); } - pub fn findGlyphIndex(self: *const Font, codepoint: u21) ?u32 { + pub fn findGlyphIndex(self: *const Font, codepoint: u21) ?u16 { const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); const res = import.stbtt_FindGlyphIndex(info, codepoint); return if (res != 0) @intCast(res) else null; } - pub fn getGlyphBox(self: *const Font, glyph: u32) ?GlyphBox { + pub fn getGlyphMetrics(self: *const Font, glyph: u16) GlyphMetrics { + const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); + var advance_width: c_int = undefined; + var left_side_bearing: c_int = undefined; + import.stbtt_GetGlyphHMetrics(info, glyph, &advance_width, &left_side_bearing); + return .{ + .advance_width = @intCast(advance_width), + .left_side_bearing = @intCast(left_side_bearing), + }; + } + + pub fn getGlyphBox(self: *const Font, glyph: u16) ?GlyphBox { const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); var x0: c_int = undefined; var y0: c_int = undefined; var x1: c_int = undefined; var y1: c_int = undefined; - const res = import.stbtt_GetGlyphBox(info, @intCast(glyph), &x0, &y0, &x1, &y1); + const res = import.stbtt_GetGlyphBox(info, glyph, &x0, &y0, &x1, &y1); return if (res != 0) .{ .x_min = @intCast(x0), .y_min = @intCast(y0), @@ -158,6 +169,26 @@ pub const Font = opaque { } else null; } + pub fn isGlyphEmpty(self: *const Font, glyph: u16) bool { + const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); + const res = import.stbtt_IsGlyphEmpty(info, glyph); + return res != 0; + } + + pub fn getGlyphShape(self: *const Font, glyph: u16) []import.stbtt_vertex { + const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); + var vertices_ptr: [*]import.stbtt_vertex = undefined; + const vertices_len = @as(usize, @intCast(import.stbtt_GetGlyphShape(info, glyph, &vertices_ptr))); + return if (vertices_len == 0) &.{} else vertices_ptr[0..vertices_len]; + } + + pub fn freeShape(self: *const Font, vertices: []import.stbtt_vertex) void { + const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); + if (vertices.len > 0) { + import.stbtt_FreeShape(info, vertices.ptr); + } + } + pub fn unitsPerEm(self: *const Font) u16 { const info = @as(*const import.stbtt_fontinfo, @ptrCast(@alignCast(self))); const res = import.stbtt_UnitsPerEm(info); @@ -193,6 +224,11 @@ pub const GlyphBox = struct { y_max: i16, }; +pub const GlyphMetrics = struct { + advance_width: i16, + left_side_bearing: i16, +}; + // --- MALLOC INTERFACE -------------------------------------------------------- export fn castle_media_stbtt_malloc(self: *Self, size: usize) callconv(.c) VoidPtr {