1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/*
* pdf.c
*
* pdf file support for page
*/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>
#include <thread.h>
#include <bio.h>
#include "page.h"
typedef struct PDFInfo PDFInfo;
struct PDFInfo {
GSInfo gs;
Rectangle *pagebbox;
};
static Image* pdfdrawpage(Document *d, int page);
static char* pdfpagename(Document*, int);
char *pdfprolog =
#include "pdfprolog.c"
;
Rectangle
pdfbbox(GSInfo *gs)
{
char *p;
char *f[4];
Rectangle r;
r = Rect(0,0,0,0);
waitgs(gs);
gscmd(gs, "/CropBox knownoget {} {[0 0 0 0]} ifelse PAGE==\n");
p = Brdline(&gs->gsrd, '\n');
p[Blinelen(&gs->gsrd)-1] ='\0';
if(p[0] != '[')
return r;
if(tokenize(p+1, f, 4) != 4)
return r;
r = Rect(atoi(f[0]), atoi(f[1]), atoi(f[2]), atoi(f[3]));
waitgs(gs);
return r;
}
Document*
initpdf(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf)
{
Document *d;
PDFInfo *pdf;
char *p;
char *fn;
char fdbuf[20];
int fd;
int i, npage;
Rectangle bbox;
if(argc > 1) {
fprint(2, "can only view one pdf file at a time\n");
return nil;
}
fprint(2, "reading through pdf...\n");
if(b == nil){ /* standard input; spool to disk (ouch) */
fd = spooltodisk(buf, nbuf, &fn);
sprint(fdbuf, "/dev/fd/%d", fd);
b = Bopen(fdbuf, OREAD);
if(b == nil){
fprint(2, "cannot open disk spool file\n");
wexits("Bopen temp");
}
}else
fn = argv[0];
/* sanity check */
Bseek(b, 0, 0);
if(!(p = Brdline(b, '\n')) && !(p = Brdline(b, '\r'))) {
fprint(2, "cannot find end of first line\n");
wexits("initps");
}
if(strncmp(p, "%PDF-", 5) != 0) {
werrstr("not pdf");
return nil;
}
/* setup structures so one free suffices */
p = emalloc(sizeof(*d) + sizeof(*pdf));
d = (Document*) p;
p += sizeof(*d);
pdf = (PDFInfo*) p;
d->extra = pdf;
d->b = b;
d->drawpage = pdfdrawpage;
d->pagename = pdfpagename;
d->fwdonly = 0;
if(spawngs(&pdf->gs, "-dDELAYSAFER") < 0)
return nil;
gscmd(&pdf->gs, "%s", pdfprolog);
waitgs(&pdf->gs);
setdim(&pdf->gs, Rect(0,0,0,0), ppi, 0);
gscmd(&pdf->gs, "(%s) (r) file { DELAYSAFER { .setsafe } if } stopped pop pdfopen begin\n", fn);
gscmd(&pdf->gs, "pdfpagecount PAGE==\n");
p = Brdline(&pdf->gs.gsrd, '\n');
npage = atoi(p);
if(npage < 1) {
fprint(2, "no pages?\n");
return nil;
}
d->npage = npage;
d->docname = argv[0];
gscmd(&pdf->gs, "Trailer\n");
bbox = pdfbbox(&pdf->gs);
pdf->pagebbox = emalloc(sizeof(Rectangle)*npage);
for(i=0; i<npage; i++) {
gscmd(&pdf->gs, "%d pdfgetpage\n", i+1);
pdf->pagebbox[i] = pdfbbox(&pdf->gs);
if(Dx(pdf->pagebbox[i]) <= 0)
pdf->pagebbox[i] = bbox;
}
return d;
}
static Image*
pdfdrawpage(Document *doc, int page)
{
PDFInfo *pdf = doc->extra;
Image *im;
gscmd(&pdf->gs, "%d DoPDFPage\n", page+1);
im = convert(&pdf->gs.g);
if(im == nil) {
fprint(2, "fatal: readimage error %r\n");
wexits("readimage");
}
waitgs(&pdf->gs);
return im;
}
static char*
pdfpagename(Document *d, int page)
{
static char str[15];
USED(d);
sprint(str, "p %d", page+1);
return str;
}
|