4 * provide postscript file reading support for page
15 typedef struct PSInfo PSInfo;
16 typedef struct Page Page;
20 int offset; /* offset of page beginning within file */
25 Rectangle bbox; /* default bounding box */
28 int clueless; /* don't know where page boundaries are */
29 long psoff; /* location of %! in file */
33 static int pswritepage(Document *d, int fd, int page);
34 static Image* psdrawpage(Document *d, int page);
35 static char* pspagename(Document*, int);
37 #define R(r) (r).min.x, (r).min.y, (r).max.x, (r).max.y
44 while(*p == ':' || *p == ' ' || *p == '\t')
46 if(tokenize(p, f, 4) != 4)
48 r = Rect(atoi(f[0]), atoi(f[1]), atoi(f[2]), atoi(f[3]));
50 if(Dx(r) <= 0 || Dy(r) <= 0)
56 /* initdraw not called yet, can't use %R */
57 if(chatty) fprint(2, "[%d %d %d %d] -> ", R(r));
59 * attempt to sniff out A4, 8½×11, others
65 if(a < 300*300){ /* really small, probably supposed to be */
67 } else if(Dx(r) <= 596 && r.max.x <= 596 && Dy(r) > 792 && Dy(r) <= 842 && r.max.y <= 842) /* A4 */
68 r = Rect(0, 0, 596, 842);
69 else { /* cast up to 8½×11 */
70 if(Dx(r) <= 612 && r.max.x <= 612){
74 if(Dy(r) <= 792 && r.max.y <= 792){
79 if(chatty) fprint(2, "[%d %d %d %d]\n", R(r));
83 #define RECT(X) X.min.x, X.min.y, X.max.x, X.max.y
86 prefix(char *x, char *y)
88 return strncmp(x, y, strlen(y)) == 0;
92 * document ps is really being printed as n-up pages.
93 * we need to treat every n pages as 1.
96 repaginate(PSInfo *ps, int n)
103 np = (ps->npage+n-1)/n;
106 for(i=0; i<=onp+1; i++)
107 print("page %d: %d\n", i, page[i].offset);
114 page[np] = page[onp];
117 page[np+1] = page[onp+1];
122 for(i=0; i<=np+1; i++)
123 print("page %d: %d\n", i, page[i].offset);
129 initps(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
150 Rectangle bbox = Rect(0,0,0,0);
153 fprint(2, "can only view one ps file at a time\n");
157 fprint(2, "reading through postscript...\n");
158 if(b == nil){ /* standard input; spool to disk (ouch) */
159 fd = spooltodisk(buf, nbuf, nil);
160 sprint(fdbuf, "/fd/%d", fd);
161 b = Bopen(fdbuf, OREAD);
163 fprint(2, "cannot open disk spool file\n");
164 wexits("Bopen temp");
170 /* find %!, perhaps after PCL nonsense */
176 if(!(p = Brdline(b, eol='\n')) && !(p = Brdline(b, eol='\r'))) {
177 fprint(2, "cannot find end of first line\n");
182 if(p[0] == '%' && p[1] == '!')
193 page = emalloc(mpage*sizeof(*page));
194 memset(page, 0, mpage*sizeof(*page));
196 cantranslate = goodps;
199 while(p = Brdline(b, eol)) {
201 if(chatty) fprint(2, "ps %.*s\n", utfnlen(p, Blinelen(b)-1), p);
204 page = erealloc(page, mpage*sizeof(*page));
205 memset(&page[npage], 0, npage*sizeof(*page));
208 if(p[0] != '%' || p[1] != '%')
211 if(prefix(p, "%%BeginDocument")) {
215 if(nesting > 0 && prefix(p, "%%EndDocument")) {
222 if(prefix(p, "%%EndComment")) {
226 if(reverse == -1 && prefix(p, "%%PageOrder")) {
227 /* glean whether we should reverse the viewing order */
228 p[Blinelen(b)-1] = 0;
229 if(strstr(p, "Ascend"))
231 else if(strstr(p, "Descend"))
233 else if(strstr(p, "Special"))
235 p[Blinelen(b)-1] = '\n';
237 } else if(prefix(p, "%%Trailer")) {
239 page[npage].offset = Boffset(b)-Blinelen(b);
242 } else if(incomments && prefix(p, "%%Orientation")) {
243 if(strstr(p, "Landscape"))
245 } else if(incomments && Dx(bbox)==0 && prefix(p, q="%%BoundingBox")) {
246 bbox = rdbbox(p+strlen(q)+1);
248 /* can't use %R because haven't initdraw() */
249 fprint(2, "document bbox [%d %d %d %d]\n",
255 * If they use the initgraphics command, we can't play our translation tricks.
257 p[Blinelen(b)-1] = 0;
258 if((q=strstr(p, "initgraphics")) && ((r=strchr(p, '%'))==nil || r > q))
260 p[Blinelen(b)-1] = eol;
262 if(!prefix(p, "%%Page:"))
266 * figure out of the %%Page: line contains a page number
267 * or some other page description to use in the menu bar.
269 * lines look like %%Page: x y or %%Page: x
270 * we prefer just x, and will generate our
273 p[Blinelen(b)-1] = 0;
274 if(chatty) fprint(2, "page %s\n", p);
276 while(*r == ' ' || *r == '\t')
279 while(*q && *q != ' ' && *q != '\t')
281 free(page[npage].name);
283 if(*r == '"' && *q == '"')
287 page[npage].name = estrdup(r);
290 snprint(tmp, sizeof tmp, "p %ld", npage+1);
291 page[npage].name = estrdup(tmp);
295 * store the offset info for later viewing
298 p[Blinelen(b)-1] = eol;
299 page[npage++].offset = Boffset(b)-Blinelen(b);
302 fprint(2, "page: linelen %d\n", Blinelen(b));
303 Bseek(b, Blinelen(b), 1);
307 if(Dx(bbox) == 0 || Dy(bbox) == 0)
308 bbox = Rect(0,0,612,792); /* 8½×11 */
310 * if we didn't find any pages, assume the document
315 if(chatty) fprint(2, "don't know where pages are\n");
319 page[npage].name = "p 1";
320 page[npage++].offset = 0;
323 if(npage+2 > mpage) {
325 page = erealloc(page, mpage*sizeof(*page));
326 memset(&page[mpage-2], 0, 2*sizeof(*page));
330 page[npage].offset = Boffset(b);
332 Bseek(b, 0, 2); /* EOF */
333 page[npage+1].offset = Boffset(b);
335 d = emalloc(sizeof(*d));
336 ps = emalloc(sizeof(*ps));
343 d->npage = ps->npage;
345 d->drawpage = psdrawpage;
346 d->pagename = pspagename;
348 d->fwdonly = ps->clueless = dumb;
349 d->docname = argv[0];
351 if(spawngs(&ps->gs) < 0)
356 setdim(&ps->gs, bbox, ppi, landscape);
360 * We want to only send the page (i.e. not header and trailer) information
361 * for each page, so initialize the device by sending the header now.
363 pswritepage(d, ps->gs.gsfd, -1);
368 fprint(ps->gs.gsfd, "(%s) run\n", argv[0]);
369 fprint(ps->gs.gsfd, "(/fd/3) (w) file dup (THIS IS NOT A PLAN9 BITMAP 01234567890123456789012345678901234567890123456789\\n) writestring flushfile\n");
378 pswritepage(Document *d, int fd, int page)
381 PSInfo *ps = d->extra;
389 begin = ps->page[page].offset;
391 end = ps->page[page+1].offset;
394 fprint(2, "writepage(%d)... from #%ld to #%ld...\n",
402 while(t > 0 && (i=Bread(b, buf, n)) > 0) {
403 if(write(fd, buf, i) != i)
413 psdrawpage(Document *d, int page)
415 PSInfo *ps = d->extra;
419 return readimage(display, ps->gs.gsdfd, 0);
424 pswritepage(d, ps->gs.gsfd, page);
426 pswritepage(d, ps->gs.gsfd, -1);
427 pswritepage(d, ps->gs.gsfd, page);
428 pswritepage(d, ps->gs.gsfd, d->npage);
431 * If last line terminator is \r, gs will read ahead to check for \n
432 * so send one to avoid deadlock.
434 write(ps->gs.gsfd, "\n", 1);
435 im = readimage(display, ps->gs.gsdfd, 0);
437 fprint(2, "fatal: readimage error %r\n");
446 pspagename(Document *d, int page)
448 PSInfo *ps = (PSInfo *) d->extra;
449 return ps->page[page].name;