fixed paragraph layout to handle forced breaks

This commit is contained in:
Agel committed 2026-11-01 12:01:26 +01:00
1 parent 8782a3be8a
commit 1ad2f5446b
6 files changed
+142 -11

No files matched your search

+63
View File
@@ -462,6 +462,69 @@ private slots:
QCOMPARE(styledBlocks[1].textIndent, 0.0);
QCOMPARE(styledBlocks[2].kind, BlockItem::Kind::ContainerEnd);
}
void lineBreakBeforeParagraphEndDoesNotStretch()
{
// Simulating the Protector.epub Summary block:
// A paragraph with short lines separated by <br/>, with text-align: justify
const QString html = QStringLiteral(
"<p style=\"text-align: justify;\">"
"This does not contain non-con or Loki treating Tony like a slave.<br/>"
"It's a hurt/comfort-story."
"</p>"
);
LayoutParams params;
params.contentWidth = 800; // Wide viewport where each line easily fits on one line
params.fontSize = 16;
params.lineSpacing = 1.0;
auto layout = ChunkLayoutEngine::layoutChunk(html, params);
QCOMPARE(layout.lines.size(), 2);
// Neither line should stretch to fill the 800px width.
// The first line has text ending at its natural width, not reaching anywhere near 800.
const auto &line1 = layout.lines[0];
QVERIFY(!line1.runs.isEmpty());
const qreal line1Right = line1.runs.last().rect.right();
QVERIFY(line1Right < 700.0);
const auto &line2 = layout.lines[1];
QVERIFY(!line2.runs.isEmpty());
const qreal line2Right = line2.runs.last().rect.right();
QVERIFY(line2Right < 400.0);
}
void overstretchedJustifiedLineFallsBackToNaturalSpacing()
{
// When a line in a justified paragraph has an extreme ratio (> 2.0),
// e.g. because the next word is slightly too wide to fit on the line,
// it must not stretch spaces across the line to fill the container.
// E.g.: "WordA WordB" (fits in ~120px) followed by "MediumLengthWord" (fits on next line in 200px width).
// 120px in 200px line width: shortfall = 80px, glue stretch = ~2-3px -> ratio = 80/2 = 40.0 >> 2.0!
BlockItem p;
p.kind = BlockItem::Kind::Paragraph;
p.align = TextAlign::Justify;
p.fontSize = 16.0;
InlineItem t1;
t1.kind = InlineItem::Kind::Text;
t1.text = QStringLiteral("WordA WordB MediumLengthWordThatCannotFitOnLineOne");
p.inlines.append(t1);
LayoutParams params;
params.contentWidth = 200;
params.fontSize = 16;
params.lineSpacing = 1.0;
auto layout = ChunkLayoutEngine::layoutBlocks({p}, params);
QVERIFY(layout.lines.size() >= 2);
// First line has "WordA WordB". Since ratio > 2.0, it should NOT be stretched to fill 200px.
const auto &line0 = layout.lines[0];
QVERIFY(!line0.runs.isEmpty());
// Natural width of "WordA WordB" is ~120px; stretched to 200px it would be 200px.
QVERIFY(line0.runs.last().rect.right() < 160.0);
}
};
QTEST_MAIN(ChunkLayoutEngineTest)