From 52c4b40cffe2ad7d423fdb8de70f102f5fea0f68 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Fri, 15 Nov 2024 09:21:19 -0600 Subject: [PATCH] Fix more font issues --- bootstrap/bloat-buster/font.c | 21 +++++++++++---------- bootstrap/bloat-buster/gui.c | 13 +++++++------ bootstrap/include/bloat-buster/font.h | 4 +++- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/bootstrap/bloat-buster/font.c b/bootstrap/bloat-buster/font.c index 1923be4..e90ca1d 100644 --- a/bootstrap/bloat-buster/font.c +++ b/bootstrap/bloat-buster/font.c @@ -5,7 +5,6 @@ TextureAtlas font_create_texture_atlas(Arena* arena, String font_path) { - auto font_file = file_read(arena, font_path); stbtt_fontinfo font_info; if (!stbtt_InitFont(&font_info, font_file.pointer, stbtt_GetFontOffsetForIndex(font_file.pointer, 0))) @@ -14,37 +13,39 @@ TextureAtlas font_create_texture_atlas(Arena* arena, String font_path) } TextureAtlas result = {}; + u32 char_count = 256; + result.characters = arena_allocate(arena, FontCharacter, char_count); result.character_height = 64; result.character_width = result.character_height; result.height = 1024; result.width = result.height; auto atlas_size = result.width * result.height; result.pointer = arena_allocate(arena, u8, atlas_size); - - float scale_x = 0.0f; - float scale_y = stbtt_ScaleForPixelHeight(&font_info, result.character_height); + result.scale = stbtt_ScaleForPixelHeight(&font_info, result.character_height); // Starting position in the atlas u32 x = 0; u32 y = 0; - u32 char_count = 256; for (u32 i = 0; i < char_count; ++i) { u32 width; u32 height; - u32 x_offset; - u32 y_offset; + int x_offset; + int y_offset; auto ch = (u8)i; - u8* bitmap = stbtt_GetCodepointBitmap(&font_info, scale_x, scale_y, ch, (int*)&width, (int*)&height, (int*)&x_offset, (int*)&y_offset); - if (bitmap) + auto* character = &result.characters[i]; + stbtt_GetCodepointHMetrics(&font_info, ch, (int*)&character->advance, (int*)&character->left_bearing); + + u8* bitmap = stbtt_GetCodepointBitmap(&font_info, 0.0f, result.scale, ch, (int*)&width, (int*)&height, &x_offset, &y_offset); + if (width > 0 && height > 0) { for (u32 j = 0; j < height; ++j) { for (u32 i = 0; i < width; ++i) { - auto atlas_index = (y + j) * result.width + (x + i); + auto atlas_index = (y + j) * result.width + (x + i ) + 1; auto bitmap_index = (height - j - 1) * width + i; assert(atlas_index < atlas_size); result.pointer[atlas_index] = bitmap[bitmap_index]; diff --git a/bootstrap/bloat-buster/gui.c b/bootstrap/bloat-buster/gui.c index 61d90eb..f628033 100644 --- a/bootstrap/bloat-buster/gui.c +++ b/bootstrap/bloat-buster/gui.c @@ -1241,17 +1241,16 @@ strlit("/usr/share/fonts/TTF/FiraSans-Regular.ttf") Vec4 color = {1, 1, 1, 1}; static_assert(sizeof(color) == 4 * sizeof(float)); - auto width_float = (f32)initial_width; - auto height_float = (f32)initial_height; - auto string = strlit("abc"); + auto string = strlit("abcdefghi"); VirtualBuffer(Vertex) vertices = {}; VirtualBuffer(u32) indices = {}; u32 index_offset = 0; - u32 x_offset = width_float / 2; - u32 y_offset = height_float / 2; + f32 x_offset = initial_width / 2.0f; + f32 y_offset = initial_height / 2.0f; + f32 font_scale = texture_atlas.scale; - for (u64 i = 0; i < string.length; i += 1, index_offset += 4, x_offset += texture_atlas.character_width) + for (u64 i = 0; i < string.length; i += 1, index_offset += 4) { auto ch = string.pointer[i]; auto character_count_per_row = texture_atlas.width / texture_atlas.character_width; @@ -1297,6 +1296,8 @@ strlit("/usr/share/fonts/TTF/FiraSans-Regular.ttf") *vb_add(&indices, 1) = index_offset + 1; *vb_add(&indices, 1) = index_offset + 3; *vb_add(&indices, 1) = index_offset + 2; + + x_offset += texture_atlas.characters[i].advance * font_scale; } auto vertex_buffer_size = sizeof(*vertices.pointer) * vertices.length; diff --git a/bootstrap/include/bloat-buster/font.h b/bootstrap/include/bloat-buster/font.h index 2e7c313..aae9805 100644 --- a/bootstrap/include/bloat-buster/font.h +++ b/bootstrap/include/bloat-buster/font.h @@ -3,7 +3,8 @@ STRUCT(FontCharacter) { - int advance; + u32 advance; + u32 left_bearing; }; STRUCT(TextureAtlas) @@ -14,6 +15,7 @@ STRUCT(TextureAtlas) u32 height; u32 character_width; u32 character_height; + f32 scale; }; EXPORT TextureAtlas font_create_texture_atlas(Arena* arena, String font_path);