From 02b592cd7bd9cd62cebc8f4430d1dd1d475a23af Mon Sep 17 00:00:00 2001 From: moe li Date: Thu, 5 Mar 2026 18:49:57 +0800 Subject: [PATCH] [rtext] Add `MeasureTextCodepoints()` for direct measurement of codepoints (#5623) * Measuring length of an array of codepoints * Applied style changes and removed default measurement function --- src/raylib.h | 1 + src/rtext.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/raylib.h b/src/raylib.h index c8c5c85a5..6a23400c6 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1509,6 +1509,7 @@ RLAPI void DrawTextCodepoints(Font font, const int *codepoints, int codepointCou RLAPI void SetTextLineSpacing(int spacing); // Set vertical line spacing when drawing with line-breaks RLAPI int MeasureText(const char *text, int fontSize); // Measure string width for default font RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing); // Measure string size for Font +RLAPI Vector2 MeasureTextCodepoints(Font font, const int *codepoints, int length, float fontSize, float spacing); // Measure string size for an existing array of codepoints for Font RLAPI int GetGlyphIndex(Font font, int codepoint); // Get glyph index position in font for a codepoint (unicode character), fallback to '?' if not found RLAPI GlyphInfo GetGlyphInfo(Font font, int codepoint); // Get glyph font info data for a codepoint (unicode character), fallback to '?' if not found RLAPI Rectangle GetGlyphAtlasRec(Font font, int codepoint); // Get glyph rectangle in font atlas for a codepoint (unicode character), fallback to '?' if not found diff --git a/src/rtext.c b/src/rtext.c index 4263284b4..ff59d667e 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -1388,6 +1388,64 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing return textSize; } +// Measure string size for an existing array of codepoints for Font +Vector2 MeasureTextCodepoints(Font font, const int *codepoints, int length, float fontSize, float spacing) +{ + Vector2 textSize = { 0 }; + + // Security check + if ((font.texture.id == 0) || (codepoints == NULL) || (length == 0)) return textSize; + + float textWidth = 0.0f; + // Used to count longer text line width + float tempTextWidth = 0.0f; + + // Used to count longer text line num chars + int tempGlyphCounter = 0; + int glyphCounter = 0; + + float textHeight = fontSize; + float scaleFactor = fontSize/(float)font.baseSize; + + // Current character + int letter = 0; + // Index position in sprite font + int index = 0; + + for (int i = 0; i < length; i++) + { + letter = codepoints[i]; + index = GetGlyphIndex(font, letter); + + if (letter != '\n') + { + glyphCounter++; + + if (font.glyphs[index].advanceX > 0) textWidth += font.glyphs[index].advanceX; + else textWidth += (font.recs[index].width + font.glyphs[index].offsetX); + } + else + { + if (tempTextWidth < textWidth) tempTextWidth = textWidth; + + textWidth = 0; + glyphCounter = 0; + + // NOTE: Line spacing is a global variable, use SetTextLineSpacing() to setup + textHeight += (fontSize + textLineSpacing); + } + + if (tempGlyphCounter < glyphCounter) tempGlyphCounter = glyphCounter; + } + + if (tempTextWidth < textWidth) tempTextWidth = textWidth; + + textSize.x = tempTextWidth*scaleFactor + (float)((tempGlyphCounter - 1)*spacing); + textSize.y = textHeight; + + return textSize; +} + // Get index position for a unicode character on font // NOTE: If codepoint is not found in the font it fallbacks to '?' int GetGlyphIndex(Font font, int codepoint)