Getting font rendering better

main
noah metz 2024-10-22 20:24:34 -06:00
parent df441f6560
commit 06196c6b20
5 changed files with 53 additions and 14 deletions

@ -50,7 +50,7 @@ typedef struct GPUSymbolStruct {
float left;
float width;
float height;
float advance;
vec2 advance;
} GPUSymbol;
typedef struct FontStruct {

@ -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];

@ -20,7 +20,7 @@ struct Symbol {
float left;
float width;
float height;
float advance;
vec2 advance;
};
layout(std430, buffer_reference) buffer SymbolList {

@ -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,
},
};

@ -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;
}