ADDED: LoadFontFromMemory() (TTF only) #1327
This commit is contained in:
parent
88c5deac87
commit
db652daf42
@ -1222,8 +1222,9 @@ RLAPI Font GetFontDefault(void);
|
|||||||
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, int *fontChars, int charsCount); // Load font from file with extended parameters
|
RLAPI Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCount); // Load font from file with extended parameters
|
||||||
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 Image GenImageFontAtlas(const CharInfo *chars, Rectangle **recs, int charsCount, int fontSize, int padding, int packMethod); // Generate image font atlas using chars info
|
RLAPI Font LoadFontFromMemory(const char *fileType, const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount); // Load font from memory buffer, fileType refers to extension: i.e. "ttf"
|
||||||
RLAPI CharInfo *LoadFontData(const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type); // Load font data for further use
|
RLAPI CharInfo *LoadFontData(const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type); // Load font data for further use
|
||||||
|
RLAPI Image GenImageFontAtlas(const CharInfo *chars, Rectangle **recs, int charsCount, int fontSize, int padding, int packMethod); // Generate image font atlas using chars info
|
||||||
RLAPI void UnloadFont(Font font); // Unload Font from GPU memory (VRAM)
|
RLAPI void UnloadFont(Font font); // Unload Font from GPU memory (VRAM)
|
||||||
|
|
||||||
// Text drawing functions
|
// Text drawing functions
|
||||||
|
66
src/text.c
66
src/text.c
@ -336,29 +336,14 @@ Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCou
|
|||||||
{
|
{
|
||||||
Font font = { 0 };
|
Font font = { 0 };
|
||||||
|
|
||||||
#if defined(SUPPORT_FILEFORMAT_TTF)
|
// Loading file to memory
|
||||||
font.baseSize = fontSize;
|
unsigned int fileSize = 0;
|
||||||
font.charsCount = (charsCount > 0)? charsCount : 95;
|
unsigned char *fileData = LoadFileData(fileName, &fileSize);
|
||||||
font.chars = LoadFontData(fileName, font.baseSize, fontChars, font.charsCount, FONT_DEFAULT);
|
|
||||||
|
|
||||||
if (font.chars != NULL)
|
// Loading font from memory data
|
||||||
{
|
font = LoadFontFromMemory(GetFileExtension(fileName), (char *)fileData, fileSize, fontSize, fontChars, charsCount);
|
||||||
Image atlas = GenImageFontAtlas(font.chars, &font.recs, font.charsCount, font.baseSize, 2, 0);
|
|
||||||
font.texture = LoadTextureFromImage(atlas);
|
|
||||||
|
|
||||||
// Update chars[i].image to use alpha, required to be used on ImageDrawText()
|
RL_FREE(fileData);
|
||||||
for (int i = 0; i < font.charsCount; i++)
|
|
||||||
{
|
|
||||||
UnloadImage(font.chars[i].image);
|
|
||||||
font.chars[i].image = ImageFromImage(atlas, font.recs[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
UnloadImage(atlas);
|
|
||||||
}
|
|
||||||
else font = GetFontDefault();
|
|
||||||
#else
|
|
||||||
font = GetFontDefault();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
@ -485,6 +470,45 @@ Font LoadFontFromImage(Image image, Color key, int firstChar)
|
|||||||
return font;
|
return font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load font from memory buffer, fileType refers to extension: i.e. "ttf"
|
||||||
|
Font LoadFontFromMemory(const char *fileType, const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount)
|
||||||
|
{
|
||||||
|
Font font = { 0 };
|
||||||
|
|
||||||
|
char fileExtLower[16] = { 0 };
|
||||||
|
strcpy(fileExtLower, TextToLower(fileType));
|
||||||
|
|
||||||
|
#if defined(SUPPORT_FILEFORMAT_TTF)
|
||||||
|
if (TextIsEqual(fileExtLower, "ttf") ||
|
||||||
|
TextIsEqual(fileExtLower, "otf"))
|
||||||
|
{
|
||||||
|
font.baseSize = fontSize;
|
||||||
|
font.charsCount = (charsCount > 0)? charsCount : 95;
|
||||||
|
font.chars = LoadFontData(fileData, dataSize, font.baseSize, fontChars, font.charsCount, FONT_DEFAULT);
|
||||||
|
|
||||||
|
if (font.chars != NULL)
|
||||||
|
{
|
||||||
|
Image atlas = GenImageFontAtlas(font.chars, &font.recs, font.charsCount, font.baseSize, 2, 0);
|
||||||
|
font.texture = LoadTextureFromImage(atlas);
|
||||||
|
|
||||||
|
// Update chars[i].image to use alpha, required to be used on ImageDrawText()
|
||||||
|
for (int i = 0; i < font.charsCount; i++)
|
||||||
|
{
|
||||||
|
UnloadImage(font.chars[i].image);
|
||||||
|
font.chars[i].image = ImageFromImage(atlas, font.recs[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
UnloadImage(atlas);
|
||||||
|
}
|
||||||
|
else font = GetFontDefault();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
font = GetFontDefault();
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return font;
|
||||||
|
}
|
||||||
|
|
||||||
// Load font data for further use
|
// Load font data for further use
|
||||||
// NOTE: Requires TTF font memory data and can generate SDF data
|
// NOTE: Requires TTF font memory data and can generate SDF data
|
||||||
CharInfo *LoadFontData(const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type)
|
CharInfo *LoadFontData(const char *fileData, int dataSize, int fontSize, int *fontChars, int charsCount, int type)
|
||||||
|
Loading…
Reference in New Issue
Block a user