mirror of
https://github.com/raysan5/raylib.git
synced 2025-10-03 08:28:30 +00:00
[rtext] Fix GetCodepointNext() to return default value on invalid input with size=0 (#2997)
* Fix GetCodepointNext to return default value with size=0 on invalid input. Modify LoadCodepoints to work when GetCodepointNext returns a size of 0. All internal use of GetCodepointNext and GetCodepointPrev checked. This fix may break external code dealing with invalid input as the old code erroneously never returned a size of 0, external code that doesn't properly check for size=0 may endlessly loop or overflow a buffer on invalid input. * Change default behaviour of GetCodepointNext to return a size of 1 instead of 0. This matches existing prod behaviour and guarantees size 1..4 is returned. Simplify internal code that uses GetCodepointNext that previously had to account for size=0. * Simplified progressing through a UTF-8 string in ImageTextEx and MeasureTextEx. This change matches existing precedent in DrawTextEx * GetCodepointNext: Add 10xxxxxx checks to multibyte encodings. --------- Co-authored-by: anon <anon>
This commit is contained in:
@@ -1266,17 +1266,13 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co
|
||||
// Create image to store text
|
||||
imText = GenImageColor((int)imSize.x, (int)imSize.y, BLANK);
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
for (int i = 0; i < size;)
|
||||
{
|
||||
// Get next codepoint from byte string and glyph index in font
|
||||
int codepointByteCount = 0;
|
||||
int codepoint = GetCodepointNext(&text[i], &codepointByteCount); // WARNING: Module required: rtext
|
||||
int index = GetGlyphIndex(font, codepoint); // WARNING: Module required: rtext
|
||||
|
||||
// NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)
|
||||
// but we need to draw all the bad bytes using the '?' symbol moving one byte
|
||||
if (codepoint == 0x3f) codepointByteCount = 1;
|
||||
|
||||
if (codepoint == '\n')
|
||||
{
|
||||
// NOTE: Fixed line spacing of 1.5 line-height
|
||||
@@ -1296,7 +1292,7 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co
|
||||
else textOffsetX += font.glyphs[index].advanceX + (int)spacing;
|
||||
}
|
||||
|
||||
i += (codepointByteCount - 1); // Move text bytes counter to next codepoint
|
||||
i += codepointByteCount; // Move text bytes counter to next codepoint
|
||||
}
|
||||
|
||||
// Scale image depending on text size
|
||||
|
Reference in New Issue
Block a user