From e3c90b41386986ef53e512994d6e2f7ceadfc177 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 8 Nov 2021 02:07:23 +0100 Subject: Add window function to get key state, allow invalid utf8 --- src/graphics/text.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'src/graphics/text.c') diff --git a/src/graphics/text.c b/src/graphics/text.c index 2f410d4..28840ce 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -20,8 +20,13 @@ static mgl_vec2f mgl_text_calculate_bounds(mgl_text *self) { float width = 0.0f; for(size_t i = 0; i < self->text_size;) { unsigned char *cp = (unsigned char*)&self->text[i]; - uint32_t codepoint = 0; - const size_t clen = mgl_utf8_decode(cp, &codepoint); + uint32_t codepoint; + size_t clen; + if(!mgl_utf8_decode(cp, self->text_size - i, &codepoint, &clen)) { + codepoint = *cp; + clen = 1; + } + if(codepoint == '\t') { if(mgl_font_get_glyph(self->font, ' ', &glyph) == 0) { width += (glyph.advance * TAB_WIDTH); @@ -60,20 +65,12 @@ void mgl_text_deinit(mgl_text *self) { } int mgl_text_set_string(mgl_text *self, const char *str, size_t str_size) { - if(str) { - if(!mgl_utf8_is_valid((const unsigned char*)str, str_size)) { - fprintf(stderr, "Error: mgl_text_set_string received an invalid utf8 string\n"); - return -1; - } - } - self->text = str; self->text_size = str_size; if(self->text && self->text_size > 0 && self->font) self->bounds = mgl_text_calculate_bounds(self); else self->bounds = (mgl_vec2f){ 0.0f, 0.0f }; - return 0; } @@ -128,8 +125,13 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { context->gl.glBegin(GL_QUADS); for(size_t i = 0; i < text->text_size;) { unsigned char *cp = (unsigned char*)&text->text[i]; - uint32_t codepoint = 0; - const size_t clen = mgl_utf8_decode(cp, &codepoint); + uint32_t codepoint; + size_t clen; + if(!mgl_utf8_decode(cp, text->text_size - i, &codepoint, &clen)) { + codepoint = *cp; + clen = 1; + } + if(codepoint == '\t') { if(mgl_font_get_glyph(text->font, ' ', &glyph) == 0) { position.x += (glyph.advance * TAB_WIDTH); -- cgit v1.2.3