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