hopefully true virtual list layout

This commit is contained in:
Agel committed 2026-10-28 23:44:54 +01:00
1 parent 905c61da52
commit 6c821c6a3c
10 files changed
+522 -37

No files matched your search

+46
View File
@@ -51,6 +51,29 @@ public:
}
};
class ManyChunksTextReader : public FormatReader {
public:
ReadingMode mode() const override { return ReadingMode::Text; }
bool open(const QString &) override { return true; }
QString lastError() const override { return {}; }
QString title() const override { return QStringLiteral("Many Chunks"); }
QString author() const override { return QStringLiteral("Author"); }
QString filePath() const override { return QStringLiteral("/fake/many.epub"); }
QString coverImageUrl() const override { return {}; }
QByteArray contentHash() const override { return QByteArray("manychunks"); }
bool loadMetadata(const QString &) override { return true; }
QStringList chunks() const override
{
QStringList list;
list.reserve(60);
for (int i = 0; i < 60; ++i) {
list.append(QStringLiteral("<p>Paragraph chunk number %1 with some text content.</p>").arg(i));
}
return list;
}
};
class ReadingModelTest : public QObject {
Q_OBJECT
@@ -243,6 +266,29 @@ private slots:
model.setReader(nullptr);
QCOMPARE(model.useBookColours(), false);
}
void visibleFirstLayoutAndOnDemandLayout()
{
Ctx ctx;
ReadingModel model(*ctx.store);
ManyChunksTextReader reader;
QSignalSpy restoreSpy(&model, &ReadingModel::restoreReady);
model.setReader(&reader);
// restoreReady must be emitted immediately from Phase 1 (<1ms)
QCOMPARE(restoreSpy.count(), 1);
QCOMPARE(restoreSpy.first().at(0).toInt(), 0);
// Row 0..25 are laid out immediately
QVERIFY(model.data(model.index(0, 0), ReadingModel::PixelHeightRole).toReal() > 0);
QVERIFY(!model.data(model.index(0, 0), ReadingModel::LayoutRole).value<ChunkLayout>().lines.isEmpty());
// Far-away row (e.g. 55) layout should be computed on-demand if queried
QVERIFY(model.data(model.index(55, 0), ReadingModel::PixelHeightRole).toReal() > 0);
const auto layout55 = model.data(model.index(55, 0), ReadingModel::LayoutRole).value<ChunkLayout>();
QVERIFY(!layout55.lines.isEmpty());
}
};
QTEST_MAIN(ReadingModelTest)
+118
View File
@@ -189,6 +189,124 @@ private slots:
QVERIFY(!reader.chunks().isEmpty());
QVERIFY(reader.chunks().first().contains(QStringLiteral("Hello EPUB 3")));
}
void chapterWrapperUnwrappedIntoGranularChunks()
{
auto src = std::make_unique<InMemoryByteSource>();
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
" <rootfiles>\n"
" <rootfile full-path=\"EPUB/package.opf\" media-type=\"application/oebps-package+xml\"/>\n"
" </rootfiles>\n"
"</container>");
src->m_entries.insert(QStringLiteral("EPUB/package.opf"),
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
" <dc:title>Unwrap Test</dc:title>\n"
" </metadata>\n"
" <manifest>\n"
" <item id=\"c1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
" </manifest>\n"
" <spine>\n"
" <itemref idref=\"c1\"/>\n"
" </spine>\n"
"</package>");
// Generates several long paragraphs inside a single wrapper div
QString xhtml = QStringLiteral(
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>\n"
"<div class=\"calibre1\" style=\"text-align: justify;\">\n"
);
for (int i = 0; i < 10; ++i) {
xhtml += QStringLiteral("<p>This is paragraph %1 with sufficient text to exceed the target chunk size when combined together with other paragraphs in the chapter.</p>\n").arg(i);
}
xhtml += QStringLiteral("</div></body></html>");
src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), xhtml.toUtf8());
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
auto factory = [sharedEntries]() {
auto s = std::make_unique<InMemoryByteSource>();
s->m_entries = *sharedEntries;
return s;
};
EpubReader reader(factory, QStringLiteral("epub"));
QVERIFY(reader.open(QStringLiteral("/virtual/unwrap.epub")));
// Must be split into multiple granular chunks instead of one single monolithic chunk
QVERIFY(reader.chunks().size() >= 3);
// Children must inherit text-align: justify
for (const QString &c : reader.chunks()) {
QVERIFY(c.contains(QStringLiteral("text-align:justify")) || c.contains(QStringLiteral("text-align: justify")));
}
}
void divParagraphWithInlineElementsNotBroken()
{
auto src = std::make_unique<InMemoryByteSource>();
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
" <rootfiles>\n"
" <rootfile full-path=\"EPUB/package.opf\" media-type=\"application/oebps-package+xml\"/>\n"
" </rootfiles>\n"
"</container>");
src->m_entries.insert(QStringLiteral("EPUB/package.opf"),
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
" <dc:title>Inline Div Test</dc:title>\n"
" </metadata>\n"
" <manifest>\n"
" <item id=\"c1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
" </manifest>\n"
" <spine>\n"
" <itemref idref=\"c1\"/>\n"
" </spine>\n"
"</package>");
QString xhtml = QStringLiteral(
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>\n"
"<div class=\"calibre1\">\n"
" <div class=\"p\">«<em>Одноглазый</em>».</div>\n"
" <div class=\"p\"><em>«Когда я умру, они съедят меня и оставят только кости»</em>, — эта мысль странным образом утешала.</div>\n"
"</div></body></html>"
);
src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"), xhtml.toUtf8());
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
auto factory = [sharedEntries]() {
auto s = std::make_unique<InMemoryByteSource>();
s->m_entries = *sharedEntries;
return s;
};
EpubReader reader(factory, QStringLiteral("epub"));
QVERIFY(reader.open(QStringLiteral("/virtual/inline_div.epub")));
bool foundOdnoglazyiTogether = false;
bool foundThoughtTogether = false;
for (const QString &chunk : reader.chunks()) {
if (chunk.contains(QStringLiteral("Одноглазый"))) {
// Must have both opening and closing quotes together in the same chunk
QVERIFY(chunk.contains(QStringLiteral("«")));
QVERIFY(chunk.contains(QStringLiteral("»")));
foundOdnoglazyiTogether = true;
}
if (chunk.contains(QStringLiteral("Когда я умру"))) {
// Must have the attribution text in the same chunk
QVERIFY(chunk.contains(QStringLiteral("эта мысль странным образом утешала")));
foundThoughtTogether = true;
}
}
QVERIFY(foundOdnoglazyiTogether);
QVERIFY(foundThoughtTogether);
}
};
QTEST_MAIN(EpubReaderTest)
+58
View File
@@ -311,6 +311,64 @@ private slots:
// With useBookColours = true, (testX, testY) is drawn with border or background
QVERIFY(imgTrue.pixelColor(testX, testY).alpha() > 0);
}
void clipRectCullingSkipsOffscreenImage()
{
class TrackingResolver : public IBookImageResolver {
public:
int resolveCount = 0;
QImage resolveImage(const QString &) override {
++resolveCount;
QImage img(100, 100, QImage::Format_RGB32);
img.fill(Qt::blue);
return img;
}
QSizeF imageSize(const QString &) override {
return QSizeF(100, 100);
}
};
TrackingResolver resolver;
KnuthPlassItem::setImageResolver(&resolver);
const QString html = QStringLiteral(
"<p>First paragraph taking vertical space</p>"
"<p><img src=\"image://epub/fig1.png\" width=\"100\" height=\"100\"/></p>"
);
LayoutParams params;
params.contentWidth = 400;
params.fontSize = 16;
params.lineSpacing = 1.0;
auto layout = ChunkLayoutEngine::layoutChunk(html, params, &resolver);
QVERIFY(!layout.images.isEmpty());
const qreal imageTop = layout.images[0].targetRect.top();
QVERIFY(imageTop > 20.0);
KnuthPlassItem item;
item.setSize(QSizeF(400, layout.totalHeight));
item.setLayout(QVariant::fromValue(layout));
// 1. Paint with clip above the image: image resolver must NOT be called
QImage img(400, int(layout.totalHeight), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
{
QPainter p(&img);
p.setClipRect(QRectF(0, 0, 400, imageTop - 5.0));
item.paint(&p);
}
QCOMPARE(resolver.resolveCount, 0);
// 2. Paint with clip covering the image: image resolver must be called
{
QPainter p(&img);
p.setClipRect(QRectF(0, 0, 400, imageTop + 50.0));
item.paint(&p);
}
QVERIFY(resolver.resolveCount >= 1);
KnuthPlassItem::setImageResolver(nullptr);
}
};
QTEST_MAIN(ChunkLayoutEngineTest)