added image support
This commit is contained in:
1 parent
1c8277f7e9
commit
bf31f5ca98
12 files changed
+644
-33
No files matched your search
@@ -110,6 +110,8 @@ void BookController::openBook(const QUrl &fileUrl)
|
||||
|
||||
m_reader = std::move(reader);
|
||||
m_currentFilePath = filePath;
|
||||
if (m_imageProvider)
|
||||
m_imageProvider->setEpubPath(filePath);
|
||||
m_bookTitle = m_reader->title();
|
||||
m_bookAuthor = m_reader->author();
|
||||
|
||||
@@ -141,6 +143,8 @@ void BookController::closeBook()
|
||||
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_bookTitle, m_bookAuthor,
|
||||
m_readingModel->progress());
|
||||
}
|
||||
if (m_imageProvider)
|
||||
m_imageProvider->clear();
|
||||
m_reader.reset();
|
||||
m_bookTitle.clear();
|
||||
m_bookAuthor.clear();
|
||||
@@ -159,3 +163,14 @@ void BookController::clearError()
|
||||
m_errorMessage.clear();
|
||||
emit errorMessageChanged();
|
||||
}
|
||||
|
||||
void BookController::setImageProvider(EpubImageProvider *provider)
|
||||
{
|
||||
m_imageProvider = provider;
|
||||
}
|
||||
|
||||
void BookController::setContentWidth(int width)
|
||||
{
|
||||
if (m_imageProvider)
|
||||
m_imageProvider->setDisplayWidth(width);
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <QList>
|
||||
#include <memory>
|
||||
|
||||
#include "EpubImageProvider.h"
|
||||
#include "FormatReader.h"
|
||||
#include "IFileDialogService.h"
|
||||
|
||||
@@ -37,12 +38,14 @@ public:
|
||||
void setReadingModel(ReadingModel *model);
|
||||
void setAppState(AppState *state);
|
||||
void setFileDialogService(IFileDialogService *service);
|
||||
void setImageProvider(EpubImageProvider *provider);
|
||||
|
||||
Q_INVOKABLE void openBook(const QUrl &fileUrl);
|
||||
Q_INVOKABLE void openBookFromPath(const QString &filePath);
|
||||
Q_INVOKABLE void browseAndOpen();
|
||||
Q_INVOKABLE void closeBook();
|
||||
Q_INVOKABLE void clearError();
|
||||
Q_INVOKABLE void setContentWidth(int width);
|
||||
|
||||
signals:
|
||||
void bookReady();
|
||||
@@ -56,6 +59,7 @@ private:
|
||||
ReadingModel *m_readingModel = nullptr;
|
||||
AppState *m_appState = nullptr;
|
||||
IFileDialogService *m_fileDialogService = nullptr;
|
||||
EpubImageProvider *m_imageProvider = nullptr;
|
||||
QString m_bookTitle;
|
||||
QString m_bookAuthor;
|
||||
QString m_currentFilePath;
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
#include "EpubImageProvider.h"
|
||||
|
||||
#include <QImage>
|
||||
#include <QMutexLocker>
|
||||
#include <QQuickTextureFactory>
|
||||
#include <QThreadPool>
|
||||
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
#include "backend/ZipUtils.h"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EpubImageResponse
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
EpubImageResponse::EpubImageResponse(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
: m_epubPath(epubPath)
|
||||
, m_id(id)
|
||||
, m_displayWidth(displayWidth)
|
||||
, m_requestedSize(requestedSize)
|
||||
{
|
||||
setAutoDelete(false);
|
||||
}
|
||||
|
||||
QQuickTextureFactory *EpubImageResponse::textureFactory() const
|
||||
{
|
||||
if (m_image.isNull())
|
||||
return nullptr;
|
||||
return QQuickTextureFactory::textureFactoryForImage(m_image);
|
||||
}
|
||||
|
||||
void EpubImageResponse::cancel()
|
||||
{
|
||||
m_cancelled.storeRelaxed(1);
|
||||
}
|
||||
|
||||
void EpubImageResponse::run()
|
||||
{
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
m_image = EpubImageProvider::loadImageFromEpub(
|
||||
m_epubPath, m_id, m_displayWidth, m_requestedSize);
|
||||
|
||||
if (m_cancelled.loadRelaxed())
|
||||
return;
|
||||
|
||||
emit finished();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// EpubImageProvider
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
QQuickImageResponse *EpubImageProvider::requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize)
|
||||
{
|
||||
QString epubPath;
|
||||
int displayWidth;
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
epubPath = m_epubPath;
|
||||
displayWidth = m_displayWidth;
|
||||
}
|
||||
|
||||
auto *response = new EpubImageResponse(epubPath, id, displayWidth, requestedSize);
|
||||
QThreadPool::globalInstance()->start(response);
|
||||
return response;
|
||||
}
|
||||
|
||||
QImage EpubImageProvider::loadImageFromEpub(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize)
|
||||
{
|
||||
if (epubPath.isEmpty())
|
||||
return {};
|
||||
|
||||
unzFile zf = unzOpen(epubPath.toUtf8().constData());
|
||||
if (!zf)
|
||||
return {};
|
||||
|
||||
const QByteArray data = ZipUtils::readZipEntry(zf, id.toUtf8().constData());
|
||||
unzClose(zf);
|
||||
|
||||
if (data.isEmpty())
|
||||
return {};
|
||||
|
||||
QImage image;
|
||||
if (!image.loadFromData(data))
|
||||
return {};
|
||||
|
||||
// Sizing logic: honor EPUB explicit dimensions, scale down to fit display width otherwise
|
||||
if (requestedSize.width() > 0 || requestedSize.height() > 0) {
|
||||
int targetW = requestedSize.width() > 0 ? requestedSize.width() : -1;
|
||||
int targetH = requestedSize.height() > 0 ? requestedSize.height() : -1;
|
||||
|
||||
// Clamp to display width
|
||||
if (displayWidth > 0 && targetW > 0 && targetW > displayWidth)
|
||||
targetW = displayWidth;
|
||||
|
||||
if (targetW > 0 && targetH > 0) {
|
||||
image = image.scaled(QSize(targetW, targetH), Qt::KeepAspectRatio,
|
||||
Qt::SmoothTransformation);
|
||||
} else if (targetW > 0) {
|
||||
image = image.scaledToWidth(targetW, Qt::SmoothTransformation);
|
||||
} else {
|
||||
image = image.scaledToHeight(targetH, Qt::SmoothTransformation);
|
||||
}
|
||||
} else {
|
||||
// No EPUB hints: scale down if wider than display, never enlarge
|
||||
if (displayWidth > 0 && image.width() > displayWidth)
|
||||
image = image.scaledToWidth(displayWidth, Qt::SmoothTransformation);
|
||||
}
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
void EpubImageProvider::setEpubPath(const QString &filePath)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_epubPath = filePath;
|
||||
}
|
||||
|
||||
void EpubImageProvider::setDisplayWidth(int width)
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_displayWidth = width;
|
||||
}
|
||||
|
||||
void EpubImageProvider::clear()
|
||||
{
|
||||
QMutexLocker lock(&m_mutex);
|
||||
m_epubPath.clear();
|
||||
m_displayWidth = 0;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
|
||||
#include <QAtomicInt>
|
||||
#include <QImage>
|
||||
#include <QMutex>
|
||||
#include <QQuickAsyncImageProvider>
|
||||
#include <QRunnable>
|
||||
#include <QString>
|
||||
|
||||
class EpubImageResponse : public QQuickImageResponse, public QRunnable {
|
||||
// NO Q_OBJECT — already inherited from QQuickImageResponse
|
||||
public:
|
||||
EpubImageResponse(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
QQuickTextureFactory *textureFactory() const override;
|
||||
void cancel() override;
|
||||
void run() override;
|
||||
|
||||
private:
|
||||
const QString m_epubPath;
|
||||
const QString m_id;
|
||||
const int m_displayWidth;
|
||||
const QSize m_requestedSize;
|
||||
QAtomicInt m_cancelled{0};
|
||||
QImage m_image;
|
||||
};
|
||||
|
||||
class EpubImageProvider : public QQuickAsyncImageProvider {
|
||||
public:
|
||||
QQuickImageResponse *requestImageResponse(const QString &id,
|
||||
const QSize &requestedSize) override;
|
||||
|
||||
void setEpubPath(const QString &filePath);
|
||||
void setDisplayWidth(int width);
|
||||
void clear();
|
||||
|
||||
static QImage loadImageFromEpub(const QString &epubPath, const QString &id,
|
||||
int displayWidth, const QSize &requestedSize);
|
||||
|
||||
private:
|
||||
QMutex m_mutex;
|
||||
QString m_epubPath;
|
||||
int m_displayWidth = 0;
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <QByteArray>
|
||||
#include <minizip/unzip.h>
|
||||
|
||||
namespace ZipUtils {
|
||||
|
||||
// Read a single file from an open unzFile handle by name.
|
||||
// Returns empty QByteArray on failure.
|
||||
inline 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{};
|
||||
}
|
||||
|
||||
} // namespace ZipUtils
|
||||
@@ -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])};
|
||||
}
|
||||
@@ -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
|
||||
Reference in new issue
Block a user