Added a new example a fix to an issue with font loading when not counting the whitespace

This commit is contained in:
Luca Sas 2020-04-02 13:45:39 +01:00
parent 7ef114d1da
commit 87a6fa34a2
2 changed files with 29 additions and 5 deletions

View File

@ -0,0 +1,24 @@
#include "raylib.h"
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - font custom characters");
SetTargetFPS(60);
int chars[126 - 33];
for (int i = 0; i < 126 - 33; i++) chars[i] = 33 + i;
Font font = LoadFontEx("resources/KAISG.ttf", 64, chars, 126 - 33);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(WHITE);
DrawTexture(font.texture, 0, 0, BLACK);
EndDrawing();
}
}

View File

@ -613,7 +613,7 @@ Image GenImageFontAtlas(const CharInfo *chars, Rectangle **charRecs, int charsCo
// NOTE 2: SDF font characters already contain an internal padding, // NOTE 2: SDF font characters already contain an internal padding,
// so image size would result bigger than default font type // so image size would result bigger than default font type
float requiredArea = 0; float requiredArea = 0;
for (int i = 0; i < charsCount; i++) requiredArea += ((chars[i].image.width + 2*padding)*(chars[i].image.height + 2*padding)); for (int i = 0; i < charsCount; i++) requiredArea += ((chars[i].image.width + padding)*(chars[i].image.height + padding));
float guessSize = sqrtf(requiredArea)*1.3f; float guessSize = sqrtf(requiredArea)*1.3f;
int imageSize = (int)powf(2, ceilf(logf((float)guessSize)/logf(2))); // Calculate next POT int imageSize = (int)powf(2, ceilf(logf((float)guessSize)/logf(2))); // Calculate next POT
@ -650,7 +650,7 @@ Image GenImageFontAtlas(const CharInfo *chars, Rectangle **charRecs, int charsCo
recs[i].height = (float)chars[i].image.height; recs[i].height = (float)chars[i].image.height;
// Move atlas position X for next character drawing // Move atlas position X for next character drawing
offsetX += (chars[i].image.width + 2*padding); offsetX += (chars[i].image.width + padding);
if (offsetX >= (atlas.width - chars[i].image.width - padding)) if (offsetX >= (atlas.width - chars[i].image.width - padding))
{ {
@ -659,7 +659,7 @@ Image GenImageFontAtlas(const CharInfo *chars, Rectangle **charRecs, int charsCo
// NOTE: Be careful on offsetY for SDF fonts, by default SDF // NOTE: Be careful on offsetY for SDF fonts, by default SDF
// use an internal padding of 4 pixels, it means char rectangle // use an internal padding of 4 pixels, it means char rectangle
// height is bigger than fontSize, it could be up to (fontSize + 8) // height is bigger than fontSize, it could be up to (fontSize + 8)
offsetY += (fontSize + 2*padding); offsetY += (fontSize + padding);
if (offsetY > (atlas.height - fontSize - padding)) break; if (offsetY > (atlas.height - fontSize - padding)) break;
} }
@ -677,8 +677,8 @@ Image GenImageFontAtlas(const CharInfo *chars, Rectangle **charRecs, int charsCo
for (int i = 0; i < charsCount; i++) for (int i = 0; i < charsCount; i++)
{ {
rects[i].id = i; rects[i].id = i;
rects[i].w = chars[i].image.width + 2*padding; rects[i].w = chars[i].image.width + padding;
rects[i].h = chars[i].image.height + 2*padding; rects[i].h = chars[i].image.height + padding;
} }
// Package rectangles into atlas // Package rectangles into atlas