-
Notifications
You must be signed in to change notification settings - Fork 8
/
epubdocument.cpp
310 lines (262 loc) · 10 KB
/
epubdocument.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
#include "epubdocument.h"
#include "epubcontainer.h"
#include <QIODevice>
#include <QDebug>
#include <QDir>
#include <QTextCursor>
#include <QThread>
#include <QElapsedTimer>
#include <QDomDocument>
#include <QSvgRenderer>
#include <QPainter>
#include <QTextBlock>
#include <QRegularExpression>
#include <QFontDatabase>
#include <QTextDocumentFragment>
#include <QImageReader>
#include <QAbstractTextDocumentLayout>
#include <qmath.h>
#ifdef DEBUG_CSS
#include <private/qcssparser_p.h>
#endif
EPubDocument::EPubDocument(QObject *parent) : QTextDocument(parent),
m_container(nullptr),
m_loaded(false)
{
setUndoRedoEnabled(false);
connect(documentLayout(), &QAbstractTextDocumentLayout::documentSizeChanged, this, [=](const QSizeF &newSize) {
qDebug() << "doc size changed" << newSize;
m_docSize = newSize;
});
}
EPubDocument::~EPubDocument()
{
for (const int fontId : m_loadedFonts) {
QFontDatabase::removeApplicationFont(fontId);
}
}
void EPubDocument::openDocument(const QString &path)
{
m_documentPath = path;
loadDocument();
}
void EPubDocument::loadDocument()
{
QElapsedTimer timer;
timer.start();
m_container = new EPubContainer(this);
connect(m_container, &EPubContainer::errorHappened, this, [](QString error) {
qWarning().noquote() << error;
});
if (!m_container->openFile(m_documentPath)) {
return;
}
qDebug() << "Opened in" << timer.restart() << "ms";
//QTextCursor cursor(this);
//cursor.movePosition(QTextCursor::End);
QStringList items = m_container->getItems();
QString cover = m_container->getStandardPage(EpubPageReference::CoverPage);
if (!cover.isEmpty()) {
items.prepend(cover);
qDebug() << cover;
}
QDomDocument domDoc;
QTextCursor textCursor(this);
textCursor.beginEditBlock();
textCursor.movePosition(QTextCursor::End);
QTextBlockFormat pageBreak;
pageBreak.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
//for (const QString &chapter : items) {
while(!items.isEmpty()) {
const QString &chapter = items.takeFirst();
m_currentItem = m_container->getEpubItem(chapter);
if (m_currentItem.path.isEmpty()) {
continue;
}
QSharedPointer<QIODevice> ioDevice = m_container->getIoDevice(m_currentItem.path);
if (!ioDevice) {
qWarning() << "Unable to get iodevice for chapter" << chapter;
continue;
}
domDoc.setContent(ioDevice.data());
setBaseUrl(QUrl(m_currentItem.path));
fixImages(domDoc);
textCursor.insertFragment(QTextDocumentFragment::fromHtml(domDoc.toString()));
textCursor.insertBlock(pageBreak);
}
qDebug() << "Base url:" << baseUrl();
setBaseUrl(QUrl());
emit loadCompleted();
qDebug() << "Load done in" << timer.restart() << "ms";
{
QElapsedTimer timer;
timer.start();
QFont f = defaultFont();
QFontMetrics fm(f);
int mw = fm.horizontalAdvance(QLatin1Char('x')) * 80;
int w = mw;
setTextWidth(w);
qDebug() << "Text width set in" << timer.restart() << "ms";
QSizeF size = m_docSize;
if (size.width() != 0) {
w = qSqrt((uint)(5 * size.height() * size.width() / 3));
setTextWidth(qMin(w, mw));
size = m_docSize;//documentLayout()->documentSize();
if (w*3 < 5*size.height()) {
w = qSqrt((uint)(2 * size.height() * size.width()));
setTextWidth(qMin(w, mw));
}
}
qDebug() << "Text width changed in" << timer.restart() << "ms";
w = idealWidth();
qDebug() << "Ideal width in" << timer.restart() << "ms";
setTextWidth(w);
qDebug() << "Final text width in" << timer.restart() << "ms";
}
qDebug() << "Adjust size done in" << timer.elapsed() << "ms";
textCursor.endEditBlock();
m_loaded = true;
}
void EPubDocument::fixImages(QDomDocument &newDocument)
{
// TODO: FIXME: replace this with not smushing all HTML together in one document
{ // Fix relative URLs, images are lazily loaded so the base URL might not
// be correct when they are loaded
QDomNodeList imageNodes = newDocument.elementsByTagName("img");
for (int i=0; i<imageNodes.count(); i++) {
QDomElement image = imageNodes.at(i).toElement();
if (!image.hasAttribute("src")) {
continue;
}
QUrl href = QUrl(image.attribute("src"));
href = baseUrl().resolved(href);
image.setAttribute("src", href.toString());
}
}
{ // QImage which QtSvg uses isn't able to read files from inside the archive, so embed image data inline
QDomNodeList imageNodes = newDocument.elementsByTagName("image"); // SVG images
for (int i=0; i<imageNodes.count(); i++) {
QDomElement image = imageNodes.at(i).toElement();
if (!image.hasAttribute("xlink:href")) {
continue;
}
QString path = image.attribute("xlink:href");
QByteArray fileData = loadResource(0, QUrl(path)).toByteArray();
QByteArray data = "data:image/jpeg;base64," +fileData.toBase64();
image.setAttribute("xlink:href", QString::fromLatin1(data));
}
}
static int svgCounter = 0;
// QTextDocument isn't fond of SVGs, so rip them out and store them separately, and give it <img> instead
QDomNodeList svgNodes = newDocument.elementsByTagName("svg");
for (int i=0; i<svgNodes.count(); i++) {
QDomElement svgNode = svgNodes.at(i).toElement();
// Serialize out the old SVG, store it
QDomDocument tempDocument;
tempDocument.appendChild(tempDocument.importNode(svgNode, true));
QString svgId = QString::number(++svgCounter);
m_svgs.insert(svgId, tempDocument.toByteArray());
// Create <img> node pointing to our SVG image
QDomElement imageElement = newDocument.createElement("img");
imageElement.setAttribute("src", "svgcache:" + svgId);
// Replace <svg> node with our <img> node
QDomNode parent = svgNodes.at(i).parentNode();
parent.replaceChild(imageElement, svgNode);
}
}
const QImage &EPubDocument::getSvgImage(const QString &id)
{
if (m_renderedSvgs.contains(id)) {
return m_renderedSvgs[id];
}
if (!m_svgs.contains(id)) {
qWarning() << "Couldn't find SVG" << id;
static QImage nullImg;
return nullImg;
}
QSize imageSize(pageSize().width() - documentMargin() * 4,
pageSize().height() - documentMargin() * 4);
QSvgRenderer renderer(m_svgs.value(id));
QSize svgSize(renderer.viewBox().size());
if (svgSize.isValid()) {
if (svgSize.scaled(imageSize, Qt::KeepAspectRatio).isValid()) {
svgSize.scale(imageSize, Qt::KeepAspectRatio);
}
} else {
svgSize = imageSize;
}
QImage rendered(svgSize, QImage::Format_ARGB32);
QPainter painter(&rendered);
if (!painter.isActive()) {
qWarning() << "Unable to activate painter" << svgSize;
static const QImage dummy = QImage();
return dummy;
}
renderer.render(&painter);
painter.end();
m_renderedSvgs.insert(id, rendered);
return m_renderedSvgs[id];
}
QVariant EPubDocument::loadResource(int type, const QUrl &url)
{
Q_UNUSED(type);
if (url.scheme() == "svgcache") {
return getSvgImage(url.path());
}
if (url.scheme() == "data") {
QByteArray data = url.path().toUtf8();
const int start = data.indexOf(';');
if (start == -1) {
qWarning() << "unable to decode data:, no ;" << data.left(100);
data = QByteArray();
addResource(type, url, data);
return data;
}
data = data.mid(start + 1);
if (data.startsWith("base64,")) {
data = QByteArray::fromBase64(data.mid(data.indexOf(',') + 1));
} else {
qWarning() << "unable to decode data:, unknown encoding" << data.left(100);
data = QByteArray();
}
addResource(type, url, data);
return data;
}
QSharedPointer<QIODevice> ioDevice = m_container->getIoDevice(url.path());
if (!ioDevice) {
qWarning() << "Unable to get io device for" << url.toString().left(100);
qDebug() << url.scheme();
return QVariant();
}
QByteArray data = ioDevice->readAll();
if (type == QTextDocument::StyleSheetResource) {
const QString cssData = QString::fromUtf8(data);
// Extract embedded fonts
static const QRegularExpression fontfaceRegex("@font-face\\s*{[^}]+}", QRegularExpression::MultilineOption);
QRegularExpressionMatchIterator fontfaceIterator = fontfaceRegex.globalMatch(cssData);
while (fontfaceIterator.hasNext()) {
QString fontface = fontfaceIterator.next().captured();
static const QRegularExpression urlExpression("url\\s*\\(([^\\)]+)\\)");
QString fontPath = urlExpression.match(fontface).captured(1);
// Resolve relative and whatnot shit
fontPath = QDir::cleanPath(QFileInfo(baseUrl().path()).path() + '/' + fontPath);
QSharedPointer<QIODevice> ioDevice = m_container->getIoDevice(fontPath);
if (ioDevice) {
m_loadedFonts.append(QFontDatabase::addApplicationFontFromData(ioDevice->readAll()));
qDebug() << "Loaded font" << QFontDatabase::applicationFontFamilies(m_loadedFonts.last());
} else {
qWarning() << "Failed to load font from" << fontPath << baseUrl();
}
}
data = cssData.toUtf8();
//#ifdef DEBUG_CSS
QCss::Parser parser(cssData);
QCss::StyleSheet stylesheet;
qDebug() << "=====================";
qDebug() << "Parse success?" << parser.parse(&stylesheet);
qDebug().noquote() << parser.errorIndex << parser.errorSymbol().lexem();
//#endif
}
addResource(type, url, data);
return data;
}