shithub: misc

ref: 2b2c47d8c2f2572412994087c360a30fec5a6d71
dir: /tosubf.c/

View raw version
#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);
}