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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
#include "fitz/fitz.h"
#include "pdf/mupdf.h"
#include "xps/muxps.h"
#include "cbz/mucbz.h"
#include "document.h"
#include <ctype.h> /* for tolower() */
struct document *
open_document(fz_context *ctx, char *filename)
{
struct document *doc;
doc = fz_malloc_struct(ctx, struct document);
memset(doc, 0, sizeof *doc);
doc->ctx = ctx;
doc->number = -1;
fz_try(ctx)
{
if (strstr(filename, ".pdf") || strstr(filename, ".PDF"))
doc->pdf = pdf_open_document(ctx, filename);
else if (strstr(filename, ".xps") || strstr(filename, ".XPS"))
doc->xps = xps_open_document(ctx, filename);
else if (strstr(filename, ".cbz") || strstr(filename, ".CBZ") || strstr(filename, ".zip") || strstr(filename, ".ZIP"))
doc->cbz = cbz_open_document(ctx, filename);
else
fz_throw(ctx, "unknown document format");
}
fz_catch(ctx)
{
close_document(doc);
return NULL;
}
return doc;
}
int
needs_password(struct document *doc)
{
if (doc->pdf)
return pdf_needs_password(doc->pdf);
return 0;
}
int
authenticate_password(struct document *doc, char *password)
{
if (doc->pdf)
return pdf_authenticate_password(doc->pdf, password);
return 1;
}
fz_outline *
load_outline(struct document *doc)
{
fz_outline *outline;
fz_var(outline);
fz_try (doc->ctx)
{
if (doc->pdf)
outline = pdf_load_outline(doc->pdf);
else if (doc->xps)
outline = xps_load_outline(doc->xps);
else
outline = NULL;
}
fz_catch (doc->ctx)
{
outline = NULL;
}
return outline;
}
int
count_pages(struct document *doc)
{
if (doc->pdf)
return pdf_count_pages(doc->pdf);
else if (doc->xps)
return xps_count_pages(doc->xps);
else if (doc->cbz)
return cbz_count_pages(doc->cbz);
else
return 1;
}
static void
load_page(struct document *doc, int number)
{
if (doc->number == number)
return;
fz_try (doc->ctx)
{
doc->number = number;
if (doc->pdf) {
if (doc->pdf_page)
pdf_free_page(doc->ctx, doc->pdf_page);
doc->pdf_page = NULL;
doc->pdf_page = pdf_load_page(doc->pdf, number);
}
if (doc->xps) {
if (doc->xps_page)
xps_free_page(doc->xps, doc->xps_page);
doc->xps_page = NULL;
doc->xps_page = xps_load_page(doc->xps, number);
}
if (doc->cbz) {
if (doc->cbz_page)
cbz_free_page(doc->cbz, doc->cbz_page);
doc->cbz_page = NULL;
doc->cbz_page = cbz_load_page(doc->cbz, number);
}
}
fz_catch (doc->ctx)
{
fprintf(stderr, "cannot load page %d", number);
}
}
void
measure_page(struct document *doc, int number, float *w, float *h)
{
load_page(doc, number);
if (doc->pdf_page) {
fz_rect bounds = pdf_bound_page(doc->pdf, doc->pdf_page);
*w = bounds.x1 - bounds.x0;
*h = bounds.y1 - bounds.y0;
}
else if (doc->xps_page) {
fz_rect bounds = xps_bound_page(doc->xps, doc->xps_page);
*w = bounds.x1 - bounds.x0;
*h = bounds.y1 - bounds.y0;
}
else if (doc->cbz_page) {
fz_rect bounds = cbz_bound_page(doc->cbz, doc->cbz_page);
*w = bounds.x1 - bounds.x0;
*h = bounds.y1 - bounds.y0;
}
else {
*w = *h = 72;
}
fz_flush_warnings(doc->ctx);
}
void
draw_page(struct document *doc, int number, fz_device *dev, fz_matrix ctm, fz_cookie *cookie)
{
load_page(doc, number);
fz_try (doc->ctx)
{
if (doc->pdf_page)
pdf_run_page(doc->pdf, doc->pdf_page, dev, ctm, cookie);
else if (doc->xps_page)
xps_run_page(doc->xps, doc->xps_page, dev, ctm, cookie);
else if (doc->cbz_page)
cbz_run_page(doc->cbz, doc->cbz_page, dev, ctm, cookie);
}
fz_catch (doc->ctx)
{
fprintf(stderr, "cannot draw page %d", number);
}
fz_flush_warnings(doc->ctx);
}
static int
charat(fz_text_span *span, int idx)
{
int ofs = 0;
while (span) {
if (idx < ofs + span->len)
return span->text[idx - ofs].c;
if (span->eol) {
if (idx == ofs + span->len)
return ' ';
ofs ++;
}
ofs += span->len;
span = span->next;
}
return 0;
}
static fz_bbox
bboxat(fz_text_span *span, int idx)
{
int ofs = 0;
while (span) {
if (idx < ofs + span->len)
return span->text[idx - ofs].bbox;
if (span->eol) {
if (idx == ofs + span->len)
return fz_empty_bbox;
ofs ++;
}
ofs += span->len;
span = span->next;
}
return fz_empty_bbox;
}
static int
textlen(fz_text_span *span)
{
int len = 0;
while (span) {
len += span->len;
if (span->eol)
len ++;
span = span->next;
}
return len;
}
static int
match(fz_text_span *span, char *s, int n)
{
int start = n, c;
while (*s) {
s += chartorune(&c, s);
if (c == ' ' && charat(span, n) == ' ') {
while (charat(span, n) == ' ')
n++;
} else {
if (tolower(c) != tolower(charat(span, n)))
return 0;
n++;
}
}
return n - start;
}
int
search_page(struct document *doc, int number, char *needle, fz_cookie *cookie)
{
int pos, len, i, n;
if (strlen(needle) == 0)
return 0;
fz_text_span *text = fz_new_text_span(doc->ctx);
fz_device *dev = fz_new_text_device(doc->ctx, text);
draw_page(doc, number, dev, fz_identity, cookie);
fz_free_device(dev);
doc->hit_count = 0;
len = textlen(text);
for (pos = 0; pos < len; pos++) {
n = match(text, needle, pos);
if (n) {
for (i = 0; i < n; i++) {
fz_bbox r = bboxat(text, pos + i);
if (!fz_is_empty_bbox(r) && doc->hit_count < nelem(doc->hit_bbox))
doc->hit_bbox[doc->hit_count++] = r;
}
}
}
fz_free_text_span(doc->ctx, text);
return doc->hit_count;
}
fz_bbox
search_result_bbox(struct document *doc, int i)
{
return doc->hit_bbox[i];
}
void
close_document(struct document *doc)
{
if (doc->pdf) {
if (doc->pdf_page)
pdf_free_page(doc->ctx, doc->pdf_page);
pdf_close_document(doc->pdf);
}
if (doc->xps) {
if (doc->xps_page)
xps_free_page(doc->xps, doc->xps_page);
xps_close_document(doc->xps);
}
if (doc->cbz) {
if (doc->cbz_page)
cbz_free_page(doc->cbz, doc->cbz_page);
cbz_close_document(doc->cbz);
}
fz_flush_warnings(doc->ctx);
fz_free(doc->ctx, doc);
}
|