diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-11-08 02:07:23 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-11-08 02:07:23 +0100 |
commit | e3c90b41386986ef53e512994d6e2f7ceadfc177 (patch) | |
tree | 6fc1ad55e194a1c7663de3f2c9f178b1f67f2695 /src/graphics | |
parent | 16b0ce3748f1b3ea788bbaf4caaeb342a8f58d6f (diff) |
Add window function to get key state, allow invalid utf8
Diffstat (limited to 'src/graphics')
-rw-r--r-- | src/graphics/text.c | 26 |
1 files changed, 14 insertions, 12 deletions
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); |