more performance optimizations + cleanup

This commit is contained in:
Agel committed 2026-10-29 12:12:02 +01:00
1 parent b23713bea6
commit d9a0991f9b
15 files changed
+396 -469

No files matched your search

@@ -157,7 +157,7 @@ private:
dec.background = box.background;
dec.borderColor = box.borderColor;
dec.borderLeft = box.borderLeft;
dec.borderTop = box.borderTop;
dec.borderTop = ab.box.borderTop;
dec.borderBottom = box.borderBottom;
dec.borderRight = box.borderRight;
@@ -466,6 +466,8 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
PreparedChunk prepared;
prepared.blocks.reserve(blocks.size());
const qreal fontScale = (baseFontSize > 0) ? (baseFontSize / 16.0) : 1.0;
// Word metrics cache across the chunk
QHash<QString, WordMetrics> metricsCache;
@@ -475,11 +477,39 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
case BlockItem::Kind::ContainerStart:
pb.kind = PreparedBlock::Kind::ContainerStart;
pb.box = b.box;
if (!qFuzzyCompare(fontScale, 1.0)) {
pb.box.marginTop *= fontScale;
pb.box.marginBottom *= fontScale;
pb.box.marginLeft *= fontScale;
pb.box.marginRight *= fontScale;
pb.box.paddingTop *= fontScale;
pb.box.paddingBottom *= fontScale;
pb.box.paddingLeft *= fontScale;
pb.box.paddingRight *= fontScale;
pb.box.borderTop *= fontScale;
pb.box.borderBottom *= fontScale;
pb.box.borderLeft *= fontScale;
pb.box.borderRight *= fontScale;
}
prepared.blocks.append(pb);
break;
case BlockItem::Kind::ContainerEnd:
pb.kind = PreparedBlock::Kind::ContainerEnd;
pb.box = b.box;
if (!qFuzzyCompare(fontScale, 1.0)) {
pb.box.marginTop *= fontScale;
pb.box.marginBottom *= fontScale;
pb.box.marginLeft *= fontScale;
pb.box.marginRight *= fontScale;
pb.box.paddingTop *= fontScale;
pb.box.paddingBottom *= fontScale;
pb.box.paddingLeft *= fontScale;
pb.box.paddingRight *= fontScale;
pb.box.borderTop *= fontScale;
pb.box.borderBottom *= fontScale;
pb.box.borderLeft *= fontScale;
pb.box.borderRight *= fontScale;
}
prepared.blocks.append(pb);
break;
case BlockItem::Kind::Rule:
@@ -493,9 +523,9 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
const auto &imgInline = b.inlines[pureImageIdx];
pb.kind = PreparedBlock::Kind::ImageBlock;
pb.imageBlock.imageUrl = imgInline.imageUrl;
pb.imageBlock.cssWidth = imgInline.cssWidth;
pb.imageBlock.cssHeight = imgInline.cssHeight;
pb.imageBlock.cssMaxWidth = imgInline.cssMaxWidth;
pb.imageBlock.cssWidth = (imgInline.cssWidth > 0) ? (imgInline.cssWidth * fontScale) : imgInline.cssWidth;
pb.imageBlock.cssHeight = (imgInline.cssHeight > 0) ? (imgInline.cssHeight * fontScale) : imgInline.cssHeight;
pb.imageBlock.cssMaxWidth = (imgInline.cssMaxWidth > 0) ? (imgInline.cssMaxWidth * fontScale) : imgInline.cssMaxWidth;
if (imageResolver) {
QSizeF sz = imageResolver->imageSize(pb.imageBlock.imageUrl);
if (sz.isValid() && sz.width() > 0 && sz.height() > 0) {
@@ -506,14 +536,15 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
} else {
pb.kind = PreparedBlock::Kind::Paragraph;
pb.paragraph.align = b.align;
pb.paragraph.textIndent = b.textIndent;
pb.paragraph.textIndent = b.textIndent * fontScale;
pb.paragraph.lineHeight = b.lineHeight;
pb.paragraph.fontSize = b.fontSize;
pb.paragraph.fontSize = b.fontSize * fontScale;
pb.paragraph.marker = b.marker;
pb.paragraph.markerStyle = b.markerStyle;
pb.paragraph.markerStyle.fontSize *= fontScale;
if (b.textIndent > 0) {
pb.paragraph.kpItems.append(KnuthPlass::Item::box(b.textIndent, -1));
if (pb.paragraph.textIndent > 0) {
pb.paragraph.kpItems.append(KnuthPlass::Item::box(pb.paragraph.textIndent, -1));
}
for (const auto &item : b.inlines) {
@@ -523,7 +554,7 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
}
if (item.kind == InlineItem::Kind::Image) {
qreal w = 24.0, h = 24.0;
qreal w = 24.0 * fontScale, h = 24.0 * fontScale;
if (imageResolver) {
QSizeF sz = imageResolver->imageSize(item.imageUrl);
if (sz.isValid() && sz.width() > 0 && sz.height() > 0) {
@@ -533,13 +564,14 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
}
if (item.cssWidth > 0) {
qreal aspect = w > 0 ? (h / w) : 1.0;
w = item.cssWidth;
h = item.cssHeight > 0 ? item.cssHeight : (w * aspect);
w = item.cssWidth * fontScale;
h = item.cssHeight > 0 ? (item.cssHeight * fontScale) : (w * aspect);
}
PreparedWord tok;
tok.imageUrl = item.imageUrl;
tok.style = item.style;
tok.style.fontSize *= fontScale;
tok.width = w;
tok.ascent = h;
tok.descent = 0;
@@ -549,12 +581,19 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
continue;
}
QFont font = resolveFont(item.style, baseFontSize, QString());
TextStyle itemStyle = item.style;
if (!qFuzzyCompare(fontScale, 1.0)) {
itemStyle.fontSize *= fontScale;
itemStyle.letterSpacing *= fontScale;
itemStyle.baselineShift *= fontScale;
}
QFont font = resolveFont(itemStyle, baseFontSize, QString());
QFontMetricsF fm(font);
qreal spaceW = fm.horizontalAdvance(QLatin1Char(' '));
qreal hyphenW = fm.horizontalAdvance(QLatin1Char('-'));
QString rawText = transformText(item.text, item.style.transform);
QString rawText = transformText(item.text, itemStyle.transform);
const QStringList words = rawText.split(QLatin1Char(' '), Qt::SkipEmptyParts);
for (int wIdx = 0; wIdx < words.size(); ++wIdx) {
@@ -565,7 +604,7 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
for (int pIdx = 0; pIdx < parts.size(); ++pIdx) {
PreparedWord tok;
tok.text = parts[pIdx];
tok.style = item.style;
tok.style = itemStyle;
tok.font = font;
QString cacheKey = font.key() + QLatin1Char('|') + tok.text;
@@ -592,7 +631,7 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
} else {
PreparedWord tok;
tok.text = wStr;
tok.style = item.style;
tok.style = itemStyle;
tok.font = font;
QString cacheKey = font.key() + QLatin1Char('|') + tok.text;
@@ -632,12 +671,19 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
return prepared;
}
PreparedChunk prepareChunk(const ContentChunk &chunk,
qreal baseFontSize,
IBookImageResolver *imageResolver)
{
return prepareBlocks(chunk.blocks, baseFontSize, imageResolver);
}
PreparedChunk prepareChunk(const QString &html,
qreal baseFontSize,
IBookImageResolver *imageResolver)
{
ParseParams pp;
pp.baseFontSize = baseFontSize;
pp.baseFontSize = 16.0;
pp.contentWidth = 600;
const auto blocks = HtmlBlockParser::parse(html, pp);
return prepareBlocks(blocks, baseFontSize, imageResolver);
@@ -112,6 +112,10 @@ struct PreparedChunk {
QVector<PreparedBlock> blocks;
};
PreparedChunk prepareChunk(const ContentChunk &chunk,
qreal baseFontSize,
IBookImageResolver *imageResolver = nullptr);
PreparedChunk prepareChunk(const QString &html,
qreal baseFontSize,
IBookImageResolver *imageResolver = nullptr);
+127 -2
View File
@@ -579,6 +579,15 @@ private:
} // namespace
QVector<BlockItem> parse(const QDomElement &root, const ParseParams &params)
{
if (root.isNull())
return {};
Builder builder(params);
return builder.build(root);
}
QVector<BlockItem> parse(const QString &html, const ParseParams &params)
{
if (html.trimmed().isEmpty())
@@ -606,8 +615,124 @@ QVector<BlockItem> parse(const QString &html, const ParseParams &params)
return {b};
}
Builder builder(params);
return builder.build(doc.documentElement());
return parse(doc.documentElement(), params);
}
QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
const QString &sectionHref,
int targetChars)
{
if (blocks.isEmpty())
return {};
QVector<ContentChunk> chunks;
ContentChunk current;
current.sectionHref = sectionHref;
QVector<BoxStyle> openContainers;
auto flushCurrent = [&]() {
if (current.isEmpty())
return;
// Close outer containers at chunk boundary
for (int j = openContainers.size() - 1; j >= 0; --j) {
BlockItem endItem;
endItem.kind = BlockItem::Kind::ContainerEnd;
endItem.box = openContainers[j];
endItem.box.borderBottom = 0;
endItem.box.paddingBottom = 0;
endItem.box.marginBottom = 0;
current.blocks.append(endItem);
}
chunks.append(std::move(current));
current = ContentChunk();
current.sectionHref = sectionHref;
current.unnestedContainers = openContainers.size();
// Reopen outer containers at start of next chunk
for (int j = 0; j < openContainers.size(); ++j) {
BlockItem startItem;
startItem.kind = BlockItem::Kind::ContainerStart;
startItem.box = openContainers[j];
startItem.box.borderTop = 0;
startItem.box.paddingTop = 0;
startItem.box.marginTop = 0;
current.blocks.append(startItem);
}
};
for (int i = 0; i < blocks.size(); ++i) {
const auto &b = blocks[i];
if (b.kind == BlockItem::Kind::ContainerStart) {
openContainers.append(b.box);
current.blocks.append(b);
} else if (b.kind == BlockItem::Kind::ContainerEnd) {
if (!openContainers.isEmpty())
openContainers.removeLast();
current.blocks.append(b);
} else if (b.kind == BlockItem::Kind::Rule) {
current.blocks.append(b);
current.charCount += 1;
if (current.charCount >= targetChars && i + 1 < blocks.size()) {
flushCurrent();
}
} else if (b.kind == BlockItem::Kind::Paragraph) {
int pChars = 0;
int pWords = 0;
for (const auto &inl : b.inlines) {
if (inl.kind == InlineItem::Kind::Text) {
pChars += inl.text.size();
const QStringList words = inl.text.split(QLatin1Char(' '), Qt::SkipEmptyParts);
pWords += words.size();
} else if (inl.kind == InlineItem::Kind::Image) {
pChars += 1;
pWords += 1;
}
}
current.blocks.append(b);
current.charCount += pChars;
current.wordCount += pWords;
// Consume immediately following ContainerEnd that closes this paragraph's leaf box
if (i + 1 < blocks.size() && blocks[i + 1].kind == BlockItem::Kind::ContainerEnd) {
if (!openContainers.isEmpty())
openContainers.removeLast();
current.blocks.append(blocks[i + 1]);
++i;
}
if (current.charCount >= targetChars && i + 1 < blocks.size()) {
flushCurrent();
}
}
}
if (!current.isEmpty()) {
chunks.append(std::move(current));
}
return chunks;
}
QVector<ContentChunk> parseChunks(const QDomElement &root,
const QString &sectionHref,
int targetChars,
const ParseParams &params)
{
const auto blocks = parse(root, params);
return partitionBlocks(blocks, sectionHref, targetChars);
}
QVector<ContentChunk> parseChunks(const QString &html,
const QString &sectionHref,
int targetChars,
const ParseParams &params)
{
const auto blocks = parse(html, params);
return partitionBlocks(blocks, sectionHref, targetChars);
}
} // namespace HtmlBlockParser
+32 -3
View File
@@ -97,11 +97,40 @@ struct ParseParams {
qreal contentWidth = 600; // px, resolves percentage lengths
};
struct ContentChunk {
QString sectionHref;
QVector<BlockItem> blocks;
int wordCount = 0;
int charCount = 0;
int unnestedContainers = 0;
bool isEmpty() const { return blocks.isEmpty(); }
};
class QDomElement;
namespace HtmlBlockParser {
// Parses one standalone HTML chunk (an XHTML fragment whose elements carry
// their cascaded CSS in `style` attributes) into a flat block list.
QVector<BlockItem> parse(const QString &html, const ParseParams &params);
// Parses HTML or DOM element (whose elements carry cascaded CSS in `style` attributes)
// into a flat block list.
QVector<BlockItem> parse(const QString &html, const ParseParams &params = {});
QVector<BlockItem> parse(const QDomElement &root, const ParseParams &params = {});
// Partitions a block list into granular ContentChunks (~targetChars per chunk).
QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
const QString &sectionHref = {},
int targetChars = 512);
// Parses and partitions into granular ContentChunks in a single pass.
QVector<ContentChunk> parseChunks(const QDomElement &root,
const QString &sectionHref = {},
int targetChars = 512,
const ParseParams &params = {});
QVector<ContentChunk> parseChunks(const QString &html,
const QString &sectionHref = {},
int targetChars = 512,
const ParseParams &params = {});
} // namespace HtmlBlockParser