From 456d67803407a1f8006b829a6299e06eb24254d5 Mon Sep 17 00:00:00 2001 From: g-rden Date: Wed, 10 Jul 2024 11:24:06 +0200 Subject: [PATCH] fix font height offsets Signed-off-by: g-rden --- src/font.c | 23 ++++++++--------------- src/text.c | 6 ++---- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/src/font.c b/src/font.c index c6b8d91..2a54951 100644 --- a/src/font.c +++ b/src/font.c @@ -13,6 +13,7 @@ #include FT_FREETYPE_H #include FT_GLYPH_H +#define POINT_FACTOR 64 // default points per pixel for 26.6 format #define SPACE_WH_REL 2 #define GLYPH_GW_REL 4 @@ -108,7 +109,7 @@ void font_create(void) void font_init(void) { char file[256]; - const FT_F26Dot6 size = ctx.size * 64; + const FT_F26Dot6 size = ctx.size * POINT_FACTOR; if (!search_font_file(ctx.name, file, sizeof(file)) || FT_Init_FreeType(&ctx.lib) != 0 || @@ -142,7 +143,7 @@ bool font_render(const char* text, struct text_surface* surface) return false; } - space_size = ctx.face->size->metrics.y_ppem / SPACE_WH_REL; + space_size = ctx.face->size->metrics.x_ppem / SPACE_WH_REL; wide = str_to_wide(text, NULL); if (!wide) { @@ -156,15 +157,14 @@ bool font_render(const char* text, struct text_surface* surface) if (*ptr == L' ') { x += space_size; } else if (FT_Load_Char(ctx.face, *ptr, FT_LOAD_RENDER) == 0) { - x += ctx.face->glyph->advance.x >> - 6; // why 6? from freetype tutorial! + x += ctx.face->glyph->advance.x / POINT_FACTOR; } ++ptr; } // allocate surface buffer surface->width = x; - surface->height = ctx.face->size->metrics.y_ppem; + surface->height = ctx.face->size->metrics.height / POINT_FACTOR; surface->data = calloc(1, surface->height * surface->width); if (!surface->data) { free(wide); @@ -180,15 +180,8 @@ bool font_render(const char* text, struct text_surface* surface) } else if (FT_Load_Char(ctx.face, *ptr, FT_LOAD_RENDER) == 0) { const FT_GlyphSlot glyph = ctx.face->glyph; const FT_Bitmap* bmp = &glyph->bitmap; - size_t y = ctx.face->size->metrics.y_ppem - glyph->bitmap_top; - - // it's a hack, but idk how to up the baseline - const size_t baseline_offset = ctx.size / 3; - if (y > baseline_offset) { - y -= baseline_offset; - } else { - y = 0; - } + const size_t y = ctx.face->size->metrics.y_ppem - + glyph->bitmap_top; for (size_t glyph_y = 0; glyph_y < bmp->rows; ++glyph_y) { uint8_t* dst; @@ -200,7 +193,7 @@ bool font_render(const char* text, struct text_surface* surface) memcpy(dst, &bmp->buffer[glyph_y * bmp->pitch], bmp->width); } - x += glyph->advance.x >> 6; + x += glyph->advance.x / POINT_FACTOR; } ++ptr; } diff --git a/src/text.c b/src/text.c index 2a0aa44..900a101 100644 --- a/src/text.c +++ b/src/text.c @@ -14,7 +14,6 @@ #define TEXT_SHADOW 0x00000000 #define TEXT_NO_SHADOW 0xff000000 #define TEXT_PADDING 10 // space between text layout and window edge -#define TEXT_LINESP 4 // line spacing factor /** Text context. */ struct text { @@ -84,8 +83,7 @@ void text_print(struct pixmap* wnd, enum info_position pos, const struct info_line* lines, size_t lines_num) { size_t max_key_width = 0; - const size_t height = - lines[0].value.height + lines[0].value.height / TEXT_LINESP; + const size_t height = lines[0].value.height; // calc max width of keys, used if block on the left side for (size_t i = 0; i < lines_num; ++i) { @@ -152,7 +150,7 @@ void text_print(struct pixmap* wnd, enum info_position pos, void text_print_centered(struct pixmap* wnd, const struct text_surface* lines, size_t lines_num) { - const size_t line_height = lines[0].height + lines[0].height / TEXT_LINESP; + const size_t line_height = lines[0].height; const size_t row_max = (wnd->height - TEXT_PADDING * 2) / line_height; const size_t columns = (lines_num / row_max) + (lines_num % row_max ? 1 : 0);