aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-09-11 01:46:26 +0200
committerdec05eba <dec05eba@protonmail.com>2024-09-11 01:47:06 +0200
commit6578337d2d7e407cf8cc5af20849401d14e93e4e (patch)
tree30f992b84298d3732ac0d5c08312ce1ac8bedf57 /src
parentd20a42bc640e4cd4c3bb2a15c08660319a21dcc5 (diff)
Fix incorrect position for mgl_text_find_character_pos
Diffstat (limited to 'src')
-rw-r--r--src/graphics/text.c17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/graphics/text.c b/src/graphics/text.c
index e17dd5a..e84e4b1 100644
--- a/src/graphics/text.c
+++ b/src/graphics/text.c
@@ -178,25 +178,30 @@ mgl_vec2f mgl_text_get_bounds(mgl_text *self) {
}
typedef struct {
- mgl_vec2f position;
+ mgl_vec2f glyph_offset;
const mgl_text *text;
size_t index;
} FindCharacterPosUserdata;
static bool find_character_pos_callback(codepoint_loop_callback_data callback_data, void *userdata) {
FindCharacterPosUserdata *find_character_pos_userdata = userdata;
- find_character_pos_userdata->position.x = find_character_pos_userdata->position.x + callback_data.glyph_offset.x;
- find_character_pos_userdata->position.y = find_character_pos_userdata->position.y + callback_data.glyph_offset.y;
- return callback_data.codepoint_index < find_character_pos_userdata->index;
+ find_character_pos_userdata->glyph_offset = callback_data.glyph_offset;
+ if(callback_data.codepoint_index >= find_character_pos_userdata->index)
+ return false;
+
+ find_character_pos_userdata->glyph_offset.x += callback_data.glyph_width;
+ if(callback_data.codepoint == '\n')
+ find_character_pos_userdata->glyph_offset.y += find_character_pos_userdata->text->font->character_size;
+ 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 = self->position;
+ find_character_pos_userdata.glyph_offset = (mgl_vec2f){0.0f, 0.0f};
find_character_pos_userdata.text = self;
find_character_pos_userdata.index = index;
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 };
+ return (mgl_vec2f){ self->position.x + find_character_pos_userdata.glyph_offset.x, self->position.y + find_character_pos_userdata.glyph_offset.y };
}
static void mgl_text_draw_glyph(mgl_context *context, const mgl_font_glyph *glyph, mgl_vec2i position) {