Add optional bitmap/no-AA font loading (LoadFontExBitmap)

This commit is contained in:
Marcos De La Torre 2025-12-10 18:58:43 -08:00
parent dad93abcf8
commit a22ac4fe21
2 changed files with 51 additions and 6 deletions

View File

@ -1474,8 +1474,10 @@ RLAPI int GetPixelDataSize(int width, int height, int format); // G
RLAPI Font GetFontDefault(void); // Get the default Font RLAPI Font GetFontDefault(void); // Get the default Font
RLAPI Font LoadFont(const char *fileName); // Load font from file into GPU memory (VRAM) RLAPI Font LoadFont(const char *fileName); // Load font from file into GPU memory (VRAM)
RLAPI Font LoadFontEx(const char *fileName, int fontSize, const int *codepoints, int codepointCount); // Load font from file with extended parameters, use NULL for codepoints and 0 for codepointCount to load the default character set, font size is provided in pixels height RLAPI Font LoadFontEx(const char *fileName, int fontSize, const int *codepoints, int codepointCount); // Load font from file with extended parameters, use NULL for codepoints and 0 for codepointCount to load the default character set, font size is provided in pixels height
RLAPI Font LoadFontExBitmap(const char *fileName, int fontSize, const int *codepoints, int codepointCount); // Load font from file with bitmap glyphs (no anti-aliasing for TTF/OTF)
RLAPI Font LoadFontFromImage(Image image, Color key, int firstChar); // Load font from Image (XNA style) RLAPI Font LoadFontFromImage(Image image, Color key, int firstChar); // Load font from Image (XNA style)
RLAPI Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount); // Load font from memory buffer, fileType refers to extension: i.e. '.ttf' RLAPI Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount); // Load font from memory buffer, fileType refers to extension: i.e. '.ttf'
RLAPI Font LoadFontFromMemoryBitmap(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount); // Load font from memory buffer with bitmap glyphs (no anti-aliasing for TTF/OTF)
RLAPI bool IsFontValid(Font font); // Check if a font is valid (font data loaded, WARNING: GPU texture not checked) RLAPI bool IsFontValid(Font font); // Check if a font is valid (font data loaded, WARNING: GPU texture not checked)
RLAPI GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount, int type, int *glyphCount); // Load font data for further use RLAPI GlyphInfo *LoadFontData(const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount, int type, int *glyphCount); // Load font data for further use
RLAPI Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyphCount, int fontSize, int padding, int packMethod); // Generate image font atlas using chars info RLAPI Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyphCount, int fontSize, int padding, int packMethod); // Generate image font atlas using chars info

View File

@ -423,6 +423,22 @@ Font LoadFontEx(const char *fileName, int fontSize, const int *codepoints, int c
return font; return font;
} }
Font LoadFontExBitmap(const char *fileName, int fontSize, const int *codepoints, int codepointCount)
{
Font font = { 0 };
int dataSize = 0;
unsigned char *fileData = LoadFileData(fileName, &dataSize);
if (fileData != NULL)
{
font = LoadFontFromMemoryBitmap(GetFileExtension(fileName), fileData, dataSize,
fontSize, codepoints, codepointCount);
UnloadFileData(fileData);
}
return font;
}
// Load an Image font file (XNA style) // Load an Image font file (XNA style)
Font LoadFontFromImage(Image image, Color key, int firstChar) Font LoadFontFromImage(Image image, Color key, int firstChar)
{ {
@ -548,8 +564,9 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
return font; return font;
} }
// Load font from memory buffer, fileType refers to extension: i.e. ".ttf" static Font LoadFontFromMemoryInternal(const char *fileType, const unsigned char *fileData, int dataSize,
Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount) int fontSize, const int *codepoints, int codepointCount,
bool antialias)
{ {
Font font = { 0 }; Font font = { 0 };
@ -563,14 +580,21 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
if (TextIsEqual(fileExtLower, ".ttf") || if (TextIsEqual(fileExtLower, ".ttf") ||
TextIsEqual(fileExtLower, ".otf")) TextIsEqual(fileExtLower, ".otf"))
{ {
font.glyphs = LoadFontData(fileData, dataSize, font.baseSize, codepoints, (codepointCount > 0)? codepointCount : 95, FONT_DEFAULT, &font.glyphCount); int fontType = antialias ? FONT_DEFAULT : FONT_BITMAP;
font.glyphs = LoadFontData(fileData, dataSize, font.baseSize,
codepoints,
(codepointCount > 0)? codepointCount : 95,
fontType,
&font.glyphCount);
} }
else else
#endif #endif
#if defined(SUPPORT_FILEFORMAT_BDF) #if defined(SUPPORT_FILEFORMAT_BDF)
if (TextIsEqual(fileExtLower, ".bdf")) if (TextIsEqual(fileExtLower, ".bdf"))
{ {
font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints, (codepointCount > 0)? codepointCount : 95, &font.baseSize); font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints,
(codepointCount > 0)? codepointCount : 95, &font.baseSize);
font.glyphCount = (codepointCount > 0)? codepointCount : 95; font.glyphCount = (codepointCount > 0)? codepointCount : 95;
} }
else else
@ -584,7 +608,8 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
{ {
font.glyphPadding = FONT_TTF_DEFAULT_CHARS_PADDING; font.glyphPadding = FONT_TTF_DEFAULT_CHARS_PADDING;
Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount, font.baseSize, font.glyphPadding, 0); Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount,
font.baseSize, font.glyphPadding, 0);
if (isGpuReady) font.texture = LoadTextureFromImage(atlas); if (isGpuReady) font.texture = LoadTextureFromImage(atlas);
// Update glyphs[i].image to use alpha, required to be used on ImageDrawText() // Update glyphs[i].image to use alpha, required to be used on ImageDrawText()
@ -596,7 +621,8 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
UnloadImage(atlas); UnloadImage(atlas);
TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)", font.baseSize, font.glyphCount); TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)",
font.baseSize, font.glyphCount);
} }
else font = GetFontDefault(); else font = GetFontDefault();
#else #else
@ -606,6 +632,23 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
return font; return font;
} }
// Load font from memory buffer, fileType refers to extension: i.e. ".ttf"
Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize,
int fontSize, const int *codepoints, int codepointCount)
{
return LoadFontFromMemoryInternal(fileType, fileData, dataSize,
fontSize, codepoints, codepointCount,
true); // AA ON
}
Font LoadFontFromMemoryBitmap(const char *fileType, const unsigned char *fileData, int dataSize,
int fontSize, const int *codepoints, int codepointCount)
{
return LoadFontFromMemoryInternal(fileType, fileData, dataSize,
fontSize, codepoints, codepointCount,
false); // AA OFF
}
// Check if a font is valid (font data loaded) // Check if a font is valid (font data loaded)
// WARNING: GPU texture not checked // WARNING: GPU texture not checked
bool IsFontValid(Font font) bool IsFontValid(Font font)