-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdfapp.c
218 lines (180 loc) · 4.32 KB
/
pdfapp.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "fitz.h"
#include "mupdf.h"
#include "pdfapp.h"
static void pdfapp_warn(pdfapp_t *app, const char *fmt, ...)
{
fprintf(stderr, "this warning function was eaten by a grue,"
"but feel better knowing that something"
"went wrong, and it wasn't your fault\n");
}
static void pdfapp_error(pdfapp_t *app, fz_error error)
{
fprintf(stderr, "this error module was eaten by a grue,"
"but feel better knowing that something"
"went wrong, and it wasn't your fault\n");
}
void pdfapp_init(pdfapp_t *app)
{
#if 0
fz_error error;
memset(app, 0, sizeof(pdfapp_t));
error = fz_newrenderer(&app->rast, pdf_devicergb, 0, 1024 * 512);
if (error)
pdfapp_error(app, error);
#else
memset(app, 0, sizeof(pdfapp_t));
#endif
}
void pdfapp_open(pdfapp_t *app, char *filename)
{
#if 0
fz_error error;
fz_obj *obj;
char *password = "";
#else
fz_error error;
fz_stream *file;
char *password = "";
fz_obj *obj;
fz_obj *info;
int fd;
fd = open(filename, O_BINARY | O_RDONLY, 0666);
if (fd < 0)
fprintf(stderr, "error, file %s does not exist\n", filename);
#endif
/*
* Open PDF and load xref table
*/
app->filename = filename;
#if 0
app->xref = pdf_newxref();
error = pdf_loadxref(app->xref, filename);
if (error)
{
fz_catch(error, "trying to air");
error = pdf_repairxref(app->xref, filename);
if (error)
pdfapp_error(app, error);
}
error = pdf_decryptxref(app->xref);
if (error)
pdfapp_error(app, error);
#else
file = fz_open_fd(fd);
error = pdf_open_xref_with_stream(&app->xref, file, NULL);
if (error)
pdfapp_error(app, fz_rethrow(error, "cannot open document '%s'", filename));
fz_close(file);
#endif
/*
* Handle encrypted PDF files
*/
if (pdf_needs_password(app->xref))
{
int okay = pdf_authenticate_password(app->xref, password);
while (!okay)
{
//password = winpassword(app, filename);
if (!password)
exit(1);
okay = pdf_authenticate_password(app->xref, password);
if (!okay)
pdfapp_warn(app, "Invalid password.");
}
}
/*
* Load meta information
* TODO: move this into mupdf library
*/
#if 0
obj = fz_dictgets(app->xref->trailer, "Root");
app->xref->root = fz_resolveindirect(obj);
if (!app->xref->root)
pdfapp_error(app, fz_throw("syntaxerror: missing Root object"));
fz_keepobj(app->xref->root);
obj = fz_dictgets(app->xref->trailer, "Info");
app->xref->info = fz_resolveindirect(obj);
if (!app->xref->info)
pdfapp_warn(app, "Could not load PDF meta information.");
if (app->xref->info)
fz_keepobj(app->xref->info);
/*app->outline = pdf_loadoutline(app->xref);*/
app->doctitle = filename;
if (strrchr(app->doctitle, '\\'))
app->doctitle = strrchr(app->doctitle, '\\') + 1;
if (strrchr(app->doctitle, '/'))
app->doctitle = strrchr(app->doctitle, '/') + 1;
if (app->xref->info)
{
obj = fz_dictgets(app->xref->info, "Title");
if (obj)
{
app->doctitle = pdf_toutf8(obj);
}
}
#else
app->outline = pdf_load_outline(app->xref);
app->doctitle = filename;
if (strrchr(app->doctitle, '\\'))
app->doctitle = strrchr(app->doctitle, '\\') + 1;
if (strrchr(app->doctitle, '/'))
app->doctitle = strrchr(app->doctitle, '/') + 1;
info = fz_dict_gets(app->xref->trailer, "Info");
if (info)
{
obj = fz_dict_gets(info, "Title");
if (obj)
app->doctitle = pdf_to_utf8(obj);
}
#endif
/*
* Start at first page
*/
#if 0
app->pagecount = pdf_getpagecount(app->xref);
#else
error = pdf_load_page_tree(app->xref);
if (error)
pdfapp_error(app, fz_rethrow(error, "cannot load page tree"));
app->pagecount = pdf_count_pages(app->xref);
#endif
app->rotate = 0;
}
void pdfapp_close(pdfapp_t *app)
{
#if 0
if (app->page)
pdf_droppage(app->page);
app->page = nil;
if (app->image)
fz_droppixmap(app->image);
app->image = nil;
/* if (app->outline)
pdf_dropoutline(app->outline);
app->outline = nil;*/
if (app->xref->store)
pdf_dropstore(app->xref->store);
app->xref->store = nil;
pdf_closexref(app->xref);
app->xref = nil;
#else
if (app->cache)
fz_free_glyph_cache(app->cache);
app->cache = NULL;
if (app->image)
fz_drop_pixmap(app->image);
app->image = NULL;
if (app->outline)
pdf_free_outline(app->outline);
app->outline = NULL;
if (app->xref)
{
if (app->xref->store)
pdf_free_store(app->xref->store);
app->xref->store = NULL;
pdf_free_xref(app->xref);
app->xref = NULL;
}
fz_flush_warnings();
#endif
}