reintroduced epub support
This commit is contained in:
49 files changed
+5048
-205
No files matched your search
@@ -16,6 +16,7 @@ static FormatRegistry makeRegistry()
|
||||
[] { return std::make_unique<RarByteSource>(); }),
|
||||
FormatSpec::comic({}, /*matchesDirectory=*/true, QStringLiteral("dir"),
|
||||
[] { return std::make_unique<DirByteSource>(); }),
|
||||
FormatSpec::epub([] { return std::make_unique<ZipByteSource>(); }),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -43,6 +44,20 @@ private slots:
|
||||
QVERIFY(registry.createReader("file.cbr") != nullptr);
|
||||
}
|
||||
|
||||
void createEpubReader()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
auto reader = registry.createReader("file.epub");
|
||||
QVERIFY(reader != nullptr);
|
||||
QCOMPARE(reader->mode(), ReadingMode::Text);
|
||||
}
|
||||
|
||||
void createEpubReaderUpperCase()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
QVERIFY(registry.createReader("file.EPUB") != nullptr);
|
||||
}
|
||||
|
||||
void createUnknownFormat()
|
||||
{
|
||||
auto registry = makeRegistry();
|
||||
@@ -77,8 +92,9 @@ private slots:
|
||||
const QStringList exts = registry.supportedExtensions();
|
||||
QVERIFY(exts.contains(QStringLiteral("cbz")));
|
||||
QVERIFY(exts.contains(QStringLiteral("cbr")));
|
||||
QVERIFY(exts.contains(QStringLiteral("epub")));
|
||||
// The directory row contributes no extension
|
||||
QCOMPARE(exts.size(), 2);
|
||||
QCOMPARE(exts.size(), 3);
|
||||
}
|
||||
|
||||
void registerImageProvidersRegistersSchemes()
|
||||
@@ -90,6 +106,7 @@ private slots:
|
||||
QVERIFY(engine.imageProvider(QStringLiteral("cbz")) != nullptr);
|
||||
QVERIFY(engine.imageProvider(QStringLiteral("cbr")) != nullptr);
|
||||
QVERIFY(engine.imageProvider(QStringLiteral("dir")) != nullptr);
|
||||
QVERIFY(engine.imageProvider(QStringLiteral("epub")) != nullptr);
|
||||
QVERIFY(engine.imageProvider(QStringLiteral("unknown")) == nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
+16
-28
@@ -41,16 +41,13 @@ public:
|
||||
QByteArray contentHash() const override { return QByteArray("texthash"); }
|
||||
bool loadMetadata(const QString &) override { return true; }
|
||||
|
||||
int chapterCount() const override { return 2; }
|
||||
Chapter loadChapter(int index) const override
|
||||
QStringList chunks() const override
|
||||
{
|
||||
if (index == 0)
|
||||
return {0, QStringLiteral("Chapter 1"), {QStringLiteral("<p>Chunk 1</p>"), QStringLiteral("<p>Chunk 2</p>")}};
|
||||
return {1, QStringLiteral("Chapter 2"), {QStringLiteral("<p>Chunk 3</p>")}};
|
||||
}
|
||||
QVector<TocEntry> tableOfContents() const override
|
||||
{
|
||||
return {{QStringLiteral("Chapter 1"), 0, 0}, {QStringLiteral("Chapter 2"), 1, 0}};
|
||||
return {
|
||||
QStringLiteral("<p>Chunk 1</p>"),
|
||||
QStringLiteral("<p>Chunk 2</p>"),
|
||||
QStringLiteral("<p>Chunk 3</p>")
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
@@ -167,7 +164,7 @@ private slots:
|
||||
QVERIFY(!model.mangaRTL());
|
||||
}
|
||||
|
||||
void textModeChaptersAndChunks()
|
||||
void textModeContinuousChunks()
|
||||
{
|
||||
Ctx ctx; ReadingModel model(*ctx.store);
|
||||
FakeTextReader reader;
|
||||
@@ -178,31 +175,22 @@ private slots:
|
||||
|
||||
QCOMPARE(modeSpy.count(), 1);
|
||||
QVERIFY(model.isTextMode());
|
||||
QCOMPARE(model.chapterCount(), 2);
|
||||
QCOMPARE(model.currentChapter(), 0);
|
||||
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1"));
|
||||
QCOMPARE(model.rowCount(), 2);
|
||||
QCOMPARE(model.rowCount(), 3);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 1</p>"));
|
||||
QCOMPARE(model.data(model.index(1, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 2</p>"));
|
||||
QCOMPARE(model.data(model.index(2, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 3</p>"));
|
||||
|
||||
QTRY_COMPARE(restoreSpy.count(), 1);
|
||||
|
||||
// Next chapter navigation
|
||||
QSignalSpy chapterSpy(&model, &ReadingModel::currentChapterChanged);
|
||||
model.nextChapter();
|
||||
QCOMPARE(chapterSpy.count(), 1);
|
||||
QCOMPARE(model.currentChapter(), 1);
|
||||
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 2"));
|
||||
QCOMPARE(model.rowCount(), 1);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::ContentRole).toString(),
|
||||
QStringLiteral("<p>Chunk 3</p>"));
|
||||
|
||||
// Previous chapter navigation
|
||||
model.previousChapter();
|
||||
QCOMPARE(model.currentChapter(), 0);
|
||||
QCOMPARE(model.rowCount(), 2);
|
||||
// Test resize fast path
|
||||
QSignalSpy dataSpy(&model, &ReadingModel::dataChanged);
|
||||
model.updateLayoutMetrics(800, 16, 1.2);
|
||||
QVERIFY(dataSpy.count() >= 1);
|
||||
QCOMPARE(model.rowCount(), 3);
|
||||
QCOMPARE(model.data(model.index(0, 0), ReadingModel::LayoutRole).value<ChunkLayout>().contentWidth, 800);
|
||||
}
|
||||
|
||||
void setReaderNullResetsState()
|
||||
|
||||
@@ -63,7 +63,6 @@ private slots:
|
||||
QCOMPARE(ctx.book->filePath(), QString());
|
||||
QCOMPARE(ctx.book->coverImageUrl(),QString());
|
||||
QCOMPARE(ctx.book->errorMessage(), QString());
|
||||
QVERIFY(ctx.book->tableOfContents().isEmpty());
|
||||
}
|
||||
|
||||
void openValidFile()
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
#include <QtTest>
|
||||
#include "backend/formats/EpubReader.h"
|
||||
#include "backend/formats/IByteSource.h"
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
|
||||
class InMemoryByteSource : public IByteSource {
|
||||
public:
|
||||
bool open(const QString &path) override {
|
||||
m_path = path;
|
||||
return m_openResult;
|
||||
}
|
||||
QStringList listEntries() const override { return m_entries.keys(); }
|
||||
QByteArray readEntry(const QString &name) const override { return m_entries.value(name); }
|
||||
QByteArray contentHash() const override { return QByteArray("testhash123"); }
|
||||
|
||||
bool m_openResult = true;
|
||||
QString m_path;
|
||||
QHash<QString, QByteArray> m_entries;
|
||||
};
|
||||
|
||||
class EpubReaderTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void openFailsIfSourceFails()
|
||||
{
|
||||
auto factory = [] {
|
||||
auto src = std::make_unique<InMemoryByteSource>();
|
||||
src->m_openResult = false;
|
||||
return src;
|
||||
};
|
||||
EpubReader reader(factory, QStringLiteral("epub"));
|
||||
QVERIFY(!reader.open(QStringLiteral("/path/to/missing.epub")));
|
||||
QVERIFY(!reader.lastError().isEmpty());
|
||||
}
|
||||
|
||||
void openFailsIfContainerMissing()
|
||||
{
|
||||
auto factory = [] {
|
||||
auto src = std::make_unique<InMemoryByteSource>();
|
||||
return src;
|
||||
};
|
||||
EpubReader reader(factory, QStringLiteral("epub"));
|
||||
QVERIFY(!reader.open(QStringLiteral("/path/to/bad.epub")));
|
||||
QVERIFY(reader.lastError().contains(QStringLiteral("container.xml")));
|
||||
}
|
||||
|
||||
void openEpub2WithMetadataCoverAndNcx()
|
||||
{
|
||||
auto src = std::make_unique<InMemoryByteSource>();
|
||||
|
||||
// 1. container.xml
|
||||
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=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
||||
" </rootfiles>\n"
|
||||
"</container>");
|
||||
|
||||
// 2. content.opf
|
||||
src->m_entries.insert(QStringLiteral("OEBPS/content.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>Test EPUB Title</dc:title>\n"
|
||||
" <dc:creator>Test Author</dc:creator>\n"
|
||||
" <meta name=\"cover\" content=\"cover-img\"/>\n"
|
||||
" </metadata>\n"
|
||||
" <manifest>\n"
|
||||
" <item id=\"cover-img\" href=\"images/cover.jpg\" media-type=\"image/jpeg\"/>\n"
|
||||
" <item id=\"style\" href=\"css/style.css\" media-type=\"text/css\"/>\n"
|
||||
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
||||
" <item id=\"ncx\" href=\"toc.ncx\" media-type=\"application/x-dtbncx+xml\"/>\n"
|
||||
" </manifest>\n"
|
||||
" <spine toc=\"ncx\">\n"
|
||||
" <itemref idref=\"ch1\"/>\n"
|
||||
" </spine>\n"
|
||||
"</package>");
|
||||
|
||||
// 3. toc.ncx
|
||||
src->m_entries.insert(QStringLiteral("OEBPS/toc.ncx"),
|
||||
"<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">\n"
|
||||
" <navMap>\n"
|
||||
" <navPoint id=\"np1\" playOrder=\"1\">\n"
|
||||
" <navLabel><text>Chapter 1: The Beginning</text></navLabel>\n"
|
||||
" <content src=\"text/ch1.xhtml\"/>\n"
|
||||
" </navPoint>\n"
|
||||
" </navMap>\n"
|
||||
"</ncx>");
|
||||
|
||||
// 4. css
|
||||
src->m_entries.insert(QStringLiteral("OEBPS/css/style.css"),
|
||||
"p { color: red; }\n"
|
||||
".quote { font-style: italic; }\n");
|
||||
|
||||
// 5. chapter 1 xhtml
|
||||
src->m_entries.insert(QStringLiteral("OEBPS/text/ch1.xhtml"),
|
||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
|
||||
"<head>\n"
|
||||
" <link rel=\"stylesheet\" href=\"../css/style.css\"/>\n"
|
||||
"</head>\n"
|
||||
"<body>\n"
|
||||
" <h1>Heading</h1>\n"
|
||||
" <p class=\"quote\">A stylish quote with an embedded image: <img src=\"../images/fig1.png\"/></p>\n"
|
||||
" <p>External image: <img src=\"https://example.com/pic.jpg\"/></p>\n"
|
||||
"</body>\n"
|
||||
"</html>");
|
||||
|
||||
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/book.epub")));
|
||||
|
||||
QCOMPARE(reader.title(), QStringLiteral("Test EPUB Title"));
|
||||
QCOMPARE(reader.author(), QStringLiteral("Test Author"));
|
||||
QCOMPARE(reader.coverImageUrl(), ImageUrl::build(QStringLiteral("epub"),
|
||||
QStringLiteral("OEBPS/images/cover.jpg"),
|
||||
QStringLiteral("/virtual/book.epub")));
|
||||
|
||||
QVERIFY(!reader.chunks().isEmpty());
|
||||
|
||||
QString fullHtml = reader.chunks().join(QString());
|
||||
// Verify CSS styling inlined
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("color:red")) || fullHtml.contains(QStringLiteral("italic")));
|
||||
// Verify embedded image resolved to image://epub/ with source path
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("image://epub/OEBPS/images/fig1.png")));
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("src=%2Fvirtual%2Fbook.epub")) || fullHtml.contains(QStringLiteral("src=/virtual/book.epub")));
|
||||
// Verify web image resolved to image://web/
|
||||
QVERIFY(fullHtml.contains(QStringLiteral("image://web/https://example.com/pic.jpg")));
|
||||
}
|
||||
|
||||
void openEpub3WithNavDocument()
|
||||
{
|
||||
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=\"3.0\">\n"
|
||||
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
||||
" <dc:title>EPUB 3 Sample</dc:title>\n"
|
||||
" </metadata>\n"
|
||||
" <manifest>\n"
|
||||
" <item id=\"cover\" href=\"cover.png\" media-type=\"image/png\" properties=\"cover-image\"/>\n"
|
||||
" <item id=\"nav\" href=\"nav.xhtml\" media-type=\"application/xhtml+xml\" properties=\"nav\"/>\n"
|
||||
" <item id=\"c1\" href=\"ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
||||
" </manifest>\n"
|
||||
" <spine>\n"
|
||||
" <itemref idref=\"c1\"/>\n"
|
||||
" </spine>\n"
|
||||
"</package>");
|
||||
|
||||
src->m_entries.insert(QStringLiteral("EPUB/nav.xhtml"),
|
||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
|
||||
"<body>\n"
|
||||
" <nav epub:type=\"toc\">\n"
|
||||
" <ol>\n"
|
||||
" <li><a href=\"ch1.xhtml\">First Section</a></li>\n"
|
||||
" </ol>\n"
|
||||
" </nav>\n"
|
||||
"</body>\n"
|
||||
"</html>");
|
||||
|
||||
src->m_entries.insert(QStringLiteral("EPUB/ch1.xhtml"),
|
||||
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><p>Hello EPUB 3</p></body></html>");
|
||||
|
||||
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/epub3.epub")));
|
||||
QCOMPARE(reader.title(), QStringLiteral("EPUB 3 Sample"));
|
||||
QCOMPARE(reader.coverImageUrl(), ImageUrl::build(QStringLiteral("epub"),
|
||||
QStringLiteral("EPUB/cover.png"),
|
||||
QStringLiteral("/virtual/epub3.epub")));
|
||||
QVERIFY(!reader.chunks().isEmpty());
|
||||
QVERIFY(reader.chunks().first().contains(QStringLiteral("Hello EPUB 3")));
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(EpubReaderTest)
|
||||
#include "EpubReaderTest.moc"
|
||||
@@ -0,0 +1,114 @@
|
||||
#include <QtTest>
|
||||
#include <QBuffer>
|
||||
#include <QHash>
|
||||
#include "backend/formats/WebImageProvider.h"
|
||||
|
||||
class MockWebStorage : public IWebImageStorage {
|
||||
public:
|
||||
bool contains(const QString &key) const override {
|
||||
return m_store.contains(key);
|
||||
}
|
||||
QByteArray read(const QString &key) const override {
|
||||
return m_store.value(key);
|
||||
}
|
||||
bool write(const QString &key, const QByteArray &data) override {
|
||||
m_store.insert(key, data);
|
||||
return true;
|
||||
}
|
||||
|
||||
QHash<QString, QByteArray> m_store;
|
||||
};
|
||||
|
||||
static QByteArray createSamplePng()
|
||||
{
|
||||
QImage img(100, 100, QImage::Format_RGB32);
|
||||
img.fill(Qt::red);
|
||||
QByteArray bytes;
|
||||
QBuffer buf(&bytes);
|
||||
buf.open(QIODevice::WriteOnly);
|
||||
img.save(&buf, "PNG");
|
||||
return bytes;
|
||||
}
|
||||
|
||||
class WebImageProviderTest : public QObject {
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void cacheKeyFormat()
|
||||
{
|
||||
QString k1 = WebImageProvider::cacheKey(QStringLiteral("https://example.com/photo.jpg"));
|
||||
QVERIFY(k1.endsWith(QStringLiteral(".jpg")));
|
||||
|
||||
QString k2 = WebImageProvider::cacheKey(QStringLiteral("https://example.com/vector.svg"));
|
||||
QVERIFY(k2.endsWith(QStringLiteral(".svg")));
|
||||
|
||||
QString k3 = WebImageProvider::cacheKey(QStringLiteral("https://example.com/noext"));
|
||||
QVERIFY(!k3.contains(QLatin1Char('.')));
|
||||
}
|
||||
|
||||
void fetchFromNetworkAndStore()
|
||||
{
|
||||
auto storage = std::make_shared<MockWebStorage>();
|
||||
int fetchCount = 0;
|
||||
const QByteArray sampleData = createSamplePng();
|
||||
|
||||
WebImageProvider::HttpFetcher fetcher = [&](const QString &url) -> QByteArray {
|
||||
Q_UNUSED(url);
|
||||
++fetchCount;
|
||||
return sampleData;
|
||||
};
|
||||
|
||||
WebImageProvider provider(storage, fetcher);
|
||||
|
||||
const QString url = QStringLiteral("https://example.com/test.png");
|
||||
QImage img = provider.fetchImage(url);
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(img.size(), QSize(100, 100));
|
||||
QCOMPARE(fetchCount, 1);
|
||||
QVERIFY(storage->contains(WebImageProvider::cacheKey(url)));
|
||||
|
||||
// Second fetch hits memory cache -> fetchCount remains 1
|
||||
QImage img2 = provider.fetchImage(url);
|
||||
QVERIFY(!img2.isNull());
|
||||
QCOMPARE(fetchCount, 1);
|
||||
}
|
||||
|
||||
void cacheHitFromStorageWithoutNetwork()
|
||||
{
|
||||
auto storage = std::make_shared<MockWebStorage>();
|
||||
const QString url = QStringLiteral("https://example.com/cached.png");
|
||||
const QString key = WebImageProvider::cacheKey(url);
|
||||
storage->write(key, createSamplePng());
|
||||
|
||||
int fetchCount = 0;
|
||||
WebImageProvider::HttpFetcher fetcher = [&](const QString &) -> QByteArray {
|
||||
++fetchCount;
|
||||
return {};
|
||||
};
|
||||
|
||||
WebImageProvider provider(storage, fetcher);
|
||||
|
||||
QImage img = provider.fetchImage(url);
|
||||
QVERIFY(!img.isNull());
|
||||
QCOMPARE(fetchCount, 0); // never hit network!
|
||||
}
|
||||
|
||||
void warmupReturnsNullImmediately()
|
||||
{
|
||||
int fetchCount = 0;
|
||||
WebImageProvider::HttpFetcher fetcher = [&](const QString &) -> QByteArray {
|
||||
++fetchCount;
|
||||
return {};
|
||||
};
|
||||
WebImageProvider provider(std::make_shared<MockWebStorage>(), fetcher);
|
||||
|
||||
auto *resp = provider.requestImageResponse(QStringLiteral("__warmup__"), {});
|
||||
QVERIFY(resp != nullptr);
|
||||
QCOMPARE(fetchCount, 0);
|
||||
delete resp;
|
||||
}
|
||||
};
|
||||
|
||||
QTEST_MAIN(WebImageProviderTest)
|
||||
#include "WebImageProviderTest.moc"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
@@ -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"
|
||||
|
||||
@@ -21,7 +21,6 @@ private slots:
|
||||
BookPositionStore store(appState);
|
||||
|
||||
const BookPosition pos = store.load(QByteArray("nonexistent"));
|
||||
QCOMPARE(pos.chapterIndex, 0);
|
||||
QCOMPARE(pos.chunkIndex, 0);
|
||||
QCOMPARE(pos.progress, 0.0);
|
||||
QCOMPARE(pos.pageShift, true);
|
||||
@@ -39,7 +38,6 @@ private slots:
|
||||
|
||||
const QByteArray hash = QByteArray("abc123deadbeef");
|
||||
BookPosition toSave;
|
||||
toSave.chapterIndex = 2;
|
||||
toSave.chunkIndex = 7;
|
||||
toSave.progress = 0.65;
|
||||
toSave.mangaRTL = false;
|
||||
@@ -47,7 +45,6 @@ private slots:
|
||||
store.save(hash, toSave);
|
||||
|
||||
const BookPosition pos = store.load(hash);
|
||||
QCOMPARE(pos.chapterIndex, 2);
|
||||
QCOMPARE(pos.chunkIndex, 7);
|
||||
QCOMPARE(pos.progress, 0.65);
|
||||
QCOMPARE(pos.mangaRTL, false);
|
||||
|
||||
Reference in new issue
Block a user