henesyinit from p9fce6d0435 years agoHistory
1895 lines · 33.9 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <libg.h>#include <bio.h> /* * This program keeps the system's and the application's bitmap and * subfont structures consistent, but that is really not necessary; * in fact it isn't even necessary to hold the subfonts in the system * at all.  Doing it this way makes it easy to do I/O on subfonts. * Even so, edits to individual character's Fontchars are not passed * to the system. */ typedef struct	Thing	Thing; struct Thing{	Bitmap	*b;	Subfont *s;	char	*name;		/* file name */	int	face;		/* is 48x48 face file or cursor file*/	Rectangle r;		/* drawing region */	Rectangle tr;		/* text region */	Rectangle er;		/* entire region */	long	c;		/* character number in subfont */	int	mod;		/* modified */	int	mag;		/* magnification */	Rune	off;		/* offset for subfont indices */	Thing	*parent;	/* thing of which i'm an edit */	Thing	*next;}; enum{	Border	= 1,	Up	= 1,	Down	= 0,	Mag	= 4,	Maxmag	= 10,}; enum{	Mopen,	Mread,	Mwrite,	Mcopy,	Mchar,	Mclose,	Mexit,};  char	*menu3str[] = {	[Mopen]		"open",	[Mread]		"read",	[Mwrite]	"write",	[Mcopy]		"copy",	[Mchar]		"char",	[Mclose]	"close",	[Mexit]		"exit",	0,}; Menu	menu3 = {	menu3str}; Cursor sweep0 = {	{-7, -7},	{0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0,	 0x03, 0xC0, 0x03, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF,	 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0xC0, 0x03, 0xC0,	 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0},	{0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,	 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x7F, 0xFE,	 0x7F, 0xFE, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,	 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00}}; Cursor box = {	{-7, -7},	{0xFF, 0xFF, 0xFF, 0xF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x82,	 0x04, 0x41, 0xff, 0xe1, 0x5f, 0xf1, 0x3f, 0xfe, 	 0x17, 0xf0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00,}}; Cursor skull = {	{-7,-7},	{0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0xe7, 0xe7, 	 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfc, 0x1f, 0xf8, 	 0x0f, 0xf0, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0xff, 	 0xef, 0xf7, 0xc7, 0xe3, 0x00, 0x00, 0x00, 0x00,},	{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x03,	 0xE7, 0xE7, 0x3F, 0xFC, 0x0F, 0xF0, 0x0D, 0xB0,	 0x07, 0xE0, 0x06, 0x60, 0x37, 0xEC, 0xE4, 0x27,	 0xC3, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}}; char	*fns[] = {	"0",      "Zero",		0,	"~(S|D)", "~(D|S)", "DnorS",	0,	"~S&D",   "D&~S",   "DandnotS",	0,	"~S",     "notS",		0,	"S&~D",   "~D&S",   "notDandS",	0,	"~D",     "notD",		0,	"S^D",    "D^S",    "DxorS",	0,	"~(S&D)", "~(D&S)", "DnandS",	0,	"S&D",    "D&S",    "DandS",	0,	"~(S^D)", "~(D^S)", "DxnorS",	0,	"D",				0,	"~S|D",   "D|~S",   "DornotS",	0,	"S",				0,	"S|~D",   "~D|S",   "notDorS",	0,	"S|D",    "D|S",    "DorS",	0,	"F",				0,	0}; Rectangle	cntlr;		/* control region */Rectangle	editr;		/* editing region */Rectangle	textr;		/* text region */Thing		*thing;Mouse		mouse;char		hex[] = "0123456789abcdefABCDEF";jmp_buf		err;char		*file;int		mag;uint		copyfn = S;uint		but1fn = S;uint		but2fn = 0;Bitmap		*values;ulong		val = ~0;uchar		data[8192]; Thing*	tget(char*);void	mesg(char*, ...);void	draw(Thing*, int);void	select(void);void	menu(void);void	error(char*);void	buttons(int);char	*fntostr(uint);int	strtofn(char*);void	drawall(void);void	tclose1(Thing*); voidmain(int argc, char *argv[]){	int i, j;	Event e;	Thing *t; 	mag = Mag;	binit(error, 0, "tweak");	values = balloc(Rect(0, 0, 256*Maxmag, Maxmag), screen.ldepth);	if(values == 0)		berror("can't balloc");	for(i=0; i<256; i++)		for(j=0; j<Maxmag; j++)			segment(values, Pt(i*Maxmag, j), Pt((i+1)*Maxmag, j), i, S);	einit(Emouse|Ekeyboard);	ereshaped(screen.r);	i = 1;	setjmp(err);	for(; i<argc; i++){		file = argv[i];		t = tget(argv[i]);		if(t)			draw(t, 1);		bflush();	}	file = 0;	setjmp(err);	for(;;)		switch(event(&e)){		case Ekeyboard:			break;		case Emouse:			mouse = e.mouse;			if(mouse.buttons & 3){				select();				break;			}			if(mouse.buttons & 4)				menu();		}} voiderror(char *s){	if(file)		mesg("can't read %s: %s: %r", file, s);	else		mesg("/dev/bitblt error: %s", s);	if(err[0])		longjmp(err, 1);	exits(s);} voidredraw(Thing *t){	Thing *nt;	Point p; 	if(thing==0 || thing==t)		bitblt(&screen, editr.min, &screen, editr, 0);	if(thing == 0)		return;	if(thing != t){		for(nt=thing; nt->next!=t; nt=nt->next)			;		bitblt(&screen, Pt(screen.r.min.x, nt->er.max.y), &screen,			Rect(screen.r.min.x, nt->er.max.y, editr.max.x, editr.max.y), 0);	}	for(nt=t; nt; nt=nt->next){		draw(nt, 0);		if(nt->next == 0){			p = Pt(editr.min.x, nt->er.max.y);			bitblt(&screen, p, &screen, Rpt(p, editr.max), 0);		}	}	mesg("");} voidereshaped(Rectangle r){	USED(r); 	screen.r = bscreenrect(&screen.clipr);	cntlr = inset(screen.clipr, 1);	editr = cntlr;	textr = editr;	textr.min.y = textr.max.y - font->height;	cntlr.max.y = cntlr.min.y + font->height;	editr.min.y = cntlr.max.y+1;	editr.max.y = textr.min.y-1;	bitblt(&screen, screen.clipr.min, &screen, screen.clipr, 0);	segment(&screen, Pt(editr.min.x, editr.max.y),			 Pt(editr.max.x+1, editr.max.y), ~0, F);	clipr(&screen, editr);	drawall();} voidmesgstr(Point p, int line, char *s){	Rectangle c, r; 	r.min = p;	r.min.y += line*font->height;	r.max.y = r.min.y+font->height;	r.max.x = editr.max.x;	c = screen.clipr;	clipr(&screen, r);	bitblt(&screen, r.min, &screen, r, 0);	r.min.x++;	p = string(&screen, r.min, font, s, S);	r.max.x = p.x + 1;	r.min.x--;	bitblt(&screen, r.min, &screen, r, ~D);	clipr(&screen, c);	bflush();} voidmesg(char *fmt, ...){	char buf[1024]; 	doprint(buf, buf+sizeof(buf), fmt, (&fmt+1));	mesgstr(textr.min, 0, buf);} voidtmesg(Thing *t, int line, char *fmt, ...){	char buf[1024]; 	doprint(buf, buf+sizeof(buf), fmt, (&fmt+1));	mesgstr(t->tr.min, line, buf);}  voidscntl(char *l){	char vals[16]; 	if(val & 0x80000000)		sprint(vals, "~%lux", ~val);	else		sprint(vals, "%lux", val);	sprint(l, "mag: %d val(hex): %s but1: %s but2: %s copy: %s",		mag, vals, fntostr(but1fn), fntostr(but2fn), fntostr(copyfn));} voidcntl(void){	char buf[256]; 	scntl(buf);	mesgstr(cntlr.min, 0, buf);} voidstext(Thing *t, char *l0, char *l1){	Fontchar *fc;	char buf[256]; 	l1[0] = 0;	sprint(buf, "ldepth:%d r:%d %d  %d %d ", 		t->b->ldepth, t->b->r.min.x, t->b->r.min.y,		t->b->r.max.x, t->b->r.max.y);	if(t->parent)		sprint(buf+strlen(buf), "mag: %d ", t->mag);	sprint(l0, "%s file: %s", buf, t->name);	if(t->c >= 0){		fc = &t->parent->s->info[t->c];		sprint(l1, "c(hex): %x c(char): %C x: %d "			   "top: %d bottom: %d left: %d width: %d iwidth: %d",			t->c+t->parent->off, t->c+t->parent->off,			fc->x, fc->top, fc->bottom, fc->left,			fc->width, Dx(t->b->r));	}else if(t->s)		sprint(l1, "offset(hex): %ux n:%d  height:%d  ascent:%d",			t->off, t->s->n, t->s->height, t->s->ascent);} voidtext(Thing *t){	char l0[256], l1[256]; 	stext(t, l0, l1);	tmesg(t, 0, l0);	if(l1[0])		tmesg(t, 1, l1);} voiddrawall(void){	Thing *t; 	cntl();	for(t=thing; t; t=t->next)		draw(t, 0);} intvalue(Bitmap *b, int x){	int v, l, w;	uchar mask; 	l = b->ldepth;	if(l > 3){		mesg("ldepth too large");		return 0;	}	w = 1<<l;	mask = (2<<w)-1;		/* ones at right end of word */	x -= b->r.min.x&~(7>>l);	/* adjust x relative to first pixel */	v = data[x>>(3-l)];	v >>= ((7>>l)<<l) - ((x&(7>>l))<<l);	/* pixel at right end of word */	v &= mask;			/* pixel at right end of word */	return v;} intbvalue(int v, int l){	v &= (1<<(1<<l))-1;	if(l > screen.ldepth)		v >>= (1<<l) - (1<<screen.ldepth);	else		while(l < screen.ldepth){			v |= v << (1<<l);			l++;		}	return v*Maxmag;} voiddraw(Thing *nt, int link){	int nl, nf, i, x, y, sx, sy, fdx, dx, dy, v;	Thing *t;	Subfont *s;	Bitmap *b;	Point p, p1, p2; 	if(link){		nt->next = 0;		if(thing == 0){			thing = nt;			y = editr.min.y;		}else{			for(t=thing; t->next; t=t->next)				;			t->next = nt;			y = t->er.max.y;		}	}else{		if(thing == nt)			y = editr.min.y;		else{			for(t=thing; t->next!=nt; t=t->next)				;			y = t->er.max.y;		}	}	s = nt->s;	b = nt->b;	nl = font->height;	if(s || nt->c>=0)		nl += font->height;	fdx = Dx(editr) - 2*Border;	dx = Dx(b->r);	dy = Dy(b->r);	if(nt->mag > 1){		dx *= nt->mag;		dy *= nt->mag;		fdx -= fdx%nt->mag;	}	nf = 1 + dx/fdx;	nt->er.min.y = y;	nt->er.min.x = editr.min.x;	nt->er.max.x = nt->er.min.x + Border + dx + Border;	if(nt->er.max.x > editr.max.x)		nt->er.max.x = editr.max.x;	nt->er.max.y = nt->er.min.y + Border + nf*(dy+Border);	nt->r = inset(nt->er, Border);	nt->er.max.x = editr.max.x;	bitblt(&screen, nt->er.min, &screen, nt->er, 0);	for(i=0; i<nf; i++){		p1 = Pt(nt->r.min.x-1, nt->r.min.y+i*(Border+dy));		/* draw portion of bitmap */		p = Pt(p1.x+1, p1.y);		if(nt->mag == 1)			bitblt(&screen, p, b,				Rect(b->r.min.x+i*fdx, b->r.min.y,				     b->r.max.x+(i+1)*fdx, b->r.max.y), S);		else{			for(y=b->r.min.y; y<b->r.max.y; y++){				sy = p.y+(y-b->r.min.y)*nt->mag;				rdbitmap(b, y, y+1, data);				for(x=b->r.min.x+i*(fdx/nt->mag); x<b->r.max.x; x++){					sx = p.x+(x-i*(fdx/nt->mag)-b->r.min.x)*nt->mag;					if(sx >= nt->r.max.x)						break;					v = bvalue(value(b, x), b->ldepth);					if(v)						bitblt(&screen, Pt(sx, sy),							values,							Rect(v, 0, v+nt->mag, nt->mag), S);				} 			}		}		/* line down left */		segment(&screen, p1, Pt(p1.x, p1.y+dy+Border+1), i==0? ~0 : 0, S);		/* line across top */

Lines 1–500 of 1895.