forked from gustawho/okular-backend-mupdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.cpp
325 lines (257 loc) · 6.77 KB
/
document.cpp
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/***************************************************************************
* Copyright (C) 2008 by Pino Toscano <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
***************************************************************************/
#include "document.hpp"
#include "page.hpp"
extern "C" {
#include <mupdf/pdf.h>
}
#include <QFile>
#include <cstring>
namespace QMuPDF
{
QRectF convert_fz_rect(const fz_rect &rect, const QSizeF &dpi);
struct Document::Data {
Data()
: ctx(fz_new_context(nullptr, nullptr, FZ_STORE_DEFAULT))
, mdoc(nullptr), stream(nullptr), pageCount(0), info(nullptr)
, pageMode(Document::UseNone), locked(false) { }
fz_context *ctx;
fz_document *mdoc;
fz_stream *stream;
int pageCount;
pdf_obj *info;
PageMode pageMode;
bool locked;
pdf_document *pdf() const
{
return reinterpret_cast<pdf_document *>(mdoc);
}
pdf_obj *dict(const char *key) const
{
return pdf_dict_gets(ctx, pdf_trailer(ctx, pdf()), key);
}
void loadInfoDict()
{
if (!info) {
info = dict("Info");
}
}
bool load()
{
pdf_obj *root = dict("Root");
if (!root) {
return false;
}
pageCount = fz_count_pages(ctx, mdoc);
pdf_obj *obj = pdf_dict_gets(ctx, root, "PageMode");
if (obj && pdf_is_name(ctx, obj)) {
const char *mode = pdf_to_name(ctx, obj);
if (!std::strcmp(mode, "UseNone")) {
pageMode = Document::UseNone;
} else if (!std::strcmp(mode, "UseOutlines")) {
pageMode = Document::UseOutlines;
} else if (!std::strcmp(mode, "UseThumbs")) {
pageMode = Document::UseThumbs;
} else if (!std::strcmp(mode, "FullScreen")) {
pageMode = Document::FullScreen;
} else if (!std::strcmp(mode, "UseOC")) {
pageMode = Document::UseOC;
} else if (!std::strcmp(mode, "UseAttachments")) {
pageMode = Document::UseAttachments;
}
}
return true;
}
void convertOutline(fz_outline *out, Outline *item)
{
for (; out; out = out->next) {
Outline *child = new Outline(out);
item->appendChild(child);
convertOutline(out->down, child);
}
}
};
Document::Document()
: d(new Data)
{
fz_register_document_handlers(d->ctx);
}
Document::~Document()
{
close();
fz_drop_context(d->ctx);
delete d;
}
bool Document::load(const QString &fileName)
{
QByteArray fileData = QFile::encodeName(fileName);
d->stream = fz_open_file(d->ctx, fileData.constData());
if (!d->stream) {
return false;
}
char *oldlocale = std::setlocale(LC_NUMERIC, "C");
fz_try(d->ctx) {
d->mdoc = fz_open_document_with_stream(d->ctx, "pdf", d->stream);
}
fz_catch(d->ctx) {
qWarning() << "Error when trying to load document";
d->mdoc = nullptr;
return false;
}
if (oldlocale) {
std::setlocale(LC_NUMERIC, oldlocale);
}
if (!d->mdoc) {
return false;
}
d->locked = fz_needs_password(d->ctx, d->mdoc);
if (!d->locked) {
if (!d->load()) {
return false;
}
}
return true;
}
void Document::close()
{
if (!d->mdoc) {
return;
}
fz_drop_document(d->ctx, d->mdoc);
d->mdoc = nullptr;
fz_drop_stream(d->ctx, d->stream);
d->stream = nullptr;
d->pageCount = 0;
d->info = nullptr;
d->pageMode = UseNone;
d->locked = false;
}
bool Document::isLocked() const
{
return d->locked;
}
bool Document::unlock(const QByteArray &password)
{
if (!d->locked) {
return false;
}
QByteArray a = password;
if (!fz_authenticate_password(d->ctx, d->mdoc, a.data())) {
return false;
}
d->locked = false;
if (!d->load()) {
return false;
}
return true;
}
int Document::pageCount() const
{
return d->pageCount;
}
Page Document::page(int pageno) const
{
return Page(d->ctx, d->mdoc, pageno);
}
QList<QByteArray> Document::infoKeys() const
{
QList<QByteArray> keys;
if (!d->mdoc) {
return keys;
}
d->loadInfoDict();
if (!d->info) {
return keys;
}
const int dictSize = pdf_dict_len(d->ctx, d->info);
for (int i = 0; i < dictSize; ++i) {
pdf_obj *obj = pdf_dict_get_key(d->ctx, d->info, i);
if (pdf_is_name(d->ctx, obj)) {
keys.append(QByteArray(pdf_to_name(d->ctx, obj)));
}
}
return keys;
}
QString Document::infoKey(const QByteArray &key) const
{
if (!d->mdoc) {
return QString();
}
d->loadInfoDict();
if (!d->info) {
return QString();
}
pdf_obj *obj = pdf_dict_gets(d->ctx, d->info, key.constData());
if (obj) {
obj = pdf_resolve_indirect(d->ctx, obj);
if (!pdf_is_string(d->ctx, obj)) {
qWarning() << "info object not a string!";
return {};
}
char *value = pdf_new_utf8_from_pdf_string_obj(d->ctx, obj);
if (value) {
const QString res = QString::fromUtf8(value);
fz_free(d->ctx, value);
return res;
}
}
return QString();
}
Outline *Document::outline() const
{
fz_outline *out = fz_load_outline(d->ctx, d->mdoc);
if (!out) {
return nullptr;
}
Outline *item = new Outline;
d->convertOutline(out, item);
fz_drop_outline(d->ctx, out);
return item;
}
fz_context *Document::ctx() const
{
return d->ctx;
}
fz_document *Document::doc() const
{
return d->mdoc;
}
float Document::pdfVersion() const
{
if (!d->mdoc) {
return 0.0f;
}
char buf[64];
if (fz_lookup_metadata(d->ctx, d->mdoc, FZ_META_FORMAT, buf, sizeof(buf)) != -1) {
int major, minor;
if (sscanf(buf, "PDF %d.%d", &major, &minor) == 2) {
return float(major + minor / 10.0);
}
}
return 0.0f;
}
Document::PageMode Document::pageMode() const
{
return d->pageMode;
}
/******************************************************************************/
Outline::Outline(const fz_outline *out)
{
if (out->title) {
m_title = QString::fromUtf8(out->title);
}
if (out->uri) {
m_link = std::string(out->uri);
}
}
Outline::~Outline()
{
qDeleteAll(m_children);
}
} // namespace QMuPDF