Update rtext.c

Changed do-while to while loop, and also added an extra step to calculate maximum glyph width and excluding it from image width for extra safety.
This commit is contained in:
Hanaxar 2023-03-21 01:45:35 +03:00 committed by GitHub
parent c2e1e38009
commit e65a0d460b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -695,16 +695,18 @@ Image GenImageFontAtlas(const GlyphInfo *chars, Rectangle **charRecs, int glyphC
Rectangle *recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle)); Rectangle *recs = (Rectangle *)RL_MALLOC(glyphCount*sizeof(Rectangle));
// Calculate image size based on total glyph width and glyph row count // Calculate image size based on total glyph width and glyph row count
int totalWidth = 0; int totalWidth = 0, maxGlyphWidth = 0;
for (int i = 0; i < glyphCount; i++) totalWidth += chars[i].image.width + 2*padding; for (int i = 0; i < glyphCount; i++)
int imageSize = 64; // A minimum starting value to avoid unnecessary calculation steps for very small images {
int rowCount = 0; if (chars[i].image.width > maxGlyphWidth) maxGlyphWidth = chars[i].image.width;
do totalWidth += chars[i].image.width + 2*padding;
}
int rowCount = 0, imageSize = 64; // A minimum starting value to avoid unnecessary calculation steps for very small images
while (totalWidth > (imageSize - maxGlyphWidth)*rowCount) // maxGlyphWidth is maximum possible space left at the end of row
{ {
imageSize *= 2; // Double the size of image (to keep POT) imageSize *= 2; // Double the size of image (to keep POT)
rowCount = imageSize/(fontSize + 2*padding); // Calculate new row count for the new image size rowCount = imageSize/(fontSize + 2*padding); // 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