Calculate exact image size in GenImageFontAtlas
Calculate exact image size with a method based on total glyph width and glyph row count Current method seemed a little bit overkill with square root, log and power functions and only approximates image size which can be wonky with some weird fonts like cursive fonts. Proposed method calculates image size directly with a simpler method and results exact image size needed.
This commit is contained in:
parent
40d01162b9
commit
c819af1d40
19
src/rtext.c
19
src/rtext.c
|
|
@ -694,14 +694,17 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC
|
||||||
// NOTE: Rectangles memory is loaded here!
|
// NOTE: Rectangles memory is loaded here!
|
||||||
Rectangle *recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
|
Rectangle *recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
|
||||||
|
|
||||||
// Calculate image size based on required pixel area
|
// Calculate image size based on total glyph width and glyph row count
|
||||||
// NOTE 1: Image is forced to be squared and POT... very conservative!
|
int totalWidth = 0;
|
||||||
// NOTE 2: SDF font characters already contain an internal padding,
|
for (int i = 0; i < glyphCount; i++) totalWidth += chars[i].image.width + 2*padding;
|
||||||
// so image size would result bigger than default font type
|
int imageSize = 64; // A minimum starting value to avoid unnecessary calculation steps for very small images
|
||||||
float requiredArea = 0;
|
int rowCount = 0;
|
||||||
for (int i = 0; i < glyphCount; i++) requiredArea += ((chars[i].image.width + 2*padding)*(fontSize + 2*padding));
|
do
|
||||||
float guessSize = sqrtf(requiredArea)*1.4f;
|
{
|
||||||
int imageSize = (int)powf(2, ceilf(logf((float)guessSize)/logf(2))); // Calculate next POT
|
imageSize *= 2; // Double the size of image (to keep POT)
|
||||||
|
rowCount = imageSize/(fontSize + 2*padding) + 1; // Calculate new row count for the new image size
|
||||||
|
}
|
||||||
|
while (totalWidth > imageSize*rowCount);
|
||||||
|
|
||||||
atlas.width = imageSize; // Atlas bitmap width
|
atlas.width = imageSize; // Atlas bitmap width
|
||||||
atlas.height = imageSize; // Atlas bitmap height
|
atlas.height = imageSize; // Atlas bitmap height
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user