[rtext] `DrawTextCodepoint()` doesn't check if font is valid, which causes SIGSEGV
Issue description
Calling DrawTextCodepoint (and other functions working on fonts, I assume) with an invalid Font structure causes SIGSEGV, presumably due to the following line in the GetGlyphIndex function:
if ((index == 0) && (font.glyphs[0].value != codepoint)) index = fallbackIndex;
Causing this SIGSEGV is easy, as LoadFont doesn't indicate that it failed with an error, and IsFontValid is not used in the examples after loading fonts.
Possible solutions:
- DrawTextCodepoint and similar functions call IsFontValid instead of assuming that font is valid.
- DrawTextCodepoint and similar functions check font.glyphCount>0 instead of assuming that font is valid.
- A boolean flag called isValid is added to the Font structure, indicating that the font is valid, and functions like DrawTextCodepoint check it each time they're called.
- LoadFont returns an int which indicates that loading the font failed.
- IsFontValid is shown in all examples.
Environment
Linux 6.11
Issue Screenshot
-
Code Example
int main(void) {
Font font = LoadFont("font.ttf"); // a non-existent font
printf("Glyph count = %d\n", font.glyphCount); // 0
InitWindow(800, 600, "title");
while (!WindowShouldClose()) {
BeginDrawing();
DrawTextCodepoint(
font,
'a',
(Vector2){100,100},
100,
GREEN
);
EndDrawing();
}
}
@DenizBasgoren thanks for reporting, fixed!
A simple check like if (font.glyphCount == 0) return 0; would also work instead of a call to IsFontValid(). I think that wouldn't incur a significant delay. Honestly, a call to IsFontValid() is also not that wasteful imo, as it only does a few if checks. Compared to the rest of GetGlyphIndex() it's no big deal. We have to measure the added delays before drawing the conclusion that it's really wasteful...
In my opinion, what's important here than performance is to prevent a possible SIGSEGV, which I demonstrated that it happens.