diff --git a/examples/text/text_draw_3d.c b/examples/text/text_draw_3d.c index a579a528..8ce576b6 100644 --- a/examples/text/text_draw_3d.c +++ b/examples/text/text_draw_3d.c @@ -291,7 +291,7 @@ int main(void) for (int i = 0; i < layers; ++i) { Color clr = light; - if(multicolor) clr = multi[i]; + if (multicolor) clr = multi[i]; DrawTextWave3D(font, text, (Vector3){ -tbox.x/2.0f, layerDistance*i, -4.5f }, fontSize, fontSpacing, lineSpacing, true, &wcfg, time, clr); } @@ -465,7 +465,7 @@ void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontS float width = (float)(font.recs[index].width + 2.0f*font.charsPadding)/(float)font.baseSize*scale; float height = (float)(font.recs[index].height + 2.0f*font.charsPadding)/(float)font.baseSize*scale; - if(font.texture.id > 0) + if (font.texture.id > 0) { const float x = 0.0f; const float y = 0.0f; @@ -477,7 +477,7 @@ void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontS const float tw = (srcRec.x+srcRec.width)/font.texture.width; const float th = (srcRec.y+srcRec.height)/font.texture.height; - if(SHOW_LETTER_BOUNDRY) + if (SHOW_LETTER_BOUNDRY) DrawCubeWiresV((Vector3){ position.x + width/2, position.y, position.z + height/2}, (Vector3){ width, LETTER_BOUNDRY_SIZE, height }, LETTER_BOUNDRY_COLOR); #if defined(RAYLIB_NEW_RLGL) @@ -533,7 +533,7 @@ void DrawText3D(Font font, const char *text, Vector3 position, float fontSize, f { // Get next codepoint from byte string and glyph index in font int codepointByteCount = 0; - int codepoint = GetNextCodepoint(&text[i], &codepointByteCount); + int codepoint = GetCodepoint(&text[i], &codepointByteCount); int index = GetGlyphIndex(font, codepoint); // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -582,7 +582,7 @@ Vector3 MeasureText3D(Font font, const char* text, float fontSize, float fontSpa lenCounter++; int next = 0; - letter = GetNextCodepoint(&text[i], &next); + letter = GetCodepoint(&text[i], &next); index = GetGlyphIndex(font, letter); // NOTE: normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -632,7 +632,7 @@ void DrawTextWave3D(Font font, const char *text, Vector3 position, float fontSiz { // Get next codepoint from byte string and glyph index in font int codepointByteCount = 0; - int codepoint = GetNextCodepoint(&text[i], &codepointByteCount); + int codepoint = GetCodepoint(&text[i], &codepointByteCount); int index = GetGlyphIndex(font, codepoint); // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -649,7 +649,7 @@ void DrawTextWave3D(Font font, const char *text, Vector3 position, float fontSiz } else if (codepoint == '~') { - if (GetNextCodepoint(&text[i+1], &codepointByteCount) == '~') + if (GetCodepoint(&text[i+1], &codepointByteCount) == '~') { codepointByteCount += 1; wave = !wave; @@ -698,7 +698,7 @@ Vector3 MeasureTextWave3D(Font font, const char* text, float fontSize, float fon lenCounter++; int next = 0; - letter = GetNextCodepoint(&text[i], &next); + letter = GetCodepoint(&text[i], &next); index = GetGlyphIndex(font, letter); // NOTE: normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -708,7 +708,7 @@ Vector3 MeasureTextWave3D(Font font, const char* text, float fontSize, float fon if (letter != '\n') { - if(letter == '~' && GetNextCodepoint(&text[i+1], &next) == '~') + if (letter == '~' && GetCodepoint(&text[i+1], &next) == '~') { i++; } diff --git a/src/config.h b/src/config.h index 3ca16032..6479d409 100644 --- a/src/config.h +++ b/src/config.h @@ -165,7 +165,6 @@ //------------------------------------------------------------------------------------ #define MAX_TEXT_BUFFER_LENGTH 1024 // Size of internal static buffers used on some functions: // TextFormat(), TextSubtext(), TextToUpper(), TextToLower(), TextToPascal(), TextSplit() -#define MAX_TEXT_UNICODE_CHARS 512 // Maximum number of unicode codepoints: GetCodepoints() #define MAX_TEXTSPLIT_COUNT 128 // Maximum number of substrings to split: TextSplit() diff --git a/src/raylib.h b/src/raylib.h index ea3dc693..6c4ab45e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1369,9 +1369,10 @@ RLAPI int TextToInteger(const char *text); // Get int RLAPI char *TextToUtf8(int *codepoints, int length); // Encode text codepoint into utf8 text (memory must be freed!) // UTF8 text strings management functions -RLAPI int *GetCodepoints(const char *text, int *count); // Get all codepoints in a string, codepoints count returned by parameters +RLAPI int *LoadCodepoints(const char *text, int *count); // Load all codepoints from a UTF8 text string, codepoints count returned by parameter +RLAPI void UnloadCodepoints(int *codepoints); // Unload codepoints data from memory RLAPI int GetCodepointsCount(const char *text); // Get total number of characters (codepoints) in a UTF8 encoded string -RLAPI int GetNextCodepoint(const char *text, int *bytesProcessed); // Get next codepoint in a UTF8 encoded string; 0x3f('?') is returned on failure +RLAPI int GetCodepoint(const char *text, int *bytesProcessed); // Get next codepoint in a UTF8 encoded string, 0x3f('?') is returned on failure RLAPI const char *CodepointToUtf8(int codepoint, int *byteLength); // Encode codepoint into utf8 text (char array length returned as parameter) //------------------------------------------------------------------------------------ diff --git a/src/text.c b/src/text.c index 65460d80..5b5b8564 100644 --- a/src/text.c +++ b/src/text.c @@ -861,7 +861,7 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f { // Get next codepoint from byte string and glyph index in font int codepointByteCount = 0; - int codepoint = GetNextCodepoint(&text[i], &codepointByteCount); + int codepoint = GetCodepoint(&text[i], &codepointByteCount); int index = GetGlyphIndex(font, codepoint); // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -918,7 +918,7 @@ void DrawTextRecEx(Font font, const char *text, Rectangle rec, float fontSize, f { // Get next codepoint from byte string and glyph index in font int codepointByteCount = 0; - int codepoint = GetNextCodepoint(&text[i], &codepointByteCount); + int codepoint = GetCodepoint(&text[i], &codepointByteCount); int index = GetGlyphIndex(font, codepoint); // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -1089,7 +1089,7 @@ Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing lenCounter++; int next = 0; - letter = GetNextCodepoint(&text[i], &next); + letter = GetCodepoint(&text[i], &next); index = GetGlyphIndex(font, letter); // NOTE: normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f) @@ -1563,28 +1563,38 @@ RLAPI const char *CodepointToUtf8(int codepoint, int *byteLength) return utf8; } -// Get all codepoints in a string, codepoints count returned by parameters -// REQUIRES: memset() -int *GetCodepoints(const char *text, int *count) +// Load all codepoints from a UTF8 text string, codepoints count returned by parameter +int *LoadCodepoints(const char *text, int *count) { - static int codepoints[MAX_TEXT_UNICODE_CHARS] = { 0 }; - memset(codepoints, 0, MAX_TEXT_UNICODE_CHARS*sizeof(int)); - - int bytesProcessed = 0; int textLength = TextLength(text); + + int bytesProcessed = 0; int codepointsCount = 0; + + // Allocate a big enough buffer to store as many codepoints as text bytes + int *codepoints = RL_CALLOC(textLength, sizeof(int)); for (int i = 0; i < textLength; codepointsCount++) { - codepoints[codepointsCount] = GetNextCodepoint(text + i, &bytesProcessed); + codepoints[codepointsCount] = GetCodepoint(text + i, &bytesProcessed); i += bytesProcessed; } + // Re-allocate buffer to the actual number of codepoints loaded + void *temp = RL_REALLOC(codepoints, codepointsCount*sizeof(int)); + if (temp != NULL) codepoints = temp; + *count = codepointsCount; return codepoints; } +// Unload codepoints data from memory +void UnloadCodepoints(int *codepoints) +{ + RL_FREE(codepoints); +} + // Get total number of characters(codepoints) in a UTF8 encoded text, until '\0' is found // NOTE: If an invalid UTF8 sequence is encountered a '?'(0x3f) codepoint is counted instead int GetCodepointsCount(const char *text) @@ -1595,7 +1605,7 @@ int GetCodepointsCount(const char *text) while (*ptr != '\0') { int next = 0; - int letter = GetNextCodepoint(ptr, &next); + int letter = GetCodepoint(ptr, &next); if (letter == 0x3f) ptr += 1; else ptr += next; @@ -1613,7 +1623,7 @@ int GetCodepointsCount(const char *text) // NOTE: the standard says U+FFFD should be returned in case of errors // but that character is not supported by the default font in raylib // TODO: Optimize this code for speed!! -int GetNextCodepoint(const char *text, int *bytesProcessed) +int GetCodepoint(const char *text, int *bytesProcessed) { /* UTF8 specs from https://www.ietf.org/rfc/rfc3629.txt diff --git a/src/textures.c b/src/textures.c index 7017660b..a9c40d50 100644 --- a/src/textures.c +++ b/src/textures.c @@ -1114,7 +1114,7 @@ Image ImageTextEx(Font font, const char *text, float fontSize, float spacing, Co { // Get next codepoint from byte string and glyph index in font int codepointByteCount = 0; - int codepoint = GetNextCodepoint(&text[i], &codepointByteCount); + int codepoint = GetCodepoint(&text[i], &codepointByteCount); int index = GetGlyphIndex(font, codepoint); // NOTE: Normally we exit the decoding sequence as soon as a bad byte is found (and return 0x3f)