Skip to content

Commit c3d96d6

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 #193506. Co-authored-by: Julia Lavrova <[email protected]>
1 parent b9b2802 commit c3d96d6

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

@@ -226,6 +280,7 @@ class TextLayout {
226280
double top, {
227281
required bool isSyntheticEmptyLine,
228282
required bool isLastLine,
283+
SoftHyphen? softHyphen,
229284
}) {
230285
assert(contentClusterRange.end == whitespaceClusterRange.start);
231286
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.
@@ -503,6 +568,22 @@ class TextLayout {
503568
line.updateBoundingBox(ellipsisBlock);
504569
}
505570

571+
// Add the hyphen block if any
572+
if (lineSoftHyphen != null) {
573+
final SoftHyphenBlock softHyphenBlock;
574+
if (rtlSoftHyphenBlock != null) {
575+
// The width is already reserved at the beginning of the line
576+
softHyphenBlock = rtlSoftHyphenBlock;
577+
line.visualBlocks.insert(0, softHyphenBlock);
578+
} else {
579+
softHyphenBlock = SoftHyphenBlock(lineSoftHyphen, blockShiftFromLineStart);
580+
line.visualBlocks.add(softHyphenBlock);
581+
blockShiftFromLineStart += softHyphenBlock.advance.width;
582+
}
583+
line.updateBoundingBox(softHyphenBlock);
584+
line.softHyphenWidth = softHyphenBlock.advance.width;
585+
}
586+
506587
// Now when we calculated all line metrics we have to correct placeholders that depend on it
507588
for (final LineBlock block in line.visualBlocks) {
508589
if (block is! PlaceholderBlock) {
@@ -664,7 +745,8 @@ class TextLayout {
664745
continue;
665746
}
666747
ui.Rect firstRect = block.advance;
667-
if (block is! PlaceholderBlock) {
748+
// A hyphen block covers the whole drawn hyphen for its one soft hyphen.
749+
if (block is! PlaceholderBlock && block is! SoftHyphenBlock) {
668750
// We need to calculate the intersect rectangle
669751
firstRect = (block.span as TextSpan).getTextRangeSelectionInBlock(block, intersect);
670752
}
@@ -862,6 +944,16 @@ class TextLayout {
862944
final double left = block.advance.left + lineShift;
863945
final double right = block.advance.right + lineShift;
864946

947+
if (block is SoftHyphenBlock) {
948+
// Like SkParagraph, the hyphen maps to the position after the soft hyphen.
949+
// Because that offset is also the start of the next line, use upstream affinity so
950+
// line-boundary and closest-glyph queries keep the position on the hyphenated line.
951+
if (right < offset.dx) {
952+
continue;
953+
}
954+
return ui.TextPosition(offset: block.textRange.end, affinity: ui.TextAffinity.upstream);
955+
}
956+
865957
if (right < offset.dx) {
866958
// We are not there yet; we need a block containing the offset (or the closest to it)
867959
continue;
@@ -914,6 +1006,14 @@ class TextLayout {
9141006
}
9151007
// We found the line but not the block because the offset is to the right of all blocks in this line.
9161008
final LineBlock? lastVisualBlockInLine = line.visualBlocks.lastOrNull;
1009+
if (lastVisualBlockInLine is SoftHyphenBlock) {
1010+
// Offsets past a trailing soft hyphen map to the break boundary at the end of this line,
1011+
// so use upstream affinity to keep the position on the hyphenated line.
1012+
return ui.TextPosition(
1013+
offset: lastVisualBlockInLine.textRange.end,
1014+
affinity: ui.TextAffinity.upstream,
1015+
);
1016+
}
9171017
return lastVisualBlockInLine == null
9181018
? ui.TextPosition(offset: line.allLineTextRange.start, affinity: ui.TextAffinity.upstream)
9191019
: lastVisualBlockInLine.isLtr
@@ -940,6 +1040,20 @@ class TextLayout {
9401040
// We cannot assume clusters go sequentially because of bidi reshuffling
9411041
// but we don't care about the order because we only look for a cluster that contains the offset
9421042
for (final LineBlock visualBlock in line.visualBlocks) {
1043+
if (visualBlock is SoftHyphenBlock) {
1044+
// A soft hyphen at a line break has no cluster; its glyph is the drawn hyphen.
1045+
if (visualBlock.textRange.start == codeUnitOffset) {
1046+
return ui.GlyphInfo(
1047+
visualBlock.advance.translate(
1048+
line.advance.left + line.formattingShift,
1049+
line.advance.top + line.fontBoundingBoxAscent,
1050+
),
1051+
visualBlock.textRange,
1052+
visualBlock.isLtr ? ui.TextDirection.ltr : ui.TextDirection.rtl,
1053+
);
1054+
}
1055+
continue;
1056+
}
9431057
for (
9441058
int start = visualBlock.clusterRange.start;
9451059
start < visualBlock.clusterRange.end;
@@ -1495,6 +1609,69 @@ class EllipsisBlock extends TextBlock {
14951609
);
14961610
}
14971611

1612+
/// The hyphen drawn at a line break at a soft hyphen (U+00AD).
1613+
class SoftHyphen {
1614+
SoftHyphen(this.span, this.bidiLevel, this.textRange);
1615+
1616+
/// The hyphen string, shaped in the soft hyphen's style.
1617+
final TextSpan span;
1618+
1619+
/// The bidi level of the text the hyphen ends.
1620+
final int bidiLevel;
1621+
1622+
/// The soft hyphen's range in the paragraph text.
1623+
final ui.TextRange textRange;
1624+
1625+
late final double width = span.advanceWidth()!;
1626+
}
1627+
1628+
/// A block for the hyphen drawn at the end of a line that breaks at a soft
1629+
/// hyphen (U+00AD).
1630+
///
1631+
/// The hyphen is shaped separately from the paragraph text, so the block owns
1632+
/// its clusters and covers no paragraph clusters. Its [textRange] is the soft
1633+
/// hyphen's, so that queries over the soft hyphen find the drawn hyphen, as in
1634+
/// SkParagraph.
1635+
class SoftHyphenBlock extends TextBlock {
1636+
factory SoftHyphenBlock(SoftHyphen softHyphen, double shiftFromLineStart) {
1637+
final List<TextCluster> clusters = softHyphen.span.extractClusters();
1638+
final TextCluster firstVisualCluster = softHyphen.bidiLevel.isEven
1639+
? clusters.first
1640+
: clusters.last;
1641+
return SoftHyphenBlock._(
1642+
softHyphen,
1643+
clusters,
1644+
shiftFromLineStart,
1645+
firstVisualCluster.advance.left,
1646+
);
1647+
}
1648+
1649+
SoftHyphenBlock._(
1650+
SoftHyphen softHyphen,
1651+
this.clusters,
1652+
double shiftFromLineStart,
1653+
double shiftFromSpanStart,
1654+
) : super(
1655+
softHyphen.span,
1656+
softHyphen.bidiLevel,
1657+
ClusterRange.collapsed(0),
1658+
softHyphen.textRange,
1659+
// Relative to the hyphen's own span: the whole hyphen string.
1660+
ui.TextRange(start: 0, end: softHyphen.span.text.length),
1661+
shiftFromLineStart,
1662+
shiftFromSpanStart,
1663+
);
1664+
1665+
final List<TextCluster> clusters;
1666+
1667+
@override
1668+
Iterable<(WebCluster, bool)> getTextClustersInVisualOrder(TextLayout layout) sync* {
1669+
for (final TextCluster cluster in isLtr ? clusters : clusters.reversed) {
1670+
yield (cluster, isLtr);
1671+
}
1672+
}
1673+
}
1674+
14981675
class TextLine {
14991676
TextLine(
15001677
this.textClusterRange,
@@ -1521,7 +1698,8 @@ class TextLine {
15211698
// It was not implemented in SkParagraph either; kept it as is
15221699
unscaledAscent: fontBoundingBoxAscent,
15231700
height: advance.height,
1524-
width: advance.width,
1701+
// Like SkParagraph, the width leaves out a hyphen at a soft hyphen break.
1702+
width: advance.width - softHyphenWidth,
15251703
// TODO(jlavrova): This should be set to `formattingShift`, right?
15261704
// `advance.left` is always zero.
15271705
left: advance.left,
@@ -1594,6 +1772,10 @@ class TextLine {
15941772
double formattingShift = 0.0; // For centered or right aligned text
15951773
late final double trailingSpacesWidth;
15961774

1775+
/// The width of the hyphen drawn at the end of the line, if it breaks at a
1776+
/// soft hyphen. It is included in [advance].
1777+
double softHyphenWidth = 0.0;
1778+
15971779
double get fullWidth => advance.width + formattingShift + trailingSpacesWidth;
15981780

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