Skip to content

Instantly share code, notes, and snippets.

@klange

klange/bake.c Secret

Created May 10, 2022 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klange/dbcbcb78eb3300be5b3b590f07ea8a87 to your computer and use it in GitHub Desktop.
Save klange/dbcbcb78eb3300be5b3b590f07ea8a87 to your computer and use it in GitHub Desktop.
#include <ft2build.h>
#include FT_FREETYPE_H
static unsigned long encode(unsigned char c) {
switch (c) {
case 0: return L' ';
case 1: return L'☺';
case 2: return L'☻';
case 3: return L'♥';
case 4: return L'♦';
case 5: return L'♣';
case 6: return L'♠';
case 7: return L'•';
case 8: return L'◘';
case 9: return L'○';
case 10: return L'◙';
case 11: return L'♂';
case 12: return L'♀';
case 13: return L'♪';
case 14: return L'♫';
case 15: return L'☼';
case 16: return L'►';
case 17: return L'◄';
case 18: return L'↕';
case 19: return L'‼';
case 20: return L'¶';
case 21: return L'§';
case 22: return L'▬';
case 23: return L'↨';
case 24: return L'↑';
case 25: return L'↓';
case 26: return L'→';
case 27: return L'←';
case 28: return L'∟';
case 29: return L'↔';
case 30: return L'▲';
case 31: return L'▼';
/* ASCII text */
case 127: return L'⌂';
case 128: return L'Ç';
case 129: return L'ü';
case 130: return L'é';
case 131: return L'â';
case 132: return L'ä';
case 133: return L'à';
case 134: return L'å';
case 135: return L'ç';
case 136: return L'ê';
case 137: return L'ë';
case 138: return L'è';
case 139: return L'ï';
case 140: return L'î';
case 141: return L'ì';
case 142: return L'Ä';
case 143: return L'Å';
case 144: return L'É';
case 145: return L'æ';
case 146: return L'Æ';
case 147: return L'ô';
case 148: return L'ö';
case 149: return L'ò';
case 150: return L'û';
case 151: return L'ù';
case 152: return L'ÿ';
case 153: return L'Ö';
case 154: return L'Ü';
case 155: return L'¢';
case 156: return L'£';
case 157: return L'¥';
case 158: return L'₧';
case 159: return L'ƒ';
case 160: return L'á';
case 161: return L'í';
case 162: return L'ó';
case 163: return L'ú';
case 164: return L'ñ';
case 165: return L'Ñ';
case 166: return L'ª';
case 167: return L'º';
case 168: return L'¿';
case 169: return L'⌐';
case 170: return L'¬';
case 171: return L'½';
case 172: return L'¼';
case 173: return L'¡';
case 174: return L'«';
case 175: return L'»';
case 176: return L'░';
case 177: return L'▒';
case 178: return L'▓';
case 179: return L'│';
case 180: return L'┤';
case 181: return L'╡';
case 182: return L'╢';
case 183: return L'╖';
case 184: return L'╕';
case 185: return L'╣';
case 186: return L'║';
case 187: return L'╗';
case 188: return L'╝';
case 189: return L'╜';
case 190: return L'╛';
case 191: return L'┐';
case 192: return L'└';
case 193: return L'┴';
case 194: return L'┬';
case 195: return L'├';
case 196: return L'─';
case 197: return L'┼';
case 198: return L'╞';
case 199: return L'╟';
case 200: return L'╚';
case 201: return L'╔';
case 202: return L'╩';
case 203: return L'╦';
case 204: return L'╠';
case 205: return L'═';
case 206: return L'╬';
case 207: return L'╧';
case 208: return L'╨';
case 209: return L'╤';
case 210: return L'╥';
case 211: return L'╙';
case 212: return L'╘';
case 213: return L'╒';
case 214: return L'╓';
case 215: return L'╫';
case 216: return L'╪';
case 217: return L'┘';
case 218: return L'┌';
case 219: return L'█';
case 220: return L'▄';
case 221: return L'▌';
case 222: return L'▐';
case 223: return L'▀';
case 224: return L'α';
case 225: return L'ß';
case 226: return L'Γ';
case 227: return L'π';
case 228: return L'Σ';
case 229: return L'σ';
case 230: return L'µ';
case 231: return L'τ';
case 232: return L'Φ';
case 233: return L'Θ';
case 234: return L'Ω';
case 235: return L'δ';
case 236: return L'∞';
case 237: return L'φ';
case 238: return L'ε';
case 239: return L'∩';
case 240: return L'≡';
case 241: return L'±';
case 242: return L'≥';
case 243: return L'≤';
case 244: return L'⌠';
case 245: return L'⌡';
case 246: return L'÷';
case 247: return L'≈';
case 248: return L'°';
case 249: return L'∙';
case 250: return L'·';
case 251: return L'√';
case 252: return L'ⁿ';
case 253: return L'²';
case 254: return L'■';
case 255: return L' ';
default: return c;
}
}
int main(int argc, char * argv[]) {
FT_Library library;
FT_Face face;
FT_Init_FreeType(&library);
if (FT_New_Face(library, "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",0,&face)) {
fprintf(stderr, "oops\n");
}
FT_Set_Pixel_Sizes(face, 0, 13);
fprintf(stdout, "#define LARGE_FONT_CELL_WIDTH 8\n");
fprintf(stdout, "#define LARGE_FONT_CELL_HEIGHT 17\n");
fprintf(stdout, "#define LARGE_FONT_MASK 7\n");
fprintf(stdout, "uint16_t large_font[][17] = {\n");
for (unsigned long c = 0; c < 256; ++c) {
fprintf(stdout, "\t/* %lu %lu */\n", c, encode(c));
fprintf(stdout, "\t{\n");
/* Get glyph */
int glyph_index = FT_Get_Char_Index(face, encode(c));
FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME | FT_LOAD_TARGET_MONO);
FT_Render_Glyph(face->glyph, FT_RENDER_MODE_MONO);
unsigned char rows[17] = {0};
for (int y = 0; y < face->glyph->bitmap.rows; ++y) {
for (int x = 0; x < face->glyph->bitmap.width; ++x) {
unsigned int off = face->glyph->bitmap.pitch * y + x / 8;
unsigned int shift = 7 - (x & 7);
int bit = (face->glyph->bitmap.buffer[off] >> shift) & 1;
int _y = 13 - face->glyph->bitmap_top + y;
int _x = face->glyph->bitmap_left + x;
if (_y < 0 || _x < 0 || _y >= 17 || _x >= 17) {
continue;
}
if (bit) {
rows[_y] |= (1 << (7 - (_x)));
}
}
}
for (int i = 0; i < 17; ++i) {
fprintf(stdout, "\t\t/* ");
for (int j = 0; j < 8; ++j) {
if (rows[i] & (1 << (7 - j))) {
fprintf(stdout, "#");
} else {
fprintf(stdout, " ");
}
}
fprintf(stdout," */\n");
}
for (int i = 0; i < 17; ++i) {
fprintf(stdout, "\t\t0x%02x,\n", rows[i]);
}
fprintf(stdout, "\t},\n");
}
fprintf(stdout, "};\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment