Reflow deterministico: stile iniettato nell'HTML del capitolo (chapterHtml+loadHtml), niente JS post-load; runJavaScript solo per lo scroll

This commit is contained in:
2026-08-12 16:16:21 +02:00
parent 52d09911f2
commit 4fbc6185a7
3 changed files with 65 additions and 26 deletions
+33
View File
@@ -2,8 +2,10 @@
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QHash>
#include <QStringList>
#include <QUrl>
#include <QXmlStreamReader>
#include <zlib.h>
@@ -404,6 +406,37 @@ QVariantList EpubBook::chapterList() const
return list;
}
QString EpubBook::chapterHtml(int index, int fontSizePx) const
{
if (index < 0 || index >= m_chapters.size())
return QString();
QFile f(m_chapters.at(index).path);
if (!f.open(QIODevice::ReadOnly))
return QString();
QString html = QString::fromUtf8(f.readAll());
// stile di reflow iniettato nel documento: !important vince sui CSS del libro
const QString style = QStringLiteral(
"<style id=\"cw-font\">html,body{font-size:%1px!important}"
"p,div,li,td,blockquote,section{font-size:1em!important}</style>")
.arg(fontSizePx);
const int headEnd = html.indexOf(QStringLiteral("</head>"), 0, Qt::CaseInsensitive);
if (headEnd >= 0)
html.insert(headEnd, style);
else
html.prepend(style);
return html;
}
QString EpubBook::chapterBaseUrl(int index) const
{
if (index < 0 || index >= m_chapters.size())
return QString();
return QUrl::fromLocalFile(
QFileInfo(m_chapters.at(index).path).absolutePath() + QStringLiteral("/"))
.toString();
}
QString EpubBook::makeBookmarkKey(int chapterIndex, double fraction) const
{
return QStringLiteral("cw:v1:%1:%2").arg(chapterIndex).arg(fraction, 0, 'f', 4);
+4
View File
@@ -36,6 +36,10 @@ public:
Q_INVOKABLE QString chapterTitle(int index) const;
// elenco [{index, title}] per la lista capitoli
Q_INVOKABLE QVariantList chapterList() const;
// HTML del capitolo con lo stile di reflow iniettato (font-size in px)
Q_INVOKABLE QString chapterHtml(int index, int fontSizePx) const;
// base URL (file://) per risolvere immagini/CSS relativi del capitolo
Q_INVOKABLE QString chapterBaseUrl(int index) const;
// chiave segno locale, es. "cw:v1:3:0.42" (capitolo 3, 42% del capitolo)
Q_INVOKABLE QString makeBookmarkKey(int chapterIndex, double fraction) const;