FB2 fixes

This commit is contained in:
Agel committed 2026-04-17 22:33:36 +02:00
1 parent 7fdd56a7f5
commit 232d86716d
4 files changed
+28 -7

No files matched your search

+7 -1
View File
@@ -1,4 +1,5 @@
#include "Fb2ImageProvider.h" #include "Fb2ImageProvider.h"
#include "Fb2Reader.h"
#include <QFile> #include <QFile>
#include <QImage> #include <QImage>
@@ -60,8 +61,13 @@ QImage Fb2ImageProvider::loadImageFromFb2(const QString &fb2Path, const QString
QFile f(fb2Path); QFile f(fb2Path);
if (!f.open(QIODevice::ReadOnly)) if (!f.open(QIODevice::ReadOnly))
return {}; return {};
const QByteArray raw = f.readAll();
f.close();
QXmlStreamReader xml(&f); // Construct from decoded QString so the reader operates on pure Unicode,
// bypassing QXmlStreamReader's own encoding detection which may fail for
// Windows-1251 and other encodings not natively supported by Qt 6.
QXmlStreamReader xml(Fb2Reader::decodeRawBytes(raw));
while (!xml.atEnd()) { while (!xml.atEnd()) {
xml.readNext(); xml.readNext();
if (!xml.isStartElement() || xml.name() != QLatin1String("binary")) if (!xml.isStartElement() || xml.name() != QLatin1String("binary"))
+15 -5
View File
@@ -5,6 +5,7 @@
#include <QFileInfo> #include <QFileInfo>
#include <QStringConverter> #include <QStringConverter>
#include <QTextStream> #include <QTextStream>
#include <QUrl>
#include <uchardet/uchardet.h> #include <uchardet/uchardet.h>
#include <iconv.h> #include <iconv.h>
@@ -34,7 +35,7 @@ static QString decodeWithIconv(const QByteArray &raw, const char *charset)
// Uses the XML encoding declaration as the authoritative charset source // Uses the XML encoding declaration as the authoritative charset source
// (uchardet is unreliable on files with large binary sections or sparse text). // (uchardet is unreliable on files with large binary sections or sparse text).
// QStringConverter / iconv handle the actual conversion. // QStringConverter / iconv handle the actual conversion.
static QString decodeRawBytes(const QByteArray &raw) QString Fb2Reader::decodeRawBytes(const QByteArray &raw)
{ {
// Extract encoding name from XML declaration (first ~200 bytes) // Extract encoding name from XML declaration (first ~200 bytes)
QByteArray enc; QByteArray enc;
@@ -99,7 +100,8 @@ static QDomElement firstChildByTag(const QDomElement &parent, const QString &tag
} }
// Converts a single FB2 DOM node to an HTML string. Handles inline and block elements. // Converts a single FB2 DOM node to an HTML string. Handles inline and block elements.
static QString nodeToHtml(const QDomNode &node) // fb2UrlSuffix is appended to image URLs, e.g. "?fb2=<encoded_path>" for self-contained loading.
static QString nodeToHtml(const QDomNode &node, const QString &fb2UrlSuffix = {})
{ {
if (node.isText()) if (node.isText())
return node.nodeValue().toHtmlEscaped(); return node.nodeValue().toHtmlEscaped();
@@ -113,7 +115,7 @@ static QString nodeToHtml(const QDomNode &node)
// Build inner HTML from children // Build inner HTML from children
QString inner; QString inner;
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling()) for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling())
inner += nodeToHtml(child); inner += nodeToHtml(child, fb2UrlSuffix);
if (tag == QLatin1String("p") || tag == QLatin1String("v")) { if (tag == QLatin1String("p") || tag == QLatin1String("v")) {
if (inner.trimmed().isEmpty()) if (inner.trimmed().isEmpty())
@@ -134,10 +136,12 @@ static QString nodeToHtml(const QDomNode &node)
return inner; return inner;
if (tag == QLatin1String("image")) { if (tag == QLatin1String("image")) {
QString href = el.attribute(QStringLiteral("l:href")); QString href = el.attribute(QStringLiteral("l:href"));
if (href.isEmpty())
href = el.attribute(QStringLiteral("xlink:href"));
if (href.startsWith(QLatin1Char('#'))) if (href.startsWith(QLatin1Char('#')))
href = href.mid(1); href = href.mid(1);
if (!href.isEmpty()) if (!href.isEmpty())
return QStringLiteral("<img src=\"image://fb2/") + href + QStringLiteral("\"/>\n"); return QStringLiteral("<img src=\"image://fb2/") + href + fb2UrlSuffix + QStringLiteral("\"/>\n");
return {}; return {};
} }
if (tag == QLatin1String("a")) { if (tag == QLatin1String("a")) {
@@ -183,6 +187,8 @@ static Fb2Metadata parseDescription(const QDomElement &desc)
const QDomElement img = firstChildByTag(coverpage, QStringLiteral("image")); const QDomElement img = firstChildByTag(coverpage, QStringLiteral("image"));
if (!img.isNull()) { if (!img.isNull()) {
QString href = img.attribute(QStringLiteral("l:href")); QString href = img.attribute(QStringLiteral("l:href"));
if (href.isEmpty())
href = img.attribute(QStringLiteral("xlink:href"));
if (href.startsWith(QLatin1Char('#'))) if (href.startsWith(QLatin1Char('#')))
href = href.mid(1); href = href.mid(1);
meta.coverBinaryId = href; meta.coverBinaryId = href;
@@ -240,6 +246,10 @@ bool Fb2Reader::open(const QString &filePath)
if (m_title.isEmpty()) if (m_title.isEmpty())
m_title = QFileInfo(filePath).completeBaseName(); m_title = QFileInfo(filePath).completeBaseName();
// Pre-compute URL suffix so body image URLs embed the file path (self-contained, encoding-safe).
const QString urlSuffix = QStringLiteral("?fb2=")
+ QString::fromUtf8(QUrl::toPercentEncoding(filePath));
// Body sections // Body sections
for (QDomNode bodyNode = root.firstChild(); !bodyNode.isNull(); bodyNode = bodyNode.nextSibling()) { for (QDomNode bodyNode = root.firstChild(); !bodyNode.isNull(); bodyNode = bodyNode.nextSibling()) {
const QDomElement body = bodyNode.toElement(); const QDomElement body = bodyNode.toElement();
@@ -264,7 +274,7 @@ bool Fb2Reader::open(const QString &filePath)
// Convert section children to HTML node list // Convert section children to HTML node list
QStringList htmlNodes; QStringList htmlNodes;
for (QDomNode child = section.firstChild(); !child.isNull(); child = child.nextSibling()) { for (QDomNode child = section.firstChild(); !child.isNull(); child = child.nextSibling()) {
const QString html = nodeToHtml(child); const QString html = nodeToHtml(child, urlSuffix);
if (!html.isEmpty()) if (!html.isEmpty())
htmlNodes.append(html); htmlNodes.append(html);
} }
+2
View File
@@ -7,6 +7,8 @@
class Fb2Reader : public FormatReader { class Fb2Reader : public FormatReader {
public: public:
static QString decodeRawBytes(const QByteArray &raw);
bool open(const QString &filePath) override; bool open(const QString &filePath) override;
bool loadMetadata(const QString &filePath) override; bool loadMetadata(const QString &filePath) override;
QString lastError() override; QString lastError() override;
+4 -1
View File
@@ -169,8 +169,9 @@ int main(int argc, char *argv[])
readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing()); readingModel.setFontParams(readingPreferences.fontSize(), readingPreferences.lineSpacing());
}); });
// Track the current image provider so setContentWidth can reach it // Track the current image provider and content width so both stay in sync.
IImageProvider *currentImageProvider = nullptr; IImageProvider *currentImageProvider = nullptr;
int currentContentWidth = 0;
// Before a new book open: snapshot the outgoing book's progress into the library. // Before a new book open: snapshot the outgoing book's progress into the library.
// Scroll position is handled by PageView's scroll tracker via book.saveCurrentPosition(). // Scroll position is handled by PageView's scroll tracker via book.saveCurrentPosition().
@@ -201,6 +202,7 @@ int main(int argc, char *argv[])
engine.removeImageProvider(provider->scheme()); engine.removeImageProvider(provider->scheme());
engine.addImageProvider(provider->scheme(), provider); engine.addImageProvider(provider->scheme(), provider);
provider->setFilePath(filePath); provider->setFilePath(filePath);
provider->setDisplayWidth(currentContentWidth);
currentImageProvider = provider; currentImageProvider = provider;
if (!reader->coverImagePath().isEmpty()) if (!reader->coverImagePath().isEmpty())
@@ -220,6 +222,7 @@ int main(int argc, char *argv[])
// Propagate content-width changes to image provider + reading model. // Propagate content-width changes to image provider + reading model.
// Called from Book::setContentWidth which is triggered by QML's PageView. // Called from Book::setContentWidth which is triggered by QML's PageView.
QObject::connect(&book, &Book::contentWidthChanged, [&](int width){ QObject::connect(&book, &Book::contentWidthChanged, [&](int width){
currentContentWidth = width;
if (currentImageProvider) if (currentImageProvider)
currentImageProvider->setDisplayWidth(width); currentImageProvider->setDisplayWidth(width);
readingModel.setContentWidth(width); readingModel.setContentWidth(width);