Skip to content

Commit 6bbb470

Browse files
dbebawyRusino
andcommitted
[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. A soft hyphen right after a space breaks like the space, so it does not need room for a hyphen. * 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. Hit testing the hyphen gives the position after the soft hyphen with upstream affinity, so it stays on the hyphen's line. Fixes flutter#193506. Co-authored-by: Julia Lavrova <[email protected]>
1 parent 990aa34 commit 6bbb470

4 files changed

Lines changed: 797 additions & 22 deletions

File tree

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

Lines changed: 189 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

@@ -228,6 +282,7 @@ class TextLayout {
228282
double top, {
229283
required bool isSyntheticEmptyLine,
230284
required bool isLastLine,
285+
SoftHyphen? softHyphen,
231286
}) {
232287
assert(contentClusterRange.end == whitespaceClusterRange.start);
233288
assert(whitespaceClusterRange.end == hardlineClusterRange.start);
@@ -349,11 +404,21 @@ class TextLayout {
349404
// and shift all runs for this line so this first cluster starts from 0
350405
// 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)
351406
var trailingSpacesWidth = 0.0;
352-
// In case we attach the ellipsis block at the left (RTL paragraph) we need to reserve its width
353-
double blockShiftFromLineStart =
354-
ellipsisBlock != null && paragraph.paragraphStyle.textDirection == ui.TextDirection.rtl
355-
? ellipsisBlock.advance.width
356-
: 0.0;
407+
// Like SkParagraph, a hyphen at a soft hyphen break goes at the end of the line in the
408+
// paragraph's direction, not next to the text it ends. A line with an ellipsis has none.
409+
final SoftHyphen? lineSoftHyphen = ellipsisBlock == null ? softHyphen : null;
410+
final isRtlParagraph = paragraph.paragraphStyle.textDirection == ui.TextDirection.rtl;
411+
// In an RTL paragraph the hyphen block goes at the left edge of the line, so it can be
412+
// built now.
413+
final SoftHyphenBlock? rtlSoftHyphenBlock = isRtlParagraph && lineSoftHyphen != null
414+
? SoftHyphenBlock(lineSoftHyphen, 0.0)
415+
: null;
416+
// In case we attach the ellipsis or hyphen block at the left (RTL paragraph) we need to
417+
// reserve its width
418+
var blockShiftFromLineStart = 0.0;
419+
if (isRtlParagraph) {
420+
blockShiftFromLineStart = (ellipsisBlock ?? rtlSoftHyphenBlock)?.advance.width ?? 0.0;
421+
}
357422
for (final bidiRun in lineVisualRuns) {
358423
// We (almost always true) assume that trailing whitespaces do not affect the line height.
359424
// Let's keep it as is.
@@ -501,6 +566,22 @@ class TextLayout {
501566
line.updateBoundingBox(ellipsisBlock);
502567
}
503568

569+
// Add the hyphen block if any
570+
if (lineSoftHyphen != null) {
571+
final SoftHyphenBlock softHyphenBlock;
572+
if (rtlSoftHyphenBlock != null) {
573+
// The width is already reserved at the beginning of the line
574+
softHyphenBlock = rtlSoftHyphenBlock;
575+
line.visualBlocks.insert(0, softHyphenBlock);
576+
} else {
577+
softHyphenBlock = SoftHyphenBlock(lineSoftHyphen, blockShiftFromLineStart);
578+
line.visualBlocks.add(softHyphenBlock);
579+
blockShiftFromLineStart += softHyphenBlock.advance.width;
580+
}
581+
line.updateBoundingBox(softHyphenBlock);
582+
line.softHyphenWidth = softHyphenBlock.advance.width;
583+
}
584+
504585
// Now when we calculated all line metrics we have to correct placeholders that depend on it
505586
for (final LineBlock block in line.visualBlocks) {
506587
if (block is! PlaceholderBlock) {
@@ -656,7 +737,8 @@ class TextLayout {
656737
continue;
657738
}
658739
ui.Rect firstRect = block.advance;
659-
if (block is! PlaceholderBlock) {
740+
// A hyphen block covers the whole drawn hyphen for its one soft hyphen.
741+
if (block is! PlaceholderBlock && block is! SoftHyphenBlock) {
660742
// We need to calculate the intersect rectangle
661743
firstRect = (block.span as TextSpan).getTextRangeSelectionInBlock(block, intersect);
662744
}
@@ -854,6 +936,16 @@ class TextLayout {
854936
final double left = block.advance.left + lineShift;
855937
final double right = block.advance.right + lineShift;
856938

939+
if (block is SoftHyphenBlock) {
940+
// 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.
943+
if (right < offset.dx) {
944+
continue;
945+
}
946+
return ui.TextPosition(offset: block.textRange.end, affinity: ui.TextAffinity.upstream);
947+
}
948+
857949
if (right < offset.dx) {
858950
// We are not there yet; we need a block containing the offset (or the closest to it)
859951
continue;
@@ -906,6 +998,14 @@ class TextLayout {
906998
}
907999
// We found the line but not the block because the offset is to the right of all blocks in this line.
9081000
final LineBlock? lastVisualBlockInLine = line.visualBlocks.lastOrNull;
1001+
if (lastVisualBlockInLine is SoftHyphenBlock) {
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+
);
1008+
}
9091009
return lastVisualBlockInLine == null
9101010
? ui.TextPosition(offset: line.allLineTextRange.start, affinity: ui.TextAffinity.upstream)
9111011
: lastVisualBlockInLine.isLtr
@@ -932,6 +1032,20 @@ class TextLayout {
9321032
// We cannot assume clusters go sequentially because of bidi reshuffling
9331033
// but we don't care about the order because we only look for a cluster that contains the offset
9341034
for (final LineBlock visualBlock in line.visualBlocks) {
1035+
if (visualBlock is SoftHyphenBlock) {
1036+
// A soft hyphen at a line break has no cluster; its glyph is the drawn hyphen.
1037+
if (visualBlock.textRange.start == codeUnitOffset) {
1038+
return ui.GlyphInfo(
1039+
visualBlock.advance.translate(
1040+
line.advance.left + line.formattingShift,
1041+
line.advance.top + line.fontBoundingBoxAscent,
1042+
),
1043+
visualBlock.textRange,
1044+
visualBlock.isLtr ? ui.TextDirection.ltr : ui.TextDirection.rtl,
1045+
);
1046+
}
1047+
continue;
1048+
}
9351049
for (
9361050
int start = visualBlock.clusterRange.start;
9371051
start < visualBlock.clusterRange.end;
@@ -1487,6 +1601,69 @@ class EllipsisBlock extends TextBlock {
14871601
);
14881602
}
14891603

1604+
/// The hyphen drawn at a line break at a soft hyphen (U+00AD).
1605+
class SoftHyphen {
1606+
SoftHyphen(this.span, this.bidiLevel, this.textRange);
1607+
1608+
/// The hyphen string, shaped in the soft hyphen's style.
1609+
final TextSpan span;
1610+
1611+
/// The bidi level of the text the hyphen ends.
1612+
final int bidiLevel;
1613+
1614+
/// The soft hyphen's range in the paragraph text.
1615+
final ui.TextRange textRange;
1616+
1617+
late final double width = span.advanceWidth()!;
1618+
}
1619+
1620+
/// A block for the hyphen drawn at the end of a line that breaks at a soft
1621+
/// hyphen (U+00AD).
1622+
///
1623+
/// The hyphen is shaped separately from the paragraph text, so the block owns
1624+
/// its clusters and covers no paragraph clusters. Its [textRange] is the soft
1625+
/// hyphen's, so that queries over the soft hyphen find the drawn hyphen, as in
1626+
/// SkParagraph.
1627+
class SoftHyphenBlock extends TextBlock {
1628+
factory SoftHyphenBlock(SoftHyphen softHyphen, double shiftFromLineStart) {
1629+
final List<TextCluster> clusters = softHyphen.span.extractClusters();
1630+
final TextCluster firstVisualCluster = softHyphen.bidiLevel.isEven
1631+
? clusters.first
1632+
: clusters.last;
1633+
return SoftHyphenBlock._(
1634+
softHyphen,
1635+
clusters,
1636+
shiftFromLineStart,
1637+
firstVisualCluster.advance.left,
1638+
);
1639+
}
1640+
1641+
SoftHyphenBlock._(
1642+
SoftHyphen softHyphen,
1643+
this.clusters,
1644+
double shiftFromLineStart,
1645+
double shiftFromSpanStart,
1646+
) : super(
1647+
softHyphen.span,
1648+
softHyphen.bidiLevel,
1649+
ClusterRange.collapsed(0),
1650+
softHyphen.textRange,
1651+
// Relative to the hyphen's own span: the whole hyphen string.
1652+
ui.TextRange(start: 0, end: softHyphen.span.text.length),
1653+
shiftFromLineStart,
1654+
shiftFromSpanStart,
1655+
);
1656+
1657+
final List<TextCluster> clusters;
1658+
1659+
@override
1660+
Iterable<(WebCluster, bool)> getTextClustersInVisualOrder(TextLayout layout) sync* {
1661+
for (final TextCluster cluster in isLtr ? clusters : clusters.reversed) {
1662+
yield (cluster, isLtr);
1663+
}
1664+
}
1665+
}
1666+
14901667
class TextLine {
14911668
TextLine(
14921669
this.textClusterRange,
@@ -1513,7 +1690,8 @@ class TextLine {
15131690
// It was not implemented in SkParagraph either; kept it as is
15141691
unscaledAscent: fontBoundingBoxAscent,
15151692
height: advance.height,
1516-
width: advance.width,
1693+
// Like SkParagraph, the width leaves out a hyphen at a soft hyphen break.
1694+
width: advance.width - softHyphenWidth,
15171695
// TODO(jlavrova): This should be set to `formattingShift`, right?
15181696
// `advance.left` is always zero.
15191697
left: advance.left,
@@ -1586,6 +1764,10 @@ class TextLine {
15861764
double formattingShift = 0.0; // For centered or right aligned text
15871765
late final double trailingSpacesWidth;
15881766

1767+
/// The width of the hyphen drawn at the end of the line, if it breaks at a
1768+
/// soft hyphen. It is included in [advance].
1769+
double softHyphenWidth = 0.0;
1770+
15891771
double get fullWidth => advance.width + formattingShift + trailingSpacesWidth;
15901772

15911773
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)