addGlyph returns glyph

This commit is contained in:
Mitchell Hashimoto
2022-04-06 09:47:57 -07:00
parent 263c9e4aac
commit 811b5f37f2
2 changed files with 8 additions and 5 deletions

View File

@ -112,7 +112,7 @@ pub fn getGlyph(self: FontAtlas, v: anytype) ?*Glyph {
/// Add a glyph to the font atlas. The codepoint can be either a u8 or
/// []const u8 depending on if you know it is ASCII or must be UTF-8 decoded.
pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !void {
pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !*Glyph {
assert(self.ft_face != null);
// We need a UTF32 codepoint for freetype
@ -120,7 +120,7 @@ pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !void {
// If we have this glyph loaded already then we're done.
const gop = try self.glyphs.getOrPut(alloc, utf32);
if (gop.found_existing) return;
if (gop.found_existing) return gop.value_ptr;
errdefer _ = self.glyphs.remove(utf32);
const glyph_index = ftc.FT_Get_Char_Index(self.ft_face, utf32);
@ -164,6 +164,7 @@ pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !void {
assert(region.height == tgt_h);
self.atlas.set(region, buffer);
// Store glyph metadata
gop.value_ptr.* = .{
.width = tgt_w,
.height = tgt_h,
@ -177,6 +178,8 @@ pub fn addGlyph(self: *FontAtlas, alloc: Allocator, v: anytype) !void {
};
log.debug("loaded glyph codepoint={} glyph={}", .{ utf32, gop.value_ptr.* });
return gop.value_ptr;
}
/// Convert 26.6 pixel format to f32

View File

@ -33,8 +33,8 @@ pub fn init(alloc: std.mem.Allocator) !TextRenderer {
// Load all visible ASCII characters.
var i: u8 = 32;
while (i < 127) : (i += 1) {
try font.addGlyph(alloc, i);
while (i <= 126) : (i += 1) {
_ = try font.addGlyph(alloc, i);
}
// Build our texture
@ -133,7 +133,7 @@ pub fn render(
const s1 = glyph.s1;
const t1 = glyph.t1;
std.log.info("CHAR ch={} x0={} y0={} x1={} y1={}", .{ c, x0, y0, x1, y1 });
//std.log.info("CHAR ch={} x0={} y0={} x1={} y1={}", .{ c, x0, y0, x1, y1 });
const vert = [4][9]f32{
.{ x0, y0, 0, s0, t0, r, g, b, a },