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*);
175 main(int argc, char *argv[])
182 if(initdraw(error, 0, "tweak") < 0){
183 fprint(2, "tweak: initdraw failed: %r\n");
186 for(i=0; i<256; i++){
187 values[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, cmap2rgba(i));
188 greyvalues[i] = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, (i<<24)|(i<<16)|(i<<8)|0xFF);
189 if(values[i] == 0 || greyvalues[i] == 0)
190 drawerror(display, "can't allocate image");
192 einit(Emouse|Ekeyboard);
201 flushimage(display, 1);
211 if(mouse.buttons & 3){
215 if(mouse.buttons & 4)
221 error(Display*, char *s)
224 mesg("can't read %s: %s: %r", file, s);
226 mesg("/dev/bitblt error: %s", s);
238 if(thing==0 || thing==t)
239 draw(screen, editr, display->white, nil, ZP);
243 for(nt=thing; nt->next!=t; nt=nt->next)
245 draw(screen, Rect(screen->r.min.x, nt->er.max.y, editr.max.x, editr.max.y),
246 display->white, nil, ZP);
248 for(nt=t; nt; nt=nt->next){
251 p = Pt(editr.min.x, nt->er.max.y);
252 draw(screen, Rpt(p, editr.max), display->white, nil, ZP);
261 if(new && getwindow(display, Refnone) < 0)
262 error(display, "can't reattach to window");
263 cntlr = insetrect(screen->clipr, 1);
266 textr.min.y = textr.max.y - font->height;
267 cntlr.max.y = cntlr.min.y + font->height;
268 editr.min.y = cntlr.max.y+1;
269 editr.max.y = textr.min.y-1;
270 draw(screen, screen->clipr, display->white, nil, ZP);
271 draw(screen, Rect(editr.min.x, editr.max.y, editr.max.x+1, editr.max.y+1), display->black, nil, ZP);
272 replclipr(screen, 0, editr);
277 mesgstr(Point p, int line, char *s)
282 r.min.y += line*font->height;
283 r.max.y = r.min.y+font->height;
284 r.max.x = editr.max.x;
286 replclipr(screen, 0, r);
287 draw(screen, r, values[0xDD], nil, ZP);
289 string(screen, r.min, display->black, ZP, font, s);
290 replclipr(screen, 0, c);
291 flushimage(display, 1);
301 vseprint(buf, buf+sizeof(buf), fmt, arg);
303 mesgstr(textr.min, 0, buf);
307 tmesg(Thing *t, int line, char *fmt, ...)
313 vseprint(buf, buf+sizeof(buf), fmt, arg);
315 mesgstr(t->tr.min, line, buf);
322 sprint(l, "mag: %d but1: %d but2: %d invert-on-copy: %c", mag, but1val, but2val, "ny"[invert]);
331 mesgstr(cntlr.min, 0, buf);
335 stext(Thing *t, char *l0, char *l1)
341 sprint(buf, "depth:%d r:%d %d %d %d ",
342 t->b->depth, t->b->r.min.x, t->b->r.min.y,
343 t->b->r.max.x, t->b->r.max.y);
345 sprint(buf+strlen(buf), "mag: %d ", t->mag);
346 sprint(l0, "%s file: %s", buf, t->name);
348 fc = &t->parent->s->info[t->c];
349 sprint(l1, "c(hex): %x c(char): %C x: %d "
350 "top: %d bottom: %d left: %d width: %d iwidth: %d",
351 (int)(t->c+t->parent->off), (int)(t->c+t->parent->off),
352 fc->x, fc->top, fc->bottom, fc->left,
353 fc->width, Dx(t->b->r));
355 sprint(l1, "offset(hex): %ux n:%d height:%d ascent:%d",
356 t->off, t->s->n, t->s->height, t->s->ascent);
362 char l0[256], l1[256];
376 for(t=thing; t; t=t->next)
380 /* imported from libdraw/arith.c to permit an extern log2 function */
381 static int log2[] = {
382 -1, 0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4,
383 -1, -1, -1, -1, -1, -1, -1, 4 /* BUG */, -1, -1, -1, -1, -1, -1, -1, 5
387 value(Image *b, int x)
394 mesg("ldepth too large");
398 mask = (1<<w)-1; /* ones at right end of word */
399 x -= b->r.min.x&~(7>>l); /* adjust x relative to first pixel */
401 v >>= ((7>>l)<<l) - ((x&(7>>l))<<l); /* pixel at right end of word */
402 v &= mask; /* pixel at right end of word */
410 if(d > screen->depth)
411 v >>= d - screen->depth;
413 while(d < screen->depth && d < 8){
418 mesg("internal error: bad color");
425 drawthing(Thing *nt, int link)
427 int nl, nf, i, x, y, sx, sy, fdx, dx, dy, v;
439 for(t=thing; t->next; t=t->next)
448 for(t=thing; t->next!=nt; t=t->next)
458 fdx = Dx(editr) - 2*Border;
468 nt->er.min.x = editr.min.x;
469 nt->er.max.x = nt->er.min.x + Border + dx + Border;
470 if(nt->er.max.x > editr.max.x)
471 nt->er.max.x = editr.max.x;
472 nt->er.max.y = nt->er.min.y + Border + nf*(dy+Border);
473 nt->r = insetrect(nt->er, Border);
474 nt->er.max.x = editr.max.x;
475 draw(screen, nt->er, display->white, nil, ZP);
477 p1 = Pt(nt->r.min.x-1, nt->r.min.y+i*(Border+dy));
478 /* draw portion of bitmap */
479 p = Pt(p1.x+1, p1.y);
481 draw(screen, Rect(p.x, p.y, p.x+fdx+Dx(b->r), p.y+Dy(b->r)),
482 b, nil, Pt(b->r.min.x+i*fdx, b->r.min.y));
484 for(y=b->r.min.y; y<b->r.max.y; y++){
485 sy = p.y+(y-b->r.min.y)*nt->mag;
486 unloadimage(b, Rect(b->r.min.x, y, b->r.max.x, y+1), data, sizeof data);
487 for(x=b->r.min.x+i*(fdx/nt->mag); x<b->r.max.x; x++){
488 sx = p.x+(x-i*(fdx/nt->mag)-b->r.min.x)*nt->mag;
489 if(sx >= nt->r.max.x)
491 v = bvalue(value(b, x), b->depth);
495 draw(screen, Rect(sx, sy, sx+nt->mag, sy+nt->mag),
496 greyvalues[v], nil, ZP);
498 draw(screen, Rect(sx, sy, sx+nt->mag, sy+nt->mag),
506 col = display->black;
508 col = display->white;
509 draw(screen, Rect(p1.x, p1.y, p1.x+1, p1.y+dy+Border), col, nil, ZP);
510 /* line across top */
511 draw(screen, Rect(p1.x, p1.y-1, nt->r.max.x+Border, p1.y), display->black, nil, ZP);
515 col = display->black;
518 col = display->white;
520 /* line down right */
521 draw(screen, Rect(p2.x, p2.y, p2.x+1, p2.y+dy+Border), col, nil, ZP);
522 /* line across bottom */
525 draw(screen, Rect(p1.x, p1.y-1, p2.x,p1.y), display->black, nil, ZP);
528 nt->tr.min.x = editr.min.x;
529 nt->tr.max.x = editr.max.x;
530 nt->tr.min.y = nt->er.max.y + Border;
531 nt->tr.max.y = nt->tr.min.y + nl;
532 nt->er.max.y = nt->tr.max.y + Border;
542 return 10 + (c - 'a');
544 return 10 + (c - 'A');
551 int i, j, fd, face, x, y, c, chan;
561 errstr((char*)buf, sizeof buf); /* flush pending error message */
562 memmove(oerr, err, sizeof err);
567 memmove(err, oerr, sizeof err);
570 fd = open(file, OREAD);
572 mesg("can't open %s: %r", file);
577 mesg("can't stat bitmap file %s: %r", file);
581 if(read(fd, buf, 11) != 11){
582 mesg("can't read %s: %r", file);
590 if(memcmp(data, "0x", 2)==0 && data[4]==','){
596 data = malloc(d->length+1);
598 mesg("can't malloc buffer: %r");
603 if(read(fd, data, d->length) != d->length){
604 mesg("can't read cursor file %s: %r", file);
608 b = allocimage(display, Rect(0, 0, 16, 32), GREY1, 0, DNofill);
610 mesg("image alloc failed file %s: %r", file);
617 if((c=data[i]) == '\0')
619 if(c=='0' && data[i+1] == 'x'){
624 buf[x++] = (tohex(c)<<4) | tohex(data[i+1]);
630 loadimage(b, Rect(0, 0, 16, 32), buf, sizeof buf);
632 }else if(memcmp(buf, "0x", 2)==0){
638 data = malloc(d->length+1);
640 mesg("can't malloc buffer: %r");
645 if(read(fd, data, d->length) != d->length){
646 mesg("can't read bitmap file %s: %r", file);
650 for(y=0,i=0; i<d->length; i++)
655 mesg("ill-formed face file %s", file);
660 for(x=0,i=0; (c=data[i])!='\n'; ){
661 if(c==',' || c==' ' || c=='\t'){
665 if(c=='0' && data[i+1] == 'x'){
694 b = allocimage(display, Rect(0, 0, y, y), chan, 0, -1);
696 mesg("image alloc failed file %s: %r", file);
703 for(x=0; (c=data[i])!='\n'; ){
704 if(c=='0' && data[i+1] == 'x'){
709 buf[x++] = ~((tohex(c)<<4) | tohex(data[i+1]));
716 loadimage(b, Rect(0, j, y, j+1), buf, sizeof buf);
722 b = readimage(display, fd, 0);
724 mesg("can't read bitmap file %s: %r", file);
728 if(seek(fd, 0, 1) < d->length)
729 s = readsubfonti(display, file, fd, b, 0);
732 t = malloc(sizeof(Thing));
735 mesg("malloc failed: %r");
742 t->name = strdup(file);
755 memmove(err, oerr, sizeof err);
760 atline(int x, Point p, char *line, char *buf)
762 char *s, *c, *word, *hit;
769 for(s=line; *s; s+=w){
770 w = chartorune(&r, s);
771 x += runestringnwidth(font, &r, 1);
772 if(wasblank && r!=' ')
785 c = utfrune(hit, ':');
786 strncpy(buf, hit, c-hit);
792 attext(Thing *t, Point p, char *buf)
794 char l0[256], l1[256];
796 if(!ptinrect(p, t->tr))
799 if(p.y < t->tr.min.y+font->height)
800 return atline(t->r.min.x, p, l0, buf);
802 return atline(t->r.min.x, p, l1, buf);
806 type(char *buf, int nbuf, char *tag)
813 e = buf + nbuf-UTFmax-1;
816 mesg("%s: %s", tag, buf);
823 case 0x15: /* control-U */
832 p += runetochar(p, &r);
838 textedit(Thing *t, char *tag)
847 int i, ld, d, w, c, doredraw, fdx, x;
851 if(type(buf, sizeof(buf), tag) == 0)
853 if(strcmp(tag, "file") == 0){
856 mesg("illegal file name");
859 if(strcmp(t->name, buf) != 0){
865 for(nt=thing; nt; nt=nt->next)
866 if(t==nt || t->parent==nt || nt->parent==t){
868 nt->name = strdup(buf);
870 mesg("malloc failed: %r");
877 if(strcmp(tag, "depth") == 0){
878 if(buf[0]<'0' || '9'<buf[0] || (d=atoi(buf))<0 || d>8 || log2[d]<0){
879 mesg("illegal ldepth");
891 chan = CHAN1(CGrey, d);
892 for(nt=thing; nt; nt=nt->next){
893 if(nt!=t && nt!=t->parent && nt->parent!=t)
895 b = allocimage(display, nt->b->r, chan, 0, 0);
898 mesg("image alloc failed: %r");
901 draw(b, b->r, nt->b, nil, nt->b->r.min);
905 b = allocimage(display, nt->b->r, chan, 0, -1);
908 draw(b, b->r, nt->b, nil, nt->b->r.min);
909 f = allocsubfont(t->name, nt->s->n, nt->s->height, nt->s->ascent, nt->s->info, b);
913 mesg("can't make subfont: %r");
916 nt->s->info = 0; /* prevent it being freed */
925 if(strcmp(tag, "mag") == 0){
926 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<=0 || ld>Maxmag){
927 mesg("illegal magnification");
936 if(strcmp(tag, "r") == 0){
938 mesg("can't change rectangle of subfont\n");
942 r.min.x = strtoul(s, &s, 0);
943 r.min.y = strtoul(s, &s, 0);
944 r.max.x = strtoul(s, &s, 0);
945 r.max.y = strtoul(s, &s, 0);
946 if(Dx(r)<=0 || Dy(r)<=0){
947 mesg("illegal rectangle");
952 for(nt=thing; nt; nt=nt->next){
953 if(nt->parent==t && !rectinrect(nt->b->r, r))
956 b = allocimage(display, r, t->b->chan, 0, 0);
959 draw(b, r, t->b, nil, r.min);
962 b = allocimage(display, r, t->b->chan, 0, 0);
969 if(strcmp(tag, "ascent") == 0){
970 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0 || ld>t->s->height){
971 mesg("illegal ascent");
974 if(t->s->ascent == ld)
981 if(strcmp(tag, "height") == 0){
982 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0){
983 mesg("illegal height");
986 if(t->s->height == ld)
993 if(strcmp(tag, "left")==0 || strcmp(tag, "width") == 0){
994 if(buf[0]<'0' || '9'<buf[0] || (ld=atoi(buf))<0){
995 mesg("illegal value");
998 fc = &t->parent->s->info[t->c];
999 if(strcmp(tag, "left")==0){
1012 if(strcmp(tag, "offset(hex)") == 0){
1013 if(!strchr(hex, buf[0])){
1015 mesg("illegal offset");
1019 ld = strtoul(buf, &s, 16);
1024 for(nt=thing; nt; nt=nt->next)
1029 if(strcmp(tag, "n") == 0){
1030 if(buf[0]<'0' || '9'<buf[0] || (w=atoi(buf))<=0){
1039 for(nt=thing; nt; nt=nt->next)
1040 if(nt->parent == t){
1047 r.max.x = f->info[w].x;
1048 b = allocimage(display, r, t->b->chan, 0, 0);
1051 draw(b, b->r, t->b, nil, r.min);
1052 fdx = Dx(editr) - 2*Border;
1053 if(Dx(t->b->r)/fdx != Dx(b->r)/fdx)
1057 b = allocimage(display, r, t->b->chan, 0, 0);
1060 draw(b, b->r, t->b, nil, r.min);
1061 nfc = malloc((w+1)*sizeof(Fontchar));
1063 mesg("malloc failed");
1068 for(i=0; i<=w && i<=f->n; i++)
1071 memset(nfc+i, 0, ((w+1)-i)*sizeof(Fontchar));
1075 f = allocsubfont(t->name, w, f->height, f->ascent, nfc, b);
1078 t->s->bits = nil; /* don't free it */
1089 if(strcmp(tag, "iwidth") == 0){
1090 if(buf[0]<'0' || '9'<buf[0] || (w=atoi(buf))<0){
1091 mesg("illegal iwidth");
1097 r = t->parent->b->r;
1102 b = allocimage(display, r, t->b->chan, 0, 0);
1106 draw(b, Rect(b->r.min.x, b->r.min.y,
1107 b->r.min.x+(fc[1].x-t->b->r.min.x), b->r.min.y+Dy(t->b->r)),
1108 t->b, nil, t->b->r.min);
1109 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)),
1110 t->b, nil, Pt(fc[1].x, t->b->r.min.y));
1111 fdx = Dx(editr) - 2*Border;
1113 if(Dx(t->b->r)/fdx != Dx(b->r)/fdx)
1117 b = allocimage(display, r, t->b->chan, 0, 0);
1120 draw(b, b->r, t->b, nil, t->b->r.min);
1122 for(i=c+1; i<=f->n; i++, fc++)
1124 f = allocsubfont(t->name, f->n, f->height, f->ascent,
1128 /* t->s and f share info; free carefully */
1139 /* redraw all affected chars */
1140 for(nt=thing; nt; nt=nt->next){
1141 if(nt->parent!=t || nt->c<c)
1143 fc = &f->info[nt->c];
1145 r.min.y = nt->b->r.min.y;
1147 r.max.y = nt->b->r.max.y;
1148 b = allocimage(display, r, nt->b->chan, 0, 0);
1151 draw(b, r, t->b, nil, r.min);
1153 if(Dx(nt->b->r)/fdx != Dx(b->r)/fdx)
1169 mesg("cannot edit %s in file %s", tag, t->name);
1179 if(type(buf, sizeof(buf), tag) == 0)
1181 if(strcmp(tag, "mag") == 0){
1182 if(buf[0]<'0' || '9'<buf[0] || (l=atoi(buf))<=0 || l>Maxmag){
1183 mesg("illegal magnification");
1190 if(strcmp(tag, "but1")==0
1191 || strcmp(tag, "but2")==0){
1192 if(buf[0]<'0' || '9'<buf[0] || (l=atoi(buf))<0 || l>255){
1193 mesg("illegal value");
1196 if(strcmp(tag, "but1") == 0)
1198 else if(strcmp(tag, "but2") == 0)
1203 if(strcmp(tag, "invert-on-copy")==0){
1204 if(buf[0]=='y' || buf[0]=='1')
1206 else if(buf[0]=='n' || buf[0]=='0')
1209 mesg("illegal value");
1215 mesg("cannot edit %s", tag);
1221 while((mouse.buttons==0) != ud)
1226 screenpt(Thing *t, Point realp)
1231 fdx = Dx(editr)-2*Border;
1234 p = mulpt(subpt(realp, t->b->r.min), t->mag);
1235 if(fdx < Dx(t->b->r)*t->mag){
1237 p.y += n * (Dy(t->b->r)*t->mag+Border);
1240 p = addpt(p, t->r.min);
1245 realpt(Thing *t, Point screenp)
1250 fdx = (Dx(editr)-2*Border);
1253 p.y = screenp.y-t->r.min.y;
1255 if(fdx < Dx(t->b->r)*t->mag){
1256 dy = Dy(t->b->r)*t->mag+Border;
1261 p.x += screenp.x-t->r.min.x;
1262 p = addpt(divpt(p, t->mag), t->b->r.min);
1267 sweep(int but, Rectangle *r)
1272 esetcursor(&sweep0);
1274 if(mouse.buttons != (1<<(but-1))){
1280 for(t=thing; t; t=t->next)
1281 if(ptinrect(p, t->r))
1284 p = screenpt(t, realpt(t, p));
1289 while(mouse.buttons == (1<<(but-1))){
1290 edrawgetrect(insetrect(*r, -Borderwidth), 1);
1292 edrawgetrect(insetrect(*r, -Borderwidth), 0);
1295 q = screenpt(t, realpt(t, q));
1298 *r = canonrect(Rpt(p, q));
1310 openedit(Thing *t, Point pt, int c)
1319 if(t->b->depth > 8){
1320 mesg("image has depth %d; can't handle >8", t->b->depth);
1326 /* if big enough to bother, sweep box */
1327 if(Dx(br)<=16 && Dy(br)<=16)
1332 r = rectaddpt(r, subpt(br.min, t->r.min));
1333 if(!rectclip(&r, br))
1338 }else if(Dx(r) < 4){
1340 mesg("rectangle too small");
1350 fc = &t->s->info[c];
1354 r.max.y = br.min.y + Dy(br);
1356 /* just point at character */
1358 p = addpt(pt, subpt(br.min, t->r.min));
1361 for(c=0; c<t->s->n; c++,fc++){
1365 r.max.x = x + fc[1].x - fc[0].x;
1366 r.max.y = y + Dy(br);
1369 if(r.max.x >= br.min.x+Dx(t->r)){
1372 if(fc[1].x > fc[0].x)
1375 x += fc[1].x - fc[0].x;
1383 nt = malloc(sizeof(Thing));
1386 mesg("can't allocate: %r");
1389 memset(nt, 0, sizeof(Thing));
1391 nt->b = allocimage(display, r, t->b->chan, 0, DNofill);
1396 draw(nt->b, r, t->b, nil, r.min);
1397 nt->name = strdup(t->name);
1409 ckinfo(Thing *t, Rectangle mod)
1411 int i, j, k, top, bot, n, zero;
1419 if(t->s==0 || Dy(t->b->r)==0)
1422 /* check bounding boxes */
1423 fc = &t->s->info[0];
1424 r.min.y = t->b->r.min.y;
1425 r.max.y = t->b->r.max.y;
1426 for(i=0; i<t->s->n; i++, fc++){
1429 if(!rectXrect(mod, r))
1431 if(b==0 || Dx(b->r)<Dx(r)){
1434 b = allocimage(display, rectsubpt(r, r.min), t->b->chan, 0, 0);
1436 mesg("can't alloc image");
1440 draw(b, b->r, display->white, nil, ZP);
1441 draw(b, b->r, t->b, nil, r.min);
1444 n = 2+((Dx(r)/8)*t->b->depth);
1445 for(j=0; j<b->r.max.y; j++){
1447 unloadimage(b, Rect(b->r.min.x, j, b->r.max.x, j+1), data, sizeof data);
1462 if(top!=fc->top || bot!=fc->bottom){
1465 for(nt=thing; nt; nt=nt->next)
1466 if(nt->parent==t && nt->c==i)
1475 twidpix(Thing *t, Point p, int set)
1481 if(!ptinrect(p, b->r))
1487 if(b->chan == GREY8)
1491 draw(b, Rect(p.x, p.y, p.x+1, p.y+1), v, nil, ZP);
1493 draw(screen, Rect(p.x, p.y, p.x+t->mag, p.y+t->mag), v, nil, ZP);
1505 if(mouse.buttons!=1 && mouse.buttons!=2){
1509 set = mouse.buttons==1;
1511 lastp = addpt(b->r.min, Pt(-1, -1));
1512 mod = Rpt(addpt(b->r.max, Pt(1, 1)), lastp);
1513 while(mouse.buttons){
1514 p = realpt(t, mouse.xy);
1515 if(!eqpt(p, lastp)){
1517 if(ptinrect(p, b->r)){
1518 for(nt=thing; nt; nt=nt->next)
1519 if(nt->parent==t->parent || nt==t->parent)
1520 twidpix(nt, p, set);
1529 if(p.x >= mod.max.x)
1531 if(p.y >= mod.max.y)
1544 char line[128], buf[128];
1547 if(ptinrect(mouse.xy, cntlr)){
1549 if(atline(cntlr.min.x, mouse.xy, line, buf)){
1550 if(mouse.buttons == 1)
1558 for(t=thing; t; t=t->next){
1559 if(attext(t, mouse.xy, buf)){
1560 if(mouse.buttons == 1)
1566 if(ptinrect(mouse.xy, t->r)){
1568 if(mouse.buttons == 1){
1585 int i, j, x, y, fd, ws, ld;
1592 fd = create(t->name, OWRITE, 0666);
1594 mesg("can't write %s: %r", t->name);
1597 if(t->face && t->b->depth <= 4){
1599 ld = log2[t->b->depth];
1600 /* This heuristic reflects peculiarly different formats */
1602 if(t->face == 2) /* cursor file */
1604 else if(Dx(r)<32 || ld==0)
1606 Binit(&buf, fd, OWRITE);
1607 if(t->face == CURSOR)
1609 for(y=r.min.y; y<r.max.y; y++){
1610 unloadimage(t->b, Rect(r.min.x, y, r.max.x, y+1), data, sizeof data);
1612 for(x=r.min.x; x<r.max.x; j+=ws,x+=ws*8>>ld){
1615 Bprint(&buf, "%.2x", data[i+j]);
1618 if(t->face == CURSOR){
1620 case 3: case 7: case 11: case 19: case 23: case 27:
1621 Bprint(&buf, "\n ");
1624 Bprint(&buf, "},\n{");
1627 Bprint(&buf, "}\n");
1635 if(writeimage(fd, t->b, 0)<0 || (t->s && writesubfont(fd, t->s)<0)){
1637 mesg("can't write %s: %r", t->name);
1641 mesg("wrote %s", t->name);
1653 if(mouse.buttons != 4)
1655 for(t=thing; t; t=t->next){
1657 if(ptinrect(mouse.xy, t->r)){
1658 while(ptinrect(mouse.xy, t->r) && mouse.buttons==4){
1659 p = realpt(t, mouse.xy);
1660 if(!eqpt(p, lastp)){
1662 unloadimage(t->b, Rect(t->b->r.min.x, p.y, t->b->r.max.x, p.y+1), data, sizeof data);
1663 mesg("[%d,%d] = %d=0x%ux", p.x, p.y, value(t->b, p.x), value(t->b, p.x));
1686 for(nt=thing; nt->next!=t; nt=nt->next)
1691 for(nt=thing; nt; nt=nt->next)
1692 if(nt->parent == t){
1711 mesg("%s modified", t->name);
1715 /* fiddle to save redrawing unmoved things */
1719 for(ct=thing; ct; ct=ct->next)
1720 if(ct->next==t || ct->next->parent==t)
1740 new = tget(t->name);
1745 for(nt=thing; nt; nt=nt->next)
1746 if(nt->parent == t){
1747 if(!rectinrect(nt->b->r, new->b->r)
1748 || new->b->depth!=nt->b->depth){
1755 if((t->s==0) != (new->s==0))
1757 if((t->face==0) != (new->face==0))
1759 if(t->s){ /* check same char */
1760 if(nt->c >= new->s->n)
1762 i = &new->s->info[nt->c];
1765 r.min.y = new->b->r.min.y;
1766 r.max.y = new->b->r.max.y;
1767 if(!eqrect(r, nt->b->r))
1771 draw(nt->b, nt->b->r, new->b, nil, nt->b->r.min);
1773 new->next = t->next;
1777 for(nt=thing; nt->next!=t; nt=nt->next)
1787 for(nt=thing; nt; nt=nt->next)
1788 if(nt==new || nt->parent==new)
1790 drawthing(nt, 0); /* can draw in place */
1792 redraw(nt); /* must redraw all below */
1806 if(t==0 || t->s==0){
1807 mesg("not a subfont");
1811 if(type(buf, sizeof(buf), "char (hex or character or hex-hex)") == 0)
1813 if(utflen(buf) == 1){
1814 chartorune(&r, buf);
1818 if(!strchr(hex, buf[0])){
1819 mesg("illegal hex character");
1822 c = strtoul(buf, 0, 16);
1824 p = utfrune(buf, '-');
1826 d = strtoul(p+1, 0, 16);
1828 mesg("invalid range");
1837 mesg("0x%lux not in font %s", c+t->off, t->name);
1840 openedit(t, Pt(0, 0), c);
1846 apply(void (*f)(Thing*))
1852 if(mouse.buttons == 4)
1853 for(t=thing; t; t=t->next)
1854 if(ptinrect(mouse.xy, t->er)){
1864 complement(Image *t)
1869 n = Dy(t->r)*bytesperline(t->r, t->depth);
1873 unloadimage(t, t->r, buf, n);
1876 loadimage(t, t->r, buf, n);
1884 Thing *st, *dt, *nt;
1885 Rectangle sr, dr, fr;
1892 for(st=thing; st; st=st->next)
1893 if(rectXrect(sr, st->r))
1897 /* click gives full rectangle */
1898 if(Dx(sr)<4 && Dy(sr)<4)
1900 rectclip(&sr, st->r);
1901 p1 = realpt(st, sr.min);
1902 p2 = realpt(st, Pt(sr.min.x, sr.max.y));
1904 if(p1.x != p2.x){ /* swept across a fold */
1906 mesg("sweep spans a fold");
1909 p2 = realpt(st, sr.max);
1912 fr.min = screenpt(st, sr.min);
1913 fr.max = screenpt(st, sr.max);
1914 p1 = subpt(p2, p1); /* diagonal */
1915 if(p1.x==0 || p1.y==0)
1917 border(screen, fr, -1, values[Blue], ZP);
1919 for(; mouse.buttons==0; mouse=emouse()){
1920 for(dt=thing; dt; dt=dt->next)
1921 if(ptinrect(mouse.xy, dt->er))
1924 edrawgetrect(insetrect(dr, -Borderwidth), 0);
1928 dr.max = screenpt(dt, realpt(dt, mouse.xy));
1929 dr.min = subpt(dr.max, mulpt(p1, dt->mag));
1930 if(!rectXrect(dr, dt->r))
1932 edrawgetrect(insetrect(dr, -Borderwidth), 1);
1935 /* if up==1, we had a hit */
1938 edrawgetrect(insetrect(dr, -Borderwidth), 0);
1939 but = mouse.buttons;
1944 for(nt=thing; nt; nt=nt->next)
1945 if(rectXrect(dr, nt->r)){
1947 mesg("ambiguous sweep");
1954 p1 = realpt(dt, dr.min);
1955 p2 = realpt(dt, Pt(dr.min.x, dr.max.y));
1958 p2 = realpt(dt, dr.max);
1963 tmp = allocimage(display, dr, dt->b->chan, 0, 255);
1966 mesg("can't allocate temporary");
1969 draw(tmp, dr, st->b, nil, sr.min);
1970 if(!complement(tmp))
1972 draw(dt->b, dr, tmp, nil, dr.min);
1975 draw(dt->b, dr, st->b, nil, sr.min);
1977 draw(dt->parent->b, dr, dt->b, nil, dr.min);
1981 for(nt=thing; nt; nt=nt->next)
1982 if(nt->parent==dt && rectXrect(dr, nt->b->r)){
1983 draw(nt->b, dr, dt->b, nil, dr.min);
1990 /* clear blue box */
2002 sel = emenuhit(3, &mouse, &menu3);
2005 if(type(buf, sizeof(buf), "file")){
2031 for(t=thing; t; t=t->next)
2037 mesg("%s modified", mod);
2042 if(mouse.buttons == 4){