FIX: LoadFontDataBDF() #5346

This commit is contained in:
Ray 2025-11-12 10:22:56 +01:00
parent 7f82da0031
commit 8ae2c9cf5f

View File

@ -147,7 +147,7 @@ static int textLineSpacing = 2; // Text vertical line spacing in
static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file) static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
#endif #endif
#if defined(SUPPORT_FILEFORMAT_BDF) #if defined(SUPPORT_FILEFORMAT_BDF)
static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize); static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, const int *codepoints, int codepointCount, int *outFontSize);
#endif #endif
#if defined(SUPPORT_DEFAULT_FONT) #if defined(SUPPORT_DEFAULT_FONT)
@ -647,7 +647,7 @@ GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSiz
{ {
bool genFontChars = false; bool genFontChars = false;
stbtt_fontinfo fontInfo = { 0 }; stbtt_fontinfo fontInfo = { 0 };
int *requiredCodepoints = (int *)codepoints; int *requiredCodepoints = (int *)codepoints; // TODO: Should we create a shallow copy to avoid "dealing" with a const user array?
if (stbtt_InitFont(&fontInfo, (unsigned char *)fileData, 0)) // Initialize font for data reading if (stbtt_InitFont(&fontInfo, (unsigned char *)fileData, 0)) // Initialize font for data reading
{ {
@ -2517,7 +2517,7 @@ static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, c
char buffer[MAX_BUFFER_SIZE] = { 0 }; char buffer[MAX_BUFFER_SIZE] = { 0 };
GlyphInfo *glyphs = NULL; GlyphInfo *glyphs = NULL;
bool genFontChars = false; bool internalCodepoints = false;
int totalReadBytes = 0; // Data bytes read (total) int totalReadBytes = 0; // Data bytes read (total)
int readBytes = 0; // Data bytes read (line) int readBytes = 0; // Data bytes read (line)
@ -2545,21 +2545,23 @@ static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, c
int charDWidthX = 0; // Character advance X int charDWidthX = 0; // Character advance X
int charDWidthY = 0; // Character advance Y (unused) int charDWidthY = 0; // Character advance Y (unused)
GlyphInfo *glyphs = NULL; // Pointer to output glyph info (NULL if not set) int *requiredCodepoints = (int *)RL_MALLOC(codepointCount*sizeof(int));
int *requiredCodepoints = codepoints;
if (fileData == NULL) return glyphs; if (fileData == NULL) return glyphs;
// In case no chars count provided, default to 95 // In case no chars count provided, default to 95
codepointCount = (codepointCount > 0)? codepointCount : 95; codepointCount = (codepointCount > 0)? codepointCount : 95;
// Fill fontChars in case not provided externally if (codepoints == NULL)
// NOTE: By default we fill glyphCount consecutively, starting at 32 (Space)
if (requiredCodepoints == NULL)
{ {
requiredCodepoints = (int *)RL_MALLOC(codepointCount*sizeof(int)); // Fill internal codepoints array in case not provided externally
// NOTE: By default we fill glyphCount consecutively, starting at 32 (Space)
for (int i = 0; i < codepointCount; i++) requiredCodepoints[i] = i + 32; for (int i = 0; i < codepointCount; i++) requiredCodepoints[i] = i + 32;
genFontChars = true; internalCodepoints = true;
}
else
{
for (int i = 0; i < codepointCount; i++) requiredCodepoints[i] = codepoints[i];
} }
glyphs = (GlyphInfo *)RL_CALLOC(codepointCount, sizeof(GlyphInfo)); glyphs = (GlyphInfo *)RL_CALLOC(codepointCount, sizeof(GlyphInfo));
@ -2634,11 +2636,11 @@ static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, c
// Search for glyph index in codepoints // Search for glyph index in codepoints
glyphs = NULL; glyphs = NULL;
for (int codepointIndex = 0; codepointIndex < codepointCount; codepointIndex++) for (int index = 0; index < codepointCount; index++)
{ {
if (codepoints[codepointIndex] == charEncoding) if (requiredCodepoints[index] == charEncoding)
{ {
glyphs = &glyphs[codepointIndex]; glyphs = &glyphs[index];
break; break;
} }
} }
@ -2738,7 +2740,7 @@ static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, c
} }
} }
if (genFontChars) RL_FREE(codepoints); RL_FREE(requiredCodepoints);
if (fontMalformed) if (fontMalformed)
{ {