reintroduced epub support

This commit is contained in:
Agel committed 2026-10-25 19:59:23 +01:00
1 parent 49a959a89d
commit d14170a68f
49 files changed
+5048 -205

No files matched your search

+172
View File
@@ -0,0 +1,172 @@
#include <QtTest>
#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("<p>This is a test paragraph with several words that will fit nicely.</p>");
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("<h1>Title</h1><hr/><p>Body text</p>");
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("<p style=\"text-align: center\">Centered line</p>");
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("<blockquote><p>Quote text</p></blockquote>");
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("<p><img src=\"image://epub/fig1.png\" width=\"150\"/></p>");
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("<p>\n <img src=\"image://epub/fig1.png\" width=\"150\"/>\n</p>");
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("<svg width=\"200\" height=\"100\"><image xlink:href=\"image://epub/fig1.png\" width=\"150\"/></svg>");
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("<p>Before <img src=\"image://epub/fig1.png\" width=\"40\" height=\"20\"/> after</p>");
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("<p>This is a long paragraph designed to be laid out at different widths to test rapid reflow.</p>");
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"