#include #include "backend/reading/BookImageResolver.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); } }; QTEST_MAIN(ChunkLayoutEngineTest) #include "ChunkLayoutEngineTest.moc"