Skip to content

Commit eed56a4

Browse files
committed
REVIEWED: LoadFontDataBDF() name and formating
1 parent 0932cd3 commit eed56a4

File tree

1 file changed

+63
-65
lines changed

1 file changed

+63
-65
lines changed

src/rtext.c

+63-65
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ static Font defaultFont = { 0 };
142142
static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
143143
#endif
144144
#if defined(SUPPORT_FILEFORMAT_BDF)
145-
static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int* outFontSize);
145+
static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize);
146146
#endif
147147
static int textLineSpacing = 15; // Text vertical line spacing in pixels
148148

@@ -547,7 +547,7 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
547547
#if defined(SUPPORT_FILEFORMAT_BDF)
548548
if (TextIsEqual(fileExtLower, ".bdf"))
549549
{
550-
font.glyphs = LoadBDFFontData(fileData, dataSize, codepoints, font.glyphCount, &font.baseSize);
550+
font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints, font.glyphCount, &font.baseSize);
551551
}
552552
else
553553
#endif
@@ -1457,25 +1457,33 @@ int TextToInteger(const char *text)
14571457
return value*sign;
14581458
}
14591459

1460+
// Get float value from text
1461+
// NOTE: This function replaces atof() [stdlib.h]
1462+
// WARNING: Only '.' character is understood as decimal point
14601463
float TextToFloat(const char *text)
14611464
{
14621465
float value = 0.0f;
14631466
float sign = 1.0f;
14641467

14651468
if ((text[0] == '+') || (text[0] == '-'))
14661469
{
1467-
if (text[0] == '-') sign = -1;
1470+
if (text[0] == '-') sign = -1.0f;
14681471
text++;
14691472
}
1470-
int i = 0;
1471-
for (; ((text[i] >= '0') && (text[i] <= '9')); ++i) value = value*10.0f + (float)(text[i] - '0');
1472-
if (text[i++] != '.') return value*sign;
1473-
float divisor = 10.0f;
1474-
for (; ((text[i] >= '0') && (text[i] <= '9')); ++i)
1473+
1474+
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10.0f + (float)(text[i] - '0');
1475+
1476+
if (text[i++] != '.') value *= sign;
1477+
else
14751478
{
1476-
value += ((float)(text[i] - '0'))/divisor;
1477-
divisor = divisor*10.0f;
1479+
float divisor = 10.0f;
1480+
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++)
1481+
{
1482+
value += ((float)(text[i] - '0'))/divisor;
1483+
divisor = divisor*10.0f;
1484+
}
14781485
}
1486+
14791487
return value;
14801488
}
14811489

