font: move codepoint into a public API

This commit is contained in:
Mitchell Hashimoto
2022-08-20 10:50:37 -07:00
parent 32d96dd49b
commit f93e9eedb3
3 changed files with 16 additions and 12 deletions

View File

@ -12,6 +12,7 @@ const Atlas = @import("../Atlas.zig");
const Family = @import("main.zig").Family;
const Glyph = @import("main.zig").Glyph;
const Style = @import("main.zig").Style;
const codepoint = @import("main.zig").codepoint;
const ftok = ftc.FT_Err_Ok;
const log = std.log.scoped(.font_fallback);
@ -54,7 +55,7 @@ pub fn getOrAddGlyph(
assert(self.families.items.len > 0);
// We need a UTF32 codepoint
const utf32 = Family.codepoint(v);
const utf32 = codepoint(v);
// If we have this already, load it directly
const glyphKey: GlyphKey = .{ .style = style, .codepoint = utf32 };

View File

@ -11,6 +11,7 @@ const Face = @import("main.zig").Face;
const Glyph = @import("main.zig").Glyph;
const Style = @import("main.zig").Style;
const testFont = @import("test.zig").fontRegular;
const codepoint = @import("main.zig").codepoint;
const log = std.log.scoped(.font_family);
@ -141,17 +142,6 @@ pub fn addGlyph(self: *Family, alloc: Allocator, v: anytype, style: Style) !*Gly
return gop.value_ptr;
}
/// Returns the UTF-32 codepoint for the given value.
pub fn codepoint(v: anytype) u32 {
// We need a UTF32 codepoint for freetype
return switch (@TypeOf(v)) {
u32 => v,
comptime_int, u8 => @intCast(u32, v),
[]const u8 => @intCast(u32, try std.unicode.utfDecode(v)),
else => @compileError("invalid codepoint type"),
};
}
test {
const testing = std.testing;
const alloc = testing.allocator;

View File

@ -1,3 +1,5 @@
const std = @import("std");
pub const Face = @import("Face.zig");
pub const Family = @import("Family.zig");
pub const Glyph = @import("Glyph.zig");
@ -15,6 +17,17 @@ pub const Style = enum {
bold_italic,
};
/// Returns the UTF-32 codepoint for the given value.
pub fn codepoint(v: anytype) u32 {
// We need a UTF32 codepoint for freetype
return switch (@TypeOf(v)) {
u32 => v,
comptime_int, u8 => @intCast(u32, v),
[]const u8 => @intCast(u32, try std.unicode.utfDecode(v)),
else => @compileError("invalid codepoint type"),
};
}
test {
_ = Face;
_ = Family;