4 * provide postscript file reading support for page
16 typedef struct PSInfo PSInfo;
17 typedef struct Page Page;
21 int offset; /* offset of page beginning within file */
26 Rectangle bbox; /* default bounding box */
29 int clueless; /* don't know where page boundaries are */
30 long psoff; /* location of %! in file */
34 static int pswritepage(Document *d, int fd, int page);
35 static Image* psdrawpage(Document *d, int page);
36 static char* pspagename(Document*, int);
38 #define R(r) (r).min.x, (r).min.y, (r).max.x, (r).max.y
45 while(*p == ':' || *p == ' ' || *p == '\t')
47 if(tokenize(p, f, 4) != 4)
49 r = Rect(atoi(f[0]), atoi(f[1]), atoi(f[2]), atoi(f[3]));
51 if(Dx(r) <= 0 || Dy(r) <= 0)
57 /* initdraw not called yet, can't use %R */
58 if(chatty) fprint(2, "[%d %d %d %d] -> ", R(r));
60 * attempt to sniff out A4, 8½×11, others
66 if(a < 300*300){ /* really small, probably supposed to be */
68 } else if(Dx(r) <= 596 && r.max.x <= 596 && Dy(r) > 792 && Dy(r) <= 842 && r.max.y <= 842) /* A4 */
69 r = Rect(0, 0, 596, 842);
70 else { /* cast up to 8½×11 */
71 if(Dx(r) <= 612 && r.max.x <= 612){
75 if(Dy(r) <= 792 && r.max.y <= 792){
80 if(chatty) fprint(2, "[%d %d %d %d]\n", R(r));
84 #define RECT(X) X.min.x, X.min.y, X.max.x, X.max.y
87 prefix(char *x, char *y)
89 return strncmp(x, y, strlen(y)) == 0;
93 * document ps is really being printed as n-up pages.
94 * we need to treat every n pages as 1.
97 repaginate(PSInfo *ps, int n)
104 np = (ps->npage+n-1)/n;
107 for(i=0; i<=onp+1; i++)
108 print("page %d: %d\n", i, page[i].offset);
115 page[np] = page[onp];
118 page[np+1] = page[onp+1];
123 for(i=0; i<=np+1; i++)
124 print("page %d: %d\n", i, page[i].offset);
130 initps(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
151 Rectangle bbox = Rect(0,0,0,0);
154 fprint(2, "can only view one ps file at a time\n");
158 fprint(2, "reading through postscript...\n");
159 if(b == nil){ /* standard input; spool to disk (ouch) */
160 fd = spooltodisk(buf, nbuf, nil);
161 sprint(fdbuf, "/dev/fd/%d", fd);
162 b = Bopen(fdbuf, OREAD);
164 fprint(2, "cannot open disk spool file\n");
165 wexits("Bopen temp");
171 /* find %!, perhaps after PCL nonsense */
177 if(!(p = Brdline(b, eol='\n')) && !(p = Brdline(b, eol='\r'))) {
178 fprint(2, "cannot find end of first line\n");
183 if(p[0] == '%' && p[1] == '!')
194 page = emalloc(mpage*sizeof(*page));
195 memset(page, 0, mpage*sizeof(*page));
197 cantranslate = goodps;
200 while(p = Brdline(b, eol)) {
202 if(chatty) fprint(2, "ps %.*s\n", utfnlen(p, Blinelen(b)-1), p);
205 page = erealloc(page, mpage*sizeof(*page));
206 memset(&page[npage], 0, npage*sizeof(*page));
209 if(p[0] != '%' || p[1] != '%')
212 if(prefix(p, "%%BeginDocument")) {
216 if(nesting > 0 && prefix(p, "%%EndDocument")) {
223 if(prefix(p, "%%EndComment")) {
227 if(reverse == -1 && prefix(p, "%%PageOrder")) {
228 /* glean whether we should reverse the viewing order */
229 p[Blinelen(b)-1] = 0;
230 if(strstr(p, "Ascend"))
232 else if(strstr(p, "Descend"))
234 else if(strstr(p, "Special"))
236 p[Blinelen(b)-1] = '\n';
238 } else if(prefix(p, "%%Trailer")) {
240 page[npage].offset = Boffset(b)-Blinelen(b);
243 } else if(incomments && prefix(p, "%%Orientation")) {
244 if(strstr(p, "Landscape"))
246 } else if(incomments && Dx(bbox)==0 && prefix(p, q="%%BoundingBox")) {
247 bbox = rdbbox(p+strlen(q)+1);
249 /* can't use %R because haven't initdraw() */
250 fprint(2, "document bbox [%d %d %d %d]\n",
256 * If they use the initgraphics command, we can't play our translation tricks.
258 p[Blinelen(b)-1] = 0;
259 if((q=strstr(p, "initgraphics")) && ((r=strchr(p, '%'))==nil || r > q))
261 p[Blinelen(b)-1] = eol;
263 if(!prefix(p, "%%Page:"))
267 * figure out of the %%Page: line contains a page number
268 * or some other page description to use in the menu bar.
270 * lines look like %%Page: x y or %%Page: x
271 * we prefer just x, and will generate our
274 p[Blinelen(b)-1] = 0;
275 if(chatty) fprint(2, "page %s\n", p);
277 while(*r == ' ' || *r == '\t')
280 while(*q && *q != ' ' && *q != '\t')
282 free(page[npage].name);
284 if(*r == '"' && *q == '"')
288 page[npage].name = estrdup(r);
291 snprint(tmp, sizeof tmp, "p %ld", npage+1);
292 page[npage].name = estrdup(tmp);
296 * store the offset info for later viewing
299 p[Blinelen(b)-1] = eol;
300 page[npage++].offset = Boffset(b)-Blinelen(b);
303 fprint(2, "page: linelen %d\n", Blinelen(b));
304 Bseek(b, Blinelen(b), 1);
308 if(Dx(bbox) == 0 || Dy(bbox) == 0)
309 bbox = Rect(0,0,612,792); /* 8½×11 */
311 * if we didn't find any pages, assume the document
316 if(chatty) fprint(2, "don't know where pages are\n");
320 page[npage].name = "p 1";
321 page[npage++].offset = 0;
324 if(npage+2 > mpage) {
326 page = erealloc(page, mpage*sizeof(*page));
327 memset(&page[mpage-2], 0, 2*sizeof(*page));
331 page[npage].offset = Boffset(b);
333 Bseek(b, 0, 2); /* EOF */
334 page[npage+1].offset = Boffset(b);
336 d = emalloc(sizeof(*d));
337 ps = emalloc(sizeof(*ps));
344 d->npage = ps->npage;
346 d->drawpage = psdrawpage;
347 d->pagename = pspagename;
349 d->fwdonly = ps->clueless = dumb;
350 d->docname = argv[0];
352 if(spawngs(&ps->gs, "-dSAFER") < 0)
357 setdim(&ps->gs, bbox, ppi, landscape);
361 * We want to only send the page (i.e. not header and trailer) information
362 * for each page, so initialize the device by sending the header now.
364 pswritepage(d, ps->gs.gsfd, -1);
369 fprint(ps->gs.gsfd, "(%s) run\n", argv[0]);
370 fprint(ps->gs.gsfd, "(/dev/fd/3) (w) file dup (THIS IS NOT A PLAN9 BITMAP 01234567890123456789012345678901234567890123456789\\n) writestring flushfile\n");
379 pswritepage(Document *d, int fd, int page)
382 PSInfo *ps = d->extra;
390 begin = ps->page[page].offset;
392 end = ps->page[page+1].offset;
395 fprint(2, "writepage(%d)... from #%ld to #%ld...\n",
403 while(t > 0 && (i=Bread(b, buf, n)) > 0) {
404 if(write(fd, buf, i) != i)
414 psdrawpage(Document *d, int page)
416 PSInfo *ps = d->extra;
420 return convert(&ps->gs.g);
425 pswritepage(d, ps->gs.gsfd, page);
427 pswritepage(d, ps->gs.gsfd, -1);
428 pswritepage(d, ps->gs.gsfd, page);
429 pswritepage(d, ps->gs.gsfd, d->npage);
432 * If last line terminator is \r, gs will read ahead to check for \n
433 * so send one to avoid deadlock.
435 write(ps->gs.gsfd, "\n", 1);
436 im = convert(&ps->gs.g);
438 fprint(2, "fatal: readimage error %r\n");
447 pspagename(Document *d, int page)
449 PSInfo *ps = (PSInfo *) d->extra;
450 return ps->page[page].name;