Skip to content

Instantly share code, notes, and snippets.

@TempleProgramming
Last active July 29, 2021 06:40
Show Gist options
  • Select an option

  • Save TempleProgramming/77784e4051c25b63f0f5c250640a6460 to your computer and use it in GitHub Desktop.

Select an option

Save TempleProgramming/77784e4051c25b63f0f5c250640a6460 to your computer and use it in GitHub Desktop.
ZealOS BMP Font Sheet Importer
// Tom please free the CBGR24 class name
class CBGR
{
U8 b;
U8 g;
U8 r;
};
class CBMP
{
U8 version[2];
U32 size;
U32 pad;
U32 offset;
U32 info_size;
U32 w;
U32 h;
U16 planes;
U16 px_bits;
U32 compression;
U32 imageSize;
U32 x_px_per_m;
U32 y_px_per_m;
U32 colors;
U32 important_colors;
CBGR pixels[0];
};
U0 FontImportBMP(U64 *font, U8 *bmp_fname)
{//Imports font from 128x128 black on white BMP file
U8 *buf = FileRead(bmp_fname);
// Map locations in buffer.
CBMP *bmp = buf;
if (bmp->version[0] != 'B' && bmp->version[1] != 'M')
{
"[ERROR] %s IS NOT A VALID BMP FILE!\n", bmp_fname;
return;
}
if (bmp->px_bits != 24 || bmp->compression != 0)
{
"[ERROR] %s IS NOT A 24-BIT UNCOMPRESSED BMP!\n", bmp_fname;
return;
}
I64 r, c, // Character row/column on font sheet
gx, gy, // Glyph x/y
x, y; // x/y on font sheet
MemSetI64(font, 0, 256);
for (r = 0; r < 16; r++)
{
for (c = 0; c < 16; c++)
{
for (y = 0; y < 8; y++)
{
for (x = 0; x < 8; x++)
{
gx = c * 8 + x;
gy = 127 - (r * 8 + y);
if (bmp->pixels[gx + gy * 128].r < 127)
font[c + r * 16].u8[y] |= 1 << x;
}
}
}
}
}
U0 FontWrite(U64 *font, U8 *font_name, U8 *fname)
{//Appends font hex to file
CDoc *doc;
if (FileFind(fname))
doc = DocRead(fname);
else
doc = DocNew(fname);
DocBottom(doc);
DocPrint(doc, "\npublic U64 %s[256] = {\n", font_name);
I64 i;
for (i = 0; i < 256; i++)
{
DocPrint(doc, "0x%016X,\n", font[i]);
}
DocPrint(doc, "};");
DocWrite(doc);
}
// Example usage
/*
Cd(__DIR__);;
I64 *font_regular = MAlloc(sizeof(I64) * 256);
I64 *font_bold = MAlloc(sizeof(I64) * 256);
I64 *font_italic = MAlloc(sizeof(I64) * 256);
FontImportBMP(font_regular, "ZealFont_Regular_V1.bmp");
FontImportBMP(font_bold, "ZealFont_Bold_V1.bmp");
FontImportBMP(font_italic, "ZealFont_Italic_V1.bmp");
//text.font = font_bold;
FontWrite(font_regular, "sys_font_regular", "Font.CC");
FontWrite(font_bold, "sys_font_bold", "Font.CC");
FontWrite(font_italic, "sys_font_italic", "Font.CC");
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment