FB2 fixes
This commit is contained in:
4 files changed
+28
-7
No files matched your search
@@ -1,4 +1,5 @@
|
||||
#include "Fb2ImageProvider.h"
|
||||
#include "Fb2Reader.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QImage>
|
||||
@@ -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"))
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <QFileInfo>
|
||||
#include <QStringConverter>
|
||||
#include <QTextStream>
|
||||
#include <QUrl>
|
||||
|
||||
#include <uchardet/uchardet.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
|
||||
// (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=<encoded_path>" 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("<img src=\"image://fb2/") + href + QStringLiteral("\"/>\n");
|
||||
return QStringLiteral("<img src=\"image://fb2/") + href + fb2UrlSuffix + 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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user