mirror of
https://github.com/raysan5/raylib.git
synced 2025-09-14 23:38:15 +00:00
REVIEWED: LoadFontFromImage()
Avoid crash on wrong sprite font
This commit is contained in:
12
src/rtext.c
12
src/rtext.c
@@ -366,6 +366,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
|||||||
|
|
||||||
#define COLOR_EQUAL(col1, col2) ((col1.r == col2.r) && (col1.g == col2.g) && (col1.b == col2.b) && (col1.a == col2.a))
|
#define COLOR_EQUAL(col1, col2) ((col1.r == col2.r) && (col1.g == col2.g) && (col1.b == col2.b) && (col1.a == col2.a))
|
||||||
|
|
||||||
|
Font font = GetFontDefault();
|
||||||
|
|
||||||
int charSpacing = 0;
|
int charSpacing = 0;
|
||||||
int lineSpacing = 0;
|
int lineSpacing = 0;
|
||||||
|
|
||||||
@@ -374,8 +376,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
|||||||
|
|
||||||
// We allocate a temporal arrays for chars data measures,
|
// We allocate a temporal arrays for chars data measures,
|
||||||
// once we get the actual number of chars, we copy data to a sized arrays
|
// once we get the actual number of chars, we copy data to a sized arrays
|
||||||
int tempCharValues[MAX_GLYPHS_FROM_IMAGE];
|
int tempCharValues[MAX_GLYPHS_FROM_IMAGE] = { 0 };
|
||||||
Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE];
|
Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE] = { 0 };
|
||||||
|
|
||||||
Color *pixels = LoadImageColors(image);
|
Color *pixels = LoadImageColors(image);
|
||||||
|
|
||||||
@@ -390,6 +392,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
|||||||
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
if (!COLOR_EQUAL(pixels[y*image.width + x], key)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((x == 0) || (y == 0)) return font;
|
||||||
|
|
||||||
charSpacing = x;
|
charSpacing = x;
|
||||||
lineSpacing = y;
|
lineSpacing = y;
|
||||||
|
|
||||||
@@ -445,9 +449,7 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
|||||||
.mipmaps = 1
|
.mipmaps = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
// Create font with all data parsed from image
|
// Set font with all data parsed from image
|
||||||
Font font = { 0 };
|
|
||||||
|
|
||||||
font.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
|
font.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
|
||||||
font.glyphCount = index;
|
font.glyphCount = index;
|
||||||
font.glyphPadding = 0;
|
font.glyphPadding = 0;
|
||||||
|
Reference in New Issue
Block a user