forked from xyzz/okular-backend-mupdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator_mupdf.cpp
173 lines (141 loc) · 6.91 KB
/
generator_mupdf.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
/***************************************************************************
* 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 "generator_mupdf.hpp"
#include "page.hpp"
#include <okular/core/page.h>
#include <okular/core/textpage.h>
#include <KLocalizedString>
#include <QFile>
#include <QImage>
#include <QMutexLocker>
OKULAR_EXPORT_PLUGIN(MuPDFGenerator, "libokularGenerator_mupdf.json")
MuPDFGenerator::MuPDFGenerator(QObject* parent, const QVariantList& args) : Generator(parent, args), m_synopsis{nullptr} {
setFeature(Threaded);
setFeature(TextExtraction);
}
MuPDFGenerator::~MuPDFGenerator() = default;
Okular::Document::OpenResult MuPDFGenerator::loadDocumentWithPassword(const QString& fileName, QVector<Okular::Page*>& pages, const QString& password) {
if (!m_pdfdoc.load(fileName)) { return Okular::Document::OpenError; }
if (m_pdfdoc.isLocked()) {
m_pdfdoc.unlock(password.toLocal8Bit());
if (m_pdfdoc.isLocked()) {
m_pdfdoc.close();
return Okular::Document::OpenNeedsPassword;
}
}
for (int i = 0; i < m_pdfdoc.pageCount(); ++i) {
QMuPDF::Page page = m_pdfdoc.page(i);
const QSizeF s = page.size(dpi());
const Okular::Rotation rot = Okular::Rotation0;
auto* okularPage = new Okular::Page(i, s.width(), s.height(), rot);
okularPage->setDuration(page.duration());
pages.append(okularPage);
}
return Okular::Document::OpenSuccess;
}
bool MuPDFGenerator::doCloseDocument() {
QMutexLocker locker(userMutex());
m_pdfdoc.close();
delete m_synopsis;
m_synopsis = nullptr;
return true;
}
Okular::DocumentInfo MuPDFGenerator::generateDocumentInfo(const QSet<Okular::DocumentInfo::Key>& keys) const {
QMutexLocker locker(userMutex());
Okular::DocumentInfo info;
info.set(Okular::DocumentInfo::MimeType, QStringLiteral("application/pdf"));
info.set(Okular::DocumentInfo::Pages, QString::number(m_pdfdoc.pageCount()));
#define SET(key, val) \
if (keys.contains(key)) { info.set(key, val); }
SET(Okular::DocumentInfo::Title, m_pdfdoc.infoKey("Title"))
SET(Okular::DocumentInfo::Subject, m_pdfdoc.infoKey("Subject"))
SET(Okular::DocumentInfo::Author, m_pdfdoc.infoKey("Author"))
SET(Okular::DocumentInfo::Keywords, m_pdfdoc.infoKey("Keywords"))
SET(Okular::DocumentInfo::Creator, m_pdfdoc.infoKey("Creator"))
SET(Okular::DocumentInfo::Producer, m_pdfdoc.infoKey("Producer"))
#undef SET
if (keys.contains(Okular::DocumentInfo::CustomKeys)) { info.set(QStringLiteral("format"), i18nc("PDF v. <version>", "PDF v. %1", m_pdfdoc.pdfVersion()), i18n("Format")); }
return info;
}
static void recurseCreateTOC(const QMuPDF::Document& doc, QDomDocument& mainDoc, QMuPDF::Outline* outline, QDomNode& parentDestination, const QSizeF& dpi) {
foreach (QMuPDF::Outline* child, outline->children()) {
QDomElement newel = mainDoc.createElement(child->title());
parentDestination.appendChild(newel);
if (child->isOpen()) { newel.setAttribute(QStringLiteral("Open"), QStringLiteral("true")); }
std::string link = child->link();
if (!link.size()) { continue; }
if (fz_is_external_link(doc.ctx(), link.c_str())) {
newel.setAttribute(QStringLiteral("DestinationURI"), QString::fromUtf8(link.c_str()));
} else {
float xp = 0, yp = 0;
int page = fz_resolve_link(doc.ctx(), doc.doc(), link.c_str(), &xp, &yp);
if (page == -1) continue;
Okular::DocumentViewport vp(page);
vp.rePos.pos = Okular::DocumentViewport::TopLeft;
vp.rePos.normalizedX = xp;
vp.rePos.normalizedY = yp;
vp.rePos.enabled = true;
newel.setAttribute(QStringLiteral("Viewport"), vp.toString());
}
recurseCreateTOC(doc, mainDoc, child, newel, dpi);
}
}
const Okular::DocumentSynopsis* MuPDFGenerator::generateDocumentSynopsis() {
QMutexLocker locker(userMutex());
if (m_synopsis) { return m_synopsis; }
QMuPDF::Outline* outline = m_pdfdoc.outline();
if (!outline) { return nullptr; }
m_synopsis = new Okular::DocumentSynopsis();
recurseCreateTOC(m_pdfdoc, *m_synopsis, outline, *m_synopsis, dpi());
delete outline;
return m_synopsis;
}
QImage MuPDFGenerator::image(Okular::PixmapRequest* request) {
QMutexLocker locker(userMutex());
QMuPDF::Page page = m_pdfdoc.page(request->page()->number());
QImage image = page.render(request->width(), request->height());
return image;
}
static Okular::TextPage* buildTextPage(const QVector<QMuPDF::TextBox*>& boxes, qreal width, qreal height) {
Okular::TextPage* ktp = new Okular::TextPage();
for (int i = 0; i < boxes.size(); ++i) {
QMuPDF::TextBox* box = boxes.at(i);
const QChar c = box->text();
const QRectF charBBox = box->rect();
QString text(c);
if (box->isAtEndOfLine()) { text.append(QLatin1Char('\n')); }
ktp->append(text, new Okular::NormalizedRect(charBBox.left() / width, charBBox.top() / height, charBBox.right() / width, charBBox.bottom() / height));
}
return ktp;
}
Okular::TextPage* MuPDFGenerator::textPage(Okular::TextRequest* request) {
QMutexLocker locker(userMutex());
QMuPDF::Page mp = m_pdfdoc.page(request->page()->number());
const QVector<QMuPDF::TextBox*> boxes = mp.textBoxes(dpi());
const QSizeF s = mp.size(dpi());
Okular::TextPage* tp = buildTextPage(boxes, s.width(), s.height());
qDeleteAll(boxes);
return tp;
}
QVariant MuPDFGenerator::metaData(const QString& key, const QVariant& option) const {
Q_UNUSED(option)
if (key == QStringLiteral("NamedViewport") && !option.toString().isEmpty()) {
qWarning() << "We don't store named viewports properly, but it asked for" << option.toString();
} else if (key == QLatin1String("DocumentTitle")) {
QMutexLocker locker(userMutex());
const QString title = m_pdfdoc.infoKey("Title");
return title;
} else if (key == QLatin1String("StartFullScreen")) {
if (m_pdfdoc.pageMode() == QMuPDF::Document::FullScreen) { return true; }
} else if (key == QLatin1String("OpenTOC")) {
if (m_pdfdoc.pageMode() == QMuPDF::Document::UseOutlines) { return true; }
}
return QVariant();
}
#include "generator_mupdf.moc"