From 0a1172b75f571685c264a8b9d8ee224bbf11381f Mon Sep 17 00:00:00 2001 From: xolatile Date: Wed, 6 Aug 2025 22:54:55 +0200 Subject: Please do not hate me, it makes sense... --- src/shared/cube2font.c | 192 ++++++++++++++++++++++++------------------------- 1 file changed, 96 insertions(+), 96 deletions(-) (limited to 'src/shared/cube2font.c') diff --git a/src/shared/cube2font.c b/src/shared/cube2font.c index 75ea8ff..bbd8878 100644 --- a/src/shared/cube2font.c +++ b/src/shared/cube2font.c @@ -16,8 +16,8 @@ typedef unsigned int uint; int imin(int a, int b) { return a < b ? a : b; } int imax(int a, int b) { return a > b ? a : b; } -void fatal(const char *fmt, ...) -{ +void fatal(const char *fmt, ...) { + va_list v; va_start(v, fmt); vfprintf(stderr, fmt, v); @@ -27,20 +27,20 @@ void fatal(const char *fmt, ...) exit(EXIT_FAILURE); } -uint bigswap(uint n) -{ +uint bigswap(uint n) { + const int islittleendian = 1; return *(const uchar *)&islittleendian ? (n<<24) | (n>>24) | ((n>>8)&0xFF00) | ((n<<8)&0xFF0000) : n; } -size_t writebig(FILE *f, uint n) -{ +size_t writebig(FILE *f, uint n) { + n = bigswap(n); return fwrite(&n, 1, sizeof(n), f); } -void writepngchunk(FILE *f, const char *type, uchar *data, uint len) -{ +void writepngchunk(FILE *f, const char *type, uchar *data, uint len) { + uint crc; writebig(f, len); fwrite(type, 1, 4, f); @@ -52,14 +52,14 @@ void writepngchunk(FILE *f, const char *type, uchar *data, uint len) writebig(f, crc); } -struct pngihdr -{ +struct pngihdr { + uint width, height; uchar bitdepth, colortype, compress, filter, interlace; }; -void savepng(const char *filename, uchar *data, int w, int h, int bpp, int flip) -{ +void savepng(const char *filename, uchar *data, int w, int h, int bpp, int flip) { + const uchar signature[] = { 137, 80, 78, 71, 13, 10, 26, 10 }; struct pngihdr ihdr; FILE *f; @@ -73,8 +73,8 @@ void savepng(const char *filename, uchar *data, int w, int h, int bpp, int flip) ihdr.width = bigswap(w); ihdr.height = bigswap(h); ihdr.bitdepth = 8; - switch(bpp) - { + switch(bpp) { + case 1: ihdr.colortype = 0; break; case 2: ihdr.colortype = 4; break; case 3: ihdr.colortype = 2; break; @@ -104,15 +104,15 @@ void savepng(const char *filename, uchar *data, int w, int h, int bpp, int flip) z.next_out = (Bytef *)buf; z.avail_out = sizeof(buf); - for(i = 0; i < h; i++) - { + for(i = 0; i < h; i++) { + uchar filter = 0; - for(j = 0; j < 2; j++) - { + for(j = 0; j < 2; j++) { + z.next_in = j ? (Bytef *)data + (flip ? h-i-1 : i)*w*bpp : (Bytef *)&filter; z.avail_in = j ? w*bpp : 1; - while(z.avail_in > 0) - { + while(z.avail_in > 0) { + if(deflate(&z, Z_NO_FLUSH) != Z_OK) goto cleanuperror; #define FLUSHZ do { \ int flush = sizeof(buf) - z.avail_out; \ @@ -127,8 +127,8 @@ void savepng(const char *filename, uchar *data, int w, int h, int bpp, int flip) } } - for(;;) - { + for(;;) { + int err = deflate(&z, Z_FINISH); if(err != Z_OK && err != Z_STREAM_END) goto cleanuperror; FLUSHZ; @@ -156,8 +156,8 @@ error: fatal("cube2font: failed saving PNG to %s", filename); } -enum -{ +enum { + CT_PRINT = 1<<0, CT_SPACE = 1<<1, CT_DIGIT = 1<<2, @@ -183,8 +183,8 @@ enum U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U, \ U, U, U, U, u, u, u, u, u, u, u, u, u, u, u, u, \ u, u, u, u, u, u, u, u, u, u, u, u, u, u, U, u -const uchar cubectype[256] = -{ +const uchar cubectype[256] = { + CUBECTYPE(CT_SPACE, CT_PRINT, CT_PRINT|CT_DIGIT, @@ -199,8 +199,8 @@ int iscubealpha(uchar c) { return cubectype[c]&CT_ALPHA; } int iscubealnum(uchar c) { return cubectype[c]&(CT_ALPHA|CT_DIGIT); } int iscubelower(uchar c) { return cubectype[c]&CT_LOWER; } int iscubeupper(uchar c) { return cubectype[c]&CT_UPPER; } -const int cube2unichars[256] = -{ +const int cube2unichars[256] = { + 0, 192, 193, 194, 195, 196, 197, 198, 199, 9, 10, 11, 12, 13, 200, 201, 202, 203, 204, 205, 206, 207, 209, 210, 211, 212, 213, 214, 216, 217, 218, 219, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, @@ -218,13 +218,13 @@ const int cube2unichars[256] = 0x42C, 0x42D, 0x42E, 0x42F, 0x431, 0x432, 0x433, 0x434, 0x436, 0x437, 0x438, 0x439, 0x43A, 0x43B, 0x43C, 0x43D, 0x43F, 0x442, 0x444, 0x446, 0x447, 0x448, 0x449, 0x44A, 0x44B, 0x44C, 0x44D, 0x44E, 0x44F, 0x454, 0x490, 0x491 }; -int cube2uni(uchar c) -{ +int cube2uni(uchar c) { + return cube2unichars[c]; } -const char *encodeutf8(int uni) -{ +const char *encodeutf8(int uni) { + static char buf[7]; char *dst = buf; if(uni <= 0x7F) { *dst++ = uni; goto uni1; } @@ -247,41 +247,41 @@ struct fontchar { int code, uni, tex, x, y, w, h, offx, offy, offset, advance; F const char *texdir = ""; -const char *texfilename(const char *name, int texnum) -{ +const char *texfilename(const char *name, int texnum) { + static char file[256]; snprintf(file, sizeof(file), "%s%d.png", name, texnum); return file; } -const char *texname(const char *name, int texnum) -{ +const char *texname(const char *name, int texnum) { + static char file[512]; snprintf(file, sizeof(file), "%s%s", texdir, texfilename(name, texnum)); return file; } -void writetexs(const char *name, struct fontchar *chars, int numchars, int numtexs, int tw, int th) -{ +void writetexs(const char *name, struct fontchar *chars, int numchars, int numtexs, int tw, int th) { + int tex; uchar *pixels = (uchar *)malloc(tw*th*2); if(!pixels) fatal("cube2font: failed allocating textures"); - for(tex = 0; tex < numtexs; tex++) - { + for(tex = 0; tex < numtexs; tex++) { + const char *file = texfilename(name, tex); int texchars = 0, i; uchar *dst, *src; memset(pixels, 0, tw*th*2); - for(i = 0; i < numchars; i++) - { + for(i = 0; i < numchars; i++) { + struct fontchar *c = &chars[i]; int x, y; if(c->tex != tex) continue; texchars++; dst = &pixels[2*((c->y + c->offy - c->color->top)*tw + c->x + c->color->left - c->offx)]; src = (uchar *)c->color->bitmap.buffer; - for(y = 0; y < c->color->bitmap.rows; y++) - { + for(y = 0; y < c->color->bitmap.rows; y++) { + for(x = 0; x < c->color->bitmap.width; x++) dst[2*x] = src[x]; src += c->color->bitmap.pitch; @@ -289,8 +289,8 @@ void writetexs(const char *name, struct fontchar *chars, int numchars, int numte } dst = &pixels[2*((c->y + c->offy - c->alpha->top)*tw + c->x + c->alpha->left - c->offx)]; src = (uchar *)c->alpha->bitmap.buffer; - for(y = 0; y < c->alpha->bitmap.rows; y++) - { + for(y = 0; y < c->alpha->bitmap.rows; y++) { + for(x = 0; x < c->alpha->bitmap.width; x++) dst[2*x+1] = src[x]; src += c->alpha->bitmap.pitch; @@ -303,8 +303,8 @@ void writetexs(const char *name, struct fontchar *chars, int numchars, int numte free(pixels); } -void writecfg(const char *name, struct fontchar *chars, int numchars, int x1, int y1, int x2, int y2, int sw, int sh, int argc, char **argv) -{ +void writecfg(const char *name, struct fontchar *chars, int numchars, int x1, int y1, int x2, int y2, int sw, int sh, int argc, char **argv) { + FILE *f; char file[256]; int i, lastcode = 0, lasttex = 0; @@ -317,24 +317,24 @@ void writecfg(const char *name, struct fontchar *chars, int numchars, int x1, in fprintf(f, " %s", argv[i]); fprintf(f, "\n"); fprintf(f, "font \"%s\" \"%s\" %d %d\n", name, texname(name, 0), sw, sh); - for(i = 0; i < numchars; i++) - { + for(i = 0; i < numchars; i++) { + struct fontchar *c = &chars[i]; - if(!lastcode && lastcode < c->code) - { + if(!lastcode && lastcode < c->code) { + fprintf(f, "fontoffset \"%s\"\n", encodeutf8(c->uni)); lastcode = c->code; } - else if(lastcode < c->code) - { + else if(lastcode < c->code) { + if(lastcode + 1 == c->code) fprintf(f, "fontskip // %d\n", lastcode); else fprintf(f, "fontskip %d // %d .. %d\n", c->code - lastcode, lastcode, c->code-1); lastcode = c->code; } - if(lasttex != c->tex) - { + if(lasttex != c->tex) { + fprintf(f, "\nfonttex \"%s\"\n", texname(name, c->tex)); lasttex = c->tex; } @@ -347,10 +347,10 @@ void writecfg(const char *name, struct fontchar *chars, int numchars, int x1, in fclose(f); } -int groupchar(int c) -{ - switch(c) - { +int groupchar(int c) { + + switch(c) { + case 0x152: case 0x153: case 0x178: return 1; } if(c < 127 || c >= 0x2000) return 0; @@ -359,8 +359,8 @@ int groupchar(int c) return 3; } -int sortchars(const void *x, const void *y) -{ +int sortchars(const void *x, const void *y) { + const struct fontchar *xc = *(const struct fontchar **)x, *yc = *(const struct fontchar **)y; int xg = groupchar(xc->uni), yg = groupchar(yc->uni); if(xg < yg) return -1; @@ -370,16 +370,16 @@ int sortchars(const void *x, const void *y) return yc->uni - xc->uni; } -int scorechar(struct fontchar *f, int pad, int tw, int th, int rw, int rh, int ry) -{ +int scorechar(struct fontchar *f, int pad, int tw, int th, int rw, int rh, int ry) { + int score = 0; if(rw + f->w > tw) { ry += rh + pad; score = 1; } if(ry + f->h > th) score = 2; return score; } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { + FT_Library l; FT_Face f; FT_Stroker s, s2; @@ -411,8 +411,8 @@ int main(int argc, char **argv) fatal("cube2font: failed loading font %s", argv[1]); if(outborder > 0) FT_Stroker_Set(s, (FT_Fixed)(outborder * 64), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0); if(inborder > 0) FT_Stroker_Set(s2, (FT_Fixed)(inborder * 64), FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0); - for(c = 0; c < 256; c++) if(iscubeprint(c)) - { + for(c = 0; c < 256; c++) if(iscubeprint(c)) { + FT_Glyph p, p2; FT_BitmapGlyph b, b2; struct fontchar *dst = &chars[numchars]; @@ -443,38 +443,38 @@ int main(int argc, char **argv) order[numchars++] = dst; } qsort(order, numchars, sizeof(order[0]), sortchars); - for(i = 0; i < numchars;) - { + for(i = 0; i < numchars;) { + struct fontchar *dst; int j, k, trial0, prevscore, dstscore, fitscore; - for(trial0 = trial, prevscore = -1; (trial -= 2) >= trial0-512;) - { + for(trial0 = trial, prevscore = -1; (trial -= 2) >= trial0-512;) { + int g, fw = rw, fh = rh, fy = ry, curscore = 0, reused = 0; - for(j = i; j < numchars; j++) - { + for(j = i; j < numchars; j++) { + dst = order[j]; if(dst->tex >= 0 || dst->tex <= trial) continue; g = groupchar(dst->uni); dstscore = scorechar(dst, pad, tw, th, fw, fh, fy); - for(k = j; k < numchars; k++) - { + for(k = j; k < numchars; k++) { + struct fontchar *fit = order[k]; if(fit->tex >= 0 || fit->tex <= trial) continue; if(fit->tex >= trial0 && groupchar(fit->uni) != g) break; fitscore = scorechar(fit, pad, tw, th, fw, fh, fy); - if(fitscore < dstscore || (fitscore == dstscore && fit->h > dst->h)) - { + if(fitscore < dstscore || (fitscore == dstscore && fit->h > dst->h)) { + dst = fit; dstscore = fitscore; } } - if(fw + dst->w > tw) - { + if(fw + dst->w > tw) { + fy += fh + pad; fw = fh = 0; } - if(fy + dst->h > th) - { + if(fy + dst->h > th) { + fy = fw = fh = 0; if(curscore > 0) break; } @@ -488,30 +488,30 @@ int main(int argc, char **argv) if(reused < prevscore || curscore <= prevscore) break; prevscore = curscore; } - for(; i < numchars; i++) - { + for(; i < numchars; i++) { + dst = order[i]; if(dst->tex >= 0) continue; dstscore = scorechar(dst, pad, tw, th, rw, rh, ry); - for(j = i; j < numchars; j++) - { + for(j = i; j < numchars; j++) { + struct fontchar *fit = order[j]; if(fit->tex < trial || fit->tex > trial+2) continue; fitscore = scorechar(fit, pad, tw, th, rw, rh, ry); - if(fitscore < dstscore || (fitscore == dstscore && fit->h > dst->h)) - { + if(fitscore < dstscore || (fitscore == dstscore && fit->h > dst->h)) { + dst = fit; dstscore = fitscore; } } if(dst->tex < trial || dst->tex > trial+2) break; - if(rw + dst->w > tw) - { + if(rw + dst->w > tw) { + ry += rh + pad; rw = rh = 0; } - if(ry + dst->h > th) - { + if(ry + dst->h > th) { + ry = rw = rh = 0; numtex++; } @@ -531,8 +531,8 @@ int main(int argc, char **argv) } if(rh > 0) numtex++; #if 0 - if(sw <= 0) - { + if(sw <= 0) { + if(FT_Load_Char(f, ' ', FT_LOAD_DEFAULT)) fatal("cube2font: failed loading space character"); sw = (f->glyph->advance.x+0x3F)>>6; @@ -542,8 +542,8 @@ int main(int argc, char **argv) if(sw <= 0) sw = sh/3; writetexs(argv[2], chars, numchars, numtex, tw, th); writecfg(argv[2], chars, numchars, x1, y1, x2, y2, sw, sh, argc, argv); - for(i = 0; i < numchars; i++) - { + for(i = 0; i < numchars; i++) { + if(chars[i].alpha != chars[i].color) FT_Done_Glyph((FT_Glyph)chars[i].alpha); FT_Done_Glyph((FT_Glyph)chars[i].color); } -- cgit v1.2.3