Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[web_paragraph] Fix soft hyphen edge cases
Fix five edge cases in cluster mapping, line layout, and soft-hyphen
wrapping:

1. In _TextClusterMapping.toClusterRange, decrement endClusterIndex when
   _clusters[endClusterIndex - 1].start >= end so leading clusterless
   code units at offset 0 map to an empty ClusterRange(0, 0) instead of
   ClusterRange(0, 1).
2. In TextLayout.extractBidiRuns and TextLayout.addLine, skip empty
   clusterRange and bidiLineSpanRange slices produced by standalone
   clusterless characters (such as U+200F, U+00AD, or U+200B).
3. In TextLayout.addLine, start contentTextRange and allTextRange at 0
   for the first line and at lines.last.hardLineBreakRange.end for
   subsequent lines so leading clusterless characters do not leave gaps
   in WebParagraph.getLineNumberAt.
4. In _LineBuilder.markSoftLineBreak and consumePendingText, ignore
   softHyphen when !hasPendingText so a soft hyphen immediately after
   whitespace breaks at the space without reserving hyphen width or
   inflating minIntrinsicWidth.
5. In TextLayout.getPositionForOffset, return TextAffinity.upstream when
   hit-testing a SoftHyphenBlock so line-boundary and closest-glyph
   queries stay on the hyphenated line.

Without this fix, 9 tests in paragraph_edge_cases_test.dart and
paragraph_soft_hyphen_test.dart fail (+73 -7 in felt test).

Assisted-by: Gemini 3.8 Flash (router)
  • Loading branch information
Rusino committed Oct 26, 2026
commit 269f171b2c44096b0868d369326f93cfdbc599fe
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,14 @@ class TextLayout {
);

for (final region in regions) {
// Regions operate in text indexes, not cluster indexes (one cluster can contain several text points)
// We need to convert one into another
// Regions operate in text indexes, not cluster indexes (one cluster can contain several text points).
// We need to convert one into another.
final ClusterRange clusterRange = _mapping.toClusterRange(region.start, region.end);
if (clusterRange.isEmpty) {
// A bidi region containing only clusterless characters (such as a standalone directional
// mark or soft hyphen) owns no clusters and produces no visual run.
continue;
}
final run = BidiRun(clusterRange, region.level);
bidiRuns.add(run);
}
Expand Down Expand Up @@ -311,8 +316,16 @@ class TextLayout {
);
}

