some refactor

This commit is contained in:
Agel committed 2026-10-30 12:00:49 +01:00
1 parent 870bd1c0be
commit c25e8ddb90
20 files changed
+1204 -948

No files matched your search

@@ -0,0 +1,305 @@
#include "DocumentChunkBuilder.h"
#include <QRegularExpression>
namespace {
void countInlines(const QVector<InlineItem> &inlines, int &pChars, int &pWords)
{
pChars = 0;
pWords = 0;
for (const auto &inl : inlines) {
if (inl.kind == InlineItem::Kind::Text) {
pChars += inl.text.size();
pWords += inl.text.split(QLatin1Char(' '), Qt::SkipEmptyParts).size();
} else if (inl.kind == InlineItem::Kind::Image) {
pChars += 1;
pWords += 1;
}
}
}
} // namespace
DocumentChunkBuilder::DocumentChunkBuilder(QString sectionHref, int targetChars)
: m_sectionHref(std::move(sectionHref))
, m_targetChars(targetChars)
{
m_currentChunk.sectionHref = m_sectionHref;
}
void DocumentChunkBuilder::checkPendingFlush()
{
if (m_pendingFlush) {
m_pendingFlush = false;
flushCurrentChunk();
}
}
void DocumentChunkBuilder::flushCurrentChunk()
{
if (m_currentChunk.isEmpty())
return;
// Close outer open containers at chunk boundary
for (int j = m_openContainers.size() - 1; j >= 0; --j) {
BlockItem endItem;
endItem.kind = BlockItem::Kind::ContainerEnd;
endItem.box = m_openContainers[j];
endItem.box.borderBottom = 0;
endItem.box.paddingBottom = 0;
endItem.box.marginBottom = 0;
m_currentChunk.blocks.append(endItem);
}
m_chunks.append(std::move(m_currentChunk));
m_currentChunk = ContentChunk();
m_currentChunk.sectionHref = m_sectionHref;
m_currentChunk.unnestedContainers = m_openContainers.size();
// Reopen outer containers at start of next chunk
for (int j = 0; j < m_openContainers.size(); ++j) {
BlockItem startItem;
startItem.kind = BlockItem::Kind::ContainerStart;
startItem.box = m_openContainers[j];
startItem.box.borderTop = 0;
startItem.box.paddingTop = 0;
startItem.box.marginTop = 0;
m_currentChunk.blocks.append(startItem);
}
}
void DocumentChunkBuilder::startContainer(const BoxStyle &box)
{
finishParagraph();
checkPendingFlush();
m_openContainers.append(box);
BlockItem start;
start.kind = BlockItem::Kind::ContainerStart;
start.box = box;
m_currentChunk.blocks.append(start);
}
void DocumentChunkBuilder::endContainer(const BoxStyle &box)
{
finishParagraph();
BoxStyle effectiveBox = box;
if (!m_openContainers.isEmpty()) {
BoxStyle popped = m_openContainers.takeLast();
if (effectiveBox.marginTop == 0 && effectiveBox.marginBottom == 0
&& effectiveBox.marginLeft == 0 && effectiveBox.marginRight == 0
&& effectiveBox.paddingTop == 0 && effectiveBox.paddingBottom == 0
&& effectiveBox.borderLeft == 0 && !effectiveBox.background.isValid()) {
effectiveBox = popped;
}
}
BlockItem end;
end.kind = BlockItem::Kind::ContainerEnd;
end.box = effectiveBox;
m_currentChunk.blocks.append(end);
checkPendingFlush();
}
void DocumentChunkBuilder::startParagraph(TextAlign align,
qreal textIndent,
qreal lineHeight,
qreal fontSize,
bool preformatted,
const QString &marker,
const TextStyle &markerStyle)
{
finishParagraph();
checkPendingFlush();
m_hasActiveParagraph = true;
m_currentParagraph = BlockItem();
m_currentParagraph.kind = BlockItem::Kind::Paragraph;
m_currentParagraph.align = align;
m_currentParagraph.textIndent = textIndent;
m_currentParagraph.lineHeight = lineHeight;
m_currentParagraph.fontSize = fontSize;
m_currentParagraph.preformatted = preformatted;
m_currentParagraph.marker = marker;
m_currentParagraph.markerStyle = markerStyle;
}
void DocumentChunkBuilder::appendText(const QString &text, const TextStyle &style, bool preformatted)
{
if (text.isEmpty())
return;
if (!m_hasActiveParagraph) {
startParagraph(TextAlign::Justify, 0, 0, style.fontSize, preformatted);
}
QString processed = text;
if (!preformatted && !m_currentParagraph.preformatted) {
static const QRegularExpression kSpaces(QStringLiteral("\\s+"));
processed.replace(kSpaces, QStringLiteral(" "));
if (m_currentParagraph.inlines.isEmpty() && processed == QLatin1String(" "))
return;
}
if (processed.isEmpty())
return;
InlineItem item;
item.kind = InlineItem::Kind::Text;
item.text = processed;
item.style = style;
m_currentParagraph.inlines.append(item);
}
void DocumentChunkBuilder::appendImage(const QString &imageUrl, qreal cssWidth, qreal cssHeight, qreal cssMaxWidth, const TextStyle &style)
{
if (imageUrl.isEmpty())
return;
if (!m_hasActiveParagraph) {
startParagraph(TextAlign::Justify, 0, 0, style.fontSize > 0 ? style.fontSize : 16);
}
InlineItem item;
item.kind = InlineItem::Kind::Image;
item.imageUrl = imageUrl;
item.cssWidth = cssWidth;
item.cssHeight = cssHeight;
item.cssMaxWidth = cssMaxWidth;
item.style = style;
m_currentParagraph.inlines.append(item);
}
void DocumentChunkBuilder::appendLineBreak()
{
if (!m_hasActiveParagraph) {
startParagraph();
}
InlineItem br;
br.kind = InlineItem::Kind::LineBreak;
m_currentParagraph.inlines.append(br);
}
void DocumentChunkBuilder::finishParagraph()
{
if (!m_hasActiveParagraph)
return;
auto &inlines = m_currentParagraph.inlines;
if (!m_currentParagraph.preformatted) {
// Drop trailing line breaks and whitespace
while (!inlines.isEmpty()) {
const auto &last = inlines.last();
if (last.kind == InlineItem::Kind::LineBreak) {
inlines.removeLast();
} else if (last.kind == InlineItem::Kind::Text && last.text.trimmed().isEmpty()) {
inlines.removeLast();
} else {
break;
}
}
if (!inlines.isEmpty() && inlines.last().kind == InlineItem::Kind::Text) {
while (inlines.last().text.endsWith(QLatin1Char(' '))) {
inlines.last().text.chop(1);
}
}
// Drop leading whitespace
while (!inlines.isEmpty()) {
const auto &first = inlines.first();
if (first.kind == InlineItem::Kind::Text && first.text.trimmed().isEmpty()) {
inlines.removeFirst();
} else {
break;
}
}
if (!inlines.isEmpty() && inlines.first().kind == InlineItem::Kind::Text) {
while (inlines.first().text.startsWith(QLatin1Char(' '))) {
inlines.first().text.remove(0, 1);
}
}
}
if (inlines.isEmpty()) {
m_hasActiveParagraph = false;
m_currentParagraph = BlockItem();
return;
}
int pChars = 0;
int pWords = 0;
countInlines(inlines, pChars, pWords);
m_currentChunk.blocks.append(m_currentParagraph);
m_currentChunk.charCount += pChars;
m_currentChunk.wordCount += pWords;
m_hasActiveParagraph = false;
m_currentParagraph = BlockItem();
if (m_currentChunk.charCount >= m_targetChars) {
m_pendingFlush = true;
}
}
void DocumentChunkBuilder::appendRule(const QColor &ruleColor)
{
finishParagraph();
checkPendingFlush();
BlockItem r;
r.kind = BlockItem::Kind::Rule;
r.ruleColor = ruleColor;
m_currentChunk.blocks.append(r);
m_currentChunk.charCount += 1;
if (m_currentChunk.charCount >= m_targetChars) {
m_pendingFlush = true;
}
}
void DocumentChunkBuilder::appendBlock(const BlockItem &block)
{
finishParagraph();
checkPendingFlush();
if (block.kind == BlockItem::Kind::ContainerStart) {
startContainer(block.box);
} else if (block.kind == BlockItem::Kind::ContainerEnd) {
endContainer(block.box);
} else if (block.kind == BlockItem::Kind::Rule) {
appendRule(block.ruleColor);
} else if (block.kind == BlockItem::Kind::Paragraph) {
int pChars = 0;
int pWords = 0;
countInlines(block.inlines, pChars, pWords);
m_currentChunk.blocks.append(block);
m_currentChunk.charCount += pChars;
m_currentChunk.wordCount += pWords;
if (m_currentChunk.charCount >= m_targetChars) {
m_pendingFlush = true;
}
}
}
QVector<ContentChunk> DocumentChunkBuilder::build()
{
finishParagraph();
m_pendingFlush = false;
if (!m_currentChunk.isEmpty()) {
m_chunks.append(std::move(m_currentChunk));
m_currentChunk = ContentChunk();
m_currentChunk.sectionHref = m_sectionHref;
}
return std::move(m_chunks);
}