aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-17 11:39:18 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-17 11:39:18 +0100
commit909ee26e03d6a7f93b83fad9709139565a62629a (patch)
tree0e7a72d66fe71f3c36ccce68342e983ce82c3ecd
parenta14eb65e82d226fe8b33ca23d29eeba02066357d (diff)
Readd kerning
-rw-r--r--include/mgl/graphics/font.h2
-rw-r--r--include/mgl/graphics/font_glyph.h2
-rw-r--r--src/graphics/font.c6
-rw-r--r--src/graphics/text.c34
4 files changed, 28 insertions, 16 deletions
diff --git a/include/mgl/graphics/font.h b/include/mgl/graphics/font.h
index 6b7eb34..4bd0428 100644
--- a/include/mgl/graphics/font.h
+++ b/include/mgl/graphics/font.h
@@ -40,6 +40,6 @@ void mgl_font_unload(mgl_font *self);
int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph);
/* Returns the kerning */
-int mgl_font_get_kerning(const mgl_font *self, uint32_t prev_codepoint, uint32_t codepoint);
+float mgl_font_get_kerning(const mgl_font *self, uint32_t prev_codepoint, uint32_t codepoint);
#endif /* MGL_FONT_H */
diff --git a/include/mgl/graphics/font_glyph.h b/include/mgl/graphics/font_glyph.h
index 0d9cb2d..11c54cf 100644
--- a/include/mgl/graphics/font_glyph.h
+++ b/include/mgl/graphics/font_glyph.h
@@ -8,7 +8,7 @@ typedef struct {
mgl_vec2i size;
mgl_vec2i texture_position; /* In pixel space */
mgl_vec2i texture_size; /* In pixel space */
- int advance;
+ float advance;
} mgl_font_glyph;
#endif /* MGL_FONT_GLYPH_H */
diff --git a/src/graphics/font.c b/src/graphics/font.c
index 1c3631d..17362f6 100644
--- a/src/graphics/font.c
+++ b/src/graphics/font.c
@@ -228,8 +228,6 @@ int mgl_font_get_glyph(mgl_font *self, uint32_t codepoint, mgl_font_glyph *glyph
return res;
}
-int mgl_font_get_kerning(const mgl_font *self, uint32_t prev_codepoint, uint32_t codepoint) {
- return 0;
- /* TODO: */
- /*return stbtt_GetCodepointKernAdvance(self->font_info, prev_codepoint, codepoint);*/
+float mgl_font_get_kerning(const mgl_font *self, uint32_t prev_codepoint, uint32_t codepoint) {
+ return stbtt_GetCodepointKernAdvance(self->font_info, prev_codepoint, codepoint) * stbtt_ScaleForPixelHeight(self->font_info, self->character_size);
}
diff --git a/src/graphics/text.c b/src/graphics/text.c
index cd9d0d9..3620659 100644
--- a/src/graphics/text.c
+++ b/src/graphics/text.c
@@ -36,6 +36,7 @@ static void mgl_text_for_each_codepoint(const mgl_text *self, codepoint_loop_cal
return;
}
}
+
i += clen;
++codepoint_index;
}
@@ -49,21 +50,26 @@ typedef struct {
mgl_vec2f bounds;
float line_width;
const mgl_font *font;
+ uint32_t prev_codepoint;
} CalculateBoundsUserdata;
static bool calculate_bounds_callback(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata) {
(void)codepoint_index;
+
CalculateBoundsUserdata *calculate_bounds_userdata = userdata;
if(codepoint == '\t') {
- calculate_bounds_userdata->line_width += (glyph->advance * TAB_WIDTH);
+ calculate_bounds_userdata->line_width += (glyph->advance * TAB_WIDTH) + mgl_font_get_kerning(calculate_bounds_userdata->font, calculate_bounds_userdata->prev_codepoint, codepoint);
calculate_bounds_userdata->bounds.x = max_float(calculate_bounds_userdata->bounds.x, calculate_bounds_userdata->line_width);
} else if(codepoint == '\n') {
calculate_bounds_userdata->line_width = 0.0f;
calculate_bounds_userdata->bounds.y += calculate_bounds_userdata->font->character_size;
+ calculate_bounds_userdata->prev_codepoint = 0;
} else {
- calculate_bounds_userdata->line_width += glyph->advance;
+ calculate_bounds_userdata->line_width += glyph->advance + mgl_font_get_kerning(calculate_bounds_userdata->font, calculate_bounds_userdata->prev_codepoint, codepoint);
calculate_bounds_userdata->bounds.x = max_float(calculate_bounds_userdata->bounds.x, calculate_bounds_userdata->line_width);
}
+
+ calculate_bounds_userdata->prev_codepoint = codepoint;
return true;
}
@@ -73,6 +79,7 @@ static mgl_vec2f mgl_text_calculate_bounds(mgl_text *self) {
calculate_bounds_userdata.bounds.y = self->font->character_size;
calculate_bounds_userdata.line_width = 0.0f;
calculate_bounds_userdata.font = self->font;
+ calculate_bounds_userdata.prev_codepoint = 0;
mgl_text_for_each_codepoint(self, calculate_bounds_callback, &calculate_bounds_userdata);
return calculate_bounds_userdata.bounds;
}
@@ -126,9 +133,10 @@ mgl_vec2f mgl_text_get_bounds(const mgl_text *self) {
}
typedef struct {
- mgl_vec2i position;
+ mgl_vec2f position;
const mgl_text *text;
size_t index;
+ uint32_t prev_codepoint;
} FindCharacterPosUserdata;
static bool find_character_pos_callback(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata) {
@@ -137,21 +145,23 @@ static bool find_character_pos_callback(size_t codepoint_index, uint32_t codepoi
return false;
if(codepoint == '\t') {
- find_character_pos_userdata->position.x += (glyph->advance * TAB_WIDTH);
+ find_character_pos_userdata->position.x += (glyph->advance * TAB_WIDTH) + mgl_font_get_kerning(find_character_pos_userdata->text->font, find_character_pos_userdata->prev_codepoint, codepoint);
} else if(codepoint == '\n') {
find_character_pos_userdata->position.x = find_character_pos_userdata->text->position.x;
find_character_pos_userdata->position.y += find_character_pos_userdata->text->font->character_size;
+ find_character_pos_userdata->prev_codepoint = 0;
} else {
- find_character_pos_userdata->position.x += glyph->advance;
+ find_character_pos_userdata->position.x += glyph->advance + mgl_font_get_kerning(find_character_pos_userdata->text->font, find_character_pos_userdata->prev_codepoint, codepoint);
}
return true;
}
mgl_vec2f mgl_text_find_character_pos(mgl_text *self, size_t index) {
FindCharacterPosUserdata find_character_pos_userdata;
- find_character_pos_userdata.position = (mgl_vec2i){ self->position.x, self->position.y };
+ find_character_pos_userdata.position = self->position;
find_character_pos_userdata.text = self;
find_character_pos_userdata.index = index;
+ find_character_pos_userdata.prev_codepoint = 0;
mgl_text_for_each_codepoint(self, find_character_pos_callback, &find_character_pos_userdata);
return (mgl_vec2f){ find_character_pos_userdata.position.x, find_character_pos_userdata.position.y };
}
@@ -171,21 +181,24 @@ static void mgl_text_draw_glyph(mgl_context *context, const mgl_font_glyph *glyp
}
typedef struct {
- mgl_vec2i position;
+ mgl_vec2f position;
const mgl_text *text;
mgl_context *context;
+ uint32_t prev_codepoint;
} TextDrawUserdata;
static bool text_draw_callback(size_t codepoint_index, uint32_t codepoint, const mgl_font_glyph *glyph, void *userdata) {
(void)codepoint_index;
TextDrawUserdata *text_draw_userdata = userdata;
if(codepoint == '\t') {
- text_draw_userdata->position.x += (glyph->advance * TAB_WIDTH);
+ text_draw_userdata->position.x += (glyph->advance * TAB_WIDTH) + mgl_font_get_kerning(text_draw_userdata->text->font, text_draw_userdata->prev_codepoint, codepoint);
} else if(codepoint == '\n') {
text_draw_userdata->position.x = text_draw_userdata->text->position.x;
text_draw_userdata->position.y += text_draw_userdata->text->font->character_size;
+ text_draw_userdata->prev_codepoint = 0;
} else {
- mgl_text_draw_glyph(text_draw_userdata->context, glyph, text_draw_userdata->position);
+ text_draw_userdata->position.x += mgl_font_get_kerning(text_draw_userdata->text->font, text_draw_userdata->prev_codepoint, codepoint);
+ mgl_text_draw_glyph(text_draw_userdata->context, glyph, (mgl_vec2i){ text_draw_userdata->position.x, text_draw_userdata->position.y });
text_draw_userdata->position.x += glyph->advance;
}
return true;
@@ -198,9 +211,10 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) {
return;
TextDrawUserdata text_draw_userdata;
- text_draw_userdata.position = (mgl_vec2i){ text->position.x, text->position.y + text->font->character_size };
+ text_draw_userdata.position = (mgl_vec2f){ text->position.x, text->position.y + text->font->character_size };
text_draw_userdata.text = text;
text_draw_userdata.context = context;
+ text_draw_userdata.prev_codepoint = 0;
context->gl.glColor4ub(text->color.r, text->color.g, text->color.b, text->color.a);
mgl_texture_use(&text->font->texture);