From 08fe64b91e7e43db54628f922f07864a8d8e5402 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 27 Nov 2021 19:42:03 +0100 Subject: Use correct offset y for font position (ascent) --- include/mgl/graphics/font.h | 3 +++ src/graphics/font.c | 15 ++++++++++++++- src/graphics/text.c | 3 ++- tests/main.c | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/mgl/graphics/font.h b/include/mgl/graphics/font.h index 55d364f..69ac462 100644 --- a/include/mgl/graphics/font.h +++ b/include/mgl/graphics/font.h @@ -31,6 +31,9 @@ struct mgl_font { mgl_texture texture; mgl_font_atlas font_atlas; unsigned int character_size; + int ascent; + int descent; + int linegap; mgl_font_char_map char_map; int current_line_max_height; void *font_info; diff --git a/src/graphics/font.c b/src/graphics/font.c index 82a8029..cad5790 100644 --- a/src/graphics/font.c +++ b/src/graphics/font.c @@ -50,6 +50,16 @@ int mgl_font_load_from_file(mgl_font *self, const mgl_memory_mapped_file *mapped return -1; } + int ascent = 0; + int descent = 0; + int linegap = 0; + stbtt_GetFontVMetrics(self->font_info, &ascent, &descent, &linegap); + + const float font_scale = stbtt_ScaleForPixelHeight(self->font_info, self->character_size*GLYPH_UPSAMPLE); + self->ascent = round_float(font_scale * ascent); + self->descent = round_float(font_scale * descent); + self->linegap = round_float(font_scale * linegap); + /* TODO: Use stbtt_GetCodepointSDF */ return 0; } @@ -59,6 +69,9 @@ void mgl_font_unload(mgl_font *self) { mgl_font_char_map_deinit(&self->char_map); self->current_line_max_height = 0; + self->ascent = 0; + self->descent = 0; + self->linegap = 0; free(self->font_info); self->font_info = NULL; @@ -216,7 +229,7 @@ int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph render_offset.y = self->font_atlas.pointer_position.y; mgl_font_glyph new_glyph; - new_glyph.position = (mgl_vec2i){ xoff/GLYPH_UPSAMPLE, yoff/GLYPH_UPSAMPLE }; + new_glyph.position = (mgl_vec2i){ xoff/GLYPH_UPSAMPLE, self->ascent + yoff/GLYPH_UPSAMPLE }; new_glyph.size = (mgl_vec2i){ width/GLYPH_UPSAMPLE, height/GLYPH_UPSAMPLE }; new_glyph.texture_position = (mgl_vec2i){ render_offset.x, render_offset.y }; new_glyph.texture_size = (mgl_vec2i){ width, height }; diff --git a/src/graphics/text.c b/src/graphics/text.c index 66ef7e3..92c578c 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -213,10 +213,11 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { return; /* Calculate bounds beforehand if needed, which also generates glyphs because opengl outputs an error if that is done inside glBegin/glEnd */ + /* TODO: Only run this on the new glyphs */ mgl_text_get_bounds(text); TextDrawUserdata text_draw_userdata; - text_draw_userdata.position = (mgl_vec2f){ text->position.x, text->position.y + text->font->character_size }; + text_draw_userdata.position = (mgl_vec2f){ text->position.x, text->position.y }; text_draw_userdata.text = text; text_draw_userdata.context = context; text_draw_userdata.prev_codepoint = 0; diff --git a/tests/main.c b/tests/main.c index 0d4f096..359d92b 100644 --- a/tests/main.c +++ b/tests/main.c @@ -57,7 +57,7 @@ static void draw(mgl_window *window, void *userdata) { fprintf(stderr, "fps: %d\n", u->fps); } char str[512]; - snprintf(str, sizeof(str), ",.-_/1234567890½!\"#¤%%&/()=?¡@£$€¥{[]}\\'abcdefghijklmnopqrstuvwxyzáàäåãâíìîïúùũüûéèẽëêóòõôöABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÄÅÃÂÍÌÎÏÚÙŨÜÛÉÈẼËÊÓÒÕÔÖ └> pacman -Q quickmedia-git"); + snprintf(str, sizeof(str), "|,.-_/1234567890½!\"#¤%%&/()=?¡@£$€¥{[]}\\'abcdefghijklmnopqrstuvwxyzáàäåãâíìîïúùũüûéèẽëêóòõôöABCDEFGHIJKLMNOPQRSTUVWXYZÁÀÄÅÃÂÍÌÎÏÚÙŨÜÛÉÈẼËÊÓÒÕÔÖ └> pacman -Q quickmedia-git"); mgl_text text; mgl_text_init(&text, u->font, str, strlen(str)); -- cgit v1.2.3