From d7e072fe25e7d5b6078ea1aee1a2e3baba6f6fe2 Mon Sep 17 00:00:00 2001 From: Iaroslav Erokhin Date: Thu, 6 Mar 2025 14:20:46 +0100 Subject: [PATCH] fix heap buffer overflow and support unicode characters --- renderers/raylib/clay_renderer_raylib.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/renderers/raylib/clay_renderer_raylib.c b/renderers/raylib/clay_renderer_raylib.c index 2b370c6..9be2a5e 100644 --- a/renderers/raylib/clay_renderer_raylib.c +++ b/renderers/raylib/clay_renderer_raylib.c @@ -99,16 +99,22 @@ static inline Clay_Dimensions Raylib_MeasureText(Clay_StringSlice text, Clay_Tex float scaleFactor = config->fontSize/(float)fontToUse.baseSize; - for (int i = 0; i < text.length; ++i) - { - if (text.chars[i] == '\n') { + int byte_index = 0; + while (byte_index < text.length) { + if (text.chars[byte_index] == '\n') { maxTextWidth = fmax(maxTextWidth, lineTextWidth); lineTextWidth = 0; + byte_index++; continue; } - int index = text.chars[i] - 32; - if (fontToUse.glyphs[index].advanceX != 0) lineTextWidth += fontToUse.glyphs[index].advanceX; - else lineTextWidth += (fontToUse.recs[index].width + fontToUse.glyphs[index].offsetX); + + int codepoint_bytes = 0; + int codepoint = GetCodepoint(&text.chars[byte_index], &codepoint_bytes); + int glyph_index = GetGlyphIndex(fontToUse, codepoint); + byte_index += codepoint_bytes; + + if (fontToUse.glyphs[glyph_index].advanceX != 0) lineTextWidth += fontToUse.glyphs[glyph_index].advanceX; + else lineTextWidth += (fontToUse.recs[glyph_index].width + fontToUse.glyphs[glyph_index].offsetX); } maxTextWidth = fmax(maxTextWidth, lineTextWidth);