ref: 2b2c47d8c2f2572412994087c360a30fec5a6d71
parent: 0fdd9b51c7c4ada3a441c3a7baa58be31e24a22c
author: qwx <qwx@sciops.net>
date: Fri Sep 26 05:26:49 EDT 2025
add tosubf: convert image to subfont image
--- a/mkfile
+++ b/mkfile
@@ -8,6 +8,7 @@
rfx\
spd\
tev\
+ tosubf\
vol\
wstat\
--- /dev/null
+++ b/tosubf.c
@@ -1,0 +1,80 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s [-a ASC] [-h HEIGHT] [-n NAME] [-o PATH] IMG [IMG..]\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+ int x, k, n, fh, fa, fd, ofd;
+ char *name, *path;
+ Rectangle r;
+ Subfont *sf;
+ Fontchar *fc;
+ Memimage *i, **in, *out;
+
+ fh = 16;
+ fa = 12;
+ name = "urmom";
+ path = nil;
+ ofd = 1;
+ ARGBEGIN{
+ case 'a': fa = atoi(EARGF(usage())); break;
+ case 'h': fh = atoi(EARGF(usage())); break;
+ case 'n': name = EARGF(usage()); break;
+ case 'o': path = EARGF(usage()); break;
+ }ARGEND
+ if((n = argc) < 1)
+ usage();
+ if(path != nil && (ofd = create(path, OWRITE, 0664)) < 0)
+ sysfatal("open: %r");
+ if(memimageinit() < 0)
+ sysfatal("memimageinit: %r");
+ if((sf = mallocz(sizeof *sf, 1)) == nil
+ || (fc = mallocz((n+1) * sizeof *fc, 1)) == nil
+ || (in = mallocz(n * sizeof *in, 1)) == nil)
+ sysfatal("mallocz: %r");
+ x = k = 0;
+ while(*argv != nil){
+ if((fd = open(*argv, OREAD)) < 0)
+ sysfatal("open: %r");
+ if((i = readmemimage(fd)) == nil)
+ sysfatal("readmemimage: %r");
+ close(fd);
+ fc[k].x = x;
+ fc[k].top = i->r.min.y;
+ fc[k].bottom = i->r.max.y;
+ fc[k].left = i->r.min.x;
+ fc[k].width = Dx(i->r);
+ x += fc[k].width;
+ in[k] = i;
+ k++;
+ argv++;
+ }
+ fc[k].x = x;
+ if((out = allocmemimage(Rect(0, 0, x, fh), GREY1)) == nil)
+ sysfatal("allocmemimage: %r");
+ x = k = 0;
+ while(n-- > 0){
+ r = Rect(x, 0, x+fc[k].width, fc[k].bottom);
+ memimagedraw(out, r, in[k], in[k]->r.min, nil, ZP, SoverD);
+ x += fc[k].width;
+ k++;
+ }
+ sf->name = name;
+ sf->n = k;
+ sf->height = fh;
+ sf->ascent = fa;
+ sf->info = fc;
+ sf->ref = 1;
+ writememimage(ofd, out);
+ writesubfont(ofd, sf);
+ exits(nil);
+}
--
⑨