aboutsummaryrefslogtreecommitdiff
path: root/src/graphics/text.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/graphics/text.c')
-rw-r--r--src/graphics/text.c34
1 files changed, 24 insertions, 10 deletions
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);