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

+195
View File
@@ -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"