9 typedef struct Page Page;
40 char pagespool[] = "/tmp/pagespool.";
49 char *pagemenugen(int i);
84 {0xff, 0x80, 0xff, 0x80, 0xff, 0x00, 0xfe, 0x00,
85 0xff, 0x00, 0xff, 0x80, 0xff, 0xc0, 0xef, 0xe0,
86 0xc7, 0xf0, 0x03, 0xf0, 0x01, 0xe0, 0x00, 0xc0,
87 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, 0x03, 0xff, },
88 {0x00, 0x00, 0x7f, 0x00, 0x7e, 0x00, 0x7c, 0x00,
89 0x7e, 0x00, 0x7f, 0x00, 0x6f, 0x80, 0x47, 0xc0,
90 0x03, 0xe0, 0x01, 0xf0, 0x00, 0xe0, 0x00, 0x40,
91 0x00, 0x00, 0x01, 0xb6, 0x01, 0xb6, 0x00, 0x00, }
94 void showpage(Page *);
95 void drawpage(Page *);
96 Point pagesize(Page *);
99 addpage(Page *up, char *label, int (*popen)(Page *), void *pdata, int fd)
103 p = mallocz(sizeof(*p), 1);
104 p->label = strdup(label);
119 up->down = up->tail = p;
127 if(up && current == up)
133 resizewin(Point size)
137 if((wctl = open("/dev/wctl", OWRITE)) < 0)
140 size = addpt(size, Pt(Borderwidth*2, Borderwidth*2));
141 fprint(wctl, "resize -dx %d -dy %d\n", size.x, size.y);
146 createtmp(ulong id, char *pfx)
150 snprint(nam, sizeof nam, "%s%s%.12d%.8lux", pagespool, pfx, getpid(), id ^ 0xcafebabe);
151 return create(nam, OEXCL|ORCLOSE|ORDWR, 0600);
155 catchnote(void *, char *msg)
157 if(strstr(msg, "sys: write on closed pipe"))
159 if(strstr(msg, "hangup"))
161 if(strstr(msg, "alarm"))
167 pipeline(int fd, char *fmt, ...)
169 char buf[128], *argv[4];
178 switch(rfork(RFPROC|RFFDG|RFREND|RFNOWAIT)){
192 vsnprint(buf, sizeof buf, fmt, arg);
199 exec("/bin/rc", argv);
200 sysfatal("exec: %r");
216 if((fd = dup(p->fd, -1)) < 0){
224 pipeline(fd, "%s", (char*)p->data);
227 * dont keep the file descriptor arround if it can simply
230 fd2path(p->fd, nam, sizeof(nam));
231 if(strncmp(nam, pagespool, strlen(pagespool))){
234 p->data = strdup(nam);
241 typedef struct Ghost Ghost;
260 switch(rfork(RFFDG|RFPROC|RFMEM|RFNOWAIT)){
269 fprint(gs->pin, "%s DoPDFPage\n"
272 "dup (THIS IS NOT AN INFERNO BITMAP\\n) writestring "
273 "flushfile\n", p->label);
274 while((n = read(gs->pdat, buf, sizeof buf)) > 0){
275 if(memcmp(buf, "THIS IS NOT AN INFERNO BITMAP\n", 30) == 0)
279 if(write(pfd[1], buf, n) != n){
292 infernobithdr(char *buf, int n)
295 if(memcmp(buf, "compressed\n", 11) == 0)
297 if(strtochan((char*)buf))
299 if(memcmp(buf, " ", 10) == 0 &&
300 '0' <= buf[10] && buf[10] <= '9' &&
310 int n, i, pdf, ifd, ofd, pin[2], pout[2], pdat[2];
311 char buf[NBUF], nam[32], *argv[16];
317 if(read(ifd, buf, 5) != 5)
320 if(memcmp(buf, "%PDF-", 5) == 0)
322 p->text = strdup(p->label);
341 switch(rfork(RFREND|RFPROC|RFFDG|RFNOWAIT)){
348 if(dup(pout[1], 1)<0)
358 if(dup(pdat[1], 3)<0)
372 pipeline(4, "%s", (char*)p->data);
376 argv[2] = "-sDEVICE=plan9";
377 argv[3] = "-sOutputFile=/fd/3";
379 argv[5] = pdf ? "-dDELAYSAFER" : "-dSAFER";
381 argv[7] = "-dTextAlphaBits=4";
382 argv[8] = "-dGraphicsAlphaBits=4";
383 snprint(buf, sizeof buf, "-r%d", ppi);
385 argv[10] = "-dDOINTERPOLATE";
386 argv[11] = pdf ? "-" : "/fd/4";
388 exec("/bin/gs", argv);
389 sysfatal("exec: %r");
400 "/PAGEOUT (/fd/1) (w) file def\n"
401 "/PAGE== { PAGEOUT exch write==only PAGEOUT (\\n) writestring PAGEOUT flushfile } def\n"
405 "/PDFSave null def\n"
406 "/DSCPageCount 0 def\n"
407 "/DoPDFPage {dup /Page# exch store pdfgetpage pdfshowpage } def\n"
409 "GS_PDF_ProcSet begin\n"
411 "(/fd/4) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n"
413 "pdfpagecount PAGE==\n";
416 if(write(pin[0], prolog, n) != n)
418 if((n = read(pout[0], buf, sizeof(buf)-1)) < 0)
426 gs = mallocz(sizeof(*gs), 1);
431 snprint(nam, sizeof nam, "%d", i);
432 addpage(p, nam, popenpdf, gs, -1);
435 /* keep ghostscript arround */
440 while((n = read(pdat[0], buf, sizeof(buf))) >= 0){
441 if(ofd >= 0 && (n <= 0 || infernobithdr(buf, n))){
442 snprint(nam, sizeof nam, "%d", i);
443 addpage(p, nam, popenconv, nil, ofd);
449 snprint(nam, sizeof nam, "%.4d", ++i);
450 if((ofd = createtmp((ulong)p, nam)) < 0)
451 ofd = dup(nullfd, -1);
453 if(write(ofd, buf, n) != n)
469 char buf[NBUF], *file;
477 if((fd = open(file, OREAD)) < 0){
485 if((d = dirfstat(fd)) == nil){
493 if((n = dirreadall(fd, &d)) < 0)
496 addpage(p, d[i].name, popenfile, smprint("%s/%s", file, d[i].name), -1);
498 p->text = strdup(p->label);
503 memset(buf, 0, 32+1);
504 if((n = read(fd, buf, 32)) <= 0)
510 if(memcmp(buf, "%PDF-", 5) == 0 || strstr(buf, "%!"))
512 else if(memcmp(buf, "x T ", 4) == 0){
513 p->data = "lp -dstdout";
516 else if(memcmp(buf, "\xF7\x02\x01\x83\x92\xC0\x1C;", 8) == 0){
517 p->data = "dvips -Pps -r0 -q1 -f1";
520 else if(memcmp(buf, "\x1F\x8B", 2) == 0){
524 else if(memcmp(buf, "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1", 8) == 0){
528 else if(memcmp(buf, "GIF", 3) == 0)
530 else if(memcmp(buf, "\111\111\052\000", 4) == 0)
531 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
532 else if(memcmp(buf, "\115\115\000\052", 4) == 0)
533 p->data = "fb/tiff2pic | fb/3to1 rgbv | fb/pcp -tplan9";
534 else if(memcmp(buf, "\377\330\377", 3) == 0)
536 else if(memcmp(buf, "\211PNG\r\n\032\n", 3) == 0)
538 else if(memcmp(buf, "\0PC Research, Inc", 17) == 0)
539 p->data = "aux/g3p9bit -g";
540 else if(memcmp(buf, "TYPE=ccitt-g31", 14) == 0)
541 p->data = "aux/g3p9bit -g";
542 else if(memcmp(buf, "II*", 3) == 0)
543 p->data = "aux/g3p9bit -g";
544 else if(memcmp(buf, "TYPE=", 5) == 0)
545 p->data = "fb/3to1 rgbv |fb/pcp -tplan9";
546 else if(buf[0] == 'P' && '0' <= buf[1] && buf[1] <= '9')
548 else if(memcmp(buf, "BM", 2) == 0)
550 else if(infernobithdr(buf, n))
553 werrstr("unknown image format");
557 if(seek(fd, 0, 0) < 0)
559 if((i = read(fd, buf+n, n)) < 0)
561 if(i != n || memcmp(buf, buf+n, i)){
564 if((tfd = createtmp((ulong)p, "file")) < 0)
567 if(write(tfd, buf, n) != n)
569 if((n = read(fd, buf, sizeof(buf))) < 0)
572 if(dup(tfd, fd) < 0){
603 for(p = root->down; p; p = t)
604 if((t = nextpage(p)) == x)
616 if(p->open == nil || (fd = p->open(p)) < 0)
620 pipeline(fd, "rotate -r %d", rotate);
622 pipeline(fd, "resize -x %d", resize.x);
624 pipeline(fd, "resize -y %d", resize.y);
634 if(p->open && p->image == nil && p->text == nil){
635 if((fd = openpage(p)) >= 0){
637 p->image = readimage(display, fd, 1);
640 if(p->image == nil && p->text == nil)
641 p->text = smprint("%s: %r", p->label);
654 lockdisplay(display);
656 unlockdisplay(display);
667 for(p = root->down; p; p = nextpage(p)){
668 if(age == 0) /* synchronous flush */
670 else if(!canqlock(p))
672 if((pagegen - p->gen) >= age)
679 loadpages(Page *p, int ahead, int oviewgen)
683 ahead++; /* load at least one */
684 unloadpages(ahead*2);
685 for(i = 0; i < ahead && p; p = nextpage(p), i++){
686 if(viewgen != oviewgen)
690 if(viewgen != oviewgen){
700 if(size.x && size.y && newwin){
704 lockdisplay(display);
706 unlockdisplay(display);
714 * A draw operation that touches only the area contained in bot but not in top.
715 * mp and sp get aligned with bot.min.
718 gendrawdiff(Image *dst, Rectangle bot, Rectangle top,
719 Image *src, Point sp, Image *mask, Point mp, int op)
727 if(Dx(bot)*Dy(bot) == 0)
730 /* no points in bot - top */
731 if(rectinrect(bot, top))
734 /* bot - top ≡ bot */
735 if(Dx(top)*Dy(top)==0 || rectXrect(bot, top)==0){
736 gendrawop(dst, bot, src, sp, mask, mp, op);
741 /* split bot into rectangles that don't intersect top */
743 if(bot.min.x < top.min.x){
744 r = Rect(bot.min.x, bot.min.y, top.min.x, bot.max.y);
745 delta = subpt(r.min, origin);
746 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
747 bot.min.x = top.min.x;
751 if(bot.max.x > top.max.x){
752 r = Rect(top.max.x, bot.min.y, bot.max.x, bot.max.y);
753 delta = subpt(r.min, origin);
754 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
755 bot.max.x = top.max.x;
759 if(bot.min.y < top.min.y){
760 r = Rect(bot.min.x, bot.min.y, bot.max.x, top.min.y);
761 delta = subpt(r.min, origin);
762 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
763 bot.min.y = top.min.y;
767 if(bot.max.y > top.max.y){
768 r = Rect(bot.min.x, top.max.y, bot.max.x, bot.max.y);
769 delta = subpt(r.min, origin);
770 gendrawop(dst, r, src, addpt(sp, delta), mask, addpt(mp, delta), op);
771 bot.max.y = top.max.y;
776 zoomdraw(Image *d, Rectangle r, Rectangle top, Image *s, Point sp, int f)
783 gendrawdiff(d, r, top, s, sp, nil, ZP, S);
787 if(r.min.x < d->r.min.x){
788 sp.x += (d->r.min.x - r.min.x)/f;
789 a.x = (d->r.min.x - r.min.x)%f;
790 r.min.x = d->r.min.x;
792 if(r.min.y < d->r.min.y){
793 sp.y += (d->r.min.y - r.min.y)/f;
794 a.y = (d->r.min.y - r.min.y)%f;
795 r.min.y = d->r.min.y;
798 w = s->r.max.x - sp.x;
801 t = allocimage(display, Rect(r.min.x, r.min.y, r.min.x+w, r.max.y), s->chan, 0, DNofill);
804 for(y=r.min.y; y<r.max.y; y++){
805 draw(t, Rect(r.min.x, y, r.min.x+w, y+1), s, nil, sp);
812 for(x=r.min.x; x<r.max.x; x++){
813 gendrawdiff(d, Rect(x, r.min.y, x+1, r.max.y), top, t, sp, nil, ZP, S);
825 return p->image ? mulpt(subpt(p->image->r.max, p->image->r.min), zoom) : ZP;
834 if((i = p->image) == nil){
837 if((s = p->text) == nil)
840 r.max = stringsize(font, p->text);
841 r = rectaddpt(r, addpt(subpt(divpt(subpt(screen->r.max, screen->r.min), 2), divpt(r.max, 2)),
843 draw(screen, r, display->white, nil, ZP);
844 string(screen, r.min, display->black, ZP, font, s);
846 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
847 zoomdraw(screen, r, ZR, i, i->r.min, zoom);
849 gendrawdiff(screen, screen->r, r, display->white, ZP, nil, ZP, S);
850 border(screen, r, -Borderwidth, display->black, ZP);
851 flushimage(display, 1);
855 translate(Page *p, Point d)
861 if((i==0) || (d.x==0 && d.y==0))
863 r = rectaddpt(Rpt(ZP, pagesize(p)), addpt(pos, screen->r.min));
865 nr = rectaddpt(r, d);
867 rectclip(&or, screen->r);
868 draw(screen, rectaddpt(or, d), screen, nil, or.min);
869 zoomdraw(screen, nr, rectaddpt(or, d), i, i->r.min, zoom);
870 gendrawdiff(screen, screen->r, nr, display->white, ZP, nil, ZP, S);
871 border(screen, nr, -Borderwidth, display->black, ZP);
872 flushimage(display, 1);
882 /* look in current document first */
883 if(current && current->up){
884 for(p = current->up->down; p; p = p->next)
885 if(cistrncmp(p->label, name, n) == 0)
888 /* look everywhere */
889 for(p = root->down; p; p = nextpage(p))
890 if(cistrncmp(p->label, name, n) == 0)
900 for(p = root->down; i > 0 && p; p = nextpage(p))
911 for(i = 0, p = root->down; p && p != x; p = nextpage(p))
913 return (p == x) ? i : -1;
933 esetcursor(&reading);
939 switch(rfork(RFPROC|RFMEM)){
941 sysfatal("rfork: %r");
943 loadpages(p, NAHEAD, oviewgen);
951 char nam[64], *argv[4];
956 esetcursor(&reading);
958 if((fd = openpage(p)) < 0)
960 if(rfork(RFREND|RFFDG|RFPROC|RFENVG|RFNOTEG|RFNOWAIT) == 0){
964 snprint(nam, sizeof nam, "/bin/%s", argv0);
969 sysfatal("exec: %r");
982 lockdisplay(display);
983 if(new && getwindow(display, Refnone) == -1)
984 sysfatal("getwindow: %r");
991 unlockdisplay(display);
994 void killcohort(void)
997 for(i=0;i!=3;i++){ /* It's a long way to the kitchen */
998 postnote(PNGROUP, getpid(), "kill");
1003 void drawerr(Display *, char *msg)
1005 sysfatal("draw: %s", msg);
1012 if(x = strrchr(s, '/'))
1021 fprint(2, "usage: %s [ -iRw ] [ -p ppi ] [ file ... ]\n", argv0);
1026 main(int argc, char *argv[])
1028 enum { Eplumb = 4 };
1053 ppi = atoi(EARGF(usage()));
1060 * so that we can stop all subprocesses with a note,
1061 * and to isolate rendezvous from other processes
1063 rfork(RFNOTEG|RFNAMEG|RFREND);
1065 atnotify(catchnote, 1);
1067 s = smprint("-pid %d", getpid());
1068 if(newwindow(s) < 0)
1069 sysfatal("newwindow: %r");
1072 initdraw(drawerr, nil, argv0);
1073 display->locking = 1;
1074 unlockdisplay(display);
1075 einit(Ekeyboard|Emouse);
1076 eplumb(Eplumb, "image");
1077 nullfd = open("/dev/null", ORDWR);
1078 current = root = addpage(nil, "root", nil, nil, -1);
1080 if(*argv == nil && !imode)
1081 addpage(root, "stdin", popenfile, strdup("/fd/0"), -1);
1082 for(; *argv; argv++)
1083 addpage(root, shortname(*argv), popenfile, strdup(*argv), -1);
1090 lockdisplay(display);
1093 if(current == nil || !canqlock(current))
1098 if((m.buttons & 1) == 0)
1100 translate(current, subpt(m.xy, o));
1106 i = emenuhit(2, &m, &menu);
1107 if(i < 0 || i >= nelem(menuitems) || menuitems[i]==nil)
1110 if(strcmp(s, "orig size")==0){
1117 unlockdisplay(display);
1118 esetcursor(&reading);
1123 if(strncmp(s, "rotate ", 7)==0){
1124 rotate += atoi(s+7);
1128 if(strcmp(s, "upside down")==0){
1132 if(strcmp(s, "fit width")==0){
1135 resize = subpt(screen->r.max, screen->r.min);
1139 if(strcmp(s, "fit height")==0){
1142 resize = subpt(screen->r.max, screen->r.min);
1146 if(strncmp(s, "zoom", 4)==0){
1147 if(current && canqlock(current)){
1148 o = subpt(m.xy, screen->r.min);
1149 if(strstr(s, "in")){
1150 if(zoom < 0x40000000){
1152 pos = addpt(mulpt(subpt(pos, o), 2), o);
1157 pos = addpt(divpt(subpt(pos, o), 2), o);
1164 unlockdisplay(display);
1165 if(strcmp(s, "next")==0)
1166 showpage(nextpage(current));
1167 if(strcmp(s, "prev")==0)
1168 showpage(prevpage(current));
1169 if(strcmp(s, "zerox")==0)
1171 if(strcmp(s, "quit")==0)
1176 if(root->down == nil)
1178 pagemenu.lasthit = pageindex(current);
1179 i = emenuhit(3, &m, &pagemenu);
1180 unlockdisplay(display);
1182 showpage(pageat(i));
1186 unlockdisplay(display);
1195 if(current == nil || !canqlock(current))
1197 lockdisplay(display);
1199 translate(current, Pt(0, Dy(screen->r)/2));
1200 unlockdisplay(display);
1204 unlockdisplay(display);
1206 if(prevpage(current))
1211 showpage(prevpage(current));
1214 if(current == nil || !canqlock(current))
1216 o = addpt(pos, pagesize(current));
1217 lockdisplay(display);
1218 if(o.y > Dy(screen->r)){
1219 translate(current, Pt(0, -Dy(screen->r)/2));
1220 unlockdisplay(display);
1224 unlockdisplay(display);
1226 if(nextpage(current))
1230 showpage(findpage(jump));
1236 showpage(nextpage(current));
1240 if(i+1 < sizeof(jump)){
1248 if(pm && pm->ndata > 0){
1252 s = plumblookup(pm->attr, "action");
1253 if(s && strcmp(s, "quit")==0)
1255 if(s && strcmp(s, "showdata")==0){
1256 static ulong plumbid;
1258 if((fd = createtmp(plumbid++, "plumb")) < 0){
1259 fprint(2, "plumb: createtmp: %r\n");
1263 if(fd2path(fd, s, NPATH) < 0){
1267 write(fd, pm->data, pm->ndata);
1268 }else if(pm->data[0] == '/'){
1269 s = strdup(pm->data);
1271 s = malloc(strlen(pm->wdir)+1+pm->ndata+1);
1272 sprint(s, "%s/%s", pm->wdir, pm->data);
1275 showpage(addpage(root, shortname(s), popenfile, s, fd));