4 * provide postscript file reading support for page
16 static int pswritepage(Document *d, int fd, int page);
17 static Image* psdrawpage(Document *d, int page);
18 static char* pspagename(Document*, int);
20 #define R(r) (r).min.x, (r).min.y, (r).max.x, (r).max.y
27 while(*p == ':' || *p == ' ' || *p == '\t')
29 if(tokenize(p, f, 4) != 4)
31 r = Rect(atoi(f[0]), atoi(f[1]), atoi(f[2]), atoi(f[3]));
33 if(Dx(r) <= 0 || Dy(r) <= 0)
39 /* initdraw not called yet, can't use %R */
40 if(chatty) fprint(2, "[%d %d %d %d] -> ", R(r));
42 * attempt to sniff out A4, 8½×11, others
48 if(a < 300*300){ /* really small, probably supposed to be */
50 } else if(Dx(r) <= 596 && r.max.x <= 596 && Dy(r) > 792 && Dy(r) <= 842 && r.max.y <= 842) /* A4 */
51 r = Rect(0, 0, 596, 842);
52 else { /* cast up to 8½×11 */
53 if(Dx(r) <= 612 && r.max.x <= 612){
57 if(Dy(r) <= 792 && r.max.y <= 792){
62 if(chatty) fprint(2, "[%d %d %d %d]\n", R(r));
66 #define RECT(X) X.min.x, X.min.y, X.max.x, X.max.y
69 prefix(char *x, char *y)
71 return strncmp(x, y, strlen(y)) == 0;
75 * document ps is really being printed as n-up pages.
76 * we need to treat every n pages as 1.
79 repaginate(PSInfo *ps, int n)
86 np = (ps->npage+n-1)/n;
89 for(i=0; i<=onp+1; i++)
90 print("page %d: %d\n", i, page[i].offset);
100 page[np+1] = page[onp+1];
105 for(i=0; i<=np+1; i++)
106 print("page %d: %d\n", i, page[i].offset);
112 initps(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
133 Rectangle bbox = Rect(0,0,0,0);
136 fprint(2, "can only view one ps file at a time\n");
140 fprint(2, "reading through postscript...\n");
141 if(b == nil){ /* standard input; spool to disk (ouch) */
142 fd = spooltodisk(buf, nbuf, nil);
143 sprint(fdbuf, "/dev/fd/%d", fd);
144 b = Bopen(fdbuf, OREAD);
146 fprint(2, "cannot open disk spool file\n");
147 wexits("Bopen temp");
153 /* find %!, perhaps after PCL nonsense */
159 if(!(p = Brdline(b, eol='\n')) && !(p = Brdline(b, eol='\r'))) {
160 fprint(2, "cannot find end of first line\n");
165 if(p[0] == '%' && p[1] == '!')
176 page = emalloc(mpage*sizeof(*page));
177 memset(page, 0, mpage*sizeof(*page));
179 cantranslate = goodps;
182 while(p = Brdline(b, eol)) {
184 if(chatty) fprint(2, "ps %.*s\n", utfnlen(p, Blinelen(b)-1), p);
187 page = erealloc(page, mpage*sizeof(*page));
188 memset(&page[npage], 0, npage*sizeof(*page));
191 if(p[0] != '%' || p[1] != '%')
194 if(prefix(p, "%%BeginDocument")) {
198 if(nesting > 0 && prefix(p, "%%EndDocument")) {
205 if(prefix(p, "%%EndComment")) {
209 if(reverse == -1 && prefix(p, "%%PageOrder")) {
210 /* glean whether we should reverse the viewing order */
211 p[Blinelen(b)-1] = 0;
212 if(strstr(p, "Ascend"))
214 else if(strstr(p, "Descend"))
216 else if(strstr(p, "Special"))
218 p[Blinelen(b)-1] = '\n';
220 } else if(prefix(p, "%%Trailer")) {
222 page[npage].offset = Boffset(b)-Blinelen(b);
225 } else if(incomments && prefix(p, "%%Orientation")) {
226 if(strstr(p, "Landscape"))
228 } else if(incomments && Dx(bbox)==0 && prefix(p, q="%%BoundingBox")) {
229 bbox = rdbbox(p+strlen(q)+1);
231 /* can't use %R because haven't initdraw() */
232 fprint(2, "document bbox [%d %d %d %d]\n",
238 * If they use the initgraphics command, we can't play our translation tricks.
240 p[Blinelen(b)-1] = 0;
241 if((q=strstr(p, "initgraphics")) && ((r=strchr(p, '%'))==nil || r > q))
243 p[Blinelen(b)-1] = eol;
245 if(!prefix(p, "%%Page:"))
249 * figure out of the %%Page: line contains a page number
250 * or some other page description to use in the menu bar.
252 * lines look like %%Page: x y or %%Page: x
253 * we prefer just x, and will generate our
256 p[Blinelen(b)-1] = 0;
257 if(chatty) fprint(2, "page %s\n", p);
259 while(*r == ' ' || *r == '\t')
262 while(*q && *q != ' ' && *q != '\t')
264 free(page[npage].name);
266 if(*r == '"' && *q == '"')
270 page[npage].name = estrdup(r);
273 snprint(tmp, sizeof tmp, "p %ld", npage+1);
274 page[npage].name = estrdup(tmp);
278 * store the offset info for later viewing
281 p[Blinelen(b)-1] = eol;
282 page[npage++].offset = Boffset(b)-Blinelen(b);
285 fprint(2, "page: linelen %d\n", Blinelen(b));
286 Bseek(b, Blinelen(b), 1);
290 if(Dx(bbox) == 0 || Dy(bbox) == 0)
291 bbox = Rect(0,0,612,792); /* 8½×11 */
293 * if we didn't find any pages, assume the document
298 if(chatty) fprint(2, "don't know where pages are\n");
302 page[npage].name = "p 1";
303 page[npage++].offset = 0;
306 if(npage+2 > mpage) {
308 page = erealloc(page, mpage*sizeof(*page));
309 memset(&page[mpage-2], 0, 2*sizeof(*page));
313 page[npage].offset = Boffset(b);
315 Bseek(b, 0, 2); /* EOF */
316 page[npage+1].offset = Boffset(b);
318 d = emalloc(sizeof(*d));
319 ps = emalloc(sizeof(*ps));
326 d->npage = ps->npage;
328 d->drawpage = psdrawpage;
329 d->pagename = pspagename;
331 d->fwdonly = ps->clueless = dumb;
332 d->docname = argv[0];
334 * "tag" the doc as an image for now since there still is the "blank page"
335 * problem for ps files.
339 if(spawngs(&ps->gs, "-dSAFER") < 0)
344 setdim(&ps->gs, bbox, ppi, landscape);
348 * We want to only send the page (i.e. not header and trailer) information
349 * for each page, so initialize the device by sending the header now.
351 pswritepage(d, ps->gs.gsfd, -1);
356 fprint(ps->gs.gsfd, "(%s) run\n", argv[0]);
357 fprint(ps->gs.gsfd, "(/dev/fd/3) (w) file dup (THIS IS NOT A PLAN9 BITMAP 01234567890123456789012345678901234567890123456789\\n) writestring flushfile\n");
366 pswritepage(Document *d, int fd, int page)
369 PSInfo *ps = d->extra;
377 begin = ps->page[page].offset;
379 end = ps->page[page+1].offset;
382 fprint(2, "writepage(%d)... from #%ld to #%ld...\n",
390 while(t > 0 && (i=Bread(b, buf, n)) > 0) {
391 if(write(fd, buf, i) != i)
401 psdrawpage(Document *d, int page)
403 PSInfo *ps = d->extra;
407 return convert(&ps->gs.g);
412 pswritepage(d, ps->gs.gsfd, page);
414 pswritepage(d, ps->gs.gsfd, -1);
415 pswritepage(d, ps->gs.gsfd, page);
416 pswritepage(d, ps->gs.gsfd, d->npage);
419 * If last line terminator is \r, gs will read ahead to check for \n
420 * so send one to avoid deadlock.
422 write(ps->gs.gsfd, "\n", 1);
423 im = convert(&ps->gs.g);
425 fprint(2, "fatal: readimage error %r\n");
434 pspagename(Document *d, int page)
436 PSInfo *ps = (PSInfo *) d->extra;
437 return ps->page[page].name;