added image support

This commit is contained in:
Agel committed 2026-03-29 20:49:45 +02:00
1 parent 1c8277f7e9
commit bf31f5ca98
12 files changed
+644 -33

No files matched your search

+138 -6
View File
@@ -118,6 +118,106 @@ static QString buildSampleEpub()
return ok ? path : QString{};
}
// Build an EPUB with an <img> tag in the chapter (image at OEBPS/Images/test.png).
static QString buildEpubWithImage()
{
static const char opfImg[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Image Book</dc:title>"
" <dc:creator>Tester</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ch1\" href=\"chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" <item id=\"img\" href=\"Images/test.png\" media-type=\"image/png\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
static const char chapterImg[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
" <head><title>Image Chapter</title></head>"
" <body>"
" <p>Chapter with image</p>"
" <img src=\"Images/test.png\"/>"
" </body>"
"</html>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_img_XXXXXX.epub");
tmp.setAutoRemove(false);
if (!tmp.open()) return {};
const QString path = tmp.fileName();
tmp.close();
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
if (!zf) return {};
bool ok = true;
ok = ok && addEntry(zf, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfImg));
ok = ok && addEntry(zf, "OEBPS/chapter1.xhtml", QByteArray(chapterImg));
ok = ok && addEntry(zf, "OEBPS/Images/test.png", QByteArray("DUMMY_PNG"));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB with a chapter in a subdirectory and a relative image path.
static QString buildEpubWithRelativeImage()
{
static const char opfRel[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">"
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">"
" <dc:title>Relative Image Book</dc:title>"
" <dc:creator>Tester</dc:creator>"
" </metadata>"
" <manifest>"
" <item id=\"ch1\" href=\"Text/chapter1.xhtml\" media-type=\"application/xhtml+xml\"/>"
" <item id=\"img\" href=\"Images/test.png\" media-type=\"image/png\"/>"
" </manifest>"
" <spine>"
" <itemref idref=\"ch1\"/>"
" </spine>"
"</package>";
static const char chapterRel[] =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"
" <head><title>Relative Image Chapter</title></head>"
" <body>"
" <p>Chapter with relative image</p>"
" <img src=\"../Images/test.png\"/>"
" </body>"
"</html>";
QTemporaryFile tmp;
tmp.setFileTemplate(QDir::tempPath() + "/epub_relimg_XXXXXX.epub");
tmp.setAutoRemove(false);
if (!tmp.open()) return {};
const QString path = tmp.fileName();
tmp.close();
zipFile zf = zipOpen(path.toUtf8().constData(), APPEND_STATUS_CREATE);
if (!zf) return {};
bool ok = true;
ok = ok && addEntry(zf, "mimetype", QByteArray(kMimetype), true);
ok = ok && addEntry(zf, "META-INF/container.xml", QByteArray(kContainer));
ok = ok && addEntry(zf, "OEBPS/content.opf", QByteArray(opfRel));
ok = ok && addEntry(zf, "OEBPS/Text/chapter1.xhtml", QByteArray(chapterRel));
ok = ok && addEntry(zf, "OEBPS/Images/test.png", QByteArray("DUMMY_PNG"));
zipClose(zf, nullptr);
return ok ? path : QString{};
}
// Build an EPUB without an NCX.
static QString buildEpubNoNcx()
{
@@ -216,16 +316,22 @@ private:
QString m_epubPath;
QString m_noNcxPath;
QString m_largePath;
QString m_imgPath;
QString m_relImgPath;
private slots:
void initTestCase()
{
m_epubPath = buildSampleEpub();
m_noNcxPath = buildEpubNoNcx();
m_largePath = buildEpubLargeChapter();
QVERIFY2(!m_epubPath.isEmpty(), "Failed to build sample EPUB fixture");
QVERIFY2(!m_noNcxPath.isEmpty(), "Failed to build no-NCX EPUB fixture");
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large chapter EPUB fixture");
m_epubPath = buildSampleEpub();
m_noNcxPath = buildEpubNoNcx();
m_largePath = buildEpubLargeChapter();
m_imgPath = buildEpubWithImage();
m_relImgPath = buildEpubWithRelativeImage();
QVERIFY2(!m_epubPath.isEmpty(), "Failed to build sample EPUB fixture");
QVERIFY2(!m_noNcxPath.isEmpty(), "Failed to build no-NCX EPUB fixture");
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large chapter EPUB fixture");
QVERIFY2(!m_imgPath.isEmpty(), "Failed to build image EPUB fixture");
QVERIFY2(!m_relImgPath.isEmpty(), "Failed to build relative-image EPUB fixture");
}
void cleanupTestCase()
@@ -233,6 +339,8 @@ private slots:
QFile::remove(m_epubPath);
QFile::remove(m_noNcxPath);
QFile::remove(m_largePath);
QFile::remove(m_imgPath);
QFile::remove(m_relImgPath);
}
// --- Phase 2: error handling ---
@@ -361,6 +469,30 @@ private slots:
QVERIFY2(hasBlock, "Chunk lacks any block element");
}
}
// --- Phase 7: image URL rewriting ---
void imageTagsRewrittenToProviderUrls()
{
EpubReader reader;
QVERIFY(reader.open(m_imgPath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY2(combined.contains(QStringLiteral("image://epub/OEBPS/Images/test.png")),
qPrintable(QStringLiteral("Expected image://epub/OEBPS/Images/test.png in: ") + combined));
}
void relativeImagePathResolved()
{
EpubReader reader;
QVERIFY(reader.open(m_relImgPath));
const Chapter ch = reader.loadChapter(0);
QVERIFY(!ch.contentChunks.isEmpty());
const QString combined = ch.contentChunks.join('\n');
QVERIFY2(combined.contains(QStringLiteral("image://epub/OEBPS/Images/test.png")),
qPrintable(QStringLiteral("Expected image://epub/OEBPS/Images/test.png in: ") + combined));
}
};
QTEST_MAIN(EpubReaderTest)