/* * gen_font.c - gera src/plot_font.h: a fonte da tinyblocks-plot (DejaVu Sans * 12 px, com antisserrilhado), renderizada pela tinyblocks-opentype. Assim a * plot desenha texto sem depender de outra library em execucao. * * make -C libraries/tinyblocks-plot font (precisa da opentype compilada) * * Caracteres: ASCII 32..126 e Latin-1 160..255 (acentos do portugues). */ #include #include #include #include #include static void utf8(unsigned cp, char* out) { if (cp < 0x80) { out[0] = (char)cp; out[1] = 0; } else { out[0] = (char)(0xC0 | (cp >> 6)); out[1] = (char)(0x80 | (cp & 0x3F)); out[2] = 0; } } int main(int argc, char** argv) { const char* font = argc > 1 ? argv[1] : tb_opentype_default_font(); int px = argc > 2 ? atoi(argv[2]) : 12; int h = tb_opentype_load(font); if (!h) { fprintf(stderr, "fonte: %s\n", tb_opentype_last_error()); return 1; } double asc, dsc, lh; tb_opentype_metrics(h, px, &asc, &dsc, &lh); unsigned cps[256]; int n = 0; for (unsigned c = 32; c < 127; c++) cps[n++] = c; for (unsigned c = 160; c < 256; c++) cps[n++] = c; int H = 0; printf("/* Gerado por tools/gen_font.c (%s, %d px). Nao editar. */\n", font, px); printf("#ifndef TB_PLOT_FONT_H\n#define TB_PLOT_FONT_H\n#include \n"); size_t total = 0; TinyBlocks_Image* imgs[256]; for (int i = 0; i < n; i++) { char s[4]; utf8(cps[i], s); imgs[i] = tb_opentype_render(h, s, px, 255, 255, 255); if (imgs[i]) { H = tb_image_height(imgs[i]); total += (size_t)tb_image_width(imgs[i]) * H; } } printf("#define PLOT_FONT_H %d\n#define PLOT_FONT_BASE %d\n#define PLOT_FONT_N %d\n", H, (int)(1 + asc + 0.5), n); printf("typedef struct { uint16_t cp; uint8_t w; uint32_t off; } PlotGlyph;\n"); printf("static const PlotGlyph s_glyphs[PLOT_FONT_N] = {\n"); size_t off = 0; for (int i = 0; i < n; i++) { int w = imgs[i] ? tb_image_width(imgs[i]) - 2 : 4; /* sem a folga (o render poe 2 px) */ printf(" {%u,%d,%zu},%s", cps[i], w, off, i % 6 == 5 ? "\n" : ""); off += (size_t)w * H; } printf("};\nstatic const uint8_t s_font[%zu] = {\n", off); size_t k = 0; for (int i = 0; i < n; i++) { int W = imgs[i] ? tb_image_width(imgs[i]) : 6, w = W - 2; for (int y = 0; y < H; y++) for (int x = 0; x < w; x++) { uint8_t r, g, b, a = 0; if (imgs[i]) tb_image_get_pixel(imgs[i], x, y, &r, &g, &b, &a); printf("%d,%s", a, ++k % 24 == 0 ? "\n" : ""); } } printf("};\n#endif\n"); (void)total; return 0; }