summaryrefslogtreecommitdiffhomepage
path: root/src/rtext.c
diff options
context:
space:
mode:
authorRay <[email protected]>2021-10-12 23:00:00 +0200
committerRay <[email protected]>2021-10-12 23:00:00 +0200
commita1db0220a1582a568ce05257d1e5b602132602a1 (patch)
tree8be0b8221e87f523a9b368c9ad78da29f47d48e9 /src/rtext.c
parentf9d46010574ea25197525ea776e51c743b005a04 (diff)
downloadraylib-a1db0220a1582a568ce05257d1e5b602132602a1.tar.gz
raylib-a1db0220a1582a568ce05257d1e5b602132602a1.zip
REVIEWED: LoadFontFromImage()
Avoid crash on wrong sprite font
Diffstat (limited to 'src/rtext.c')
-rw-r--r--src/rtext.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/rtext.c b/src/rtext.c
index dd6bcf2d..c4388cd0 100644
--- a/src/rtext.c
+++ b/src/rtext.c
@@ -364,7 +364,9 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
#define MAX_GLYPHS_FROM_IMAGE 256 // Maximum number of glyphs supported on image scan
#endif
- #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 lineSpacing = 0;
@@ -374,8 +376,8 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
// We allocate a temporal arrays for chars data measures,
// once we get the actual number of chars, we copy data to a sized arrays
- int tempCharValues[MAX_GLYPHS_FROM_IMAGE];
- Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE];
+ int tempCharValues[MAX_GLYPHS_FROM_IMAGE] = { 0 };
+ Rectangle tempCharRecs[MAX_GLYPHS_FROM_IMAGE] = { 0 };
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 ((x == 0) || (y == 0)) return font;
+
charSpacing = x;
lineSpacing = y;
@@ -445,9 +449,7 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
.mipmaps = 1
};
- // Create font with all data parsed from image
- Font font = { 0 };
-
+ // Set font with all data parsed from image
font.texture = LoadTextureFromImage(fontClear); // Convert processed image to OpenGL texture
font.glyphCount = index;
font.glyphPadding = 0;