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
This commit is contained in:
Albert Astals Cid 2022-08-25 23:38:42 +02:00
parent 818e1445f7
commit 656587ca63
2 changed files with 37 additions and 28 deletions

View file

@ -136,8 +136,12 @@ static Okular::DocumentViewport calculateViewport(QTextDocument *document, const
const QSizeF pageSize = document->pageSize();
const QRectF rect = document->documentLayout()->blockBoundingRect(block);
const int page = qRound(rect.y()) / qRound(pageSize.height());
const int offset = qRound(rect.y()) % qRound(pageSize.height());
int page = 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);
viewport.rePos.normalizedX = (double)rect.x() / (double)pageSize.width();

View file

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