Some security checks on font loading
This commit is contained in:
parent
764766bfb2
commit
3b674cd281
28
src/text.c
28
src/text.c
@ -276,15 +276,7 @@ Font LoadFont(const char *fileName)
|
||||
Font font = { 0 };
|
||||
|
||||
#if defined(SUPPORT_FILEFORMAT_TTF)
|
||||
if (IsFileExtension(fileName, ".ttf"))
|
||||
{
|
||||
font.baseSize = DEFAULT_TTF_FONTSIZE;
|
||||
font.charsCount = DEFAULT_TTF_NUMCHARS;
|
||||
font.chars = LoadFontData(fileName, font.baseSize, NULL, font.charsCount, FONT_DEFAULT);
|
||||
Image atlas = GenImageFontAtlas(font.chars, font.charsCount, font.baseSize, 4, 0);
|
||||
font.texture = LoadTextureFromImage(atlas);
|
||||
UnloadImage(atlas);
|
||||
}
|
||||
if (IsFileExtension(fileName, ".ttf")) font = LoadFontEx(fileName, DEFAULT_TTF_FONTSIZE, DEFAULT_TTF_NUMCHARS, NULL);
|
||||
else
|
||||
#endif
|
||||
#if defined(SUPPORT_FILEFORMAT_FNT)
|
||||
@ -317,9 +309,13 @@ Font LoadFontEx(const char *fileName, int fontSize, int charsCount, int *fontCha
|
||||
font.baseSize = fontSize;
|
||||
font.charsCount = (charsCount > 0) ? charsCount : 95;
|
||||
font.chars = LoadFontData(fileName, font.baseSize, fontChars, font.charsCount, FONT_DEFAULT);
|
||||
|
||||
if (font.chars != NULL)
|
||||
{
|
||||
Image atlas = GenImageFontAtlas(font.chars, font.charsCount, font.baseSize, 2, 0);
|
||||
font.texture = LoadTextureFromImage(atlas);
|
||||
UnloadImage(atlas);
|
||||
}
|
||||
|
||||
return font;
|
||||
}
|
||||
@ -336,16 +332,15 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
|
||||
|
||||
#define BITMAP_ALPHA_THRESHOLD 80
|
||||
|
||||
// In case no chars count provided, default to 95
|
||||
charsCount = (charsCount > 0) ? charsCount : 95;
|
||||
|
||||
CharInfo *chars = (CharInfo *)malloc(charsCount*sizeof(CharInfo));
|
||||
CharInfo *chars = NULL;
|
||||
|
||||
// Load font data (including pixel data) from TTF file
|
||||
// NOTE: Loaded information should be enough to generate font image atlas,
|
||||
// using any packaging method
|
||||
FILE *fontFile = fopen(fileName, "rb"); // Load font file
|
||||
|
||||
if (fontFile != NULL)
|
||||
{
|
||||
fseek(fontFile, 0, SEEK_END);
|
||||
long size = ftell(fontFile); // Get file size
|
||||
fseek(fontFile, 0, SEEK_SET); // Reset file pointer
|
||||
@ -367,6 +362,9 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
|
||||
int ascent, descent, lineGap;
|
||||
stbtt_GetFontVMetrics(&fontInfo, &ascent, &descent, &lineGap);
|
||||
|
||||
// In case no chars count provided, default to 95
|
||||
charsCount = (charsCount > 0) ? charsCount : 95;
|
||||
|
||||
// Fill fontChars in case not provided externally
|
||||
// NOTE: By default we fill charsCount consecutevely, starting at 32 (Space)
|
||||
int genFontChars = false;
|
||||
@ -377,6 +375,8 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
|
||||
for (int i = 0; i < charsCount; i++) fontChars[i] = i + 32;
|
||||
}
|
||||
|
||||
chars = (CharInfo *)malloc(charsCount*sizeof(CharInfo));
|
||||
|
||||
// NOTE: Using simple packaging, one char after another
|
||||
for (int i = 0; i < charsCount; i++)
|
||||
{
|
||||
@ -420,6 +420,8 @@ CharInfo *LoadFontData(const char *fileName, int fontSize, int *fontChars, int c
|
||||
|
||||
free(fontBuffer);
|
||||
if (genFontChars) free(fontChars);
|
||||
}
|
||||
else TraceLog(LOG_WARNING, "[%s] TTF file could not be opened", fileName);
|
||||
|
||||
return chars;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user