// Arrange line vertically, calculate metrics and bounds
final ui.TextRange contentTextRange = _mapping.toTextRange(contentClusterRange);
// Arrange line vertically, calculate metrics and bounds.
// Leading clusterless characters (at offset 0 or right after a hard line break) map to the
// first visible cluster after them, so start the line's text range where the previous line's
// hard break ended (or at 0 for the first line) to keep line ranges contiguous.
final int lineStart = lines.isEmpty ? 0 : lines.last.hardLineBreakRange.end;
final ui.TextRange rawContentTextRange = _mapping.toTextRange(contentClusterRange);
final contentTextRange = ui.TextRange(
start: math.min(lineStart, rawContentTextRange.start),
end: rawContentTextRange.end,
);
final ui.TextRange whitespaceTextRange = _mapping.toTextRange(whitespaceClusterRange);
final ui.TextRange hardlineTextRange = _mapping.toTextRange(hardlineClusterRange);
// A line includes the trailing newline if it has a hard line break that
Expand Down Expand Up @@ -461,6 +474,11 @@ class TextLayout {
bidiLineSpanTextRange.start,
bidiLineSpanTextRange.end,
);
if (bidiLineSpanRange.isEmpty) {
// A span slice containing only clusterless characters (such as a standalone U+00AD or
// U+200B span) has no clusters to measure or paint on this line.
continue;
}
final LineBlock block;
final double blockWidth;
if (span is PlaceholderSpan) {
Expand Down Expand Up @@ -920,10 +938,12 @@ class TextLayout {

if (block is SoftHyphenBlock) {
// Like SkParagraph, the hyphen maps to the position after the soft hyphen.
// Because that offset is also the start of the next line, use upstream affinity so
// line-boundary and closest-glyph queries keep the position on the hyphenated line.
if (right < offset.dx) {
continue;
}
return ui.TextPosition(offset: block.textRange.end);
return ui.TextPosition(offset: block.textRange.end, affinity: ui.TextAffinity.upstream);
}

if (right < offset.dx) {
Expand Down Expand Up @@ -979,7 +999,12 @@ class TextLayout {
// We found the line but not the block because the offset is to the right of all blocks in this line.
final LineBlock? lastVisualBlockInLine = line.visualBlocks.lastOrNull;
if (lastVisualBlockInLine is SoftHyphenBlock) {
return ui.TextPosition(offset: lastVisualBlockInLine.textRange.end);
// Offsets past a trailing soft hyphen map to the break boundary at the end of this line,
// so use upstream affinity to keep the position on the hyphenated line.
return ui.TextPosition(
offset: lastVisualBlockInLine.textRange.end,
affinity: ui.TextAffinity.upstream,
);
}
return lastVisualBlockInLine == null
? ui.TextPosition(offset: line.allLineTextRange.start, affinity: ui.TextAffinity.upstream)
Expand Down Expand Up @@ -1202,7 +1227,13 @@ class _TextClusterMapping {
// starts at the next cluster.
startClusterIndex += 1;
}
return ClusterRange(start: startClusterIndex, end: toClusterIndex(end - 1) + 1);
int endClusterIndex = toClusterIndex(end - 1) + 1;
if (_clusters[endClusterIndex - 1].start >= end) {
// The code unit at (end - 1) has no cluster of its own at the start of the text and is
// mapped to the first cluster after it, so the range ends before that cluster.
endClusterIndex -= 1;
}
return ClusterRange(start: startClusterIndex, end: endClusterIndex);
}

ui.TextRange toTextRange(ClusterRange clusterRange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,20 @@ class _LineBuilder {
}

void markSoftLineBreak(int index, {SoftHyphen? softHyphen}) {
// A soft hyphen immediately after whitespace (when there is no pending word before the break)
// breaks at the preceding whitespace and never draws a hyphen.
final SoftHyphen? effectiveSoftHyphen = hasPendingText ? softHyphen : null;

// Like SkParagraph, a break at a soft hyphen only fits if the hyphen fits too.
// If it does not, the line ends at the last earlier break that fits instead, if there
// is one; otherwise the hyphen overflows.
final double reservedWidth = softHyphen == null || _mayEllipsize ? 0.0 : softHyphen.width;
final double reservedWidth = effectiveSoftHyphen == null || _mayEllipsize
? 0.0
: effectiveSoftHyphen.width;
_isBreakTooWide = reservedWidth > 0.0 && _fallbackBreak != null && !canFit(reservedWidth);

_hasSoftLineBreak = true;
_breakSoftHyphen = softHyphen;
_breakSoftHyphen = effectiveSoftHyphen;
_breakIndex = index;

if (hasPendingText) {
Expand All @@ -335,7 +341,7 @@ class _LineBuilder {
assert(_newlineEnd == index);
}

consumePendingText(hyphenWidth: softHyphen?.width ?? 0.0);
consumePendingText(hyphenWidth: effectiveSoftHyphen?.width ?? 0.0);
assert(_newlineEnd == index);

if (!_isBreakTooWide) {
Expand Down Expand Up @@ -423,14 +429,14 @@ class _LineBuilder {

// TODO(mdebbar): Can we inline this in `markSoftLineBreak` and use that everywhere?
void consumePendingText({double hyphenWidth = 0.0}) {
// Update min intrinsic width. Like CSS min-content, a piece of a word that ends at a soft
// hyphen counts with its hyphen.
_minIntrinsicWidth = math.max(_minIntrinsicWidth, _widthPendingText + hyphenWidth);

if (!hasPendingText) {
return;
}

// Update min intrinsic width. Like CSS min-content, a piece of a word that ends at a soft
// hyphen counts with its hyphen.
_minIntrinsicWidth = math.max(_minIntrinsicWidth, _widthPendingText + hyphenWidth);

_whitespaceStart = _pendingTextEnd;
_whitespaceEnd = _pendingTextEnd;
_newlineEnd = _pendingTextEnd;
Expand Down
Loading