reintroduced epub support

This commit is contained in:
Agel committed 2026-10-25 19:59:23 +01:00
1 parent 49a959a89d
commit d14170a68f
49 files changed
+5048 -205

No files matched your search

@@ -0,0 +1,502 @@
#include "HtmlBlockParser.h"
#include <QDomDocument>
#include <QRegularExpression>
#include <algorithm>
#include "CssParser.h"
#include "backend/formats/HtmlEntities.h"
namespace HtmlBlockParser {
namespace {
struct StyleContext {
TextStyle text;
TextAlign align = TextAlign::Justify;
qreal textIndent = 0;
qreal lineHeight = 0;
bool preformatted = false;
};
QString localName(const QDomElement &e)
{
QString name = e.tagName();
const int idx = name.indexOf(QLatin1Char(':'));
if (idx >= 0)
name = name.mid(idx + 1);
return name.toLower();
}
bool isBlockTag(const QString &tag)
{
static const QStringList kBlocks = {
QStringLiteral("p"), QStringLiteral("div"),
QStringLiteral("h1"), QStringLiteral("h2"),
QStringLiteral("h3"), QStringLiteral("h4"),
QStringLiteral("h5"), QStringLiteral("h6"),
QStringLiteral("blockquote"), QStringLiteral("pre"),
QStringLiteral("ul"), QStringLiteral("ol"),
QStringLiteral("li"), QStringLiteral("dl"),
QStringLiteral("dt"), QStringLiteral("dd"),
QStringLiteral("hr"), QStringLiteral("figure"),
QStringLiteral("svg"), QStringLiteral("figcaption"),
QStringLiteral("section"), QStringLiteral("article"),
QStringLiteral("header"), QStringLiteral("footer"), QStringLiteral("aside"),
QStringLiteral("body")
};
return kBlocks.contains(tag);
}
void applyCssDeclarations(const QVector<Css::Declaration> &decls,
StyleContext &ctx,
BoxStyle &box,
const ParseParams &params)
{
Css::LengthContext lenCtx;
lenCtx.em = ctx.text.fontSize;
lenCtx.rem = params.baseFontSize;
lenCtx.percentBase = params.contentWidth;
lenCtx.absoluteScale = params.baseFontSize / 16.0;
for (const auto &d : decls) {
const QString &prop = d.property;
const QString &val = d.value;
if (prop == QLatin1String("font-family")) {
ctx.text.fontFamilies = Css::splitCommaList(val);
} else if (prop == QLatin1String("font-size")) {
if (val == QLatin1String("xx-small")) ctx.text.fontSize = params.baseFontSize * 0.6;
else if (val == QLatin1String("x-small")) ctx.text.fontSize = params.baseFontSize * 0.75;
else if (val == QLatin1String("small")) ctx.text.fontSize = params.baseFontSize * 0.875;
else if (val == QLatin1String("medium")) ctx.text.fontSize = params.baseFontSize;
else if (val == QLatin1String("large")) ctx.text.fontSize = params.baseFontSize * 1.15;
else if (val == QLatin1String("x-large")) ctx.text.fontSize = params.baseFontSize * 1.5;
else if (val == QLatin1String("xx-large")) ctx.text.fontSize = params.baseFontSize * 2.0;
else if (val == QLatin1String("smaller")) ctx.text.fontSize *= 0.85;
else if (val == QLatin1String("larger")) ctx.text.fontSize *= 1.2;
else {
auto sz = Css::parseLength(val, lenCtx);
if (sz && *sz > 0)
ctx.text.fontSize = *sz;
}
lenCtx.em = ctx.text.fontSize;
} else if (prop == QLatin1String("font-weight")) {
if (val == QLatin1String("bold") || val == QLatin1String("bolder")) ctx.text.weight = 700;
else if (val == QLatin1String("normal") || val == QLatin1String("lighter")) ctx.text.weight = 400;
else {
bool ok;
int w = val.toInt(&ok);
if (ok && w >= 100 && w <= 900)
ctx.text.weight = w;
}
} else if (prop == QLatin1String("font-style")) {
ctx.text.italic = (val == QLatin1String("italic") || val == QLatin1String("oblique"));
} else if (prop == QLatin1String("font-variant")) {
ctx.text.smallCaps = (val == QLatin1String("small-caps"));
} else if (prop == QLatin1String("text-transform")) {
if (val == QLatin1String("uppercase")) ctx.text.transform = TextTransform::Uppercase;
else if (val == QLatin1String("lowercase")) ctx.text.transform = TextTransform::Lowercase;
else if (val == QLatin1String("capitalize")) ctx.text.transform = TextTransform::Capitalize;
else ctx.text.transform = TextTransform::None;
} else if (prop == QLatin1String("letter-spacing")) {
auto ls = Css::parseLength(val, lenCtx);
if (ls) ctx.text.letterSpacing = *ls;
} else if (prop == QLatin1String("text-decoration") || prop == QLatin1String("text-decoration-line")) {
ctx.text.underline = val.contains(QLatin1String("underline"));
ctx.text.strikeOut = val.contains(QLatin1String("line-through"));
ctx.text.overline = val.contains(QLatin1String("overline"));
} else if (prop == QLatin1String("color")) {
QColor c = Css::parseColor(val);
if (c.isValid()) ctx.text.color = c;
} else if (prop == QLatin1String("background-color") || prop == QLatin1String("background")) {
QColor c = Css::parseColor(val);
if (c.isValid()) ctx.text.background = c;
} else if (prop == QLatin1String("text-align")) {
if (val == QLatin1String("left") || val == QLatin1String("start")) ctx.align = TextAlign::Left;
else if (val == QLatin1String("center")) ctx.align = TextAlign::Center;
else if (val == QLatin1String("right") || val == QLatin1String("end")) ctx.align = TextAlign::Right;
else if (val == QLatin1String("justify")) ctx.align = TextAlign::Justify;
} else if (prop == QLatin1String("text-indent")) {
auto ind = Css::parseLength(val, lenCtx);
if (ind) ctx.textIndent = *ind;
} else if (prop == QLatin1String("line-height")) {
bool ok;
qreal lh = val.toDouble(&ok);
if (ok && lh > 0) {
ctx.lineHeight = lh;
} else {
auto len = Css::parseLength(val, lenCtx);
if (len && *len > 0 && ctx.text.fontSize > 0)
ctx.lineHeight = *len / ctx.text.fontSize;
}
} else if (prop == QLatin1String("margin-top")) {
auto m = Css::parseLength(val, lenCtx); if (m) box.marginTop = *m;
} else if (prop == QLatin1String("margin-bottom")) {
auto m = Css::parseLength(val, lenCtx); if (m) box.marginBottom = *m;
} else if (prop == QLatin1String("margin-left")) {
auto m = Css::parseLength(val, lenCtx); if (m) box.marginLeft = *m;
} else if (prop == QLatin1String("margin-right")) {
auto m = Css::parseLength(val, lenCtx); if (m) box.marginRight = *m;
} else if (prop == QLatin1String("padding-top")) {
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingTop = *p;
} else if (prop == QLatin1String("padding-bottom")) {
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingBottom = *p;
} else if (prop == QLatin1String("padding-left")) {
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingLeft = *p;
} else if (prop == QLatin1String("padding-right")) {
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingRight = *p;
} else if (prop == QLatin1String("border-left") || prop == QLatin1String("border-left-width")) {
auto w = Css::parseLength(val, lenCtx); if (w) box.borderLeft = *w;
} else if (prop == QLatin1String("border-top") || prop == QLatin1String("border-top-width")) {
auto w = Css::parseLength(val, lenCtx); if (w) box.borderTop = *w;
} else if (prop == QLatin1String("border-bottom") || prop == QLatin1String("border-bottom-width")) {
auto w = Css::parseLength(val, lenCtx); if (w) box.borderBottom = *w;
} else if (prop == QLatin1String("border-color") || prop == QLatin1String("border-left-color")) {
QColor c = Css::parseColor(val);
if (c.isValid()) box.borderColor = c;
}
}
}
class Builder {
public:
Builder(const ParseParams &params) : m_params(params)
{
StyleContext root;
root.text.fontSize = params.baseFontSize;
m_stack.append(root);
}
QVector<BlockItem> build(const QDomElement &root)
{
walk(root);
finishCurrentParagraph();
return m_blocks;
}
private:
void finishCurrentParagraph()
{
if (m_currentInlines.isEmpty())
return;
BlockItem p;
p.kind = BlockItem::Kind::Paragraph;
p.align = m_currentAlign;
p.textIndent = m_currentIndent;
p.lineHeight = m_currentLineHeight;
p.fontSize = m_currentBlockFontSize;
p.preformatted = m_currentPreformatted;
p.marker = m_currentMarker;
p.markerStyle = m_currentMarkerStyle;
p.inlines = std::move(m_currentInlines);
m_blocks.append(p);
m_currentInlines.clear();
m_currentMarker.clear();
m_currentIndent = 0;
}
void startParagraph(const StyleContext &ctx, const QString &marker = {}, const TextStyle &markerStyle = {})
{
finishCurrentParagraph();
m_currentAlign = ctx.align;
m_currentIndent = ctx.textIndent;
m_currentLineHeight = ctx.lineHeight;
m_currentBlockFontSize = ctx.text.fontSize;
m_currentPreformatted = ctx.preformatted;
m_currentMarker = marker;
m_currentMarkerStyle = markerStyle;
}
void appendText(const QString &text, const TextStyle &style, bool pre)
{
if (text.isEmpty())
return;
if (m_currentInlines.isEmpty()) {
startParagraph(m_stack.last());
}
QString processed = text;
if (!pre) {
// Collapse whitespaces
static const QRegularExpression kSpaces(QStringLiteral("\\s+"));
processed.replace(kSpaces, QStringLiteral(" "));
if (m_currentInlines.isEmpty() && processed == QLatin1String(" "))
return;
}
if (processed.isEmpty())
return;
InlineItem item;
item.kind = InlineItem::Kind::Text;
item.text = processed;
item.style = style;
m_currentInlines.append(item);
}
void walk(const QDomNode &node)
{
if (node.isText()) {
const StyleContext &ctx = m_stack.last();
appendText(node.nodeValue(), ctx.text, ctx.preformatted);
return;
}
if (!node.isElement())
return;
const QDomElement el = node.toElement();
const QString tag = localName(el);
if (tag == QLatin1String("head") || tag == QLatin1String("style") || tag == QLatin1String("script"))
return;
StyleContext ctx = m_stack.last();
BoxStyle box;
// Default HTML semantics
if (tag == QLatin1String("h1")) {
ctx.text.fontSize = m_params.baseFontSize * 1.8;
ctx.text.weight = 700;
ctx.textIndent = 0;
box.marginTop = m_params.baseFontSize * 1.0;
box.marginBottom = m_params.baseFontSize * 0.5;
} else if (tag == QLatin1String("h2")) {
ctx.text.fontSize = m_params.baseFontSize * 1.5;
ctx.text.weight = 700;
ctx.textIndent = 0;
box.marginTop = m_params.baseFontSize * 0.8;
box.marginBottom = m_params.baseFontSize * 0.4;
} else if (tag == QLatin1String("h3")) {
ctx.text.fontSize = m_params.baseFontSize * 1.3;
ctx.text.weight = 700;
ctx.textIndent = 0;
box.marginTop = m_params.baseFontSize * 0.6;
box.marginBottom = m_params.baseFontSize * 0.3;
} else if (tag == QLatin1String("h4") || tag == QLatin1String("h5") || tag == QLatin1String("h6")) {
ctx.text.fontSize = m_params.baseFontSize * 1.1;
ctx.text.weight = 700;
ctx.textIndent = 0;
box.marginTop = m_params.baseFontSize * 0.5;
box.marginBottom = m_params.baseFontSize * 0.25;
} else if (tag == QLatin1String("p")) {
box.marginTop = m_params.baseFontSize * 0.25;
box.marginBottom = m_params.baseFontSize * 0.5;
ctx.textIndent = m_params.baseFontSize * 1.25;
} else if (tag == QLatin1String("blockquote")) {
box.marginLeft = m_params.baseFontSize * 1.5;
box.marginRight = m_params.baseFontSize * 1.0;
box.marginTop = m_params.baseFontSize * 0.5;
box.marginBottom = m_params.baseFontSize * 0.5;
box.borderLeft = 3;
box.paddingLeft = m_params.baseFontSize * 0.75;
ctx.text.italic = true;
} else if (tag == QLatin1String("pre")) {
ctx.text.fontFamilies = {QStringLiteral("monospace")};
ctx.preformatted = true;
box.marginTop = m_params.baseFontSize * 0.5;
box.marginBottom = m_params.baseFontSize * 0.5;
} else if (tag == QLatin1String("b") || tag == QLatin1String("strong")) {
ctx.text.weight = 700;
} else if (tag == QLatin1String("i") || tag == QLatin1String("em") || tag == QLatin1String("cite")) {
ctx.text.italic = true;
} else if (tag == QLatin1String("u") || tag == QLatin1String("ins")) {
ctx.text.underline = true;
} else if (tag == QLatin1String("s") || tag == QLatin1String("strike") || tag == QLatin1String("del")) {
ctx.text.strikeOut = true;
} else if (tag == QLatin1String("sub")) {
ctx.text.fontSize *= 0.75;
ctx.text.baselineShift = -ctx.text.fontSize * 0.35;
} else if (tag == QLatin1String("sup")) {
ctx.text.fontSize *= 0.75;
ctx.text.baselineShift = ctx.text.fontSize * 0.45;
} else if (tag == QLatin1String("small")) {
ctx.text.fontSize *= 0.85;
} else if (tag == QLatin1String("code") || tag == QLatin1String("tt") || tag == QLatin1String("kbd")) {
ctx.text.fontFamilies = {QStringLiteral("monospace")};
ctx.text.background = QColor(255, 255, 255, 20);
} else if (tag == QLatin1String("a")) {
ctx.text.underline = true;
ctx.text.link = true;
} else if (tag == QLatin1String("hr")) {
finishCurrentParagraph();
BlockItem r;
r.kind = BlockItem::Kind::Rule;
m_blocks.append(r);
return;
} else if (tag == QLatin1String("br")) {
InlineItem br;
br.kind = InlineItem::Kind::LineBreak;
m_currentInlines.append(br);
return;
} else if (tag == QLatin1String("img") || tag == QLatin1String("image")) {
handleImage(el, ctx);
return;
}
// Apply HTML attribute alignments
if (el.hasAttribute(QStringLiteral("align"))) {
const QString a = el.attribute(QStringLiteral("align")).toLower();
if (a == QLatin1String("center")) ctx.align = TextAlign::Center;
else if (a == QLatin1String("right")) ctx.align = TextAlign::Right;
else if (a == QLatin1String("justify")) ctx.align = TextAlign::Justify;
else if (a == QLatin1String("left")) ctx.align = TextAlign::Left;
}
// Apply CSS declarations from cascaded style attribute
if (el.hasAttribute(QStringLiteral("style"))) {
const auto decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
applyCssDeclarations(decls, ctx, box, m_params);
}
const bool isBlock = isBlockTag(tag);
const bool hasBox = (box.marginTop > 0 || box.marginBottom > 0 || box.marginLeft > 0 || box.marginRight > 0
|| box.paddingTop > 0 || box.paddingBottom > 0 || box.paddingLeft > 0 || box.paddingRight > 0
|| box.borderLeft > 0 || box.borderTop > 0 || box.borderBottom > 0);
if (isBlock) {
finishCurrentParagraph();
if (hasBox) {
BlockItem start;
start.kind = BlockItem::Kind::ContainerStart;
start.box = box;
m_blocks.append(start);
}
}
QString marker;
TextStyle markerStyle;
if (tag == QLatin1String("li")) {
marker = QStringLiteral("• ");
markerStyle = ctx.text;
} else if (tag == QLatin1String("dt")) {
ctx.text.weight = 700;
ctx.textIndent = 0;
} else if (tag == QLatin1String("dd")) {
box.marginLeft = m_params.baseFontSize * 1.5;
ctx.textIndent = 0;
}
if (isBlock) {
startParagraph(ctx, marker, markerStyle);
}
m_stack.append(ctx);
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling()) {
walk(child);
}
m_stack.removeLast();
if (isBlock) {
finishCurrentParagraph();
if (hasBox) {
BlockItem end;
end.kind = BlockItem::Kind::ContainerEnd;
end.box = box;
m_blocks.append(end);
}
}
}
void handleImage(const QDomElement &el, const StyleContext &ctx)
{
QString src = el.attribute(QStringLiteral("src"));
if (src.isEmpty())
src = el.attribute(QStringLiteral("xlink:href"));
if (src.isEmpty())
src = el.attribute(QStringLiteral("href"));
if (src.isEmpty())
return;
InlineItem item;
item.kind = InlineItem::Kind::Image;
item.imageUrl = src;
Css::LengthContext lenCtx;
lenCtx.em = ctx.text.fontSize;
lenCtx.rem = m_params.baseFontSize;
lenCtx.percentBase = m_params.contentWidth;
lenCtx.absoluteScale = m_params.baseFontSize / 16.0;
if (el.hasAttribute(QStringLiteral("width"))) {
auto w = Css::parseLength(el.attribute(QStringLiteral("width")), lenCtx);
if (w) item.cssWidth = *w;
}
if (el.hasAttribute(QStringLiteral("height"))) {
auto h = Css::parseLength(el.attribute(QStringLiteral("height")), lenCtx);
if (h) item.cssHeight = *h;
}
if (el.hasAttribute(QStringLiteral("style"))) {
const auto decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
for (const auto &d : decls) {
if (d.property == QLatin1String("width")) {
auto w = Css::parseLength(d.value, lenCtx); if (w) item.cssWidth = *w;
} else if (d.property == QLatin1String("height")) {
auto h = Css::parseLength(d.value, lenCtx); if (h) item.cssHeight = *h;
} else if (d.property == QLatin1String("max-width")) {
auto mw = Css::parseLength(d.value, lenCtx); if (mw) item.cssMaxWidth = *mw;
}
}
}
if (m_currentInlines.isEmpty()) {
startParagraph(ctx);
}
m_currentInlines.append(item);
}
const ParseParams &m_params;
QVector<StyleContext> m_stack;
QVector<BlockItem> m_blocks;
QVector<InlineItem> m_currentInlines;
TextAlign m_currentAlign = TextAlign::Justify;
qreal m_currentIndent = 0;
qreal m_currentLineHeight = 0;
qreal m_currentBlockFontSize = 16;
bool m_currentPreformatted = false;
QString m_currentMarker;
TextStyle m_currentMarkerStyle;
};
} // namespace
QVector<BlockItem> parse(const QString &html, const ParseParams &params)
{
if (html.trimmed().isEmpty())
return {};
QString wrapped = html.trimmed();
if (!wrapped.startsWith(QLatin1String("<")))
wrapped = QStringLiteral("<p>") + wrapped + QStringLiteral("</p>");
if (!wrapped.contains(QLatin1String("<body")))
wrapped = QStringLiteral("<body>") + wrapped + QStringLiteral("</body>");
const QString safeXml = HtmlEntities::toXmlSafe(wrapped);
QDomDocument doc;
QString errorMsg;
int errorLine = 0, errorColumn = 0;
if (!doc.setContent(safeXml, false, &errorMsg, &errorLine, &errorColumn)) {
// Fallback: return as a simple paragraph
BlockItem b;
b.kind = BlockItem::Kind::Paragraph;
InlineItem textItem;
textItem.kind = InlineItem::Kind::Text;
textItem.text = html;
b.inlines.append(textItem);
return {b};
}
Builder builder(params);
return builder.build(doc.documentElement());
}
} // namespace HtmlBlockParser