diff --git a/AGENTS.md b/AGENTS.md index 9627991..775a673 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -190,7 +190,8 @@ public: - Headings (`h1`..`h6`) default to left-aligned (`TextAlign::Left`) without first-line indentation (`textIndent = 0`). In `Fb2Reader`, `
` lines separated by ` "
+ "This does not contain non-con or Loki treating Tony like a slave.
` with `text-align: left; text-indent: 0;`, preventing nested paragraph indent on titles. Scene transitions via FB2 `
`, `InlineItem::Kind::LineBreak`) use `finishLine()` to append infinite stretch glue (`kInfiniteStretch`), ensuring short lines ending at `
` are not justified to full width and do not push the paragraph into loose/emergency fallback passes.
+ - Overly stretched line fallback: When a line's stretch ratio in a justified paragraph exceeds `tolerance` (`line.ratio > 2.0`), `ChunkLayoutEngine` falls back to natural inter-word spacing (left alignment) rather than stretching words across the viewport with large gaps.
- `BookImageResolver` resolves embedded image dimensions and URLs.
- Off-thread calculation stores `ChunkLayout` and pixel heights.
- **Two-Phase Visible-First Layout**: On book open and font changes, `ReadingModel::startLayoutMeasurement()` computes layout for the visible window around the anchor chunk synchronously (<1ms) and emits `restoreReady(chunkIndex, intraOffset)` immediately so the visible screen renders on the first frame, while the background thread computes remaining off-screen chunks and updates only non-visible model ranges. On-demand layout guards ensure any chunk scrolled to before background completion is measured instantly.
diff --git a/src/backend/reading/layout/ChunkLayoutEngine.cpp b/src/backend/reading/layout/ChunkLayoutEngine.cpp
index ec05cb5..e2509e9 100644
--- a/src/backend/reading/layout/ChunkLayoutEngine.cpp
+++ b/src/backend/reading/layout/ChunkLayoutEngine.cpp
@@ -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
),
+ // 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
).
+void finishLine(QVector
, with text-align: justify
+ const QString html = QStringLiteral(
+ "
"
+ "It's a hurt/comfort-story."
+ "
"Line 2"
+ QVector
"Line 2"
+ QVector