diff --git a/Makefile.common b/Makefile.common index 69f5a1dd87..da0b2b5e89 100644 --- a/Makefile.common +++ b/Makefile.common @@ -220,7 +220,8 @@ OBJ += frontend/frontend.o \ record/drivers/record_null.o \ libretro-common/features/features_cpu.o \ performance_counters.o \ - verbosity.o + verbosity.o \ + libretro-common/string/utf8_util.o ifeq ($(HAVE_LANGEXTRA), 1) DEFINES += -DHAVE_LANGEXTRA @@ -530,7 +531,7 @@ ifeq ($(HAVE_MENU_COMMON), 1) menu/drivers/null.o endif -ifneq ($(NO_UTF8), 1) +ifeq ($(UTF8), 1) DEFINES += -DHAVE_UTF8 OBJ += intl/msg_hash_ru.o DEFINES += -finput-charset=UTF-8 diff --git a/gfx/drivers_font/gl_raster_font.c b/gfx/drivers_font/gl_raster_font.c index 36d2236d41..cec87cd72f 100644 --- a/gfx/drivers_font/gl_raster_font.c +++ b/gfx/drivers_font/gl_raster_font.c @@ -17,6 +17,7 @@ #include "../common/gl_common.h" #include "../font_driver.h" #include "../video_shader_driver.h" +#include /* TODO: Move viewport side effects to the caller: it's a source of bugs. */ @@ -35,9 +36,13 @@ #define MAX_MSG_LEN_CHUNK 64 -#define ONEMASK ((size_t)(-1) / 0xFF) - -static uint8_t gl_strlen(const char **string); +#ifdef HAVE_UTF8 +#define string_walk utf8_walk +#define string_len utf8_strlen +#else +#define string_walk utf8_walkbyte +#define string_len strlen +#endif typedef struct { @@ -256,102 +261,6 @@ static void gl_raster_font_draw_vertices(gl_t *gl, const video_coords_t *coords) glDrawArrays(GL_TRIANGLES, 0, coords->vertices); } -static uint8_t string_walkbyte(const char **string) -{ - return *((*string)++); -} - -#ifdef HAVE_UTF8 -/* Does not validate the input, returns garbage if it's not UTF-8. */ -static uint32_t string_walk(const char **string) -{ - uint8_t first = string_walkbyte(string); - uint32_t ret; - - if (first<128) - return first; - - ret = 0; - ret = (ret<<6) | (string_walkbyte(string) & 0x3F); - if (first >= 0xE0) - ret = (ret<<6) | (string_walkbyte(string) & 0x3F); - if (first >= 0xF0) - ret = (ret<<6) | (string_walkbyte(string) & 0x3F); - - if (first >= 0xF0) - return ret | (first&31)<<18; - if (first >= 0xE0) - return ret | (first&15)<<12; - return ret | (first&7)<<6; -} - -static uint32_t gl_strlen_utf8(const char **string) -{ - const char * _s = *string; - const char * s; - size_t count = 0; - size_t u; - unsigned char b; - - /* Handle any initial misaligned bytes. */ - for (s = _s; (uintptr_t)(s) & (sizeof(size_t) - 1); s++) { - b = *s; - - /* Exit if we hit a zero byte. */ - if (b == '\0') - goto done; - - /* Is this byte NOT the first byte of a character? */ - count += (b >> 7) & ((~b) >> 6); - } - - /* Handle complete blocks. */ - for (; ; s += sizeof(size_t)) { - /* Prefetch 256 bytes ahead. */ - __builtin_prefetch(&s[256], 0, 0); - - /* Grab 4 or 8 bytes of UTF-8 data. */ - u = *(size_t *)(s); - - /* Exit the loop if there are any zero bytes. */ - if ((u - ONEMASK) & (~u) & (ONEMASK * 0x80)) - break; - - /* Count bytes which are NOT the first byte of a character. */ - u = ((u & (ONEMASK * 0x80)) >> 7) & ((~u) >> 6); - count += (u * ONEMASK) >> ((sizeof(size_t) - 1) * 8); - } - - /* Take care of any left-over bytes. */ - for (; ; s++) { - b = *s; - - /* Exit if we hit a zero byte. */ - if (b == '\0') - break; - - /* Is this byte NOT the first byte of a character? */ - count += (b >> 7) & ((~b) >> 6); - } - -done: - return ((s - _s) - count); -} - -static uint8_t gl_strlen(const char **string) -{ - return gl_strlen_utf8(string); -} -#else -#define string_walk string_walkbyte -#define gl_strlen gl_strlen_byte - -static uint8_t gl_strlen_byte(const char **string) -{ - return strlen(*string); -} -#endif - static void gl_raster_font_render_line( gl_raster_t *font, const char *msg, unsigned msg_len_full, GLfloat scale, const GLfloat color[4], GLfloat pos_x, @@ -460,7 +369,7 @@ static void gl_raster_font_render_message( /* If the font height is not supported just draw as usual */ if (!font->font_driver->get_line_height) { - gl_raster_font_render_line(font, msg, gl_strlen(&msg), + gl_raster_font_render_line(font, msg, string_len(msg), scale, color, pos_x, pos_y, text_align); return; } @@ -484,7 +393,7 @@ static void gl_raster_font_render_message( } else { - unsigned msg_len = gl_strlen(&msg); + unsigned msg_len = string_len(msg); gl_raster_font_render_line(font, msg, msg_len, scale, color, pos_x, pos_y - (float)lines*line_height, text_align); break; diff --git a/menu/drivers/materialui.c b/menu/drivers/materialui.c index 59265c112a..9164dfef1a 100644 --- a/menu/drivers/materialui.c +++ b/menu/drivers/materialui.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "menu_generic.h" @@ -46,6 +47,12 @@ #include "../../file_path_special.h" +#ifdef HAVE_UTF8 +#define string_len utf8_strlen +#else +#define string_len strlen +#endif + enum { MUI_TEXTURE_POINTER = 0, @@ -400,7 +407,7 @@ static void mui_render_messagebox(mui_handle_t *mui, for (i = 0; i < list->size; i++) { const char *msg = list->elems[i].data; - int len = strlen(msg); + int len = string_len(msg); if (len > longest) { longest = len; @@ -523,7 +530,7 @@ static void mui_render_label_value(mui_handle_t *mui, menu_animation_ctx_ticker_t ticker; char label_str[PATH_MAX_LENGTH] = {0}; char value_str[PATH_MAX_LENGTH] = {0}; - int value_len = strlen(value); + int value_len = string_len(value); int ticker_limit = 0; uintptr_t texture_switch = 0; bool do_draw_text = false; @@ -1176,7 +1183,7 @@ static void mui_frame(void *data) snprintf(title_buf_msg, sizeof(title_buf), "%s (%s)", title_buf, title_msg); - value_len = strlen(title_buf); + value_len = string_len(title_buf); ticker_limit = (usable_width / mui->glyph_width) - (value_len + 2); ticker.s = title_buf_msg_tmp; diff --git a/menu/drivers/rgui.c b/menu/drivers/rgui.c index 5fe1cc7efe..e05627723b 100644 --- a/menu/drivers/rgui.c +++ b/menu/drivers/rgui.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "menu_generic.h" @@ -45,6 +46,14 @@ #define RGUI_TERM_WIDTH(width) (((width - RGUI_TERM_START_X(width) - RGUI_TERM_START_X(width)) / (FONT_WIDTH_STRIDE))) #define RGUI_TERM_HEIGHT(width, height) (((height - RGUI_TERM_START_Y(height) - RGUI_TERM_START_X(width)) / (FONT_HEIGHT_STRIDE)) - 1) +#ifdef HAVE_UTF8 +#define string_walk utf8_walk +#define string_len utf8_strlen +#else +#define string_walk utf8_walkbyte +#define string_len strlen +#endif + typedef struct { bool force_redraw; @@ -164,39 +173,6 @@ static void rgui_color_rect( data[j * (pitch >> 1) + i] = color; } -static uint8_t string_walkbyte(const char **string) -{ - return *((*string)++); -} - -#ifdef HAVE_UTF8 -/* Does not validate the input, returns garbage if it's not UTF-8. */ -static uint32_t string_walk(const char **string) -{ - uint8_t first = string_walkbyte(string); - uint32_t ret; - - if (first<128) - return first; - - ret = 0; - ret = (ret<<6) | (string_walkbyte(string) & 0x3F); - if (first >= 0xE0) - ret = (ret<<6) | (string_walkbyte(string) & 0x3F); - if (first >= 0xF0) - ret = (ret<<6) | (string_walkbyte(string) & 0x3F); - - if (first >= 0xF0) - return ret | (first&31)<<18; - if (first >= 0xE0) - return ret | (first&15)<<12; - return ret | (first&7)<<6; -} - -#else -#define string_walk string_walkbyte -#endif - static void blit_line(int x, int y, const char *message, uint16_t color) { @@ -346,7 +322,7 @@ static void rgui_render_messagebox(const char *message) { unsigned line_width; char *msg = list->elems[i].data; - unsigned msglen = strlen(msg); + unsigned msglen = string_len(msg); if (msglen > RGUI_TERM_WIDTH(fb_width)) { @@ -381,7 +357,7 @@ static void rgui_render_messagebox(const char *message) for (i = 0; i < list->size; i++) { const char *msg = list->elems[i].data; - int offset_x = FONT_WIDTH_STRIDE * (glyphs_width - strlen(msg)) / 2; + int offset_x = FONT_WIDTH_STRIDE * (glyphs_width - string_len(msg)) / 2; int offset_y = FONT_HEIGHT_STRIDE * i; blit_line(x + 8 + offset_x, y + 8 + offset_y, msg, color); @@ -551,7 +527,7 @@ static void rgui_render(void *data) blit_line( RGUI_TERM_START_X(fb_width) + (RGUI_TERM_WIDTH(fb_width) - - strlen(title_buf)) * FONT_WIDTH_STRIDE / 2, + - string_len(title_buf)) * FONT_WIDTH_STRIDE / 2, RGUI_TERM_START_X(fb_width), title_buf, TITLE_COLOR(settings)); @@ -626,8 +602,8 @@ static void rgui_render(void *data) snprintf(message, sizeof(message), "%c %-*.*s %-*s", entry_selected ? '>' : ' ', - (int)(RGUI_TERM_WIDTH(fb_width) - (entry_spacing + 1 + 2) - utf8len(entry_title_buf) + strlen(entry_title_buf)), - (int)(RGUI_TERM_WIDTH(fb_width) - (entry_spacing + 1 + 2) - utf8len(entry_title_buf) + strlen(entry_title_buf)), + (int)(RGUI_TERM_WIDTH(fb_width) - (entry_spacing + 1 + 2) - utf8len(entry_title_buf) + string_len(entry_title_buf)), + (int)(RGUI_TERM_WIDTH(fb_width) - (entry_spacing + 1 + 2) - utf8len(entry_title_buf) + string_len(entry_title_buf)), entry_title_buf, entry_spacing, type_str_buf); diff --git a/menu/drivers/xmb.c b/menu/drivers/xmb.c index 3437c037ed..8429cb1825 100644 --- a/menu/drivers/xmb.c +++ b/menu/drivers/xmb.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "menu_generic.h" @@ -56,6 +57,12 @@ #define XMB_DELAY 10 #endif +#ifdef HAVE_UTF8 +#define string_len utf8_strlen +#else +#define string_len strlen +#endif + typedef struct { float alpha; @@ -669,38 +676,6 @@ static void xmb_messagebox(void *data, const char *message) strlcpy(xmb->box_message, message, sizeof(xmb->box_message)); } -static uint32_t xmb_strlen_byte(const char **string) -{ - return strlen(*string); -} - -#ifdef HAVE_UTF8 -/* Does not validate the input, returns garbage if it's not UTF-8. */ -static uint32_t xmb_strlen(const char **string) -{ - uint8_t first = xmb_strlen_byte(string); - uint32_t ret; - - if (first<128) - return first; - - ret = 0; - ret = (ret<<6) | (xmb_strlen_byte(string) & 0x3F); - if (first >= 0xE0) - ret = (ret<<6) | (xmb_strlen_byte(string) & 0x3F); - if (first >= 0xF0) - ret = (ret<<6) | (xmb_strlen_byte(string) & 0x3F); - - if (first >= 0xF0) - return ret | (first&31)<<18; - if (first >= 0xE0) - return ret | (first&15)<<12; - return ret | (first&7)<<6; -} -#else -#define xmb_strlen xmb_strlen_byte -#endif - static void xmb_render_messagebox_internal( xmb_handle_t *xmb, const char *message) { @@ -733,7 +708,7 @@ static void xmb_render_messagebox_internal( for (i = 0; i < list->size; i++) { const char *msg = list->elems[i].data; - int len = xmb_strlen(&msg); + int len = string_len(msg); if (len > longest) { longest = len;