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

+22 -25
View File
@@ -1,34 +1,15 @@
#include "EpubReader.h"
#include <QDir>
#include <QDomDocument>
#include <QFileInfo>
#include <QTextStream>
#include <QUrl>
#include <QXmlStreamReader>
#include <minizip/unzip.h>
#include "backend/ZipUtils.h"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
// Read a single file from an open unzFile handle by name.
// Returns empty QByteArray on failure.
static QByteArray readZipEntry(unzFile zf, const char *entryName)
{
if (unzLocateFile(zf, entryName, 0) != UNZ_OK)
return {};
if (unzOpenCurrentFile(zf) != UNZ_OK)
return {};
QByteArray result;
char buf[4096];
int bytesRead;
while ((bytesRead = unzReadCurrentFile(zf, buf, sizeof(buf))) > 0)
result.append(buf, bytesRead);
unzCloseCurrentFile(zf);
return (bytesRead == 0) ? result : QByteArray{};
}
using ZipUtils::readZipEntry;
// Serialize a QDomNode to a string (no XML declaration, no extra indentation).
static QString serializeNode(const QDomNode &node)
@@ -250,7 +231,7 @@ void EpubReader::parseNcx(const QByteArray &data)
// chunkXhtml: DOM-based chunking of a spine XHTML item
// ---------------------------------------------------------------------------
QStringList EpubReader::chunkXhtml(const QByteArray &data)
QStringList EpubReader::chunkXhtml(const QByteArray &data, const QString &xhtmlHref)
{
if (data.isEmpty())
return {};
@@ -262,6 +243,22 @@ QStringList EpubReader::chunkXhtml(const QByteArray &data)
return {QString::fromUtf8(data)};
}
// Rewrite <img src="..."> to image://epub/ URLs so Qt can load them from the provider
const int slashPos = xhtmlHref.lastIndexOf(QLatin1Char('/'));
const QString xhtmlDir = (slashPos >= 0) ? xhtmlHref.left(slashPos + 1) : QString();
const QDomNodeList imgs = doc.elementsByTagName(QStringLiteral("img"));
for (int i = 0; i < imgs.size(); ++i) {
QDomElement img = imgs.at(i).toElement();
if (img.isNull())
continue;
QString src = img.attribute(QStringLiteral("src"));
if (src.isEmpty() || src.startsWith(QStringLiteral("image://")))
continue;
src = QUrl::fromPercentEncoding(src.toUtf8());
const QString resolved = QDir::cleanPath(xhtmlDir + src);
img.setAttribute(QStringLiteral("src"), QStringLiteral("image://epub/") + resolved);
}
// Find <body> element (may have namespace prefix in some EPUBs)
QDomElement body;
const QDomNodeList bodies = doc.elementsByTagName(QStringLiteral("body"));
@@ -329,5 +326,5 @@ Chapter EpubReader::loadChapter(int index)
if (chTitle.isEmpty() && index < m_spineHrefs.size())
chTitle = QFileInfo(m_spineHrefs[index]).completeBaseName();
return Chapter{index, chTitle, chunkXhtml(m_spineRawContent[index])};
return Chapter{index, chTitle, chunkXhtml(m_spineRawContent[index], m_spineHrefs[index])};
}
+1 -1
View File
@@ -29,7 +29,7 @@ private:
bool parseContainer(const QByteArray &data, QString &opfPath);
bool parseOpf(const QByteArray &data, QString &ncxHref);
void parseNcx(const QByteArray &data);
static QStringList chunkXhtml(const QByteArray &data);
static QStringList chunkXhtml(const QByteArray &data, const QString &xhtmlHref);
};
#endif // EPUBREADER_H