@@ -2275,41 +2283,41 @@ static char HexToInt(char hex) {
22752283

22762284
// Load font data for further use
22772285
// NOTE: Requires BDF font memory data
2278-
static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int* outFontSize)
2286+
static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize)
22792287
{
22802288
#define MAX_BUFFER_SIZE 256
2289+
22812290
char buffer[MAX_BUFFER_SIZE] = { 0 };
22822291

22832292
GlyphInfo *glyphs = NULL;
2284-
22852293
bool genFontChars = false;
22862294

2287-
int totalReadBytes = 0; // Data bytes read (total)
2288-
int readBytes = 0; // Data bytes read (line)
2289-
int readVars = 0; // Variables filled by sscanf()
2295+
int totalReadBytes = 0; // Data bytes read (total)
2296+
int readBytes = 0; // Data bytes read (line)
2297+
int readVars = 0; // Variables filled by sscanf()
22902298

22912299
const char *fileText = (const char*)fileData;
22922300
const char *fileTextPtr = fileText;
22932301

2294-
bool fontMalformed = false; // Is the font malformed
2295-
bool fontStarted = false; // Has font started (STARTFONT)
2296-
int fontBBw = 0; // Font base character bounding box width
2297-
int fontBBh = 0; // Font base character bounding box height
2298-
int fontBBxoff0 = 0; // Font base character bounding box X0 offset
2299-
int fontBByoff0 = 0; // Font base character bounding box Y0 offset
2300-
int fontAscent = 0; // Font ascent
2301-
2302-
bool charStarted = false; // Has character started (STARTCHAR)
2303-
bool charBitmapStarted = false; // Has bitmap data started (BITMAP)
2304-
int charBitmapNextRow = 0; // Y position for the next row of bitmap data
2305-
int charEncoding = -1; // The unicode value of the character (-1 if not set)
2306-
int charBBw = 0; // Character bounding box width
2307-
int charBBh = 0; // Character bounding box height
2308-
int charBBxoff0 = 0; // Character bounding box X0 offset
2309-
int charBByoff0 = 0; // Character bounding box Y0 offset
2310-
int charDWidthX = 0; // Character advance X
2311-
int charDWidthY = 0; // Character advance Y (unused)
2312-
GlyphInfo *charGlyphInfo = NULL; // Pointer to output glyph info (NULL if not set)
2302+
bool fontMalformed = false; // Is the font malformed
2303+
bool fontStarted = false; // Has font started (STARTFONT)
2304+
int fontBBw = 0; // Font base character bounding box width
2305+
int fontBBh = 0; // Font base character bounding box height
2306+
int fontBBxoff0 = 0; // Font base character bounding box X0 offset
2307+
int fontBByoff0 = 0; // Font base character bounding box Y0 offset
2308+
int fontAscent = 0; // Font ascent
2309+
2310+
bool charStarted = false; // Has character started (STARTCHAR)
2311+
bool charBitmapStarted = false; // Has bitmap data started (BITMAP)
2312+
int charBitmapNextRow = 0; // Y position for the next row of bitmap data
2313+
int charEncoding = -1; // The unicode value of the character (-1 if not set)
2314+
int charBBw = 0; // Character bounding box width
2315+
int charBBh = 0; // Character bounding box height
2316+
int charBBxoff0 = 0; // Character bounding box X0 offset
2317+
int charBByoff0 = 0; // Character bounding box Y0 offset
2318+
int charDWidthX = 0; // Character advance X
2319+
int charDWidthY = 0; // Character advance Y (unused)
2320+
GlyphInfo *charGlyphInfo = NULL; // Pointer to output glyph info (NULL if not set)
23132321

23142322
if (fileData == NULL) return glyphs;
23152323

@@ -2333,12 +2341,12 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
23332341
totalReadBytes += (readBytes + 1);
23342342
fileTextPtr += (readBytes + 1);
23352343

2336-
// COMMENT
2344+
// Line: COMMENT
23372345
if (strstr(buffer, "COMMENT") != NULL) continue; // Ignore line
23382346

23392347
if (charStarted)
23402348
{
2341-
// ENDCHAR
2349+
// Line: ENDCHAR
23422350
if (strstr(buffer, "ENDCHAR") != NULL)
23432351
{
23442352
charStarted = false;
@@ -2347,59 +2355,56 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
23472355

23482356
if (charBitmapStarted)
23492357
{
2350-
if (charGlyphInfo != NULL) {
2358+
if (charGlyphInfo != NULL)
2359+
{
23512360
int pixelY = charBitmapNextRow++;
2352-
if (pixelY >= charGlyphInfo->image.height)
2353-
{
2354-
break;
2355-
}
2361+
2362+
if (pixelY >= charGlyphInfo->image.height) break;
2363+
23562364
for (int x = 0; x < readBytes; x++)
23572365
{
23582366
char byte = HexToInt(buffer[x]);
2367+
23592368
for (int bitX = 0; bitX < 4; bitX++)
23602369
{
23612370
int pixelX = ((x * 4) + bitX);
2362-
if (pixelX >= charGlyphInfo->image.width)
2363-
{
2364-
break;
2365-
}
2366-
2367-
if ((byte & (8 >> bitX)) > 0)
2368-
{
2369-
((unsigned char*)charGlyphInfo->image.data)[(pixelY * charGlyphInfo->image.width) + pixelX] = 255;
2370-
}
2371+
2372+
if (pixelX >= charGlyphInfo->image.width) break;
2373+
2374+
if ((byte & (8 >> bitX)) > 0) ((unsigned char*)charGlyphInfo->image.data)[(pixelY * charGlyphInfo->image.width) + pixelX] = 255;
23712375
}
23722376
}
23732377
}
23742378
continue;
23752379
}
23762380

2377-
// ENCODING
2381+
// Line: ENCODING
23782382
if (strstr(buffer, "ENCODING") != NULL)
23792383
{
23802384
readVars = sscanf(buffer, "ENCODING %i", &charEncoding);
23812385
continue;
23822386
}
23832387

2384-
// BBX
2388+
// Line: BBX
23852389
if (strstr(buffer, "BBX") != NULL)
23862390
{
23872391
readVars = sscanf(buffer, "BBX %i %i %i %i", &charBBw, &charBBh, &charBBxoff0, &charBByoff0);
23882392
continue;
23892393
}
23902394

2391-
// DWIDTH
2395+
// Line: DWIDTH
23922396
if (strstr(buffer, "DWIDTH") != NULL)
23932397
{
23942398
readVars = sscanf(buffer, "DWIDTH %i %i", &charDWidthX, &charDWidthY);
23952399
continue;
23962400
}
23972401

2398-
// BITMAP
2402+
// Line: BITMAP
23992403
if (strstr(buffer, "BITMAP") != NULL)
24002404
{
24012405
// Search for glyph index in codepoints
24022406
charGlyphInfo = NULL;
2407+
24032408
for (int codepointIndex = 0; codepointIndex < codepointCount; codepointIndex++)
24042409
{
24052410
if (codepoints[codepointIndex] == charEncoding)
@@ -2432,30 +2437,24 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
24322437
}
24332438
else if (fontStarted)
24342439
{
2435-
// ENDFONT
2440+
// Line: ENDFONT
24362441
if (strstr(buffer, "ENDFONT") != NULL)
24372442
{
24382443
fontStarted = false;
24392444
break;
24402445
}
24412446

2442-
// SIZE
2447+
// Line: SIZE
24432448
if (strstr(buffer, "SIZE") != NULL)
24442449
{
2445-
if (outFontSize != NULL)
2446-
{
2447-
readVars = sscanf(buffer, "SIZE %i", outFontSize);
2448-
}
2450+
if (outFontSize != NULL) readVars = sscanf(buffer, "SIZE %i", outFontSize);
24492451
continue;
24502452
}
24512453

24522454
// PIXEL_SIZE
24532455
if (strstr(buffer, "PIXEL_SIZE") != NULL)
24542456
{
2455-
if (outFontSize != NULL)
2456-
{
2457-
readVars = sscanf(buffer, "PIXEL_SIZE %i", outFontSize);
2458-
}
2457+
if (outFontSize != NULL) readVars = sscanf(buffer, "PIXEL_SIZE %i", outFontSize);
24592458
continue;
24602459
}
24612460

@@ -2520,7 +2519,6 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
25202519

25212520
return glyphs;
25222521
}
2523-
25242522
#endif
25252523

25262524
#endif // SUPPORT_MODULE_RTEXT

0 commit comments

Comments
 (0)