From 06196c6b20f0b69949e2933556e377aefb945431 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Tue, 22 Oct 2024 20:24:34 -0600 Subject: [PATCH] Getting font rendering better --- client/include/ui.h | 2 +- client/shader_src/string.comp | 6 ++--- client/shader_src/ui_common.glsl | 2 +- client/src/main.c | 41 +++++++++++++++++++++++++------- client/src/ui.c | 16 ++++++++++++- 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/client/include/ui.h b/client/include/ui.h index 9aa5eaa..87b317c 100644 --- a/client/include/ui.h +++ b/client/include/ui.h @@ -50,7 +50,7 @@ typedef struct GPUSymbolStruct { float left; float width; float height; - float advance; + vec2 advance; } GPUSymbol; typedef struct FontStruct { diff --git a/client/shader_src/string.comp b/client/shader_src/string.comp index f4af704..fea255d 100644 --- a/client/shader_src/string.comp +++ b/client/shader_src/string.comp @@ -16,12 +16,12 @@ void main() { Font font = pc.context.fonts.f[string.font]; uint buffer_pos = atomicAdd(pc.layer.draw.instance_count, string.len); - float x = 0; + vec2 pen = vec2(0.0, 0.0); for(uint i = 0; i < string.len; i++) { uint code = pc.layer.codes.c[string.offset + i]; Symbol symbol = font.symbols.s[code]; - pc.layer.drawables.d[buffer_pos + i].pos = string.pos + vec2(x, 0); - x += string.size*symbol.advance; + pc.layer.drawables.d[buffer_pos + i].pos = string.pos + pen; + pen += string.size*symbol.advance; pc.layer.drawables.d[buffer_pos + i].size = vec2(string.size, string.size); pc.layer.drawables.d[buffer_pos + i].color = string.color; pc.layer.drawables.d[buffer_pos + i].code = pc.layer.codes.c[string.offset + i]; diff --git a/client/shader_src/ui_common.glsl b/client/shader_src/ui_common.glsl index a1cd7d6..0a19e5f 100644 --- a/client/shader_src/ui_common.glsl +++ b/client/shader_src/ui_common.glsl @@ -20,7 +20,7 @@ struct Symbol { float left; float width; float height; - float advance; + vec2 advance; }; layout(std430, buffer_reference) buffer SymbolList { diff --git a/client/src/main.c b/client/src/main.c index d4c9cc2..3b06960 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -10,27 +10,52 @@ VkResult test_ui(RenderContext* render, UIContext* ui) { VkResult result; - VK_RESULT(load_font(0, "test.ttf", 16, VK_TRUE, render, ui)); - GPUString context_strings[] = { { - .pos = {0, 16}, - .size = 16, + .pos = {0, 32}, + .size = 32, .color = {1.0, 1.0, 1.0, 1.0}, .offset = 0, - .length = strlen("Example"), + .length = strlen("Example Text"), .font = 0, }, + { + .pos = {0, 64}, + .size = 32, + .color = {1.0, 1.0, 1.0, 1.0}, + .offset = strlen("Example Text"), + .length = strlen("Example Text"), + .font = 1, + }, + { + .pos = {0, 96}, + .size = 32, + .color = {1.0, 1.0, 1.0, 1.0}, + .offset = 2*strlen("Example Text"), + .length = strlen("Example Text"), + .font = 2, + }, + { + .pos = {0, 128}, + .size = 32, + .color = {1.0, 1.0, 1.0, 1.0}, + .offset = 3*strlen("Example Text"), + .length = strlen("Example Text"), + .font = 3, + }, }; - uint32_t context_codes[100]; + uint32_t context_codes[256]; map_string("Example Text", context_codes, 0, 0, ui); + map_string("Example Text", context_codes, strlen("Example Text"), 1, ui); + map_string("Example Text", context_codes, 2*strlen("Example Text"), 2, ui); + map_string("Example Text", context_codes, 3*strlen("Example Text"), 3, ui); LayerInput context_layers[] = { { - .num_strings = 1, + .num_strings = sizeof(context_strings)/sizeof(GPUString), .strings = context_strings, - .num_codes = 100, + .num_codes = sizeof(context_codes)/sizeof(uint32_t), .codes = context_codes, }, }; diff --git a/client/src/ui.c b/client/src/ui.c index fbd6898..2e83b51 100644 --- a/client/src/ui.c +++ b/client/src/ui.c @@ -678,6 +678,9 @@ VkResult load_font( for(uint32_t i = 0; i < face->num_glyphs; i++) { if(glyph_index == 0) { break; + } else if(c > 255) { + c = FT_Get_Next_Char(face, c, &glyph_index); + continue; } FT_Load_Glyph(face, glyph_index, load_flags); uint32_t width = face->glyph->bitmap.width; @@ -705,7 +708,13 @@ VkResult load_font( FT_Load_Glyph(face, glyph_index, load_flags); symbols[i].width = (float)face->glyph->bitmap.width/(float)max_width; symbols[i].height = (float)face->glyph->bitmap.rows/(float)max_height; - symbols[i].advance = (float)face->glyph->advance.x*16.0/(float)face->units_per_EM/(float)max_width; + if(face->units_per_EM == 2048) { + symbols[i].advance[0] = (float)face->glyph->advance.x*30/(float)face->units_per_EM /(float)max_width; + symbols[i].advance[1] = (float)face->glyph->advance.y*30/(float)face->units_per_EM /(float)max_height; + } else { + symbols[i].advance[0] = (float)face->glyph->advance.x*16/(float)face->units_per_EM /(float)max_width; + symbols[i].advance[1] = (float)face->glyph->advance.y*16/(float)face->units_per_EM /(float)max_height; + } symbols[i].left = (float)face->glyph->bitmap_left/(float)max_width; symbols[i].top = (float)face->glyph->bitmap_top/(float)max_height; for(uint32_t y = 0; y < face->glyph->bitmap.rows; y++) { @@ -1099,6 +1108,11 @@ VkResult create_ui_context( context->containers = malloc(max_containers*sizeof(Container)); memset(context->containers, 0, max_containers*sizeof(Container)); + VK_RESULT(load_font(0, "fonts/eracake.ttf", 256, VK_TRUE, gpu, context)); + VK_RESULT(load_font(1, "fonts/yumoda.ttf", 256, VK_TRUE, gpu, context)); + VK_RESULT(load_font(2, "fonts/runescape.ttf", 16, VK_FALSE, gpu, context)); + VK_RESULT(load_font(3, "fonts/apple_2.ttf", 256, VK_FALSE, gpu, context)); + return VK_SUCCESS; }