Просмотр исходного кода

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.
pull/4632/head
Mikhail Ilyin 3 месяцев назад
committed by GitHub
Родитель
Сommit
ef41142205
Не найден GPG ключ соответствующий данной подписи Идентификатор GPG ключа: B5690EEEBB952194
1 измененных файлов: 70 добавлений и 0 удалений
  1. +70
    -0
      src/rtext.c

+ 70
- 0
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)
{

Загрузка…
Отмена
Сохранить