Skip to content

Commit 8f0ff93

Browse files
committed
[web_paragraph] Render soft hyphens
Draws a hyphen at the end of a line that breaks at a soft hyphen (U+00AD), unless ParagraphStyle.hyphens is Hyphens.hidden. This matches SkParagraph, which CanvasKit, Skwasm and the native engine use: * The hyphen is drawn only where the line breaks at the soft hyphen: not at the end of the text, before a space or newline, or on a line with an ellipsis. * A soft hyphen break only fits if the hyphen fits too. Otherwise the line breaks at the last earlier break that fits, or the hyphen overflows if there is none. * minIntrinsicWidth and longestLine include the hyphen; maxIntrinsicWidth and LineMetrics.width do not. * The hyphen takes the soft hyphen's style, is shaped in the direction of the text it ends, and goes at the end of the line in the paragraph's direction. * Selection boxes, glyph info and hit testing for the soft hyphen use the drawn hyphen. Fixes #193506.
1 parent 3b21127 commit 8f0ff93

4 files changed

Lines changed: 725 additions & 21 deletions

File tree

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

Lines changed: 182 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class TextLayout {
3939
List<WebCluster> ellipsisClusters = <WebCluster>[];
4040
int? _ellipsisBidiLevel;
4141

42+
// The hyphen for each soft hyphen break, keyed by the index of the cluster
43+
// after the soft hyphen. Shaped on first use and kept across layouts.
44+
final _softHyphens = <int, SoftHyphen?>{};
45+
4246
void performLayout(double width) {
4347
// TODO(jlavrova): IMPLEMENT:
4448
// I suggest with go with the general code flow for an empty text
@@ -157,6 +161,56 @@ class TextLayout {
157161
return _ellipsisBidiLevel!;
158162
}
159163

164+
/// Returns the hyphen drawn if a line breaks before the cluster at
165+
/// [clusterIndex], or null if that break is not at a soft hyphen (U+00AD)
166+
/// or no hyphen is drawn there.
167+
///
168+
/// Like SkParagraph, no hyphen is drawn for a soft hyphen at the end of the
169+
/// text, or when [WebParagraphStyle.softHyphenString] is null or empty.
170+
SoftHyphen? softHyphenBefore(int clusterIndex) {
171+
// The last cluster is the artificial end of text.
172+
if (clusterIndex <= 0 || clusterIndex >= allClusters.length - 1) {
173+
return null;
174+
}
175+
// The browser gives the soft hyphen no cluster of its own, so check the
176+
// code unit just before the break.
177+
final int softHyphenOffset = allClusters[clusterIndex].start - 1;
178+
if (paragraph.text.codeUnitAt(softHyphenOffset) != 0x00AD) {
179+
return null;
180+
}
181+
return _softHyphens.putIfAbsent(
182+
clusterIndex,
183+
() => _shapeSoftHyphen(clusterIndex, softHyphenOffset),
184+
);
185+
}
186+
187+
SoftHyphen? _shapeSoftHyphen(int clusterIndex, int softHyphenOffset) {
188+
final String? hyphen = paragraph.paragraphStyle.softHyphenString;
189+
if (hyphen == null || hyphen.isEmpty) {
190+
return null;
191+
}
192+
// The hyphen takes the style of the soft hyphen, and the direction of the
193+
// text it ends.
194+
final ParagraphSpan span = paragraph.spans.firstWhere(
195+
(ParagraphSpan span) => span.start <= softHyphenOffset && softHyphenOffset < span.end,
196+
);
197+
final int bidiLevel = bidiRuns
198+
.firstWhere((BidiRun run) => run.clusterRange.overlapsWith(clusterIndex - 1, clusterIndex))
199+
.bidiLevel;
200+
final hyphenSpan = TextSpan(
201+
start: 0,
202+
end: hyphen.length,
203+
style: span.style,
204+
text: hyphen,
205+
textDirection: bidiLevel.isEven ? ui.TextDirection.ltr : ui.TextDirection.rtl,
206+
);
207+
return SoftHyphen(
208+
hyphenSpan,
209+
bidiLevel,
210+
ui.TextRange(start: softHyphenOffset, end: softHyphenOffset + 1),
211+
);
212+
}
213+
160214
void extractBidiRuns() {
161215
assert(bidiRuns.isEmpty);
162216

@@ -223,6 +277,7 @@ class TextLayout {
223277
double top, {
224278
required bool isSyntheticEmptyLine,
225279
required bool isLastLine,
280+
SoftHyphen? softHyphen,
226281
}) {
227282
assert(contentClusterRange.end == whitespaceClusterRange.start);
228283
assert(whitespaceClusterRange.end == hardlineClusterRange.start);
@@ -336,11 +391,21 @@ class TextLayout {
336391
// and shift all runs for this line so this first cluster starts from 0
337392
// Break the line into the blocks that belong to the same bidi run (monodirectional text) and to the same style block (the same text metrics)
338393
var trailingSpacesWidth = 0.0;
339-
// In case we attach the ellipsis block at the left (RTL paragraph) we need to reserve its width
340-
double blockShiftFromLineStart =
341-
ellipsisBlock != null && paragraph.paragraphStyle.textDirection == ui.TextDirection.rtl
342-
? ellipsisBlock.advance.width
343-
: 0.0;
394+
// Like SkParagraph, a hyphen at a soft hyphen break goes at the end of the line in the
395+
// paragraph's direction, not next to the text it ends. A line with an ellipsis has none.
396+
final SoftHyphen? lineSoftHyphen = ellipsisBlock == null ? softHyphen : null;
397+
final isRtlParagraph = paragraph.paragraphStyle.textDirection == ui.TextDirection.rtl;
398+
// In an RTL paragraph the hyphen block goes at the left edge of the line, so it can be
399+
// built now.
400+
final SoftHyphenBlock? rtlSoftHyphenBlock = isRtlParagraph && lineSoftHyphen != null
401+
? SoftHyphenBlock(lineSoftHyphen, 0.0)
402+
: null;
403+
// In case we attach the ellipsis or hyphen block at the left (RTL paragraph) we need to
404+
// reserve its width
405+
var blockShiftFromLineStart = 0.0;
406+
if (isRtlParagraph) {
407+
blockShiftFromLineStart = (ellipsisBlock ?? rtlSoftHyphenBlock)?.advance.width ?? 0.0;
408+
}
344409
for (final bidiRun in lineVisualRuns) {
345410
// We (almost always true) assume that trailing whitespaces do not affect the line height.
346411
// Let's keep it as is.
@@ -483,6 +548,22 @@ class TextLayout {
483548
line.updateBoundingBox(ellipsisBlock);
484549
}
485550

551+
// Add the hyphen block if any
552+
if (lineSoftHyphen != null) {
553+
final SoftHyphenBlock softHyphenBlock;
554+
if (rtlSoftHyphenBlock != null) {
555+
// The width is already reserved at the beginning of the line
556+
softHyphenBlock = rtlSoftHyphenBlock;
557+
line.visualBlocks.insert(0, softHyphenBlock);
558+
} else {
559+
softHyphenBlock = SoftHyphenBlock(lineSoftHyphen, blockShiftFromLineStart);
560+
line.visualBlocks.add(softHyphenBlock);
561+
blockShiftFromLineStart += softHyphenBlock.advance.width;
562+
}
563+
line.updateBoundingBox(softHyphenBlock);
564+
line.softHyphenWidth = softHyphenBlock.advance.width;
565+
}
566+
486567
// Now when we calculated all line metrics we have to correct placeholders that depend on it
487568
for (final LineBlock block in line.visualBlocks) {
488569
if (block is! PlaceholderBlock) {
@@ -638,7 +719,8 @@ class TextLayout {
638719
continue;
639720
}
640721
ui.Rect firstRect = block.advance;
641-
if (block is! PlaceholderBlock) {
722+
// A hyphen block covers the whole drawn hyphen for its one soft hyphen.
723+
if (block is! PlaceholderBlock && block is! SoftHyphenBlock) {
642724
// We need to calculate the intersect rectangle
643725
firstRect = (block.span as TextSpan).getTextRangeSelectionInBlock(block, intersect);
644726
}
@@ -836,6 +918,14 @@ class TextLayout {
836918
final double left = block.advance.left + lineShift;
837919
final double right = block.advance.right + lineShift;
838920

921+
if (block is SoftHyphenBlock) {
922+
// Like SkParagraph, the hyphen maps to the position after the soft hyphen.
923+
if (right < offset.dx) {
924+
continue;
925+
}
926+
return ui.TextPosition(offset: block.textRange.end);
927+
}
928+
839929
if (right < offset.dx) {
840930
// We are not there yet; we need a block containing the offset (or the closest to it)
841931
continue;
@@ -888,6 +978,9 @@ class TextLayout {
888978
}
889979
// We found the line but not the block because the offset is to the right of all blocks in this line.
890980
final LineBlock? lastVisualBlockInLine = line.visualBlocks.lastOrNull;
981+
if (lastVisualBlockInLine is SoftHyphenBlock) {
982+
return ui.TextPosition(offset: lastVisualBlockInLine.textRange.end);
983+
}
891984
return lastVisualBlockInLine == null
892985
? ui.TextPosition(offset: line.allLineTextRange.start, affinity: ui.TextAffinity.upstream)
893986
: lastVisualBlockInLine.isLtr
@@ -914,6 +1007,20 @@ class TextLayout {
9141007
// We cannot assume clusters go sequentially because of bidi reshuffling
9151008
// but we don't care about the order because we only look for a cluster that contains the offset
9161009
for (final LineBlock visualBlock in line.visualBlocks) {
1010+
if (visualBlock is SoftHyphenBlock) {
1011+
// A soft hyphen at a line break has no cluster; its glyph is the drawn hyphen.
1012+
if (visualBlock.textRange.start == codeUnitOffset) {
1013+
return ui.GlyphInfo(
1014+
visualBlock.advance.translate(
1015+
line.advance.left + line.formattingShift,
1016+
line.advance.top + line.fontBoundingBoxAscent,
1017+
),
1018+
visualBlock.textRange,
1019+
visualBlock.isLtr ? ui.TextDirection.ltr : ui.TextDirection.rtl,
1020+
);
1021+
}
1022+
continue;
1023+
}
9171024
for (
9181025
int start = visualBlock.clusterRange.start;
9191026
start < visualBlock.clusterRange.end;
@@ -1463,6 +1570,69 @@ class EllipsisBlock extends TextBlock {
14631570
);
14641571
}
14651572

1573+
/// The hyphen drawn at a line break at a soft hyphen (U+00AD).
1574+
class SoftHyphen {
1575+
SoftHyphen(this.span, this.bidiLevel, this.textRange);
1576+
1577+
/// The hyphen string, shaped in the soft hyphen's style.
1578+
final TextSpan span;
1579+
1580+
/// The bidi level of the text the hyphen ends.
1581+
final int bidiLevel;
1582+
1583+
/// The soft hyphen's range in the paragraph text.
1584+
final ui.TextRange textRange;
1585+
1586+
late final double width = span.advanceWidth()!;
1587+
}
1588+
1589+
/// A block for the hyphen drawn at the end of a line that breaks at a soft
1590+
/// hyphen (U+00AD).
1591+
///
1592+
/// The hyphen is shaped separately from the paragraph text, so the block owns
1593+
/// its clusters and covers no paragraph clusters. Its [textRange] is the soft
1594+
/// hyphen's, so that queries over the soft hyphen find the drawn hyphen, as in
1595+
/// SkParagraph.
1596+
class SoftHyphenBlock extends TextBlock {
1597+
factory SoftHyphenBlock(SoftHyphen softHyphen, double shiftFromLineStart) {
1598+
final List<TextCluster> clusters = softHyphen.span.extractClusters();
1599+
final TextCluster firstVisualCluster = softHyphen.bidiLevel.isEven
1600+
? clusters.first
1601+
: clusters.last;
1602+
return SoftHyphenBlock._(
1603+
softHyphen,
1604+
clusters,
1605+
shiftFromLineStart,
1606+
firstVisualCluster.advance.left,
1607+
);
1608+
}
1609+
1610+
SoftHyphenBlock._(
1611+
SoftHyphen softHyphen,
1612+
this.clusters,
1613+
double shiftFromLineStart,
1614+
double shiftFromSpanStart,
1615+
) : super(
1616+
softHyphen.span,
1617+
softHyphen.bidiLevel,
1618+
ClusterRange.collapsed(0),
1619+
softHyphen.textRange,
1620+
// Relative to the hyphen's own span: the whole hyphen string.
1621+
ui.TextRange(start: 0, end: softHyphen.span.text.length),
1622+
shiftFromLineStart,
1623+
shiftFromSpanStart,
1624+
);
1625+
1626+
final List<TextCluster> clusters;
1627+
1628+
@override
1629+
Iterable<(WebCluster, bool)> getTextClustersInVisualOrder(TextLayout layout) sync* {
1630+
for (final TextCluster cluster in isLtr ? clusters : clusters.reversed) {
1631+
yield (cluster, isLtr);
1632+
}
1633+
}
1634+
}
1635+
14661636
class TextLine {
14671637
TextLine(
14681638
this.textClusterRange,
@@ -1489,7 +1659,8 @@ class TextLine {
14891659
// It was not implemented in SkParagraph either; kept it as is
14901660
unscaledAscent: fontBoundingBoxAscent,
14911661
height: advance.height,
1492-
width: advance.width,
1662+
// Like SkParagraph, the width leaves out a hyphen at a soft hyphen break.
1663+
width: advance.width - softHyphenWidth,
14931664
// TODO(jlavrova): This should be set to `formattingShift`, right?
14941665
// `advance.left` is always zero.
14951666
left: advance.left,
@@ -1562,6 +1733,10 @@ class TextLine {
15621733
double formattingShift = 0.0; // For centered or right aligned text
15631734
late final double trailingSpacesWidth;
15641735

1736+
/// The width of the hyphen drawn at the end of the line, if it breaks at a
1737+
/// soft hyphen. It is included in [advance].
1738+
double softHyphenWidth = 0.0;
1739+
15651740
double get fullWidth => advance.width + formattingShift + trailingSpacesWidth;
15661741

15671742
List<LineBlock> visualBlocks = <LineBlock>[];

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@ class WebParagraphStyle implements ui.ParagraphStyle {
6464

6565
final ui.TextHeightBehavior? textHeightBehavior;
6666

67-
// TODO(dbebawy): honor hyphens in the pure-Dart line-breaker/painter (no
68-
// soft-hyphen glyph emitted at a break yet). Accepted and stored for now.
69-
// https://github.com/flutter/flutter/issues/193506
7067
final ui.Hyphens? hyphens;
7168

69+
/// The string drawn at the end of a line that breaks at a soft hyphen
70+
/// (U+00AD), or null if none is drawn.
71+
// TODO(dbebawy): Use hyphenCharacter once dart:ui has it.
72+
// https://github.com/flutter/flutter/issues/189617
73+
String? get softHyphenString => hyphens == ui.Hyphens.hidden ? null : '-';
74+
7275
WebStrutStyle? get strutStyle => _strutStyle;
7376
final WebStrutStyle? _strutStyle;
7477

0 commit comments

Comments
 (0)