From 656587ca6393663c8d652a63df7d8393b4adaac7 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Thu, 25 Aug 2022 23:38:42 +0200 Subject: [PATCH] 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 --- core/textdocumentgenerator_p.h | 8 +++-- generators/epub/converter.cpp | 61 ++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/core/textdocumentgenerator_p.h b/core/textdocumentgenerator_p.h index 955ea303f..fb1a5e395 100644 --- a/core/textdocumentgenerator_p.h +++ b/core/textdocumentgenerator_p.h @@ -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(); diff --git a/generators/epub/converter.cpp b/generators/epub/converter.cpp index 78d3b9424..b48c5d850 100644 --- a/generators/epub/converter.cpp +++ b/generators/epub/converter.cpp @@ -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); - - 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); + } 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(); + + // 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