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"
+86
View File
@@ -0,0 +1,86 @@
#include <QtTest>
#include <QDomDocument>
#include "backend/reading/layout/CssParser.h"
class CssParserTest : public QObject {
Q_OBJECT
private slots:
void parseLengthUnits()
{
Css::LengthContext ctx;
ctx.em = 20.0;
ctx.rem = 16.0;
ctx.percentBase = 500.0;
ctx.absoluteScale = 1.0;
auto l1 = Css::parseLength(QStringLiteral("2em"), ctx);
QVERIFY(l1.has_value());
QCOMPARE(*l1, 40.0);
auto l2 = Css::parseLength(QStringLiteral("1.5rem"), ctx);
QVERIFY(l2.has_value());
QCOMPARE(*l2, 24.0);
auto l3 = Css::parseLength(QStringLiteral("50%"), ctx);
QVERIFY(l3.has_value());
QCOMPARE(*l3, 250.0);
auto l4 = Css::parseLength(QStringLiteral("10px"), ctx);
QVERIFY(l4.has_value());
QCOMPARE(*l4, 10.0);
auto l5 = Css::parseLength(QStringLiteral("0"), ctx);
QVERIFY(l5.has_value());
QCOMPARE(*l5, 0.0);
}
void parseColors()
{
QColor c1 = Css::parseColor(QStringLiteral("#ff0000"));
QCOMPARE(c1, QColor(255, 0, 0));
QColor c2 = Css::parseColor(QStringLiteral("rgb(0, 128, 255)"));
QCOMPARE(c2, QColor(0, 128, 255));
QColor c3 = Css::parseColor(QStringLiteral("rgba(0, 0, 0, 0.5)"));
QCOMPARE(c3.alpha(), 128); // 0.5 * 255 rounded = 128
QColor c4 = Css::parseColor(QStringLiteral("transparent"));
QCOMPARE(c4, QColor(Qt::transparent));
QColor c5 = Css::parseColor(QStringLiteral("red"));
QCOMPARE(c5, QColor(Qt::red));
}
void stylesheetParsingAndCascade()
{
const QString css = QStringLiteral(
"p { color: black; font-size: 16px; }\n"
".highlight { color: yellow; }\n"
"#special { color: red !important; }\n"
"div > p.highlight { font-weight: bold; }\n"
"@font-face { font-family: 'MyFont'; src: url('myfont.otf'); }\n"
);
auto sheet = Css::StyleSheet::parse(css);
QCOMPARE(sheet.fontFaces.size(), 1);
QCOMPARE(sheet.fontFaces[0].family, QStringLiteral("MyFont"));
QCOMPARE(sheet.fontFaces[0].src, QStringLiteral("myfont.otf"));
QDomDocument doc;
doc.setContent(QStringLiteral("<div><p id=\"special\" class=\"highlight\" style=\"color: green\">Hello</p></div>"));
QDomElement p = doc.elementsByTagName(QStringLiteral("p")).at(0).toElement();
auto decls = Css::cascade(sheet, p);
QString serialized = Css::serialize(decls);
// font-weight: bold should match div > p.highlight
QVERIFY(serialized.contains(QStringLiteral("font-weight:bold")));
// color: red should win because it is !important in #special, beating inline style="color: green"
QVERIFY(serialized.contains(QStringLiteral("color:red")));
}
};
QTEST_MAIN(CssParserTest)
#include "CssParserTest.moc"
+87
View File
@@ -0,0 +1,87 @@
#include <QtTest>
#include "backend/reading/layout/KnuthPlass.h"
class KnuthPlassTest : public QObject {
Q_OBJECT
private slots:
void emptyItemsReturnsEmpty()
{
QVector<KnuthPlass::Item> items;
auto lines = KnuthPlass::breakLines(items, 100);
QVERIFY(lines.isEmpty());
}
void singleLineParagraph()
{
// "Hello world"
// Box(30), Glue(10, 5, 3), Box(30), finishParagraph
QVector<KnuthPlass::Item> items;
items.append(KnuthPlass::Item::box(30, 0));
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
items.append(KnuthPlass::Item::box(30, 1));
KnuthPlass::finishParagraph(items);
auto lines = KnuthPlass::breakLines(items, 100);
QCOMPARE(lines.size(), 1);
QCOMPARE(lines[0].start, 0);
// The last line of a paragraph has infinite stretch in finishing glue -> ratio is 0
QCOMPARE(lines[0].ratio, 0.0);
QVERIFY(!lines[0].hyphenated);
}
void multiLineJustification()
{
// 4 words of width 40, glue width 10, stretch 5, shrink 3
// Line width 95 -> line 1: 2 words (40 + 10 + 40 = 90) + glue needs to stretch by 5 (ratio = 1.0)
QVector<KnuthPlass::Item> items;
items.append(KnuthPlass::Item::box(40, 0));
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
items.append(KnuthPlass::Item::box(40, 1));
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
items.append(KnuthPlass::Item::box(40, 2));
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
items.append(KnuthPlass::Item::box(40, 3));
KnuthPlass::finishParagraph(items);
auto lines = KnuthPlass::breakLines(items, 95);
QCOMPARE(lines.size(), 2);
QCOMPARE(lines[0].start, 0);
QCOMPARE(lines[0].ratio, 1.0); // 95 - 90 = 5 stretch, 5 / 5 = 1.0
QCOMPARE(lines[1].ratio, 0.0); // paragraph end line
}
void softHyphenBreak()
{
// Word 1 (width 50), Glue(10), Word 2 part A (30), soft-hyphen penalty (width 8, penalty 50, flagged), Word 2 part B (30)
// Line width 98: 50 + 10 + 30 + 8 = 98 -> exactly breaks at hyphen!
QVector<KnuthPlass::Item> items;
items.append(KnuthPlass::Item::box(50, 0));
items.append(KnuthPlass::Item::glue(10, 5, 3, -1));
items.append(KnuthPlass::Item::box(30, 1));
items.append(KnuthPlass::Item::penaltyItem(8, 50, true, -1));
items.append(KnuthPlass::Item::box(30, 2));
KnuthPlass::finishParagraph(items);
auto lines = KnuthPlass::breakLines(items, 98);
QCOMPARE(lines.size(), 2);
QVERIFY(lines[0].hyphenated);
QCOMPARE(lines[1].start, 4); // starts at Word 2 part B
}
void overlongLineRescue()
{
// A single box wider than target line width should not crash or return empty;
// it falls back to emergency pass.
QVector<KnuthPlass::Item> items;
items.append(KnuthPlass::Item::box(200, 0));
KnuthPlass::finishParagraph(items);
auto lines = KnuthPlass::breakLines(items, 100);
QCOMPARE(lines.size(), 1);
}
};
QTEST_MAIN(KnuthPlassTest)
#include "KnuthPlassTest.moc"