epub: Improve TableOfContents for some files

The link can be percent encoded so try it like that if not found in the
normal way, also if the text overflows the page, it's in the next page

BUGS: 458289
remotes/origin/release/22.08
Albert Astals Cid 4 years ago
parent 818e1445f7
commit 656587ca63
  1. 8
      core/textdocumentgenerator_p.h
  2. 61
      generators/epub/converter.cpp

@ -136,8 +136,12 @@ static Okular::DocumentViewport calculateViewport(QTextDocument *document, const
const QSizeF pageSize = document->pageSize(); const QSizeF pageSize = document->pageSize();
const QRectF rect = document->documentLayout()->blockBoundingRect(block); const QRectF rect = document->documentLayout()->blockBoundingRect(block);
const int page = qRound(rect.y()) / qRound(pageSize.height()); int page = qRound(rect.y()) / qRound(pageSize.height());
const int offset = qRound(rect.y()) % qRound(pageSize.height()); int offset = qRound(rect.y()) % qRound(pageSize.height());
if (rect.y() + rect.height() > pageSize.height()) {
page = page + 1;
offset = 0;
}
Okular::DocumentViewport viewport(page); Okular::DocumentViewport viewport(page);
viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width(); viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width();

@ -380,37 +380,42 @@ QTextDocument *Converter::convert(const QString &fileName)
if (mSectionMap.contains(link)) { if (mSectionMap.contains(link)) {
block = mSectionMap.value(link); block = mSectionMap.value(link);
} else { // load missing resource } else {
char *data = nullptr; const QString percentDecodedLink = QUrl::fromPercentEncoding(link.toUtf8());
// epub_get_data can't handle whitespace url encodings if (mSectionMap.contains(percentDecodedLink)) {
QByteArray ba = link.replace(QLatin1String("%20"), QLatin1String(" ")).toLatin1(); block = mSectionMap.value(percentDecodedLink);
const char *clinkClean = ba.data(); } else { // load missing resource
int size = epub_get_data(mTextDocument->getEpub(), clinkClean, &data); char *data = nullptr;
// epub_get_data can't handle whitespace url encodings
if (data) { QByteArray ba = link.replace(QLatin1String("%20"), QLatin1String(" ")).toLatin1();
_cursor->insertBlock(); const char *clinkClean = ba.data();
int size = epub_get_data(mTextDocument->getEpub(), clinkClean, &data);
// try to load as image and if not load as html
block = _cursor->block(); if (data) {
QImage image; _cursor->insertBlock();
mSectionMap.insert(link, block);
if (image.loadFromData((unsigned char *)data, size)) { // try to load as image and if not load as html
mTextDocument->addResource(QTextDocument::ImageResource, QUrl(link), image); block = _cursor->block();
_cursor->insertImage(link); QImage image;
} else { mSectionMap.insert(link, block);
_cursor->insertHtml(QString::fromUtf8(data)); if (image.loadFromData((unsigned char *)data, size)) {
// Add anchors to hashes mTextDocument->addResource(QTextDocument::ImageResource, QUrl(link), image);
_handle_anchors(block, link); _cursor->insertImage(link);
} else {
_cursor->insertHtml(QString::fromUtf8(data));
// Add anchors to hashes
_handle_anchors(block, link);
}
// Start new file in a new page
int page = mTextDocument->pageCount();
while (mTextDocument->pageCount() == page) {
_cursor->insertText(QStringLiteral("\n"));
}
} }
// Start new file in a new page free(data);
int page = mTextDocument->pageCount();
while (mTextDocument->pageCount() == page) {
_cursor->insertText(QStringLiteral("\n"));
}
} }
free(data);
} }
if (block.isValid()) { // be sure we actually got a block if (block.isValid()) { // be sure we actually got a block

Loading…
Cancel
Save