From ef411422058d6cf8d600df34b764535fc1ffea61 Mon Sep 17 00:00:00 2001 From: Mikhail Ilyin Date: Sun, 22 Dec 2024 20:28:41 +0300 Subject: [PATCH] Added implementations for two new functions to automatically get number of all available codepoints of font file and update the array of codepoints themselves Added implementations for two new functions to get the number of all available codepoints of a font file (or font data in memory) and update the array of codepoints themselves, passed via an argument. By default, the LoadFontEx() or LoadFontFromMemory() functions do not have this feature and load only 95 code points when specifying "NULL, 0" in the arguments. But if the user wants to automatically collect all possible code points of a font and their number, he can use these functions. Having collected them with these functions, he will only have to pass them to the LoadFontEx() or LoadFontFromMemory() functions. --- src/rtext.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/rtext.c b/src/rtext.c index 005568dbf..c1ca100bf 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -326,6 +326,76 @@ Font GetFontDefault() #endif } +// Returns number of all available codepoints +// Returns 0 if not valid format or file data has 0 valid glyphs +// Returns -1 if can't allocate new array to replace codepoints (internal function error) +int GetFontAvailableCodepoints(const char *fileName, int **codepoints) { + + int dataSize = 0; + unsigned char *fileData = LoadFileData(fileName, &dataSize); + int result = 0; + + if (fileData != NULL) + { + result = GetFontAvailableCodepointsFromMemory(GetFileExtension(fileName), fileData, dataSize, codepoints); + UnloadFileData(fileData); + } + + return result; +} + +// Returns number of all available codepoints +// Returns 0 if not valid format or file data has 0 valid glyphs +// Returns -1 if can't allocate new array to replace codepoints (internal function error) +int GetFontAvailableCodepointsFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int **codepoints) { + + char fileExtLower[16] = { 0 }; + strncpy(fileExtLower, TextToLower(fileType), 16 - 1); + +#if defined(SUPPORT_FILEFORMAT_TTF) + if (TextIsEqual(fileExtLower, ".ttf") || + TextIsEqual(fileExtLower, ".otf")) + { + // Load font info from memory data + stbtt_fontinfo fontInfo; + stbtt_InitFont(&fontInfo, fileData, stbtt_GetFontOffsetForIndex(fileData, 0)); + + // First count number of available glyphs + int codepointCount = 0; + for (int i = 0; i < MAX_GLYPHS; i++) { + if (stbtt_FindGlyphIndex(&fontInfo, i) != 0) { + codepointCount++; + } + } + + // Allocate memory for codepoints array if codepointCount > 0 + int *temp_codepoints = NULL; + if (codepointCount > 0) { + temp_codepoints = (int *)malloc(codepointCount * sizeof(int)); + if (!temp_codepoints) + return -1; + } + + // Fill codepoints array + int index = 0; + for (int i = 0; i < MAX_GLYPHS; i++) { + if (stbtt_FindGlyphIndex(&fontInfo, i) != 0) { + temp_codepoints[index++] = i; + } + } + + // Replace output array + *codepoints = temp_codepoints; + return codepointCount; + } +#endif +#if defined(SUPPORT_FILEFORMAT_BDF) + // TODO: Not supported yet +#endif + + return 0; +} + // Load Font from file into GPU memory (VRAM) Font LoadFont(const char *fileName) {