#include #include "backend/reading/BookImageResolver.h" #include "backend/reading/KnuthPlassItem.h" #include "backend/reading/layout/ChunkLayoutEngine.h" class MockImageResolver : public IBookImageResolver { public: QImage resolveImage(const QString &url) override { if (url == QLatin1String("image://epub/fig1.png") || url == QLatin1String("image://web/https://example.com/pic.jpg")) { QImage img(200, 100, QImage::Format_RGB32); img.fill(Qt::blue); return img; } return {}; } QSizeF imageSize(const QString &url) override { if (url == QLatin1String("image://epub/fig1.png") || url == QLatin1String("image://web/https://example.com/pic.jpg")) { return QSizeF(200, 100); } return {}; } }; class ChunkLayoutEngineTest : public QObject { Q_OBJECT private slots: void paragraphLayout() { const QString html = QStringLiteral("

This is a test paragraph with several words that will fit nicely.

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.2; auto layout = ChunkLayoutEngine::layoutChunk(html, params); QVERIFY(!layout.lines.isEmpty()); QVERIFY(layout.totalHeight > 0); QVERIFY(layout.lines[0].runs.size() >= 1); } void headingsAndRules() { const QString html = QStringLiteral("

Title


Body text

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params); QVERIFY(layout.lines.size() >= 2); QCOMPARE(layout.rules.size(), 1); QVERIFY(layout.totalHeight > 30); } void textAlignment() { const QString html = QStringLiteral("

Centered line

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params); QVERIFY(!layout.lines.isEmpty()); const auto &run = layout.lines[0].runs.first(); // Since it's centered in 400px width, start x should be > 0 QVERIFY(run.rect.left() > 10.0); } void blockquoteDecoration() { const QString html = QStringLiteral("

Quote text

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params); QVERIFY(!layout.decorations.isEmpty()); QVERIFY(layout.decorations[0].borderLeft > 0); } void imageBlockLayout() { MockImageResolver resolver; const QString html = QStringLiteral("

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params, &resolver); QCOMPARE(layout.images.size(), 1); QCOMPARE(layout.images[0].url, QStringLiteral("image://epub/fig1.png")); QCOMPARE(layout.images[0].targetRect.width(), 150.0); QCOMPARE(layout.images[0].targetRect.height(), 75.0); // 200x100 aspect ratio -> 150x75 } void imageWithWhitespaceAroundIt() { MockImageResolver resolver; const QString html = QStringLiteral("

\n \n

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params, &resolver); QCOMPARE(layout.images.size(), 1); QCOMPARE(layout.images[0].url, QStringLiteral("image://epub/fig1.png")); QCOMPARE(layout.images[0].targetRect.width(), 150.0); } void svgImageElement() { MockImageResolver resolver; const QString html = QStringLiteral(""); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params, &resolver); QCOMPARE(layout.images.size(), 1); QCOMPARE(layout.images[0].url, QStringLiteral("image://epub/fig1.png")); } void inlineImageInParagraph() { MockImageResolver resolver; const QString html = QStringLiteral("

Before after

"); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params, &resolver); QCOMPARE(layout.images.size(), 1); QCOMPARE(layout.images[0].url, QStringLiteral("image://epub/fig1.png")); QVERIFY(!layout.lines.isEmpty()); } void preparedChunkFastResize() { const QString html = QStringLiteral("

This is a long paragraph designed to be laid out at different widths to test rapid reflow.

"); auto prepared = ChunkLayoutEngine::prepareChunk(html, 16); QVERIFY(!prepared.blocks.isEmpty()); LayoutParams p1; p1.contentWidth = 300; p1.fontSize = 16; p1.lineSpacing = 1.0; auto l1 = ChunkLayoutEngine::layoutPreparedChunk(prepared, p1); LayoutParams p2; p2.contentWidth = 600; p2.fontSize = 16; p2.lineSpacing = 1.0; auto l2 = ChunkLayoutEngine::layoutPreparedChunk(prepared, p2); QVERIFY(l1.lines.size() >= l2.lines.size()); QCOMPARE(l1.contentWidth, 300.0); QCOMPARE(l2.contentWidth, 600.0); } void calibreHeaderLayout() { const QString html = QStringLiteral( "

" "СЛУГА КУПЦА

" ); LayoutParams params; params.contentWidth = 600; params.fontSize = 16; params.lineSpacing = 1.2; auto layout = ChunkLayoutEngine::layoutChunk(html, params); // Should have exactly 1 block decoration for the h1 box QCOMPARE(layout.decorations.size(), 1); const auto &dec = layout.decorations[0]; QCOMPARE(dec.background, QColor(0xe7, 0xe7, 0xe7)); QCOMPARE(dec.borderColor, QColor(Qt::black)); QCOMPARE(dec.borderLeft, 1.0); QCOMPARE(dec.borderRight, 1.0); QCOMPARE(dec.borderTop, 1.0); QCOMPARE(dec.borderBottom, 1.0); // Trailing
must be stripped and not produce a spurious empty second line QCOMPARE(layout.lines.size(), 1); QVERIFY(!layout.lines[0].runs.isEmpty()); // Words must NOT have individual highlight backgrounds for (const auto &run : layout.lines[0].runs) { QVERIFY(!run.background.isValid()); // But they should know the enclosing box background QCOMPARE(run.surfaceBackground, QColor(0xe7, 0xe7, 0xe7)); } } void inlineBackgrounds() { const QString html = QStringLiteral("

Normal highlighted text

"); LayoutParams params; params.contentWidth = 600; params.fontSize = 16; params.lineSpacing = 1.2; auto layout = ChunkLayoutEngine::layoutChunk(html, params); // No container box decorations for an inline span QCOMPARE(layout.decorations.size(), 0); QVERIFY(!layout.lines.isEmpty()); bool foundYellow = false; bool foundNormal = false; for (const auto &run : layout.lines[0].runs) { if (run.text.contains(QLatin1String("highlighted"))) { QCOMPARE(run.background, QColor(Qt::yellow)); foundYellow = true; } else if (run.text.contains(QLatin1String("Normal"))) { QVERIFY(!run.background.isValid()); foundNormal = true; } } QVERIFY(foundYellow); QVERIFY(foundNormal); } void nestedBackgroundContainer() { const QString html = QStringLiteral("

Inner text

"); LayoutParams params; params.contentWidth = 600; params.fontSize = 16; params.lineSpacing = 1.2; auto layout = ChunkLayoutEngine::layoutChunk(html, params); QCOMPARE(layout.decorations.size(), 1); QCOMPARE(layout.decorations[0].background, QColor(0xff, 0, 0)); QVERIFY(!layout.lines.isEmpty()); QVERIFY(!layout.lines[0].runs.isEmpty()); // Enclosing background is inherited as surfaceBackground QCOMPARE(layout.lines[0].runs[0].surfaceBackground, QColor(0xff, 0, 0)); // But not as run.background QVERIFY(!layout.lines[0].runs[0].background.isValid()); } void contrastRatioHelper() { QColor lightBg(0xe7, 0xe7, 0xe7); QColor darkBg(0x19, 0x1d, 0x35); qreal crLightBlack = KnuthPlassItem::contrastRatio(Qt::black, lightBg); qreal crLightWhite = KnuthPlassItem::contrastRatio(Qt::white, lightBg); QVERIFY(crLightBlack > 10.0); QVERIFY(crLightWhite < 2.0); qreal crDarkBlack = KnuthPlassItem::contrastRatio(Qt::black, darkBg); qreal crDarkWhite = KnuthPlassItem::contrastRatio(Qt::white, darkBg); QVERIFY(crDarkWhite > 10.0); QVERIFY(crDarkBlack < 2.0); } void knuthPlassItemPaintBoxSuppression() { const QString html = QStringLiteral( "

Header Title

" ); LayoutParams params; params.contentWidth = 400; params.fontSize = 16; params.lineSpacing = 1.0; auto layout = ChunkLayoutEngine::layoutChunk(html, params); QCOMPARE(layout.decorations.size(), 1); KnuthPlassItem item; item.setSize(QSizeF(400, 200)); item.setLayout(QVariant::fromValue(layout)); // When useBookColours is false: item.setUseBookColours(false); QImage imgFalse(400, 200, QImage::Format_ARGB32_Premultiplied); imgFalse.fill(Qt::transparent); { QPainter p(&imgFalse); item.paint(&p); } // When useBookColours is true: item.setUseBookColours(true); QImage imgTrue(400, 200, QImage::Format_ARGB32_Premultiplied); imgTrue.fill(Qt::transparent); { QPainter p(&imgTrue); item.paint(&p); } const auto &dec = layout.decorations[0]; int testX = int(dec.rect.left()); int testY = int(dec.rect.top()); // With useBookColours = false, (testX, testY) should be transparent (no background, no border box) QCOMPARE(imgFalse.pixelColor(testX, testY).alpha(), 0); // With useBookColours = true, (testX, testY) is drawn with border or background QVERIFY(imgTrue.pixelColor(testX, testY).alpha() > 0); } }; QTEST_MAIN(ChunkLayoutEngineTest) #include "ChunkLayoutEngineTest.moc"