added image support
This commit is contained in:
1 parent
1c8277f7e9
commit
bf31f5ca98
12 files changed
+644
-33
No files matched your search
+10
-1
@@ -40,6 +40,9 @@ set(SOURCES
|
|||||||
src/backend/ReadingModel.cpp
|
src/backend/ReadingModel.cpp
|
||||||
src/backend/BookController.h
|
src/backend/BookController.h
|
||||||
src/backend/BookController.cpp
|
src/backend/BookController.cpp
|
||||||
|
src/backend/EpubImageProvider.h
|
||||||
|
src/backend/EpubImageProvider.cpp
|
||||||
|
src/backend/ZipUtils.h
|
||||||
src/backend/IFileDialogService.h
|
src/backend/IFileDialogService.h
|
||||||
src/backend/IFileDialogService.cpp
|
src/backend/IFileDialogService.cpp
|
||||||
src/backend/NativeFileDialogService.h
|
src/backend/NativeFileDialogService.h
|
||||||
@@ -194,11 +197,17 @@ add_asteria_test(BookControllerTest tests/BookControllerTest.cpp
|
|||||||
src/backend/ReadingModel.cpp
|
src/backend/ReadingModel.cpp
|
||||||
src/backend/IFileDialogService.cpp
|
src/backend/IFileDialogService.cpp
|
||||||
src/backend/NativeFileDialogService.cpp
|
src/backend/NativeFileDialogService.cpp
|
||||||
|
src/backend/EpubImageProvider.cpp
|
||||||
src/backend/formats/TxtReader.cpp
|
src/backend/formats/TxtReader.cpp
|
||||||
src/backend/formats/EpubReader.cpp)
|
src/backend/formats/EpubReader.cpp)
|
||||||
add_asteria_test(AppStateTest tests/AppStateTest.cpp
|
add_asteria_test(AppStateTest tests/AppStateTest.cpp
|
||||||
src/backend/AppState.cpp)
|
src/backend/AppState.cpp)
|
||||||
|
|
||||||
target_link_libraries(BookControllerTest PRIVATE Qt6::Widgets PkgConfig::MINIZIP Qt6::Xml)
|
target_link_libraries(BookControllerTest PRIVATE Qt6::Widgets Qt6::Quick Qt6::Gui PkgConfig::MINIZIP Qt6::Xml)
|
||||||
set_tests_properties(BookControllerTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
set_tests_properties(BookControllerTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
|
add_asteria_test(EpubImageProviderTest tests/EpubImageProviderTest.cpp
|
||||||
|
src/backend/EpubImageProvider.cpp)
|
||||||
|
target_link_libraries(EpubImageProviderTest PRIVATE PkgConfig::MINIZIP Qt6::Quick Qt6::Gui)
|
||||||
|
set_tests_properties(EpubImageProviderTest PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")
|
||||||
|
|
||||||
@@ -110,6 +110,8 @@ void BookController::openBook(const QUrl &fileUrl)
|
|||||||
|
|
||||||
m_reader = std::move(reader);
|
m_reader = std::move(reader);
|
||||||
m_currentFilePath = filePath;
|
m_currentFilePath = filePath;
|
||||||
|
if (m_imageProvider)
|
||||||
|
m_imageProvider->setEpubPath(filePath);
|
||||||
m_bookTitle = m_reader->title();
|
m_bookTitle = m_reader->title();
|
||||||
m_bookAuthor = m_reader->author();
|
m_bookAuthor = m_reader->author();
|
||||||
|
|
||||||
@@ -141,6 +143,8 @@ void BookController::closeBook()
|
|||||||
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_bookTitle, m_bookAuthor,
|
m_appState->addOrUpdateLibraryBook(m_currentFilePath, m_bookTitle, m_bookAuthor,
|
||||||
m_readingModel->progress());
|
m_readingModel->progress());
|
||||||
}
|
}
|
||||||
|
if (m_imageProvider)
|
||||||
|
m_imageProvider->clear();
|
||||||
m_reader.reset();
|
m_reader.reset();
|
||||||
m_bookTitle.clear();
|
m_bookTitle.clear();
|
||||||
m_bookAuthor.clear();
|
m_bookAuthor.clear();
|
||||||
@@ -159,3 +163,14 @@ void BookController::clearError()
|
|||||||
m_errorMessage.clear();
|
m_errorMessage.clear();
|
||||||
emit errorMessageChanged();
|
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 <QList>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include "EpubImageProvider.h"
|
||||||
#include "FormatReader.h"
|
#include "FormatReader.h"
|
||||||
#include "IFileDialogService.h"
|
#include "IFileDialogService.h"
|
||||||
|
|
||||||
@@ -37,12 +38,14 @@ public:
|
|||||||
void setReadingModel(ReadingModel *model);
|
void setReadingModel(ReadingModel *model);
|
||||||
void setAppState(AppState *state);
|
void setAppState(AppState *state);
|
||||||
void setFileDialogService(IFileDialogService *service);
|
void setFileDialogService(IFileDialogService *service);
|
||||||
|
void setImageProvider(EpubImageProvider *provider);
|
||||||
|
|
||||||
Q_INVOKABLE void openBook(const QUrl &fileUrl);
|
Q_INVOKABLE void openBook(const QUrl &fileUrl);
|
||||||
Q_INVOKABLE void openBookFromPath(const QString &filePath);
|
Q_INVOKABLE void openBookFromPath(const QString &filePath);
|
||||||
Q_INVOKABLE void browseAndOpen();
|
Q_INVOKABLE void browseAndOpen();
|
||||||
Q_INVOKABLE void closeBook();
|
Q_INVOKABLE void closeBook();
|
||||||
Q_INVOKABLE void clearError();
|
Q_INVOKABLE void clearError();
|
||||||
|
Q_INVOKABLE void setContentWidth(int width);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void bookReady();
|
void bookReady();
|
||||||
@@ -56,6 +59,7 @@ private:
|
|||||||
ReadingModel *m_readingModel = nullptr;
|
ReadingModel *m_readingModel = nullptr;
|
||||||
AppState *m_appState = nullptr;
|
AppState *m_appState = nullptr;
|
||||||
IFileDialogService *m_fileDialogService = nullptr;
|
IFileDialogService *m_fileDialogService = nullptr;
|
||||||
|
EpubImageProvider *m_imageProvider = nullptr;
|
||||||
QString m_bookTitle;
|
QString m_bookTitle;
|
||||||
QString m_bookAuthor;
|
QString m_bookAuthor;
|
||||||
QString m_currentFilePath;
|
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 "EpubReader.h"
|
||||||
|
|
||||||
|
#include <QDir>
|
||||||
#include <QDomDocument>
|
#include <QDomDocument>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
|
#include <QUrl>
|
||||||
#include <QXmlStreamReader>
|
#include <QXmlStreamReader>
|
||||||
|
|
||||||
#include <minizip/unzip.h>
|
#include "backend/ZipUtils.h"
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
using ZipUtils::readZipEntry;
|
||||||
// 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{};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize a QDomNode to a string (no XML declaration, no extra indentation).
|
// Serialize a QDomNode to a string (no XML declaration, no extra indentation).
|
||||||
static QString serializeNode(const QDomNode &node)
|
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
|
// 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())
|
if (data.isEmpty())
|
||||||
return {};
|
return {};
|
||||||
@@ -262,6 +243,22 @@ QStringList EpubReader::chunkXhtml(const QByteArray &data)
|
|||||||
return {QString::fromUtf8(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)
|
// Find <body> element (may have namespace prefix in some EPUBs)
|
||||||
QDomElement body;
|
QDomElement body;
|
||||||
const QDomNodeList bodies = doc.elementsByTagName(QStringLiteral("body"));
|
const QDomNodeList bodies = doc.elementsByTagName(QStringLiteral("body"));
|
||||||
@@ -329,5 +326,5 @@ Chapter EpubReader::loadChapter(int index)
|
|||||||
if (chTitle.isEmpty() && index < m_spineHrefs.size())
|
if (chTitle.isEmpty() && index < m_spineHrefs.size())
|
||||||
chTitle = QFileInfo(m_spineHrefs[index]).completeBaseName();
|
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 parseContainer(const QByteArray &data, QString &opfPath);
|
||||||
bool parseOpf(const QByteArray &data, QString &ncxHref);
|
bool parseOpf(const QByteArray &data, QString &ncxHref);
|
||||||
void parseNcx(const QByteArray &data);
|
void parseNcx(const QByteArray &data);
|
||||||
static QStringList chunkXhtml(const QByteArray &data);
|
static QStringList chunkXhtml(const QByteArray &data, const QString &xhtmlHref);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // EPUBREADER_H
|
#endif // EPUBREADER_H
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
#include <QSGRendererInterface>
|
#include <QSGRendererInterface>
|
||||||
|
|
||||||
#include "backend/BookController.h"
|
#include "backend/BookController.h"
|
||||||
|
#include "backend/EpubImageProvider.h"
|
||||||
#include "backend/ReadingModel.h"
|
#include "backend/ReadingModel.h"
|
||||||
#include "backend/AppState.h"
|
#include "backend/AppState.h"
|
||||||
#include "backend/NativeFileDialogService.h"
|
#include "backend/NativeFileDialogService.h"
|
||||||
@@ -106,6 +107,10 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
QQmlApplicationEngine engine;
|
QQmlApplicationEngine engine;
|
||||||
|
|
||||||
|
auto *epubImageProvider = new EpubImageProvider;
|
||||||
|
engine.addImageProvider(QStringLiteral("epub"), epubImageProvider);
|
||||||
|
bookController.setImageProvider(epubImageProvider);
|
||||||
|
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("bookController"), &bookController);
|
engine.rootContext()->setContextProperty(QStringLiteral("bookController"), &bookController);
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("appState"), &appState);
|
engine.rootContext()->setContextProperty(QStringLiteral("appState"), &appState);
|
||||||
|
|||||||
@@ -13,6 +13,13 @@ Rectangle {
|
|||||||
GradientStop { position: 1.0; color: Theme.gradientBottom }
|
GradientStop { position: 1.0; color: Theme.gradientBottom }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateContentWidth() {
|
||||||
|
bookController.setContentWidth(chunkListView.width - 18 - 2 * appState.horizontalMargin)
|
||||||
|
}
|
||||||
|
|
||||||
|
Component.onCompleted: updateContentWidth()
|
||||||
|
onWidthChanged: updateContentWidth()
|
||||||
|
|
||||||
ListView {
|
ListView {
|
||||||
id: chunkListView
|
id: chunkListView
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
|
|||||||
@@ -0,0 +1,235 @@
|
|||||||
|
#include <QtTest/QtTest>
|
||||||
|
#include <QBuffer>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QImage>
|
||||||
|
#include <QSignalSpy>
|
||||||
|
#include <QSize>
|
||||||
|
#include <QTemporaryFile>
|
||||||
|
#include <QThreadPool>
|
||||||
|
|
||||||
|
#include <minizip/zip.h>
|
||||||
|
|
||||||
|
#include "backend/EpubImageProvider.h"
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Helpers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static bool addZipEntry(zipFile zf, const char *name, const QByteArray &data)
|
||||||
|
{
|
||||||
|
zip_fileinfo fi{};
|
||||||
|
if (zipOpenNewFileInZip(zf, name, &fi,
|
||||||
|
nullptr, 0, nullptr, 0, nullptr,
|
||||||
|
Z_DEFLATED, Z_DEFAULT_COMPRESSION) != ZIP_OK)
|
||||||
|
return false;
|
||||||
|
if (zipWriteInFileInZip(zf, data.constData(), (unsigned)data.size()) != ZIP_OK) {
|
||||||
|
zipCloseFileInZip(zf);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return zipCloseFileInZip(zf) == ZIP_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a QByteArray containing a valid PNG for an image of given size and color.
|
||||||
|
static QByteArray makePng(int w, int h, QRgb color = qRgb(255, 0, 0))
|
||||||
|
{
|
||||||
|
QImage img(w, h, QImage::Format_RGB32);
|
||||||
|
img.fill(color);
|
||||||
|
QByteArray data;
|
||||||
|
QBuffer buf(&data);
|
||||||
|
buf.open(QIODevice::WriteOnly);
|
||||||
|
img.save(&buf, "PNG");
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build a minimal EPUB-like ZIP containing a single image entry.
|
||||||
|
// Returns the path of the temp file (caller removes).
|
||||||
|
static QString buildEpubWithPng(const char *entryPath, int imgW, int imgH)
|
||||||
|
{
|
||||||
|
QTemporaryFile tmp;
|
||||||
|
tmp.setFileTemplate(QDir::tempPath() + "/provider_test_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 {};
|
||||||
|
|
||||||
|
const bool ok = addZipEntry(zf, entryPath, makePng(imgW, imgH));
|
||||||
|
zipClose(zf, nullptr);
|
||||||
|
return ok ? path : QString{};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Test class
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class EpubImageProviderTest : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void requestImageReturnsValidImage()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
epubPath, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(!img.isNull());
|
||||||
|
QCOMPARE(img.width(), 4);
|
||||||
|
QCOMPARE(img.height(), 4);
|
||||||
|
QCOMPARE(img.size(), QSize(4, 4));
|
||||||
|
}
|
||||||
|
|
||||||
|
void requestImageReturnsNullForMissingEntry()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
epubPath, QStringLiteral("OEBPS/Images/nonexistent.png"), 0, QSize());
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(img.isNull());
|
||||||
|
}
|
||||||
|
|
||||||
|
void requestImageReturnsNullWhenNoPath()
|
||||||
|
{
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
QString{}, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||||
|
QVERIFY(img.isNull());
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearResetsState()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("OEBPS/Images/test.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
// After clear the path is empty, so loadImageFromEpub with empty path returns null
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
QString{}, QStringLiteral("OEBPS/Images/test.png"), 0, QSize());
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(img.isNull());
|
||||||
|
}
|
||||||
|
|
||||||
|
void largeImageScaledToDisplayWidth()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 600, 400);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
epubPath, QStringLiteral("img.png"), 300, QSize());
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(!img.isNull());
|
||||||
|
QCOMPARE(img.width(), 300);
|
||||||
|
QCOMPARE(img.height(), 200);
|
||||||
|
QCOMPARE(img.size(), QSize(300, 200));
|
||||||
|
}
|
||||||
|
|
||||||
|
void smallImageNotEnlarged()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 100, 100);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
epubPath, QStringLiteral("img.png"), 300, QSize());
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(!img.isNull());
|
||||||
|
QCOMPARE(img.width(), 100);
|
||||||
|
QCOMPARE(img.height(), 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
void requestedSizeHonored()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 400, 300);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
const QImage img = EpubImageProvider::loadImageFromEpub(
|
||||||
|
epubPath, QStringLiteral("img.png"), 600, QSize(200, -1));
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(!img.isNull());
|
||||||
|
QCOMPARE(img.width(), 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
// Async response tests
|
||||||
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
|
void asyncResponseEmitsFinished()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
EpubImageResponse response(epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||||
|
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||||
|
response.run();
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
QVERIFY(response.textureFactory() != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancelledResponseDoesNotEmitFinished()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
EpubImageResponse response(epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||||
|
QSignalSpy spy(&response, &QQuickImageResponse::finished);
|
||||||
|
response.cancel();
|
||||||
|
response.run();
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QCOMPARE(spy.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void cancelledTextureFactoryIsNull()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
EpubImageResponse response(epubPath, QStringLiteral("img.png"), 0, QSize());
|
||||||
|
response.cancel();
|
||||||
|
response.run();
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(response.textureFactory() == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void responseAfterClearIsNull()
|
||||||
|
{
|
||||||
|
const QString epubPath = buildEpubWithPng("img.png", 4, 4);
|
||||||
|
QVERIFY(!epubPath.isEmpty());
|
||||||
|
|
||||||
|
EpubImageProvider provider;
|
||||||
|
provider.setEpubPath(epubPath);
|
||||||
|
provider.clear();
|
||||||
|
|
||||||
|
QQuickImageResponse *resp = provider.requestImageResponse(
|
||||||
|
QStringLiteral("img.png"), QSize());
|
||||||
|
QThreadPool::globalInstance()->waitForDone();
|
||||||
|
|
||||||
|
QFile::remove(epubPath);
|
||||||
|
|
||||||
|
QVERIFY(resp->textureFactory() == nullptr);
|
||||||
|
delete resp;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(EpubImageProviderTest)
|
||||||
|
#include "EpubImageProviderTest.moc"
|
||||||
@@ -118,6 +118,106 @@ static QString buildSampleEpub()
|
|||||||
return ok ? path : QString{};
|
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.
|
// Build an EPUB without an NCX.
|
||||||
static QString buildEpubNoNcx()
|
static QString buildEpubNoNcx()
|
||||||
{
|
{
|
||||||
@@ -216,16 +316,22 @@ private:
|
|||||||
QString m_epubPath;
|
QString m_epubPath;
|
||||||
QString m_noNcxPath;
|
QString m_noNcxPath;
|
||||||
QString m_largePath;
|
QString m_largePath;
|
||||||
|
QString m_imgPath;
|
||||||
|
QString m_relImgPath;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void initTestCase()
|
void initTestCase()
|
||||||
{
|
{
|
||||||
m_epubPath = buildSampleEpub();
|
m_epubPath = buildSampleEpub();
|
||||||
m_noNcxPath = buildEpubNoNcx();
|
m_noNcxPath = buildEpubNoNcx();
|
||||||
m_largePath = buildEpubLargeChapter();
|
m_largePath = buildEpubLargeChapter();
|
||||||
QVERIFY2(!m_epubPath.isEmpty(), "Failed to build sample EPUB fixture");
|
m_imgPath = buildEpubWithImage();
|
||||||
QVERIFY2(!m_noNcxPath.isEmpty(), "Failed to build no-NCX EPUB fixture");
|
m_relImgPath = buildEpubWithRelativeImage();
|
||||||
QVERIFY2(!m_largePath.isEmpty(), "Failed to build large chapter EPUB fixture");
|
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()
|
void cleanupTestCase()
|
||||||
@@ -233,6 +339,8 @@ private slots:
|
|||||||
QFile::remove(m_epubPath);
|
QFile::remove(m_epubPath);
|
||||||
QFile::remove(m_noNcxPath);
|
QFile::remove(m_noNcxPath);
|
||||||
QFile::remove(m_largePath);
|
QFile::remove(m_largePath);
|
||||||
|
QFile::remove(m_imgPath);
|
||||||
|
QFile::remove(m_relImgPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Phase 2: error handling ---
|
// --- Phase 2: error handling ---
|
||||||
@@ -361,6 +469,30 @@ private slots:
|
|||||||
QVERIFY2(hasBlock, "Chunk lacks any block element");
|
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)
|
QTEST_MAIN(EpubReaderTest)
|
||||||
|
|||||||
Reference in new issue
Block a user