Skip to content

Commit a22ac4f

Browse files
committed
Add optional bitmap/no-AA font loading (LoadFontExBitmap)
1 parent dad93ab commit a22ac4f

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

src/raylib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,10 @@ RLAPI int GetPixelDataSize(int width, int height, int format); // G
14741474
RLAPI Font GetFontDefault(void); // Get the default Font
14751475
RLAPI Font LoadFont(const char *fileName); // Load font from file into GPU memory (VRAM)
14761476
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
1477+
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)
14771478
RLAPI Font LoadFontFromImage(Image image, Color key, int firstChar); // Load font from Image (XNA style)
14781479
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'
1480+
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)
14791481
RLAPI bool IsFontValid(Font font); // Check if a font is valid (font data loaded, WARNING: GPU texture not checked)
14801482
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
14811483
RLAPI Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyphCount, int fontSize, int padding, int packMethod); // Generate image font atlas using chars info

src/rtext.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,22 @@ Font LoadFontEx(const char *fileName, int fontSize, const int *codepoints, int c
423423
return font;
424424
}
425425

426+
Font LoadFontExBitmap(const char *fileName, int fontSize, const int *codepoints, int codepointCount)
427+
{
428+
Font font = { 0 };
429+
int dataSize = 0;
430+
unsigned char *fileData = LoadFileData(fileName, &dataSize);
431+
432+
if (fileData != NULL)
433+
{
434+
font = LoadFontFromMemoryBitmap(GetFileExtension(fileName), fileData, dataSize,
435+
fontSize, codepoints, codepointCount);
436+
UnloadFileData(fileData);
437+
}
438+
439+
return font;
440+
}
441+
426442
// Load an Image font file (XNA style)
427443
Font LoadFontFromImage(Image image, Color key, int firstChar)
428444
{
@@ -548,8 +564,9 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
548564
return font;
549565
}
550566

551-
// Load font from memory buffer, fileType refers to extension: i.e. ".ttf"
552-
Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize, int fontSize, const int *codepoints, int codepointCount)
567+
static Font LoadFontFromMemoryInternal(const char *fileType, const unsigned char *fileData, int dataSize,
568+
int fontSize, const int *codepoints, int codepointCount,
569+
bool antialias)
553570
{
554571
Font font = { 0 };
555572

@@ -563,14 +580,21 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
563580
if (TextIsEqual(fileExtLower, ".ttf") ||
564581
TextIsEqual(fileExtLower, ".otf"))
565582
{
566-
font.glyphs = LoadFontData(fileData, dataSize, font.baseSize, codepoints, (codepointCount > 0)? codepointCount : 95, FONT_DEFAULT, &font.glyphCount);
583+
int fontType = antialias ? FONT_DEFAULT : FONT_BITMAP;
584+
585+
font.glyphs = LoadFontData(fileData, dataSize, font.baseSize,
586+
codepoints,
587+
(codepointCount > 0)? codepointCount : 95,
588+
fontType,
589+
&font.glyphCount);
567590
}
568591
else
569592
#endif
570593
#if defined(SUPPORT_FILEFORMAT_BDF)
571594
if (TextIsEqual(fileExtLower, ".bdf"))
572595
{
573-
font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints, (codepointCount > 0)? codepointCount : 95, &font.baseSize);
596+
font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints,
597+
(codepointCount > 0)? codepointCount : 95, &font.baseSize);
574598
font.glyphCount = (codepointCount > 0)? codepointCount : 95;
575599
}
576600
else
@@ -584,7 +608,8 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
584608
{
585609
font.glyphPadding = FONT_TTF_DEFAULT_CHARS_PADDING;
586610

587-
Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount, font.baseSize, font.glyphPadding, 0);
611+
Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount,
612+
font.baseSize, font.glyphPadding, 0);
588613
if (isGpuReady) font.texture = LoadTextureFromImage(atlas);
589614

590615
// 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
596621

597622
UnloadImage(atlas);
598623

599-
TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)", font.baseSize, font.glyphCount);
624+
TRACELOG(LOG_INFO, "FONT: Data loaded successfully (%i pixel size | %i glyphs)",
625+
font.baseSize, font.glyphCount);
600626
}
601627
else font = GetFontDefault();
602628
#else
@@ -606,6 +632,23 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
606632
return font;
607633
}
608634

635+
// Load font from memory buffer, fileType refers to extension: i.e. ".ttf"
636+
Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int dataSize,
637+
int fontSize, const int *codepoints, int codepointCount)
638+
{
639+
return LoadFontFromMemoryInternal(fileType, fileData, dataSize,
640+
fontSize, codepoints, codepointCount,
641+
true); // AA ON
642+
}
643+
644+
Font LoadFontFromMemoryBitmap(const char *fileType, const unsigned char *fileData, int dataSize,
645+
int fontSize, const int *codepoints, int codepointCount)
646+
{
647+
return LoadFontFromMemoryInternal(fileType, fileData, dataSize,
648+
fontSize, codepoints, codepointCount,
649+
false); // AA OFF
650+
}
651+
609652
// Check if a font is valid (font data loaded)
610653
// WARNING: GPU texture not checked
611654
bool IsFontValid(Font font)

0 commit comments

Comments
 (0)