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