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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
#include "Renderer.h"
#include "TextData.h"
#include <QDateTime>
#include <mupdf/fitz.h>
#include <mupdf/pdf.h>
#include <QMutex>
#include <QFuture>
#include <QtConcurrent>
class Annot{
public:
Annot(fz_context *_ctx, pdf_annot *_fzAnnot, char *_text, char *_author, QRectF _loc = QRectF()) : fzAnnot(_fzAnnot), ctx(_ctx), loc(_loc) {
author = (_author) ? QString::fromLocal8Bit(_author) : QString();
text = (_text) ? QString::fromLocal8Bit(_text) : QString();
}
~Annot() {
fz_drop_annot(ctx, (fz_annot*)fzAnnot);
}
QString getAuthor() { return author; }
QString getText() { return text; }
QRectF getLoc() { return loc; }
private:
pdf_annot *fzAnnot;
fz_context *ctx;
QString author;
QString text;
QRectF loc;
};
class Link {
public:
Link(fz_context *_ctx, fz_link *_fzLink, char *_uri, int _page, QRectF _loc = QRectF()) : fzLink(_fzLink), ctx(_ctx) {
QString uri = QString::fromLocal8Bit(_uri);
data = new TextData(_loc, _page, uri);
}
~Link() {
fz_drop_link(ctx, fzLink);
delete data;
}
TextData* getData() { return data; }
private:
TextData *data;
fz_link *fzLink;
fz_context *ctx;
};
class Data {
public:
Data(int _pagenum, fz_context *_ctx, fz_display_list *_list, fz_rect _bbox, fz_matrix _ctm, double _sf, fz_link *_link, QList<Annot*> &_annot) : pagenumber(_pagenum), ctx(_ctx), list(_list), bbox(_bbox), ctm(_ctm), sf(_sf), annotList(_annot) {
while(_link) {
QRectF rect(sf*_link->rect.x0, sf*_link->rect.y0, sf*(_link->rect.x1 - _link->rect.x0), sf*(_link->rect.y1 - _link->rect.y0));
Link *link = new Link(_ctx, _link, _link->uri, _pagenum, rect);
linkList.append(link);
_link = _link->next;
}
}
~Data() {
fz_drop_pixmap(ctx, pix);
fz_drop_display_list(ctx, list);
qDeleteAll(linkList);
linkList.clear();
qDeleteAll(annotList);
annotList.clear();
}
int getPage() { return pagenumber; }
QList<Link*> getLinkList() { return linkList; }
QList<Annot*> getAnnotList() { return annotList; }
fz_context* getContext() { return ctx; }
fz_display_list* getDisplayList() { return list; }
QRectF getScaledRect() { return QRectF(sf*bbox.x0, sf*bbox.y0, sf*(bbox.x1-bbox.x0), sf*(bbox.y1 - bbox.y0)); }
fz_rect getBoundingBox() { return bbox; }
fz_matrix getMatrix() { return ctm; }
QImage getImage() { return img; }
void setImage(QImage _img) { img = _img; }
void setRenderThread(QFuture<void> thread) { renderThread = thread; }
void setPixmap(fz_pixmap *_pix) { pix = _pix; }
private:
int pagenumber;
fz_context *ctx;
fz_display_list *list;
fz_rect bbox;
fz_matrix ctm;
QList<Link*> linkList;
double sf;
QList<Annot*> annotList;
fz_pixmap *pix;
QImage img;
QFuture<void> renderThread;
};
fz_document *DOC;
fz_context *CTX;
QHash<int, Data*> dataHash;
QMutex mutex[FZ_LOCK_MAX];
fz_locks_context locks;
inline QString getTextInfo(QString str) {
char infoBuff[1000];
int size = DOC->lookup_metadata(CTX, DOC, ("info:"+str).toLocal8Bit().data(), infoBuff, 1000);
if(size != -1){ return QString::fromLatin1(infoBuff); }
return "";
}
void lock_mutex(void *user, int lock) {
QMutex *mutex = (QMutex*) user;
mutex[lock].lock();
}
void unlock_mutex(void *user, int lock) {
QMutex *mutex = (QMutex*) user;
mutex[lock].unlock();
}
Renderer::Renderer(){
locks.user = mutex;
locks.lock = lock_mutex;
locks.unlock = unlock_mutex;
DOC = 0;
qDebug() << "Creating Context";
CTX = fz_new_context(NULL, &locks, FZ_STORE_UNLIMITED);
needpass = false;
degrees = 0;
}
Renderer::~Renderer(){
qDebug() << "Dropping Context";
clearHash();
fz_drop_document(CTX, DOC);
DOC = NULL;
fz_drop_context(CTX);
CTX = NULL;
}
bool Renderer::loadMultiThread(){ return false; }
void Renderer::handleLink(QString link) {
float xp = 0.0, yp = 0.0;
int pagenum = 0;
QByteArray linkData = link.toLocal8Bit();
char *uri = linkData.data();
if(!link.isEmpty()) {
pagenum = fz_resolve_link(CTX, DOC, uri, &xp, &yp);
if(pagenum != -1)
emit goToPosition(pagenum+1, xp, yp);
}
}
//Traverse the outline tree through Preorder traversal
void Renderer::traverseOutline(void *link, int level) {
fz_outline *olink = (fz_outline*)link;
Bookmark *bm = new Bookmark(olink->title, olink->uri, olink->page, level);
bookmarks.push_back(bm);
if(olink->down)
traverseOutline(olink->down, level+1);
if(olink->next)
traverseOutline(olink->next, level);
}
bool Renderer::loadDocument(QString path, QString password){
//first time through
if(path != docpath) {
if(DOC != 0) {
qDebug() << "New document";
fz_drop_document(CTX, DOC);
DOC = NULL;
needpass = false;
docpath = path;
if(bookmarks.size() > 0) {
qDeleteAll(bookmarks);
bookmarks.clear();
}
}else if(DOC==0){
fz_register_document_handlers(CTX);
qDebug() << "Document handlers registered";
}
docpath = path;
DOC = fz_open_document(CTX, path.toLocal8Bit().data());
qDebug() << "File opened" << DOC;
if(DOC==0){
qDebug() << "Could not open file:" << path;
return false;
}
needpass = (fz_needs_password(CTX, DOC) != 0);
if(needpass && password.isEmpty()){
return false;
}else if(needpass){
needpass = !fz_authenticate_password(CTX, DOC, password.toLocal8Bit());
if(needpass){ return false; } //incorrect password
}
qDebug() << "Password Check cleared";
pnum = fz_count_pages(CTX, DOC);
qDebug() << "Page count: " << pnum;
doctitle.clear();
qDebug() << "Opening File:" << path;
jobj.insert("subject", getTextInfo("Subject") );
jobj.insert("author", getTextInfo("Author") );
jobj.insert("creator", getTextInfo("Creator") );
jobj.insert("producer", getTextInfo("Producer") );
jobj.insert("keywords", getTextInfo("Keywords") );
jobj.insert("dt_created", QDateTime::fromString( getTextInfo("CreationDate").left(16), "'D:'yyyyMMddHHmmss").toString() );
jobj.insert("dt_modified", QDateTime::fromString( getTextInfo("ModDate").left(16), "'D:'yyyyMMddHHmmss").toString() );
if(!jobj["title"].toString().isEmpty())
doctitle = jobj["title"].toString();
else
doctitle = path.section("/",-1);
//Possibly check Page orientation
fz_outline *outline = fz_load_outline(CTX, DOC);
if(outline)
traverseOutline(outline, 0);
else
qDebug() << "No Bookmarks";
fz_drop_outline(CTX, outline);
return true;
}
return false;
}
void renderer(Data *data, Renderer *obj)
{
int pagenum = data->getPage();
//qDebug() << "Rendering:" << pagenum;
fz_context *ctx = data->getContext();
fz_rect bbox = data->getBoundingBox();
fz_matrix ctm = data->getMatrix();
fz_device *dev;
fz_irect rbox;
ctx = fz_clone_context(ctx);
fz_pixmap *pixmap = fz_new_pixmap_with_bbox(ctx, fz_device_rgb(ctx), fz_round_rect(&rbox, &bbox), NULL, 0);
fz_clear_pixmap_with_value(ctx, pixmap, 0xff);
dev = fz_new_draw_device(ctx, &fz_identity, pixmap);
fz_run_display_list(ctx, data->getDisplayList(), dev, &ctm, &bbox, NULL);
data->setImage(QImage(pixmap->samples, pixmap->w, pixmap->h, pixmap->stride, QImage::Format_RGB888));
data->setPixmap(pixmap);
dataHash.insert(pagenum, data);
fz_close_device(ctx, dev);
fz_drop_device(ctx, dev);
fz_drop_context(ctx);
//qDebug() << "Finished rendering:" << pagenum;
emit obj->PageLoaded(pagenum);
}
void Renderer::renderPage(int pagenum, QSize DPI, int degrees){
//qDebug() << "- Rendering Page:" << pagenum << degrees;
Data *data;
fz_matrix matrix;
fz_rect bbox;
fz_display_list *list;
//double pageDPI = 96.0;
//double sf = DPI.width() / pageDPI;
double sf = 1;
//fz_scale(&matrix, sf, sf);
fz_rotate(&matrix, degrees);
fz_page *PAGE = fz_load_page(CTX, DOC, pagenum);
fz_bound_page(CTX, PAGE, &bbox);
emit OrigSize(QSizeF(bbox.x1 - bbox.x0, bbox.y1 - bbox.y0));
fz_transform_rect(&bbox, &matrix);
list = fz_new_display_list(CTX, &bbox);
fz_device *dev = fz_new_list_device(CTX, list);
fz_run_page(CTX, PAGE, dev, &fz_identity, NULL);
fz_link *link = fz_load_links(CTX, PAGE);
pdf_annot *_annot = pdf_first_annot(CTX, (pdf_page*)PAGE);
QList<Annot*> annotList;
//qDebug() << "Starting annotations for:" << pagenum;
while(_annot) {
pdf_run_annot(CTX, _annot, dev, &fz_identity, NULL);
fz_rect anotBox;
pdf_bound_annot(CTX, _annot, &anotBox);
QRectF rect(sf*anotBox.x0, sf*anotBox.y0, sf*(anotBox.x1-anotBox.x0), sf*(anotBox.y1 - anotBox.y0));
char *contents = NULL, *author = NULL;
fz_try(CTX)
contents = pdf_copy_annot_contents(CTX, _annot);
fz_catch(CTX) { }
fz_try(CTX)
author = pdf_copy_annot_author(CTX, _annot);
fz_catch(CTX) { }
Annot *annot = new Annot(CTX, _annot, contents, author, rect);
annotList.append(annot);
_annot = _annot->next;
}
fz_close_device(CTX, dev);
fz_drop_device(CTX, dev);
fz_drop_page(CTX, PAGE);
data = new Data(pagenum, CTX, list, bbox, matrix, sf, link, annotList);
data->setRenderThread(QtConcurrent::run(&renderer, data, this));
}
QList<TextData*> Renderer::searchDocument(QString text, bool matchCase){
fz_rect rectBuffer[1000];
QList<TextData*> results;
for(int i = 0; i < pnum; i++) {
int count = fz_search_display_list(CTX, dataHash[i]->getDisplayList(), text.toLocal8Bit().data(), rectBuffer, 1000);
//qDebug() << "Page " << i+1 << ": Count, " << count;
for(int j = 0; j < count; j++) {
TextData *t = new TextData(dataHash[i]->getScaledRect(), i+1, text);
//MuPDF search does not match case, so retrieve the exact text at the location found and determine whether or not it matches the case of the search text if the user selected to match case
if(matchCase){
fz_stext_page *sPage = fz_new_stext_page_from_display_list(CTX, dataHash[i]->getDisplayList(), NULL);
QString currentStr = QString(fz_copy_selection(CTX, sPage, *fz_rect_min(&rectBuffer[j]), *fz_rect_max(&rectBuffer[j]), false));
if(currentStr.contains(text, Qt::CaseSensitive)){ results.append(t); }
}else{
results.append(t);
}
}
}
return results;
}
QImage Renderer::imageHash(int pagenum) {
return dataHash[pagenum]->getImage();
}
int Renderer::hashSize() {
return dataHash.size();
}
void Renderer::clearHash() {
qDeleteAll(dataHash);
dataHash.clear();
}
TextData* Renderer::linkList(int pagenum, int entry) {
return dataHash[pagenum]->getLinkList()[entry]->getData();
}
int Renderer::linkSize(int pagenum) {
return dataHash[pagenum]->getLinkList().size();
}
QList<QString> Renderer::annotList(int pagenum, int entry) {
return QList<QString>() << dataHash[pagenum]->getAnnotList()[entry]->getAuthor() << dataHash[pagenum]->getAnnotList()[entry]->getText();
}
QRectF Renderer::annotLoc(int pagenum, int entry) {
return dataHash[pagenum]->getAnnotList()[entry]->getLoc();
}
int Renderer::annotSize(int pagenum) {
return dataHash[pagenum]->getAnnotList().size();
}
bool Renderer::isExternalLink(int pagenum, QString text) {
QList<Link*> linkList = dataHash[pagenum]->getLinkList();
foreach(Link *link, linkList) {
if(link->getData()->text() == text)
return fz_is_external_link(CTX, link->getData()->text().toLocal8Bit().data());
}
return false;
}
bool Renderer::supportsExtraFeatures() { return true; }
|