8 typedef struct Thing Thing;
14 char *name; /* file name */
15 int face; /* is 48x48 face file or cursor file*/
16 Rectangle r; /* drawing region */
17 Rectangle tr; /* text region */
18 Rectangle er; /* entire region */
19 long c; /* character number in subfont */
20 int mod; /* modified */
21 int mag; /* magnification */
22 Rune off; /* offset for subfont indices */
23 Thing *parent; /* thing of which i'm an edit */
78 {0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0,
79 0x03, 0xC0, 0x03, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF,
80 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0xC0, 0x03, 0xC0,
81 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0},
82 {0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
83 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x7F, 0xFE,
84 0x7F, 0xFE, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
85 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00}
90 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
91 0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
92 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
93 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
94 {0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
95 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
96 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
97 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00}
102 {0x1F, 0xF8, 0x3F, 0xFC, 0x7F, 0xFE, 0xFB, 0xDF,
103 0xF3, 0xCF, 0xE3, 0xC7, 0xFF, 0xFF, 0xFF, 0xFF,
104 0xFF, 0xFF, 0xFF, 0xFF, 0xE3, 0xC7, 0xF3, 0xCF,
105 0x7B, 0xDF, 0x7F, 0xFE, 0x3F, 0xFC, 0x1F, 0xF8,},
106 {0x00, 0x00, 0x0F, 0xF0, 0x31, 0x8C, 0x21, 0x84,
107 0x41, 0x82, 0x41, 0x82, 0x41, 0x82, 0x7F, 0xFE,
108 0x7F, 0xFE, 0x41, 0x82, 0x41, 0x82, 0x41, 0x82,
109 0x21, 0x84, 0x31, 0x8C, 0x0F, 0xF0, 0x00, 0x00,}
114 {0x1f, 0xf8, 0x3f, 0xfc, 0x7f, 0xfe, 0xf8, 0x1f,
115 0xf0, 0x0f, 0xe0, 0x07, 0xe0, 0x07, 0xfe, 0x7f,
116 0xfe, 0x7f, 0xe0, 0x07, 0xe0, 0x07, 0xf0, 0x0f,
117 0x78, 0x1f, 0x7f, 0xfe, 0x3f, 0xfc, 0x1f, 0xf8, },
118 {0x00, 0x00, 0x0f, 0xf0, 0x31, 0x8c, 0x21, 0x84,
119 0x41, 0x82, 0x41, 0x82, 0x41, 0x82, 0x40, 0x02,
120 0x40, 0x02, 0x41, 0x82, 0x41, 0x82, 0x41, 0x82,
121 0x21, 0x84, 0x31, 0x8c, 0x0f, 0xf0, 0x00, 0x00, }
126 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x0c, 0x00, 0x8e, 0x1d, 0xc7,
128 0xff, 0xe3, 0xff, 0xf3, 0xff, 0xff, 0x7f, 0xfe,
129 0x3f, 0xf8, 0x17, 0xf0, 0x03, 0xe0, 0x00, 0x00,},
130 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x08, 0x00, 0x04, 0x00, 0x82,
132 0x04, 0x41, 0xff, 0xe1, 0x5f, 0xf1, 0x3f, 0xfe,
133 0x17, 0xf0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00,}
138 {0x00, 0x00, 0x00, 0x00, 0xc0, 0x03, 0xe7, 0xe7,
139 0xff, 0xff, 0xff, 0xff, 0x3f, 0xfc, 0x1f, 0xf8,
140 0x0f, 0xf0, 0x3f, 0xfc, 0xff, 0xff, 0xff, 0xff,
141 0xef, 0xf7, 0xc7, 0xe3, 0x00, 0x00, 0x00, 0x00,},
142 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x03,
143 0xE7, 0xE7, 0x3F, 0xFC, 0x0F, 0xF0, 0x0D, 0xB0,
144 0x07, 0xE0, 0x06, 0x60, 0x37, 0xEC, 0xE4, 0x27,
145 0xC3, 0xC3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
148 Rectangle cntlr; /* control region */
149 Rectangle editr; /* editing region */
150 Rectangle textr; /* text region */
153 char hex[] = "0123456789abcdefABCDEF";
161 Image *greyvalues[256];
165 void mesg(char*, ...);
166 void drawthing(Thing*, int);
169 void error(Display*, char*);
172 void tclose1(Thing*);
177 fprint(2, "usage: tweak [-W winsize] file...\n");
182 main(volatile int argc, char **volatile argv)
190 winsize = EARGF(usage());
196 if(initdraw(error, 0, "tweak") < 0){
197 fprint(2, "tweak: initdraw failed: %r\n");
200 for(i=0; i<256; i++){
201 values[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, cmap2rgba(i));
202 greyvalues[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, (i<<24)|(i<<16)|(i<<8)|0xFF);
203 if(values[i] == 0 || greyvalues[i] == 0)
204 drawerror(display, "can't allocate image");
206 einit(Emouse|Ekeyboard);
215 flushimage(display, 1);
225 if(mouse.buttons & 3){
229 if(mouse.buttons & 4)
239 for(i=0; (1<<i) <= n; i++)
242 fprint(2, "log2 %d = 0\n", n);
247 error(Display *d, char *s)
252 mesg("can't read %s: %s: %r", file, s);
254 mesg("/dev/bitblt error: %s", s);
266 if(thing==0 || thing==t)
267 draw(screen, editr, display->white, nil, ZP);
271 for(nt=thing; nt->next!=t; nt=nt->next)
273 draw(screen, Rect(screen->r.min.x, nt->er.max.y, editr.max.x, editr.max.y),
274 display->white, nil, ZP);
276 for(nt=t; nt; nt=nt->next){
279 p = Pt(editr.min.x, nt->er.max.y);
280 draw(screen, Rpt(p, editr.max), display->white, nil, ZP);
289 if(new && getwindow(display, Refnone) < 0)
290 error(display, "can't reattach to window");
291 cntlr = insetrect(screen->clipr, 1);
294 textr.min.y = textr.max.y - font->height;
295 cntlr.max.y = cntlr.min.y + font->height;
296 editr.min.y = cntlr.max.y+1;
297 editr.max.y = textr.min.y-1;
298 draw(screen, screen->clipr, display->white, nil, ZP);
299 draw(screen, Rect(editr.min.x, editr.max.y, editr.max.x+1, editr.max.y+1), display->black, nil, ZP);
300 replclipr(screen, 0, editr);
305 mesgstr(Point p, int line, char *s)
310 r.min.y += line*font->height;
311 r.max.y = r.min.y+font->height;
312 r.max.x = editr.max.x;
314 replclipr(screen, 0, r);
315 draw(screen, r, values[0xDD], nil, ZP);
317 string(screen, r.min, display->black, ZP, font, s);
318 replclipr(screen, 0, c);
319 flushimage(display, 1);
329 vseprint(buf, buf+sizeof(buf), fmt, arg);
331 mesgstr(textr.min, 0, buf);
335 tmesg(Thing *t, int line, char *fmt, ...)
341 vseprint(buf, buf+sizeof(buf), fmt, arg);
343 mesgstr(t->tr.min, line, buf);
350 sprint(l, "mag: %d but1: %d but2: %d invert-on-copy: %c", mag, but1val, but2val, "ny"[invert]);
359 mesgstr(cntlr.min, 0, buf);
363 stext(Thing *t, char *l0, char *l1)
369 sprint(buf, "depth:%d r:%d %d %d %d ",
370 t->b->depth, t->b->r.min.x, t->b->r.min.y,
371 t->b->r.max.x, t->b->r.max.y);
373 sprint(buf+strlen(buf), "mag: %d ", t->mag);
374 sprint(l0, "%s file: %s", buf, t->name);
376 fc = &t->parent->s->info[t->c];
377 sprint(l1, "c(hex): %x c(char): %C x: %d "
378 "top: %d bottom: %d left: %d width: %d iwidth: %d",
379 (int)(t->c+t->parent->off), (int)(t->c+t->parent->off),
380 fc->x, fc->top, fc->bottom, fc->left,
381 fc->width, Dx(t->b->r));
383 sprint(l1, "offset(hex): %ux n:%d height:%d ascent:%d",
384 t->off, t->s->n, t->s->height, t->s->ascent);
390 char l0[256], l1[256];
404 for(t=thing; t; t=t->next)
409 value(Image *b, int x)
416 mesg("ldepth too large");
420 mask = (1<<w)-1; /* ones at right end of word */
421 x -= b->r.min.x&~(7>>l); /* adjust x relative to first pixel */
423 v >>= ((7>>l)<<l) - ((x&(7>>l))<<l); /* pixel at right end of word */
424 v &= mask; /* pixel at right end of word */
432 if(d > screen->depth)
433 v >>= d - screen->depth;
435 while(d < screen->depth && d < 8){
440 mesg("internal error: bad color");
447 drawthing(Thing *nt, int link)
449 int n, nl, nf, i, x, y, sx, sy, fdx, dx, dy, v;
461 for(t=thing; t->next; t=t->next)
470 for(t=thing; t->next!=nt; t=t->next)
480 fdx = Dx(editr) - 2*Border;
490 nt->er.min.x = editr.min.x;
491 nt->er.max.x = nt->er.min.x + Border + dx + Border;
492 if(nt->er.max.x > editr.max.x)
493 nt->er.max.x = editr.max.x;
494 nt->er.max.y = nt->er.min.y + Border + nf*(dy+Border);
495 nt->r = insetrect(nt->er, Border);
496 nt->er.max.x = editr.max.x;
497 draw(screen, nt->er, display->white, nil, ZP);
499 p1 = Pt(nt->r.min.x-1, nt->r.min.y+i*(Border+dy));
500 /* draw portion of bitmap */
501 p = Pt(p1.x+1, p1.y);
503 draw(screen, Rect(p.x, p.y, p.x+fdx+Dx(b->r), p.y+Dy(b->r)),
504 b, nil, Pt(b->r.min.x+i*fdx, b->r.min.y));
506 for(y=b->r.min.y; y<b->r.max.y; y++){
507 sy = p.y+(y-b->r.min.y)*nt->mag;
508 if((n=unloadimage(b, Rect(b->r.min.x, y, b->r.max.x, y+1), data, sizeof data)) < 0)
509 fprint(2, "unloadimage: %r\n");
510 for(x=b->r.min.x+i*(fdx/nt->mag); x<b->r.max.x; x++){
511 sx = p.x+(x-i*(fdx/nt->mag)-b->r.min.x)*nt->mag;
512 if(sx >= nt->r.max.x)
514 v = bvalue(value(b, x), b->depth);
518 draw(screen, Rect(sx, sy, sx+nt->mag, sy+nt->mag),
519 greyvalues[v], nil, ZP);
521 draw(screen, Rect(sx, sy, sx+nt->mag, sy+nt->mag),
529 col = display->black;
531 col = display->white;
532 draw(screen, Rect(p1.x, p1.y, p1.x+1, p1.y+dy+Border), col, nil, ZP);
533 /* line across top */
534 draw(screen, Rect(p1.x, p1.y-1, nt->r.max.x+Border, p1.y), display->black, nil, ZP);
538 col = display->black;
541 col = display->white;
543 /* line down right */
544 draw(screen, Rect(p2.x, p2.y, p2.x+1, p2.y+dy+Border), col, nil, ZP);
545 /* line across bottom */
548 draw(screen, Rect(p1.x, p1.y-1, p2.x,p1.y), display->black, nil, ZP);
551 nt->tr.min.x = editr.min.x;
552 nt->tr.max.x = editr.max.x;
553 nt->tr.min.y = nt->er.max.y + Border;
554 nt->tr.max.y = nt->tr.min.y + nl;
555 nt->er.max.y = nt->tr.max.y + Border;
565 return 10 + (c - 'a');
567 return 10 + (c - 'A');
574 int i, j, fd, face, x, y, c, chan;
584 errstr((char*)buf, sizeof buf); /* flush pending error message */
585 memmove(oerr, err, sizeof err);
590 memmove(err, oerr, sizeof err);
593 fd = open(file, OREAD);
595 mesg("can't open %s: %r", file);
600 mesg("can't stat bitmap file %s: %r", file);
604 if(read(fd, buf, 11) != 11){
605 mesg("can't read %s: %r", file);
613 if(memcmp(data, "0x", 2)==0 && data[4]==','){
619 data = malloc(d->length+1);
621 mesg("can't malloc buffer: %r");
626 if(read(fd, data, d->length) != d->length){
627 mesg("can't read cursor file %s: %r", file);
631 b = allocimage(display, Rect(0, 0, 16, 32), GREY1, 0, DNofill);
633 mesg("image alloc failed file %s: %r", file);
640 if((c=data[i]) == '\0')
642 if(c=='0' && data[i+1] == 'x'){
647 buf[x++] = (tohex(c)<<4) | tohex(data[i+1]);
653 loadimage(b, Rect(0, 0, 16, 32), buf, sizeof buf);
655 }else if(memcmp(buf, "0x", 2)==0){
661 data = malloc(d->length+1);
663 mesg("can't malloc buffer: %r");
668 if(read(fd, data, d->length) != d->length){
669 mesg("can't read bitmap file %s: %r", file);
673 for(y=0,i=0; i<d->length; i++)
678 mesg("ill-formed face file %s", file);
683 for(x=0,i=0; (c=data[i])!='\n'; ){
684 if(c==',' || c==' ' || c=='\t'){
688 if(c=='0' && data[i+1] == 'x'){
717 b = allocimage(display, Rect(0, 0, y, y), chan, 0, -1);
719 mesg("image alloc failed file %s: %r", file);
726 for(x=0; (c=data[i])!='\n'; ){
727 if(c=='0' && data[i+1] == 'x'){
732 buf[x++] = ~((tohex(c)<<4) | tohex(data[i+1]));
739 loadimage(b, Rect(0, j, y, j+1), buf, sizeof buf);
745 b = readimage(display, fd, 0);
747 mesg("can't read bitmap file %s: %r", file);
751 if(seek(fd, 0, 1) < d->length)
752 s = readsubfonti(display, file, fd, b, 0);
755 t = malloc(sizeof(Thing));
758 mesg("malloc failed: %r");
765 t->name = strdup(file);
778 memmove(err, oerr, sizeof err);
783 atline(int x, Point p, char *line, char *buf)
785 char *s, *c, *word, *hit;
792 for(s=line; *s; s+=w){
793 w = chartorune(&r, s);
794 x += runestringnwidth(font, &r, 1);
795 if(wasblank && r!=' ')
808 c = utfrune(hit, ':');
809 strncpy(buf, hit, c-hit);
815 attext(Thing *t, Point p, char *buf)
817 char l0[256], l1[256];
819 if(!ptinrect(p, t->tr))
822 if(p.y < t->tr.min.y+font->height)
823 return atline(t->r.min.x, p, l0, buf);
825 return atline(t->r.min.x, p, l1, buf);
829 type(char *buf, char *tag)
838 mesg("%s: %s", tag, buf);
845 case 0x15: /* control-U */
853 p += runetochar(p, &r);
856 /* return 0; shut up compiler */
860 textedit(Thing *t, char *tag)
869 int i, ld, d, w, c, doredraw, fdx, x;
873 if(type(buf, tag) == 0)
875 if(strcmp(tag, "file") == 0){
878 mesg("illegal file name");
881 if(strcmp(t->name, buf) != 0){
887 for(nt=thing; nt; nt=nt->next)
888 if(t==nt || t->parent==nt || nt->parent==t){
890 nt->name = strdup(buf);
892 mesg("malloc failed: %r");
899 if(strcmp(tag, "depth") == 0){
900 if(buf[0]<'0' || '9'<buf[0] || (d=atoi(buf))<0 || d>8 || xlog2(d)<0){
901 mesg("illegal ldepth");
913 chan = CHAN1(CGrey, d);
914 for(nt=thing; nt; nt=nt->next){
915 if(nt!=t && nt!=t->parent && nt->parent!=t)
917 b = allocimage(display, nt->b->r, chan, 0, 0);
920 mesg("image alloc failed: %r");
923 draw(b, b->r, nt->b, nil, nt->b->r.min);
927 b = allocimage(display, nt->b->r, chan, 0, -1);
930 draw(b, b->r, nt->b, nil, nt->b->r.min);
931 f = allocsubfont(t->name, nt->s->n, nt->s->height, nt->s->ascent, nt->s->info, b);
935 mesg("can't make subfont: %r");
938 nt->s->info = 0; /* prevent it being freed */
947 if(strcmp(tag, "mag") == 0){
948 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<=0 || ld>Maxmag){
949 mesg("illegal magnification");
958 if(strcmp(tag, "r") == 0){
960 mesg("can't change rectangle of subfont\n");
964 r.min.x = strtoul(s, &s, 0);
965 r.min.y = strtoul(s, &s, 0);
966 r.max.x = strtoul(s, &s, 0);
967 r.max.y = strtoul(s, &s, 0);
968 if(Dx(r)<=0 || Dy(r)<=0){
969 mesg("illegal rectangle");
974 for(nt=thing; nt; nt=nt->next){
975 if(nt->parent==t && !rectinrect(nt->b->r, r))
978 b = allocimage(display, r, t->b->chan, 0, 0);
981 draw(b, r, t->b, nil, r.min);
984 b = allocimage(display, r, t->b->chan, 0, 0);
991 if(strcmp(tag, "ascent") == 0){
992 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0 || ld>t->s->height){
993 mesg("illegal ascent");
996 if(t->s->ascent == ld)
1003 if(strcmp(tag, "height") == 0){
1004 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0){
1005 mesg("illegal height");
1008 if(t->s->height == ld)
1015 if(strcmp(tag, "left")==0 || strcmp(tag, "width") == 0){
1016 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0){
1017 mesg("illegal value");
1020 fc = &t->parent->s->info[t->c];
1021 if(strcmp(tag, "left")==0){
1034 if(strcmp(tag, "offset(hex)") == 0){
1035 if(!strchr(hex, buf[0])){
1037 mesg("illegal offset");
1041 ld = strtoul(buf, &s, 16);
1046 for(nt=thing; nt; nt=nt->next)
1051 if(strcmp(tag, "n") == 0){
1052 if(buf[0]<'0' || '9'<buf[0] || (w=atoi(buf))<=0){
1061 for(nt=thing; nt; nt=nt->next)
1062 if(nt->parent == t){
1069 r.max.x = f->info[w].x;
1070 b = allocimage(display, r, t->b->chan, 0, 0);
1073 draw(b, b->r, t->b, nil, r.min);
1074 fdx = Dx(editr) - 2*Border;
1075 if(Dx(t->b->r)/fdx != Dx(b->r)/fdx)
1079 b = allocimage(display, r, t->b->chan, 0, 0);
1082 draw(b, b->r, t->b, nil, r.min);
1083 nfc = malloc((w+1)*sizeof(Fontchar));
1085 mesg("malloc failed");
1090 for(i=0; i<=w && i<=f->n; i++)
1093 memset(nfc+i, 0, ((w+1)-i)*sizeof(Fontchar));
1097 f = allocsubfont(t->name, w, f->height, f->ascent, nfc, b);
1100 t->s->bits = nil; /* don't free it */
1111 if(strcmp(tag, "iwidth") == 0){
1112 if(buf[0]<'0' || '9'<buf[0] || (w=atoi(buf))<0){
1113 mesg("illegal iwidth");
1119 r = t->parent->b->r;
1124 b = allocimage(display, r, t->b->chan, 0, 0);
1128 draw(b, Rect(b->r.min.x, b->r.min.y,
1129 b->r.min.x+(fc[1].x-t->b->r.min.x), b->r.min.y+Dy(t->b->r)),
1130 t->b, nil, t->b->r.min);
1131 draw(b, Rect(fc[1].x+w, b->r.min.y, w+t->b->r.max.x, b->r.min.y+Dy(t->b->r)),
1132 t->b, nil, Pt(fc[1].x, t->b->r.min.y));
1133 fdx = Dx(editr) - 2*Border;
1135 if(Dx(t->b->r)/fdx != Dx(b->r)/fdx)
1139 b = allocimage(display, r, t->b->chan, 0, 0);
1142 draw(b, b->r, t->b, nil, t->b->r.min);
1144 for(i=c+1; i<=f->n; i++, fc++)
1146 f = allocsubfont(t->name, f->n, f->height, f->ascent,
1150 /* t->s and f share info; free carefully */
1161 /* redraw all affected chars */
1162 for(nt=thing; nt; nt=nt->next){
1163 if(nt->parent!=t || nt->c<c)
1165 fc = &f->info[nt->c];
1167 r.min.y = nt->b->r.min.y;
1169 r.max.y = nt->b->r.max.y;
1170 b = allocimage(display, r, nt->b->chan, 0, 0);
1173 draw(b, r, t->b, nil, r.min);
1175 if(Dx(nt->b->r)/fdx != Dx(b->r)/fdx)
1191 mesg("cannot edit %s in file %s", tag, t->name);
1201 if(type(buf, tag) == 0)
1203 if(strcmp(tag, "mag") == 0){
1204 if(buf[0]<'0' || '9'<buf[0] || (l=atoi(buf))<=0 || l>Maxmag){
1205 mesg("illegal magnification");
1212 if(strcmp(tag, "but1")==0
1213 || strcmp(tag, "but2")==0){
1214 if(buf[0]<'0' || '9'<buf[0] || (l=atoi(buf))<0 || l>255){
1215 mesg("illegal value");
1218 if(strcmp(tag, "but1") == 0)
1220 else if(strcmp(tag, "but2") == 0)
1225 if(strcmp(tag, "invert-on-copy")==0){
1226 if(buf[0]=='y' || buf[0]=='1')
1228 else if(buf[0]=='n' || buf[0]=='0')
1231 mesg("illegal value");
1237 mesg("cannot edit %s", tag);
1243 while((mouse.buttons==0) != ud)
1248 screenpt(Thing *t, Point realp)
1253 fdx = Dx(editr)-2*Border;
1256 p = mulpt(subpt(realp, t->b->r.min), t->mag);
1257 if(fdx < Dx(t->b->r)*t->mag){
1259 p.y += n * (Dy(t->b->r)*t->mag+Border);
1262 p = addpt(p, t->r.min);
1267 realpt(Thing *t, Point screenp)
1272 fdx = (Dx(editr)-2*Border);
1275 p.y = screenp.y-t->r.min.y;
1277 if(fdx < Dx(t->b->r)*t->mag){
1278 dy = Dy(t->b->r)*t->mag+Border;
1283 p.x += screenp.x-t->r.min.x;
1284 p = addpt(divpt(p, t->mag), t->b->r.min);
1289 sweep(int but, Rectangle *r)
1294 esetcursor(&sweep0);
1296 if(mouse.buttons != (1<<(but-1))){
1302 for(t=thing; t; t=t->next)
1303 if(ptinrect(p, t->r))
1306 p = screenpt(t, realpt(t, p));
1311 while(mouse.buttons == (1<<(but-1))){
1312 edrawgetrect(insetrect(*r, -Borderwidth), 1);
1314 edrawgetrect(insetrect(*r, -Borderwidth), 0);
1317 q = screenpt(t, realpt(t, q));
1320 *r = canonrect(Rpt(p, q));
1332 openedit(Thing *t, Point pt, int c)
1341 if(t->b->depth > 8){
1342 mesg("image has depth %d; can't handle >8", t->b->depth);
1348 /* if big enough to bother, sweep box */
1349 if(Dx(br)<=16 && Dy(br)<=16)
1354 r = rectaddpt(r, subpt(br.min, t->r.min));
1355 if(!rectclip(&r, br))
1360 }else if(Dx(r) < 4){
1362 mesg("rectangle too small");
1372 fc = &t->s->info[c];
1376 r.max.y = br.min.y + Dy(br);
1378 /* just point at character */
1380 p = addpt(pt, subpt(br.min, t->r.min));
1383 for(c=0; c<t->s->n; c++,fc++){
1387 r.max.x = x + fc[1].x - fc[0].x;
1388 r.max.y = y + Dy(br);
1391 if(r.max.x >= br.min.x+Dx(t->r)){
1394 if(fc[1].x > fc[0].x)
1397 x += fc[1].x - fc[0].x;
1405 nt = malloc(sizeof(Thing));
1408 mesg("can't allocate: %r");
1411 memset(nt, 0, sizeof(Thing));
1413 nt->b = allocimage(display, r, t->b->chan, 0, DNofill);
1418 draw(nt->b, r, t->b, nil, r.min);
1419 nt->name = strdup(t->name);
1431 ckinfo(Thing *t, Rectangle mod)
1433 int i, j, k, top, bot, n, zero;
1441 if(t->s==0 || Dy(t->b->r)==0)
1444 /* check bounding boxes */
1445 fc = &t->s->info[0];
1446 r.min.y = t->b->r.min.y;
1447 r.max.y = t->b->r.max.y;
1448 for(i=0; i<t->s->n; i++, fc++){
1451 if(!rectXrect(mod, r))
1453 if(b==0 || Dx(b->r)<Dx(r)){
1456 b = allocimage(display, rectsubpt(r, r.min), t->b->chan, 0, 0);
1458 mesg("can't alloc image");
1462 draw(b, b->r, display->white, nil, ZP);
1463 draw(b, b->r, t->b, nil, r.min);
1466 n = 2+((Dx(r)/8)*t->b->depth);
1467 for(j=0; j<b->r.max.y; j++){
1469 unloadimage(b, Rect(b->r.min.x, j, b->r.max.x, j+1), data, sizeof data);
1484 if(top!=fc->top || bot!=fc->bottom){
1487 for(nt=thing; nt; nt=nt->next)
1488 if(nt->parent==t && nt->c==i)
1497 twidpix(Thing *t, Point p, int set)
1503 if(!ptinrect(p, b->r))
1509 if(b->chan == GREY8)
1513 draw(b, Rect(p.x, p.y, p.x+1, p.y+1), v, nil, ZP);
1515 draw(screen, Rect(p.x, p.y, p.x+t->mag, p.y+t->mag), v, nil, ZP);
1527 if(mouse.buttons!=1 && mouse.buttons!=2){
1531 set = mouse.buttons==1;
1533 lastp = addpt(b->r.min, Pt(-1, -1));
1534 mod = Rpt(addpt(b->r.max, Pt(1, 1)), lastp);
1535 while(mouse.buttons){
1536 p = realpt(t, mouse.xy);
1537 if(!eqpt(p, lastp)){
1539 if(ptinrect(p, b->r)){
1540 for(nt=thing; nt; nt=nt->next)
1541 if(nt->parent==t->parent || nt==t->parent)
1542 twidpix(nt, p, set);
1551 if(p.x >= mod.max.x)
1553 if(p.y >= mod.max.y)
1566 char line[128], buf[128];
1569 if(ptinrect(mouse.xy, cntlr)){
1571 if(atline(cntlr.min.x, mouse.xy, line, buf)){
1572 if(mouse.buttons == 1)
1580 for(t=thing; t; t=t->next){
1581 if(attext(t, mouse.xy, buf)){
1582 if(mouse.buttons == 1)
1588 if(ptinrect(mouse.xy, t->r)){
1590 if(mouse.buttons == 1){
1607 int i, j, x, y, fd, ws, ld;
1614 fd = create(t->name, OWRITE, 0666);
1616 mesg("can't write %s: %r", t->name);
1619 if(t->face && t->b->depth <= 4){
1621 ld = xlog2(t->b->depth);
1622 /* This heuristic reflects peculiarly different formats */
1624 if(t->face == 2) /* cursor file */
1626 else if(Dx(r)<32 || ld==0)
1628 Binit(&buf, fd, OWRITE);
1629 if(t->face == CURSOR)
1631 for(y=r.min.y; y<r.max.y; y++){
1632 unloadimage(t->b, Rect(r.min.x, y, r.max.x, y+1), data, sizeof data);
1634 for(x=r.min.x; x<r.max.x; j+=ws,x+=ws*8>>ld){
1637 Bprint(&buf, "%.2x", data[i+j]);
1640 if(t->face == CURSOR){
1642 case 3: case 7: case 11: case 19: case 23: case 27:
1643 Bprint(&buf, "\n ");
1646 Bprint(&buf, "},\n{");
1649 Bprint(&buf, "}\n");
1657 if(writeimage(fd, t->b, 0)<0 || (t->s && writesubfont(fd, t->s)<0)){
1659 mesg("can't write %s: %r", t->name);
1663 mesg("wrote %s", t->name);
1675 if(mouse.buttons != 4)
1677 for(t=thing; t; t=t->next){
1679 if(ptinrect(mouse.xy, t->r)){
1680 while(ptinrect(mouse.xy, t->r) && mouse.buttons==4){
1681 p = realpt(t, mouse.xy);
1682 if(!eqpt(p, lastp)){
1684 unloadimage(t->b, Rect(t->b->r.min.x, p.y, t->b->r.max.x, p.y+1), data, sizeof data);
1685 mesg("[%d,%d] = %d=0x%ux", p.x, p.y, value(t->b, p.x), value(t->b, p.x));
1708 for(nt=thing; nt->next!=t; nt=nt->next)
1713 for(nt=thing; nt; nt=nt->next)
1714 if(nt->parent == t){
1733 mesg("%s modified", t->name);
1737 /* fiddle to save redrawing unmoved things */
1741 for(ct=thing; ct; ct=ct->next)
1742 if(ct->next==t || ct->next->parent==t)
1762 new = tget(t->name);
1767 for(nt=thing; nt; nt=nt->next)
1768 if(nt->parent == t){
1769 if(!rectinrect(nt->b->r, new->b->r)
1770 || new->b->depth!=nt->b->depth){
1777 if((t->s==0) != (new->s==0))
1779 if((t->face==0) != (new->face==0))
1781 if(t->s){ /* check same char */
1782 if(nt->c >= new->s->n)
1784 i = &new->s->info[nt->c];
1787 r.min.y = new->b->r.min.y;
1788 r.max.y = new->b->r.max.y;
1789 if(!eqrect(r, nt->b->r))
1793 draw(nt->b, nt->b->r, new->b, nil, nt->b->r.min);
1795 new->next = t->next;
1799 for(nt=thing; nt->next!=t; nt=nt->next)
1809 for(nt=thing; nt; nt=nt->next)
1810 if(nt==new || nt->parent==new)
1812 drawthing(nt, 0); /* can draw in place */
1814 redraw(nt); /* must redraw all below */
1828 if(t==0 || t->s==0){
1829 mesg("not a subfont");
1833 if(type(buf, "char (hex or character or hex-hex)") == 0)
1835 if(utflen(buf) == 1){
1836 chartorune(&r, buf);
1840 if(!strchr(hex, buf[0])){
1841 mesg("illegal hex character");
1844 c = strtoul(buf, 0, 16);
1846 p = utfrune(buf, '-');
1848 d = strtoul(p+1, 0, 16);
1850 mesg("invalid range");
1858 if(c<0 || c>=t->s->n){
1859 mesg("0x%lux not in font %s", c+t->off, t->name);
1862 openedit(t, Pt(0, 0), c);
1868 apply(void (*f)(Thing*))
1874 if(mouse.buttons == 4)
1875 for(t=thing; t; t=t->next)
1876 if(ptinrect(mouse.xy, t->er)){
1886 complement(Image *t)
1891 n = Dy(t->r)*bytesperline(t->r, t->depth);
1895 unloadimage(t, t->r, buf, n);
1898 loadimage(t, t->r, buf, n);
1906 Thing *st, *dt, *nt;
1907 Rectangle sr, dr, fr;
1914 for(st=thing; st; st=st->next)
1915 if(rectXrect(sr, st->r))
1919 /* click gives full rectangle */
1920 if(Dx(sr)<4 && Dy(sr)<4)
1922 rectclip(&sr, st->r);
1923 p1 = realpt(st, sr.min);
1924 p2 = realpt(st, Pt(sr.min.x, sr.max.y));
1926 if(p1.x != p2.x){ /* swept across a fold */
1928 mesg("sweep spans a fold");
1931 p2 = realpt(st, sr.max);
1934 fr.min = screenpt(st, sr.min);
1935 fr.max = screenpt(st, sr.max);
1936 p1 = subpt(p2, p1); /* diagonal */
1937 if(p1.x==0 || p1.y==0)
1939 border(screen, fr, -1, values[Blue], ZP);
1941 for(; mouse.buttons==0; mouse=emouse()){
1942 for(dt=thing; dt; dt=dt->next)
1943 if(ptinrect(mouse.xy, dt->er))
1946 edrawgetrect(insetrect(dr, -Borderwidth), 0);
1950 dr.max = screenpt(dt, realpt(dt, mouse.xy));
1951 dr.min = subpt(dr.max, mulpt(p1, dt->mag));
1952 if(!rectXrect(dr, dt->r))
1954 edrawgetrect(insetrect(dr, -Borderwidth), 1);
1957 /* if up==1, we had a hit */
1960 edrawgetrect(insetrect(dr, -Borderwidth), 0);
1961 but = mouse.buttons;
1966 for(nt=thing; nt; nt=nt->next)
1967 if(rectXrect(dr, nt->r)){
1969 mesg("ambiguous sweep");
1976 p1 = realpt(dt, dr.min);
1977 p2 = realpt(dt, Pt(dr.min.x, dr.max.y));
1980 p2 = realpt(dt, dr.max);
1985 tmp = allocimage(display, dr, dt->b->chan, 0, 255);
1988 mesg("can't allocate temporary");
1991 draw(tmp, dr, st->b, nil, sr.min);
1992 if(!complement(tmp))
1994 draw(dt->b, dr, tmp, nil, dr.min);
1997 draw(dt->b, dr, st->b, nil, sr.min);
1999 draw(dt->parent->b, dr, dt->b, nil, dr.min);
2003 for(nt=thing; nt; nt=nt->next)
2004 if(nt->parent==dt && rectXrect(dr, nt->b->r)){
2005 draw(nt->b, dr, dt->b, nil, dr.min);
2012 /* clear blue box */
2024 sel = emenuhit(3, &mouse, &menu3);
2027 if(type(buf, "file")){
2053 for(t=thing; t; t=t->next)
2059 mesg("%s modified", mod);
2064 if(mouse.buttons == 4){