From 232d86716d4c10663b3d35a242d737431ea2a9e4 Mon Sep 17 00:00:00 2001 From: Agel Date: Fri, 17 Apr 2026 22:33:36 +0200 Subject: [PATCH] FB2 fixes --- src/backend/formats/Fb2ImageProvider.cpp | 8 +++++++- src/backend/formats/Fb2Reader.cpp | 20 +++++++++++++++----- src/backend/formats/Fb2Reader.h | 2 ++ src/main.cpp | 5 ++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/backend/formats/Fb2ImageProvider.cpp b/src/backend/formats/Fb2ImageProvider.cpp index 66e5e91..3a04865 100644 --- a/src/backend/formats/Fb2ImageProvider.cpp +++ b/src/backend/formats/Fb2ImageProvider.cpp @@ -1,4 +1,5 @@ #include "Fb2ImageProvider.h" +#include "Fb2Reader.h" #include #include @@ -60,8 +61,13 @@ QImage Fb2ImageProvider::loadImageFromFb2(const QString &fb2Path, const QString QFile f(fb2Path); if (!f.open(QIODevice::ReadOnly)) 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()) { xml.readNext(); if (!xml.isStartElement() || xml.name() != QLatin1String("binary")) diff --git a/src/backend/formats/Fb2Reader.cpp b/src/backend/formats/Fb2Reader.cpp index 4595ff9..a41059b 100644 --- a/src/backend/formats/Fb2Reader.cpp +++ b/src/backend/formats/Fb2Reader.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -34,7 +35,7 @@ static QString decodeWithIconv(const QByteArray &raw, const char *charset) // Uses the XML encoding declaration as the authoritative charset source // (uchardet is unreliable on files with large binary sections or sparse text). // 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) 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. -static QString nodeToHtml(const QDomNode &node) +// fb2UrlSuffix is appended to image URLs, e.g. "?fb2=" for self-contained loading. +static QString nodeToHtml(const QDomNode &node, const QString &fb2UrlSuffix = {}) { if (node.isText()) return node.nodeValue().toHtmlEscaped(); @@ -113,7 +115,7 @@ static QString nodeToHtml(const QDomNode &node) // Build inner HTML from children QString inner; 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 (inner.trimmed().isEmpty()) @@ -134,10 +136,12 @@ static QString nodeToHtml(const QDomNode &node) return inner; if (tag == QLatin1String("image")) { QString href = el.attribute(QStringLiteral("l:href")); + if (href.isEmpty()) + href = el.attribute(QStringLiteral("xlink:href")); if (href.startsWith(QLatin1Char('#'))) href = href.mid(1); if (!href.isEmpty()) - return QStringLiteral("\n"); + return QStringLiteral("\n"); return {}; } if (tag == QLatin1String("a")) { @@ -183,6 +187,8 @@ static Fb2Metadata parseDescription(const QDomElement &desc) const QDomElement img = firstChildByTag(coverpage, QStringLiteral("image")); if (!img.isNull()) { QString href = img.attribute(QStringLiteral("l:href")); + if (href.isEmpty()) + href = img.attribute(QStringLiteral("xlink:href")); if (href.startsWith(QLatin1Char('#'))) href = href.mid(1); meta.coverBinaryId = href; @@ -240,6 +246,10 @@ bool Fb2Reader::open(const QString &filePath) if (m_title.isEmpty()) 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 for (QDomNode bodyNode = root.firstChild(); !bodyNode.isNull(); bodyNode = bodyNode.nextSibling()) { const QDomElement body = bodyNode.toElement(); @@ -264,7 +274,7 @@ bool Fb2Reader::open(const QString &filePath) // Convert section children to HTML node list QStringList htmlNodes; for (QDomNode child = section.firstChild(); !child.isNull(); child = child.nextSibling()) { - const QString html = nodeToHtml(child); + const QString html = nodeToHtml(child, urlSuffix); if (!html.isEmpty()) htmlNodes.append(html); } diff --git a/src/backend/formats/Fb2Reader.h b/src/backend/formats/Fb2Reader.h index 6351f01..c0858ae 100644 --- a/src/backend/formats/Fb2Reader.h +++ b/src/backend/formats/Fb2Reader.h @@ -7,6 +7,8 @@ class Fb2Reader : public FormatReader { public: + static QString decodeRawBytes(const QByteArray &raw); + bool open(const QString &filePath) override; bool loadMetadata(const QString &filePath) override; QString lastError() override; diff --git a/src/main.cpp b/src/main.cpp index a67f9b4..7dceae5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -169,8 +169,9 @@ int main(int argc, char *argv[]) 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; + int currentContentWidth = 0; // 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(). @@ -201,6 +202,7 @@ int main(int argc, char *argv[]) engine.removeImageProvider(provider->scheme()); engine.addImageProvider(provider->scheme(), provider); provider->setFilePath(filePath); + provider->setDisplayWidth(currentContentWidth); currentImageProvider = provider; if (!reader->coverImagePath().isEmpty()) @@ -220,6 +222,7 @@ int main(int argc, char *argv[]) // Propagate content-width changes to image provider + reading model. // Called from Book::setContentWidth which is triggered by QML's PageView. QObject::connect(&book, &Book::contentWidthChanged, [&](int width){ + currentContentWidth = width; if (currentImageProvider) currentImageProvider->setDisplayWidth(width); readingModel.setContentWidth(width);