aboutsummaryrefslogtreecommitdiff
path: root/raylib/src/rtext.c
diff options
context:
space:
mode:
authorUneven Prankster <unevenprankster@protonmail.com>2023-11-16 21:12:27 -0300
committerUneven Prankster <unevenprankster@protonmail.com>2023-11-16 21:12:27 -0300
commit2bbf92ad5ae7708bf18ac7ef333e9a979d8d1bde (patch)
treec9d22bb0d73d9cc0c8586e4d31c93a561ea8e910 /raylib/src/rtext.c
parent1c0cc775732201f4c4d3ee0d6772be786b3b4aa1 (diff)
Working so hard like a soldiermain
Can't afford a thing on TV
Diffstat (limited to 'raylib/src/rtext.c')
-rw-r--r--raylib/src/rtext.c79
1 files changed, 44 insertions, 35 deletions
diff --git a/raylib/src/rtext.c b/raylib/src/rtext.c
index 2ce21e5..dd7c83f 100644
--- a/raylib/src/rtext.c
+++ b/raylib/src/rtext.c
@@ -322,7 +322,7 @@ Font LoadFont(const char *fileName)
#define FONT_TTF_DEFAULT_FIRST_CHAR 32 // TTF font generation default first char for image sprite font (32-Space)
#endif
#ifndef FONT_TTF_DEFAULT_CHARS_PADDING
- #define FONT_TTF_DEFAULT_CHARS_PADDING 10 // TTF font generation default chars padding
+ #define FONT_TTF_DEFAULT_CHARS_PADDING 4 // TTF font generation default chars padding
#endif
Font font = { 0 };
@@ -716,7 +716,7 @@ Image GenImageFontAtlas(const GlyphInfo *glyphs, Rectangle **glyphRecs, int glyp
totalWidth += glyphs[i].image.width + 4*padding;
}
-#define SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE
+//#define SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE
#if defined(SUPPORT_FONT_ATLAS_SIZE_CONSERVATIVE)
int rowCount = 0;
int imageSize = 64; // Define minimum starting value to avoid unnecessary calculation steps for very small images
@@ -1344,10 +1344,12 @@ Rectangle GetGlyphAtlasRec(Font font, int codepoint)
// Get text length in bytes, check for \0 character
unsigned int TextLength(const char *text)
{
- unsigned int length = 0; //strlen(text)
+ unsigned int length = 0;
if (text != NULL)
{
+ // NOTE: Alternative: use strlen(text)
+
while (*text++) length++;
}
@@ -1415,6 +1417,8 @@ int TextCopy(char *dst, const char *src)
if ((src != NULL) && (dst != NULL))
{
+ // NOTE: Alternative: use strcpy(dst, src)
+
while (*src != '\0')
{
*dst = *src;
@@ -1460,6 +1464,8 @@ const char *TextSubtext(const char *text, int position, int length)
if (length >= textLength) length = textLength;
+ // NOTE: Alternative: memcpy(buffer, text + position, length)
+
for (int c = 0 ; c < length ; c++)
{
*(buffer + c) = *(text + position);
@@ -1996,7 +2002,6 @@ int GetCodepointPrevious(const char *text, int *codepointSize)
// Module specific Functions Definition
//----------------------------------------------------------------------------------
#if defined(SUPPORT_FILEFORMAT_FNT)
-
// Read a line from memory
// REQUIRES: memcpy()
// NOTE: Returns the number of bytes read
@@ -2026,7 +2031,9 @@ static Font LoadBMFont(const char *fileName)
int imHeight = 0;
char imFileName[129] = { 0 };
- int base = 0; // Useless data
+ int base = 0; // Useless data
+ int readBytes = 0; // Data bytes read
+ int readVars = 0; // Variables filled by sscanf()
char *fileText = LoadFileText(fileName);
@@ -2035,32 +2042,30 @@ static Font LoadBMFont(const char *fileName)
char *fileTextPtr = fileText;
// NOTE: We skip first line, it contains no useful information
- int lineBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
- fileTextPtr += (lineBytes + 1);
+ readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
+ fileTextPtr += (readBytes + 1);
// Read line data
- lineBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
+ readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
searchPoint = strstr(buffer, "lineHeight");
- sscanf(searchPoint, "lineHeight=%i base=%i scaleW=%i scaleH=%i", &fontSize, &base, &imWidth, &imHeight);
- fileTextPtr += (lineBytes + 1);
+ readVars = sscanf(searchPoint, "lineHeight=%i base=%i scaleW=%i scaleH=%i", &fontSize, &base, &imWidth, &imHeight);
+ fileTextPtr += (readBytes + 1);
+
+ if (readVars < 4) { UnloadFileText(fileText); return font; } // Some data not available, file malformed
- TRACELOGD("FONT: [%s] Loaded font info:", fileName);
- TRACELOGD(" > Base size: %i", fontSize);
- TRACELOGD(" > Texture scale: %ix%i", imWidth, imHeight);
-
- lineBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
+ readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
searchPoint = strstr(buffer, "file");
- sscanf(searchPoint, "file=\"%128[^\"]\"", imFileName);
- fileTextPtr += (lineBytes + 1);
+ readVars = sscanf(searchPoint, "file=\"%128[^\"]\"", imFileName);
+ fileTextPtr += (readBytes + 1);
- TRACELOGD(" > Texture filename: %s", imFileName);
+ if (readVars < 1) { UnloadFileText(fileText); return font; } // No fileName read
- lineBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
+ readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
searchPoint = strstr(buffer, "count");
- sscanf(searchPoint, "count=%i", &glyphCount);
- fileTextPtr += (lineBytes + 1);
+ readVars = sscanf(searchPoint, "count=%i", &glyphCount);
+ fileTextPtr += (readBytes + 1);
- TRACELOGD(" > Chars count: %i", glyphCount);
+ if (readVars < 1) { UnloadFileText(fileText); return font; } // No glyphCount read
// Compose correct path using route of .fnt file (fileName) and imFileName
char *imPath = NULL;
@@ -2118,22 +2123,26 @@ static Font LoadBMFont(const char *fileName)
for (int i = 0; i < glyphCount; i++)
{
- lineBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
- sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i",
+ readBytes = GetLine(fileTextPtr, buffer, MAX_BUFFER_SIZE);
+ readVars = sscanf(buffer, "char id=%i x=%i y=%i width=%i height=%i xoffset=%i yoffset=%i xadvance=%i",
&charId, &charX, &charY, &charWidth, &charHeight, &charOffsetX, &charOffsetY, &charAdvanceX);
- fileTextPtr += (lineBytes + 1);
-
- // Get character rectangle in the font atlas texture
- font.recs[i] = (Rectangle){ (float)charX, (float)charY, (float)charWidth, (float)charHeight };
+ fileTextPtr += (readBytes + 1);
+
+ if (readVars == 8) // Make sure all char data has been properly read
+ {
+ // Get character rectangle in the font atlas texture
+ font.recs[i] = (Rectangle){ (float)charX, (float)charY, (float)charWidth, (float)charHeight };
- // Save data properly in sprite font
- font.glyphs[i].value = charId;
- font.glyphs[i].offsetX = charOffsetX;
- font.glyphs[i].offsetY = charOffsetY;
- font.glyphs[i].advanceX = charAdvanceX;
+ // Save data properly in sprite font
+ font.glyphs[i].value = charId;
+ font.glyphs[i].offsetX = charOffsetX;
+ font.glyphs[i].offsetY = charOffsetY;
+ font.glyphs[i].advanceX = charAdvanceX;
- // Fill character image data from imFont data
- font.glyphs[i].image = ImageFromImage(imFont, font.recs[i]);
+ // Fill character image data from imFont data
+ font.glyphs[i].image = ImageFromImage(imFont, font.recs[i]);
+ }
+ else TRACELOG(LOG_WARNING, "FONT: [%s] Some characters data not correctly provided", fileName);
}
UnloadImage(imFont);