summaryrefslogtreecommitdiff
path: root/text.c
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-01-25 18:00:06 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-01-25 18:00:06 -0500
commit795262f69900af674156bed2bcd0fdb57dbbb55e (patch)
tree03723b919ff498722d7985a93f9ce7f470596abb /text.c
parenta56f549a266e14cdc00a98e8dc3e154f5ac6c23e (diff)
replaced c32rtomb, mbrtoc32 with own versions
these are nicer to use since they don't involve mbstate_t and should be faster since they don't involve locales
Diffstat (limited to 'text.c')
-rw-r--r--text.c23
1 files changed, 5 insertions, 18 deletions
diff --git a/text.c b/text.c
index fbce4eb..094db15 100644
--- a/text.c
+++ b/text.c
@@ -1,5 +1,6 @@
#include "base.h"
#include "text.h"
+#include "unicode.h"
#define STB_TRUETYPE_IMPLEMENTATION
#define STBTT_STATIC
no_warn_start
@@ -8,15 +9,6 @@ no_warn_end
#include <stdlib.h>
#include <GL/gl.h>
-#define UNICODE_BOX_CHARACTER 0x2610
-#define UNICODE_CODE_POINTS 0x110000 // number of Unicode code points
-
-static bool unicode_is_start_of_code_point(u8 byte) {
- // see https://en.wikipedia.org/wiki/UTF-8#Encoding
- // continuation bytes are of the form 10xxxxxx
- return (byte & 0xC0) != 0x80;
-}
-
// We split up code points into a bunch of pages, so we don't have to load all of the font at
// once into one texture.
#define CHAR_PAGE_SIZE 2048
@@ -249,21 +241,16 @@ void text_render_with_state(Font *font, TextRenderState *render_state, char cons
render_state->x = x;
render_state->y = y;
char32_t c = 0;
- mbstate_t mbstate = {0};
char const *end = text + strlen(text);
while (text != end) {
- size_t ret = mbrtoc32(&c, text, (size_t)(end - text), &mbstate);
+ size_t ret = unicode_utf8_to_utf32(&c, text, (size_t)(end - text));
if (ret == 0) break;
- if (ret == (size_t)(-2)) { // incomplete multi-byte character
- text_render_char(font, render_state, U'?');
- text = end; // done reading text
- } else if (ret == (size_t)(-1)) {
+ if (ret == (size_t)(-1)) {
// invalid UTF-8; skip this byte
- text_render_char(font, render_state, U'?');
+ text_render_char(font, render_state, '?');
++text;
} else {
- if (ret != (size_t)(-3))
- text += ret; // character consists of `ret` bytes
+ text += ret; // character consists of `ret` bytes
switch (c) {
default:
text_render_char(font, render_state, c);