diff --git a/src/rtext.c b/src/rtext.c index e21cb612a..681beee5c 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -381,22 +381,49 @@ Font LoadFontEx(const char *fileName, int fontSize, int *codepoints, int codepoi // Reload the font after adding the new characters to the list of desired characters void AddNewCharsToFontEx(Font *font, const char *fileName, int fontSize, char *newChars) { - int codepointCount = 0; - int *codepoints = LoadCodepoints(newChars, &codepointCount); + // Get the codepoints of the newChars + int codepointCount = 0; + int *codepoints = LoadCodepoints(newChars, &codepointCount); - int newCount = font->glyphCount + codepointCount; + // Maximum value of the number of codepoints in the updated Font + int newCount = font->glyphCount + codepointCount; - int *newCodepoints = MemAlloc(newCount * sizeof(int)); - for (int i=0; iglyphCount; i++) { - newCodepoints[i] = font->glyphs[i].value; - } - for (int i=font->glyphCount; iglyphCount]; - } + // Allocate space for the new codepoints and populate it + int *newCodepoints = MemAlloc(newCount * sizeof(int)); - *font = LoadFontEx(fileName, fontSize, newCodepoints, newCount); + int codepointCountWithNoDupes = 0; // We don't need to assign chars that already exist in the Font + for (int i = 0; i < font->glyphCount; i++) + { + newCodepoints[i] = font->glyphs[i].value; + codepointCountWithNoDupes += 1; + } - MemFree(newCodepoints); + for (int i = font->glyphCount; i < newCount; i++) + { + bool codepointIsUnique = true; + int cp = codepoints[i-font->glyphCount]; + + // This loop checks for the existence for the existence of the newChars in the input Font + for (int j = 0; j < font->glyphCount; j++) + { + if (cp == font->glyphs[j].value) + { + codepointIsUnique = false; + break; + } + } + if (codepointIsUnique) + { + newCodepoints[codepointCountWithNoDupes] = cp; + codepointCountWithNoDupes += 1; + } + } + + // Unload the Font and then reassign the newly built Font + UnloadFont(*font); + *font = LoadFontEx(fileName, fontSize, newCodepoints, codepointCountWithNoDupes); + + MemFree(newCodepoints); } // Load an Image font file (XNA style)