Skip to content

Commit 269f171

Browse files
committed
[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)
1 parent e50f01a commit 269f171

2 files changed

Lines changed: 51 additions & 14 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/layout.dart‎

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,14 @@ class TextLayout {
220220
);
221221

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

314-
// Arrange line vertically, calculate metrics and bounds
315-
final ui.TextRange contentTextRange = _mapping.toTextRange(contentClusterRange);
319+
// Arrange line vertically, calculate metrics and bounds.
320+
// Leading clusterless characters (at offset 0 or right after a hard line break) map to the
321+
// first visible cluster after them, so start the line's text range where the previous line's
322+
// hard break ended (or at 0 for the first line) to keep line ranges contiguous.
323+
final int lineStart = lines.isEmpty ? 0 : lines.last.hardLineBreakRange.end;
324+
final ui.TextRange rawContentTextRange = _mapping.toTextRange(contentClusterRange);
325+
final contentTextRange = ui.TextRange(
326+
start: math.min(lineStart, rawContentTextRange.start),
327+
end: rawContentTextRange.end,
328+
);
316329
final ui.TextRange whitespaceTextRange = _mapping.toTextRange(whitespaceClusterRange);
317330
final ui.TextRange hardlineTextRange = _mapping.toTextRange(hardlineClusterRange);
318331
// A line includes the trailing newline if it has a hard line break that
@@ -461,6 +474,11 @@ class TextLayout {
461474
bidiLineSpanTextRange.start,
462475
bidiLineSpanTextRange.end,
463476
);
477+
if (bidiLineSpanRange.isEmpty) {
478+
// A span slice containing only clusterless characters (such as a standalone U+00AD or
479+
// U+200B span) has no clusters to measure or paint on this line.
480+
continue;
481+
}
464482
final LineBlock block;
465483
final double blockWidth;
466484
if (span is PlaceholderSpan) {
@@ -920,10 +938,12 @@ class TextLayout {
920938

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

929949
if (right < offset.dx) {
@@ -979,7 +999,12 @@ class TextLayout {
979999
// We found the line but not the block because the offset is to the right of all blocks in this line.
9801000
final LineBlock? lastVisualBlockInLine = line.visualBlocks.lastOrNull;
9811001
if (lastVisualBlockInLine is SoftHyphenBlock) {
982-
return ui.TextPosition(offset: lastVisualBlockInLine.textRange.end);
1002+
// Offsets past a trailing soft hyphen map to the break boundary at the end of this line,
1003+
// so use upstream affinity to keep the position on the hyphenated line.
1004+
return ui.TextPosition(
1005+
offset: lastVisualBlockInLine.textRange.end,
1006+
affinity: ui.TextAffinity.upstream,
1007+
);
9831008
}
9841009
return lastVisualBlockInLine == null
9851010
? ui.TextPosition(offset: line.allLineTextRange.start, affinity: ui.TextAffinity.upstream)
@@ -1202,7 +1227,13 @@ class _TextClusterMapping {
12021227
// starts at the next cluster.
12031228
startClusterIndex += 1;
12041229
}
1205-
return ClusterRange(start: startClusterIndex, end: toClusterIndex(end - 1) + 1);
1230+
int endClusterIndex = toClusterIndex(end - 1) + 1;
1231+
if (_clusters[endClusterIndex - 1].start >= end) {
1232+
// The code unit at (end - 1) has no cluster of its own at the start of the text and is
1233+
// mapped to the first cluster after it, so the range ends before that cluster.
1234+
endClusterIndex -= 1;
1235+
}
1236+
return ClusterRange(start: startClusterIndex, end: endClusterIndex);
12061237
}
12071238

12081239
ui.TextRange toTextRange(ClusterRange clusterRange) {

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/wrapper.dart‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,20 @@ class _LineBuilder {
319319
}
320320

321321
void markSoftLineBreak(int index, {SoftHyphen? softHyphen}) {
322+
// A soft hyphen immediately after whitespace (when there is no pending word before the break)
323+
// breaks at the preceding whitespace and never draws a hyphen.
324+
final SoftHyphen? effectiveSoftHyphen = hasPendingText ? softHyphen : null;
325+
322326
// Like SkParagraph, a break at a soft hyphen only fits if the hyphen fits too.
323327
// If it does not, the line ends at the last earlier break that fits instead, if there
324328
// is one; otherwise the hyphen overflows.
325-
final double reservedWidth = softHyphen == null || _mayEllipsize ? 0.0 : softHyphen.width;
329+
final double reservedWidth = effectiveSoftHyphen == null || _mayEllipsize
330+
? 0.0
331+
: effectiveSoftHyphen.width;
326332
_isBreakTooWide = reservedWidth > 0.0 && _fallbackBreak != null && !canFit(reservedWidth);
327333

328334
_hasSoftLineBreak = true;
329-
_breakSoftHyphen = softHyphen;
335+
_breakSoftHyphen = effectiveSoftHyphen;
330336
_breakIndex = index;
331337

332338
if (hasPendingText) {
@@ -335,7 +341,7 @@ class _LineBuilder {
335341
assert(_newlineEnd == index);
336342
}
337343

338-
consumePendingText(hyphenWidth: softHyphen?.width ?? 0.0);
344+
consumePendingText(hyphenWidth: effectiveSoftHyphen?.width ?? 0.0);
339345
assert(_newlineEnd == index);
340346

341347
if (!_isBreakTooWide) {
@@ -423,14 +429,14 @@ class _LineBuilder {
423429

424430
// TODO(mdebbar): Can we inline this in `markSoftLineBreak` and use that everywhere?
425431
void consumePendingText({double hyphenWidth = 0.0}) {
426-
// Update min intrinsic width. Like CSS min-content, a piece of a word that ends at a soft
427-
// hyphen counts with its hyphen.
428-
_minIntrinsicWidth = math.max(_minIntrinsicWidth, _widthPendingText + hyphenWidth);
429-
430432
if (!hasPendingText) {
431433
return;
432434
}
433435

436+
// Update min intrinsic width. Like CSS min-content, a piece of a word that ends at a soft
437+
// hyphen counts with its hyphen.
438+
_minIntrinsicWidth = math.max(_minIntrinsicWidth, _widthPendingText + hyphenWidth);
439+
434440
_whitespaceStart = _pendingTextEnd;
435441
_whitespaceEnd = _pendingTextEnd;
436442
_newlineEnd = _pendingTextEnd;

0 commit comments

Comments
 (0)