fixed paragraph layout to handle forced breaks
This commit is contained in:
6 files changed
+142
-11
No files matched your search
@@ -314,10 +314,18 @@ private:
|
||||
return;
|
||||
|
||||
qreal lineMultiplier = p.lineHeight > 0 ? p.lineHeight : m_params.lineSpacing;
|
||||
constexpr qreal kMaxJustifyStretchRatio = 2.0;
|
||||
|
||||
for (int lIdx = 0; lIdx < lines.size(); ++lIdx) {
|
||||
const auto &line = lines[lIdx];
|
||||
const bool isLastLine = (lIdx == lines.size() - 1);
|
||||
const bool isForcedBreak = (line.end < kpItems.size()
|
||||
&& kpItems[line.end].type == KnuthPlass::ItemType::Penalty
|
||||
&& kpItems[line.end].penalty <= -KnuthPlass::kInfinity);
|
||||
const bool shouldJustifyStretch = (p.align == TextAlign::Justify)
|
||||
&& !isLastLine
|
||||
&& !isForcedBreak
|
||||
&& (line.ratio <= kMaxJustifyStretchRatio);
|
||||
|
||||
qreal maxAscent = p.fontSize * 0.8;
|
||||
qreal maxDescent = p.fontSize * 0.2;
|
||||
@@ -340,11 +348,13 @@ private:
|
||||
if (it.type == KnuthPlass::ItemType::Box) {
|
||||
contentW += it.width;
|
||||
} else if (it.type == KnuthPlass::ItemType::Glue) {
|
||||
if (isLastLine || p.align != TextAlign::Justify) {
|
||||
if (isLastLine || isForcedBreak || p.align != TextAlign::Justify) {
|
||||
contentW += it.width;
|
||||
} else if (line.ratio >= 0) {
|
||||
contentW += it.width + (shouldJustifyStretch ? (line.ratio * it.stretch) : 0.0);
|
||||
} else {
|
||||
qreal adj = (line.ratio >= 0) ? (line.ratio * it.stretch) : (line.ratio * it.shrink);
|
||||
contentW += (std::max)(0.0, it.width + adj);
|
||||
// Allow normal glue shrink so tight lines fit
|
||||
contentW += (std::max)(0.0, it.width + line.ratio * it.shrink);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -405,9 +415,14 @@ private:
|
||||
curX += it.width;
|
||||
} else if (it.type == KnuthPlass::ItemType::Glue) {
|
||||
qreal spaceW = it.width;
|
||||
if (!isLastLine && p.align == TextAlign::Justify) {
|
||||
qreal adj = (line.ratio >= 0) ? (line.ratio * it.stretch) : (line.ratio * it.shrink);
|
||||
spaceW = (std::max)(0.0, it.width + adj);
|
||||
if (!isLastLine && !isForcedBreak && p.align == TextAlign::Justify) {
|
||||
if (line.ratio >= 0) {
|
||||
if (shouldJustifyStretch) {
|
||||
spaceW += line.ratio * it.stretch;
|
||||
}
|
||||
} else {
|
||||
spaceW = (std::max)(0.0, it.width + line.ratio * it.shrink);
|
||||
}
|
||||
}
|
||||
curX += spaceW;
|
||||
}
|
||||
@@ -552,7 +567,7 @@ PreparedChunk prepareBlocks(const QVector<BlockItem> &blocks,
|
||||
|
||||
for (const auto &item : b.inlines) {
|
||||
if (item.kind == InlineItem::Kind::LineBreak) {
|
||||
pb.paragraph.kpItems.append(KnuthPlass::Item::penaltyItem(0, -KnuthPlass::kInfinity, false));
|
||||
KnuthPlass::finishLine(pb.paragraph.kpItems);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -131,7 +131,7 @@ public:
|
||||
if (r < -1 || r > tolerance)
|
||||
continue;
|
||||
|
||||
const qreal badness = (std::min)(100.0 * std::pow(std::abs(r), 3), kInfinity);
|
||||
const qreal badness = (std::min)(100.0 * std::pow(std::abs(r), 3), 1e7);
|
||||
qreal d = m_params.linePenalty + badness;
|
||||
d *= d;
|
||||
if (brk.type == ItemType::Penalty) {
|
||||
@@ -230,16 +230,27 @@ private:
|
||||
|
||||
} // namespace
|
||||
|
||||
void finishParagraph(QVector<Item> &items)
|
||||
void finishLine(QVector<Item> &items)
|
||||
{
|
||||
// A trailing glue would otherwise become a legal break before the end.
|
||||
// A trailing glue would otherwise become a legal break before the line end.
|
||||
while (!items.isEmpty() && items.last().type == ItemType::Glue)
|
||||
items.removeLast();
|
||||
|
||||
// If there is no content on this line (e.g. leading break or consecutive <br/><br/>),
|
||||
// append a zero-width box so that subsequent finishing glue contributes to the ratio.
|
||||
if (items.isEmpty() || isForced(items.last()))
|
||||
items.append(Item::box(0, -1));
|
||||
|
||||
items.append(Item::penaltyItem(0, kInfinity, false));
|
||||
items.append(Item::glue(0, kInfiniteStretch, 0));
|
||||
items.append(Item::penaltyItem(0, -kInfinity, false));
|
||||
}
|
||||
|
||||
void finishParagraph(QVector<Item> &items)
|
||||
{
|
||||
finishLine(items);
|
||||
}
|
||||
|
||||
QVector<Line> breakLines(const QVector<Item> &items, qreal lineWidth, const Params ¶ms)
|
||||
{
|
||||
if (items.isEmpty())
|
||||
|
||||
@@ -64,6 +64,9 @@ struct Line {
|
||||
bool hyphenated = false; // ended at a flagged penalty with width (draw a hyphen)
|
||||
};
|
||||
|
||||
// Appends an inline line ending: finishing glue + forced break (e.g. for <br/>).
|
||||
void finishLine(QVector<Item> &items);
|
||||
|
||||
// Appends the standard paragraph ending: finishing glue + forced break.
|
||||
void finishParagraph(QVector<Item> &items);
|
||||
|
||||
|
||||
Reference in new issue
Block a user