hopefully true virtual list layout
This commit is contained in:
10 files changed
+522
-37
No files matched your search
@@ -220,10 +220,155 @@ void EpubReader::appendSpineChunks(const QString &xhtml,
|
||||
}
|
||||
|
||||
QDomElement body = bodies.at(0).toElement();
|
||||
QString currentChunk;
|
||||
static constexpr int kChunkTargetSize = 4096;
|
||||
|
||||
for (QDomNode node = body.firstChild(); !node.isNull(); node = node.nextSibling()) {
|
||||
auto hasBoxDecoration = [](const QDomElement &el) -> bool {
|
||||
const QString style = el.attribute(QStringLiteral("style")).toLower();
|
||||
if (style.isEmpty())
|
||||
return false;
|
||||
const auto decls = Css::parseDeclarations(style);
|
||||
for (const auto &d : decls) {
|
||||
const QString p = d.property.toLower();
|
||||
if (p.contains(QLatin1String("background")) && d.value != QLatin1String("none") && d.value != QLatin1String("transparent"))
|
||||
return true;
|
||||
if (p.contains(QLatin1String("border")) && !d.value.startsWith(QLatin1String("none")) && !d.value.startsWith(QLatin1String("0")))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
static const QStringList kInheritableProperties = {
|
||||
QStringLiteral("color"),
|
||||
QStringLiteral("font-family"),
|
||||
QStringLiteral("font-size"),
|
||||
QStringLiteral("font-style"),
|
||||
QStringLiteral("font-weight"),
|
||||
QStringLiteral("line-height"),
|
||||
QStringLiteral("text-align"),
|
||||
QStringLiteral("letter-spacing"),
|
||||
QStringLiteral("text-transform")
|
||||
};
|
||||
|
||||
auto propagateInheritedStyles = [](const QDomElement &parentEl, QDomElement &childEl) {
|
||||
const auto parentDecls = Css::parseDeclarations(parentEl.attribute(QStringLiteral("style")));
|
||||
if (parentDecls.isEmpty())
|
||||
return;
|
||||
auto childDecls = Css::parseDeclarations(childEl.attribute(QStringLiteral("style")));
|
||||
QSet<QString> childProps;
|
||||
for (const auto &cd : childDecls) {
|
||||
childProps.insert(cd.property.toLower());
|
||||
}
|
||||
bool modified = false;
|
||||
for (const auto &pd : parentDecls) {
|
||||
const QString prop = pd.property.toLower();
|
||||
if (kInheritableProperties.contains(prop) && !childProps.contains(prop)) {
|
||||
childDecls.append(pd);
|
||||
childProps.insert(prop);
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
if (modified) {
|
||||
childEl.setAttribute(QStringLiteral("style"), Css::serialize(childDecls));
|
||||
}
|
||||
};
|
||||
|
||||
static const QSet<QString> kBlockTags = {
|
||||
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("table"),
|
||||
QStringLiteral("hr"), QStringLiteral("figure"),
|
||||
QStringLiteral("section"), QStringLiteral("article"),
|
||||
QStringLiteral("header"), QStringLiteral("footer"),
|
||||
QStringLiteral("aside")
|
||||
};
|
||||
|
||||
static const QSet<QString> kStructuralTags = {
|
||||
QStringLiteral("div"),
|
||||
QStringLiteral("section"),
|
||||
QStringLiteral("article"),
|
||||
QStringLiteral("main"),
|
||||
QStringLiteral("header"),
|
||||
QStringLiteral("footer"),
|
||||
QStringLiteral("aside")
|
||||
};
|
||||
|
||||
std::function<bool(const QDomElement &)> hasBlockChild = [&](const QDomElement &el) {
|
||||
for (QDomNode n = el.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
||||
if (n.isElement()) {
|
||||
const QString childTag = n.toElement().tagName().toLower();
|
||||
if (kBlockTags.contains(childTag)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
QVector<QDomNode> blockNodes;
|
||||
std::function<void(const QDomElement &)> collectBlocks = [&](const QDomElement &parent) {
|
||||
QDomDocument parentDoc = parent.ownerDocument();
|
||||
QDomElement pendingInlinePara;
|
||||
|
||||
auto flushPendingInline = [&]() {
|
||||
if (!pendingInlinePara.isNull()) {
|
||||
if (!pendingInlinePara.text().trimmed().isEmpty() || pendingInlinePara.elementsByTagName(QStringLiteral("img")).size() > 0) {
|
||||
blockNodes.append(pendingInlinePara);
|
||||
}
|
||||
pendingInlinePara = QDomElement();
|
||||
}
|
||||
};
|
||||
|
||||
for (QDomNode child = parent.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
||||
if (!child.isElement()) {
|
||||
if (child.isText() && !child.toText().data().trimmed().isEmpty()) {
|
||||
if (pendingInlinePara.isNull()) {
|
||||
pendingInlinePara = parentDoc.createElement(QStringLiteral("p"));
|
||||
}
|
||||
pendingInlinePara.appendChild(child.cloneNode(true));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
QDomElement childEl = child.toElement();
|
||||
const QString tag = childEl.tagName().toLower();
|
||||
|
||||
if (!kBlockTags.contains(tag)) {
|
||||
// Inline element (e.g. <span>, <em>, <a>, <img>) directly in container
|
||||
if (pendingInlinePara.isNull()) {
|
||||
pendingInlinePara = parentDoc.createElement(QStringLiteral("p"));
|
||||
}
|
||||
pendingInlinePara.appendChild(childEl.cloneNode(true));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Child is a block element: flush any preceding inline nodes into their own block
|
||||
flushPendingInline();
|
||||
|
||||
// Unwrap only structural containers that have block-level children and no box decoration
|
||||
if (kStructuralTags.contains(tag) && !hasBoxDecoration(childEl) && hasBlockChild(childEl)) {
|
||||
for (QDomNode n = childEl.firstChild(); !n.isNull(); n = n.nextSibling()) {
|
||||
if (n.isElement()) {
|
||||
QDomElement subEl = n.toElement();
|
||||
propagateInheritedStyles(childEl, subEl);
|
||||
}
|
||||
}
|
||||
collectBlocks(childEl);
|
||||
} else {
|
||||
blockNodes.append(childEl);
|
||||
}
|
||||
}
|
||||
|
||||
flushPendingInline();
|
||||
};
|
||||
collectBlocks(body);
|
||||
|
||||
QString currentChunk;
|
||||
static constexpr int kChunkTargetSize = 512;
|
||||
|
||||
for (const QDomNode &node : blockNodes) {
|
||||
QString serialized;
|
||||
QTextStream ts(&serialized);
|
||||
node.save(ts, 0);
|
||||
|
||||
Reference in new issue
Block a user