aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2016-03-02 18:35:30 +0100
committerraysan5 <raysan5@gmail.com>2016-03-02 18:35:30 +0100
commita167067cbd1a330d435dbe652c2ecbeda5e1f35d (patch)
treec4d75411426f8b65186a49d89d497e940e337b88 /src
parent4476a9e241b952f631afb6c8c3f3c69a481219e3 (diff)
downloadraylib-a167067cbd1a330d435dbe652c2ecbeda5e1f35d.tar.gz
raylib-a167067cbd1a330d435dbe652c2ecbeda5e1f35d.zip
Security check for unsupported BMFonts
- Check if first character is the expected Space char (32) - Check if characters are ordered in definition file (.fnt)
Diffstat (limited to 'src')
-rw-r--r--src/text.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/text.c b/src/text.c
index e4c7bbf3..f89d5e4e 100644
--- a/src/text.c
+++ b/src/text.c
@@ -819,11 +819,17 @@ static SpriteFont LoadBMFont(const char *fileName)
int charId, charX, charY, charWidth, charHeight, charOffsetX, charOffsetY, charAdvanceX;
+ bool unorderedChars = false;
+ int firstChar = 0;
+
for (int i = 0; i < numChars; i++)
{
fgets(buffer, MAX_BUFFER_SIZE, fntFile);
sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i",
&charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX);
+
+ if (i == 0) firstChar = charId;
+ else if (i != (charId - firstChar)) unorderedChars = true;
// Save data properly in sprite font
font.charValues[i] = charId;
@@ -832,14 +838,20 @@ static SpriteFont LoadBMFont(const char *fileName)
font.charAdvanceX[i] = charAdvanceX;
}
- // TODO: Font data could be not ordered by charId: 32,33,34,35... review charValues and charRecs order
-
fclose(fntFile);
-
- TraceLog(INFO, "[%s] SpriteFont loaded successfully", fileName);
+
+ if (firstChar != FONT_FIRST_CHAR) TraceLog(WARNING, "BMFont not supported: expected SPACE(32) as first character, falling back to default font");
+ else if (unorderedChars) TraceLog(WARNING, "BMFont not supported: unordered chars data, falling back to default font");
+
+ // NOTE: Font data could be not ordered by charId: 32,33,34,35... raylib does not support unordered BMFonts
+ if ((firstChar != FONT_FIRST_CHAR) || (unorderedChars))
+ {
+ UnloadSpriteFont(font);
+ font = GetDefaultFont();
+ }
+ else TraceLog(INFO, "[%s] SpriteFont loaded successfully", fileName);
return font;
-
}
// Generate a sprite font from TTF file data (font size required)