diff options
| author | Ray <[email protected]> | 2020-09-13 16:42:31 +0200 |
|---|---|---|
| committer | Ray <[email protected]> | 2020-09-13 16:42:31 +0200 |
| commit | db652daf42971ae737bebe51947dc31f15046bdc (patch) | |
| tree | 7b9dc668ac7e405c13efb3999b6a22b0be414b8c /src/text.c | |
| parent | 88c5deac8777ed9176de344cd09672618ec99070 (diff) | |
| download | raylib-db652daf42971ae737bebe51947dc31f15046bdc.tar.gz raylib-db652daf42971ae737bebe51947dc31f15046bdc.zip | |
ADDED: LoadFontFromMemory() (TTF only) #1327
Diffstat (limited to 'src/text.c')
| -rw-r--r-- | src/text.c | 72 |
1 files changed, 48 insertions, 24 deletions
@@ -335,30 +335,15 @@ Font LoadFont(const char *fileName) Font LoadFontEx(const char *fileName, int fontSize, int *fontChars, int charsCount) { Font font = { 0 }; - -#if defined(SUPPORT_FILEFORMAT_TTF) - font.baseSize = fontSize; - font.charsCount = (charsCount > 0)? charsCount : 95; - font.chars = LoadFontData(fileName, 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 + + // Loading file to memory + unsigned int fileSize = 0; + unsigned char *fileData = LoadFileData(fileName, &fileSize); + + // Loading font from memory data + font = LoadFontFromMemory(GetFileExtension(fileName), (char *)fileData, fileSize, fontSize, fontChars, charsCount); + + RL_FREE(fileData); return font; } @@ -485,6 +470,45 @@ Font LoadFontFromImage(Image image, Color key, int firstChar) 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 // 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) |
