REVIEWED: LoadFontDataBDF()
name and formating
This commit is contained in:
parent
0932cd3059
commit
eed56a45d6
1 changed files with 61 additions and 63 deletions
76
src/rtext.c
76
src/rtext.c
|
@ -142,7 +142,7 @@ static Font defaultFont = { 0 };
|
|||
static Font LoadBMFont(const char *fileName); // Load a BMFont file (AngelCode font file)
|
||||
#endif
|
||||
#if defined(SUPPORT_FILEFORMAT_BDF)
|
||||
static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int* outFontSize);
|
||||
static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize);
|
||||
#endif
|
||||
static int textLineSpacing = 15; // Text vertical line spacing in pixels
|
||||
|
||||
|
@ -547,7 +547,7 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int
|
|||
#if defined(SUPPORT_FILEFORMAT_BDF)
|
||||
if (TextIsEqual(fileExtLower, ".bdf"))
|
||||
{
|
||||
font.glyphs = LoadBDFFontData(fileData, dataSize, codepoints, font.glyphCount, &font.baseSize);
|
||||
font.glyphs = LoadFontDataBDF(fileData, dataSize, codepoints, font.glyphCount, &font.baseSize);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
@ -1457,6 +1457,9 @@ int TextToInteger(const char *text)
|
|||
return value*sign;
|
||||
}
|
||||
|
||||
// Get float value from text
|
||||
// NOTE: This function replaces atof() [stdlib.h]
|
||||
// WARNING: Only '.' character is understood as decimal point
|
||||
float TextToFloat(const char *text)
|
||||
{
|
||||
float value = 0.0f;
|
||||
|
@ -1464,18 +1467,23 @@ float TextToFloat(const char *text)
|
|||
|
||||
if ((text[0] == '+') || (text[0] == '-'))
|
||||
{
|
||||
if (text[0] == '-') sign = -1;
|
||||
if (text[0] == '-') sign = -1.0f;
|
||||
text++;
|
||||
}
|
||||
int i = 0;
|
||||
for (; ((text[i] >= '0') && (text[i] <= '9')); ++i) value = value*10.0f + (float)(text[i] - '0');
|
||||
if (text[i++] != '.') return value*sign;
|
||||
|
||||
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++) value = value*10.0f + (float)(text[i] - '0');
|
||||
|
||||
if (text[i++] != '.') value *= sign;
|
||||
else
|
||||
{
|
||||
float divisor = 10.0f;
|
||||
for (; ((text[i] >= '0') && (text[i] <= '9')); ++i)
|
||||
for (int i = 0; ((text[i] >= '0') && (text[i] <= '9')); i++)
|
||||
{
|
||||
value += ((float)(text[i] - '0'))/divisor;
|
||||
divisor = divisor*10.0f;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
@ -2275,13 +2283,13 @@ static char HexToInt(char hex) {
|
|||
|
||||
// Load font data for further use
|
||||
// NOTE: Requires BDF font memory data
|
||||
static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int* outFontSize)
|
||||
static GlyphInfo *LoadFontDataBDF(const unsigned char *fileData, int dataSize, int *codepoints, int codepointCount, int *outFontSize)
|
||||
{
|
||||
#define MAX_BUFFER_SIZE 256
|
||||
|
||||
char buffer[MAX_BUFFER_SIZE] = { 0 };
|
||||
|
||||
GlyphInfo *glyphs = NULL;
|
||||
|
||||
bool genFontChars = false;
|
||||
|
||||
int totalReadBytes = 0; // Data bytes read (total)
|
||||
|
@ -2333,12 +2341,12 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
|
|||
totalReadBytes += (readBytes + 1);
|
||||
fileTextPtr += (readBytes + 1);
|
||||
|
||||
// COMMENT
|
||||
// Line: COMMENT
|
||||
if (strstr(buffer, "COMMENT") != NULL) continue; // Ignore line
|
||||
|
||||
if (charStarted)
|
||||
{
|
||||
// ENDCHAR
|
||||
// Line: ENDCHAR
|
||||
if (strstr(buffer, "ENDCHAR") != NULL)
|
||||
{
|
||||
charStarted = false;
|
||||
|
@ -2347,59 +2355,56 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
|
|||
|
||||
if (charBitmapStarted)
|
||||
{
|
||||
if (charGlyphInfo != NULL) {
|
||||
int pixelY = charBitmapNextRow++;
|
||||
if (pixelY >= charGlyphInfo->image.height)
|
||||
if (charGlyphInfo != NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
int pixelY = charBitmapNextRow++;
|
||||
|
||||
if (pixelY >= charGlyphInfo->image.height) break;
|
||||
|
||||
for (int x = 0; x < readBytes; x++)
|
||||
{
|
||||
char byte = HexToInt(buffer[x]);
|
||||
|
||||
for (int bitX = 0; bitX < 4; bitX++)
|
||||
{
|
||||
int pixelX = ((x * 4) + bitX);
|
||||
if (pixelX >= charGlyphInfo->image.width)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if ((byte & (8 >> bitX)) > 0)
|
||||
{
|
||||
((unsigned char*)charGlyphInfo->image.data)[(pixelY * charGlyphInfo->image.width) + pixelX] = 255;
|
||||
}
|
||||
if (pixelX >= charGlyphInfo->image.width) break;
|
||||
|
||||
if ((byte & (8 >> bitX)) > 0) ((unsigned char*)charGlyphInfo->image.data)[(pixelY * charGlyphInfo->image.width) + pixelX] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// ENCODING
|
||||
// Line: ENCODING
|
||||
if (strstr(buffer, "ENCODING") != NULL)
|
||||
{
|
||||
readVars = sscanf(buffer, "ENCODING %i", &charEncoding);
|
||||
continue;
|
||||
}
|
||||
|
||||
// BBX
|
||||
// Line: BBX
|
||||
if (strstr(buffer, "BBX") != NULL)
|
||||
{
|
||||
readVars = sscanf(buffer, "BBX %i %i %i %i", &charBBw, &charBBh, &charBBxoff0, &charBByoff0);
|
||||
continue;
|
||||
}
|
||||
|
||||
// DWIDTH
|
||||
// Line: DWIDTH
|
||||
if (strstr(buffer, "DWIDTH") != NULL)
|
||||
{
|
||||
readVars = sscanf(buffer, "DWIDTH %i %i", &charDWidthX, &charDWidthY);
|
||||
continue;
|
||||
}
|
||||
|
||||
// BITMAP
|
||||
// Line: BITMAP
|
||||
if (strstr(buffer, "BITMAP") != NULL)
|
||||
{
|
||||
// Search for glyph index in codepoints
|
||||
charGlyphInfo = NULL;
|
||||
|
||||
for (int codepointIndex = 0; codepointIndex < codepointCount; codepointIndex++)
|
||||
{
|
||||
if (codepoints[codepointIndex] == charEncoding)
|
||||
|
@ -2432,30 +2437,24 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
|
|||
}
|
||||
else if (fontStarted)
|
||||
{
|
||||
// ENDFONT
|
||||
// Line: ENDFONT
|
||||
if (strstr(buffer, "ENDFONT") != NULL)
|
||||
{
|
||||
fontStarted = false;
|
||||
break;
|
||||
}
|
||||
|
||||
// SIZE
|
||||
// Line: SIZE
|
||||
if (strstr(buffer, "SIZE") != NULL)
|
||||
{
|
||||
if (outFontSize != NULL)
|
||||
{
|
||||
readVars = sscanf(buffer, "SIZE %i", outFontSize);
|
||||
}
|
||||
if (outFontSize != NULL) readVars = sscanf(buffer, "SIZE %i", outFontSize);
|
||||
continue;
|
||||
}
|
||||
|
||||
// PIXEL_SIZE
|
||||
if (strstr(buffer, "PIXEL_SIZE") != NULL)
|
||||
{
|
||||
if (outFontSize != NULL)
|
||||
{
|
||||
readVars = sscanf(buffer, "PIXEL_SIZE %i", outFontSize);
|
||||
}
|
||||
if (outFontSize != NULL) readVars = sscanf(buffer, "PIXEL_SIZE %i", outFontSize);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
@ -2520,7 +2519,6 @@ static GlyphInfo *LoadBDFFontData(const unsigned char *fileData, int dataSize, i
|
|||
|
||||
return glyphs;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif // SUPPORT_MODULE_RTEXT
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue