Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix font height offsets #151

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
6 changes: 2 additions & 4 deletions src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
Loading