more performance optimizations + cleanup
This commit is contained in:
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);
|
||||
|
||||
Reference in new issue
Block a user