aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-19 03:43:27 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-19 03:43:27 +0100
commit6da1df6204ae118ceada0eff08a0ac7d42b3548a (patch)
tree527886112ec361058c63a43ee8f2286c9a60edb7
parentf4833afa1afa56def5a6d54fa8ce22d3a9b4eb55 (diff)
Slightly improve font rendering quality
-rw-r--r--src/graphics/font.c5
-rw-r--r--tests/main.c6
2 files changed, 6 insertions, 5 deletions
diff --git a/src/graphics/font.c b/src/graphics/font.c
index aa349a6..b6ceaf5 100644
--- a/src/graphics/font.c
+++ b/src/graphics/font.c
@@ -7,6 +7,7 @@
/* Need padding so filtering doesn't touch pixels in another glyphs area */
#define GLYPH_PADDING 2
#define GLYPH_UPSAMPLE 1
+#define GLYPH_SHIFT 0.5f
int mgl_font_load_from_file(mgl_font *self, const mgl_memory_mapped_file *mapped_file, unsigned int character_size) {
self->texture.id = 0;
@@ -167,7 +168,7 @@ int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph
const float glyph_advance = font_scale * advance;
int x0, y0, x1, y1;
- stbtt_GetGlyphBitmapBox(self->font_info, glyph_index, font_scale, font_scale, &x0, &y0, &x1, &y1);
+ stbtt_GetGlyphBitmapBoxSubpixel(self->font_info, glyph_index, font_scale, font_scale, GLYPH_SHIFT, GLYPH_SHIFT, &x0, &y0, &x1, &y1);
int width = x1 - x0;
int height = y1 - y0;
@@ -181,7 +182,7 @@ int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph
unsigned char *pixels = calloc(pixels_size, 1);
if(pixels) {
const int top_padding = GLYPH_PADDING;
- stbtt_MakeGlyphBitmap(self->font_info, pixels + pixels_width * top_padding + GLYPH_PADDING, width, height, pixels_width, font_scale, font_scale, glyph_index);
+ stbtt_MakeGlyphBitmapSubpixel(self->font_info, pixels + pixels_width * top_padding + GLYPH_PADDING, width, height, pixels_width, font_scale, font_scale, GLYPH_SHIFT, GLYPH_SHIFT, glyph_index);
}
mgl_vec2i render_offset;
diff --git a/tests/main.c b/tests/main.c
index b341bb3..a544761 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[255];
- snprintf(str, sizeof(str), "hello|fps\nåäö: %d", u->fps);
+ snprintf(str, sizeof(str), "I finally did it!! Me and my sock puppets riled Connor and Punk");
mgl_text text;
mgl_text_init(&text, u->font, str, strlen(str));
@@ -256,10 +256,10 @@ int main(int argc, char **argv) {
if(mgl_mapped_file_load("/usr/share/fonts/noto-cjk/NotoSansCJK-Regular.ttc", &cjk_font_file, &(mgl_memory_mapped_file_load_options){ .readable = true, .writable = false }) != 0)
return 1;
- if(mgl_font_load_from_file(&font, &font_file, 26) != 0)
+ if(mgl_font_load_from_file(&font, &font_file, 22) != 0)
return 1;
- if(mgl_font_load_from_file(&cjk_font, &cjk_font_file, 26) != 0)
+ if(mgl_font_load_from_file(&cjk_font, &cjk_font_file, 22) != 0)
return 1;
if(mgl_shader_program_init(&shader_program) != 0)