@@ -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
@@ -230,6 +284,7 @@ class TextLayout {
230284 double top, {
231285 required bool isSyntheticEmptyLine,
232286 required bool isLastLine,
287+ SoftHyphen ? softHyphen,
233288 }) {
234289 assert (contentClusterRange.end == whitespaceClusterRange.start);
235290 assert (whitespaceClusterRange.end == hardlineClusterRange.start);
@@ -353,11 +408,21 @@ class TextLayout {
353408 // and shift all runs for this line so this first cluster starts from 0
354409 // 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)
355410 var trailingSpacesWidth = 0.0 ;
356- // In case we attach the ellipsis block at the left (RTL paragraph) we need to reserve its width
357- double blockShiftFromLineStart =
358- ellipsisBlock != null && paragraph.paragraphStyle.textDirection == ui.TextDirection .rtl
359- ? ellipsisBlock.advance.width
360- : 0.0 ;
411+ // Like SkParagraph, a hyphen at a soft hyphen break goes at the end of the line in the
412+ // paragraph's direction, not next to the text it ends. A line with an ellipsis has none.
413+ final SoftHyphen ? lineSoftHyphen = ellipsisBlock == null ? softHyphen : null ;
414+ final isRtlParagraph = paragraph.paragraphStyle.textDirection == ui.TextDirection .rtl;
415+ // In an RTL paragraph the hyphen block goes at the left edge of the line, so it can be
416+ // built now.
417+ final SoftHyphenBlock ? rtlSoftHyphenBlock = isRtlParagraph && lineSoftHyphen != null
418+ ? SoftHyphenBlock (lineSoftHyphen, 0.0 )
419+ : null ;
420+ // In case we attach the ellipsis or hyphen block at the left (RTL paragraph) we need to
421+ // reserve its width
422+ var blockShiftFromLineStart = 0.0 ;
423+ if (isRtlParagraph) {
424+ blockShiftFromLineStart = (ellipsisBlock ?? rtlSoftHyphenBlock)? .advance.width ?? 0.0 ;
425+ }
361426 for (final bidiRun in lineVisualRuns) {
362427 // We (almost always true) assume that trailing whitespaces do not affect the line height.
363428 // Let's keep it as is.
@@ -505,6 +570,22 @@ class TextLayout {
505570 line.updateBoundingBox (ellipsisBlock);
506571 }
507572
573+ // Add the hyphen block if any
574+ if (lineSoftHyphen != null ) {
575+ final SoftHyphenBlock softHyphenBlock;
576+ if (rtlSoftHyphenBlock != null ) {
577+ // The width is already reserved at the beginning of the line
578+ softHyphenBlock = rtlSoftHyphenBlock;
579+ line.visualBlocks.insert (0 , softHyphenBlock);
580+ } else {
581+ softHyphenBlock = SoftHyphenBlock (lineSoftHyphen, blockShiftFromLineStart);
582+ line.visualBlocks.add (softHyphenBlock);
583+ blockShiftFromLineStart += softHyphenBlock.advance.width;
584+ }
585+ line.updateBoundingBox (softHyphenBlock);
586+ line.softHyphenWidth = softHyphenBlock.advance.width;
587+ }
588+
508589 // Now when we calculated all line metrics we have to correct placeholders that depend on it
509590 for (final LineBlock block in line.visualBlocks) {
510591 if (block is ! PlaceholderBlock ) {
@@ -666,7 +747,8 @@ class TextLayout {
666747 continue ;
667748 }
668749 ui.Rect firstRect = block.advance;
669- if (block is ! PlaceholderBlock ) {
750+ // A hyphen block covers the whole drawn hyphen for its one soft hyphen.
751+ if (block is ! PlaceholderBlock && block is ! SoftHyphenBlock ) {
670752 // We need to calculate the intersect rectangle
671753 firstRect = (block.span as TextSpan ).getTextRangeSelectionInBlock (block, intersect);
672754 }
@@ -864,6 +946,16 @@ class TextLayout {
864946 final double left = block.advance.left + lineShift;
865947 final double right = block.advance.right + lineShift;
866948
949+ if (block is SoftHyphenBlock ) {
950+ // Like SkParagraph, the hyphen maps to the position after the soft hyphen.
951+ // Because that offset is also the start of the next line, use upstream affinity so
952+ // line-boundary and closest-glyph queries keep the position on the hyphenated line.
953+ if (right < offset.dx) {
954+ continue ;
955+ }
956+ return ui.TextPosition (offset: block.textRange.end, affinity: ui.TextAffinity .upstream);
957+ }
958+
867959 if (right < offset.dx) {
868960 // We are not there yet; we need a block containing the offset (or the closest to it)
869961 continue ;
@@ -916,6 +1008,14 @@ class TextLayout {
9161008 }
9171009 // We found the line but not the block because the offset is to the right of all blocks in this line.
9181010 final LineBlock ? lastVisualBlockInLine = line.visualBlocks.lastOrNull;
1011+ if (lastVisualBlockInLine is SoftHyphenBlock ) {
1012+ // Offsets past a trailing soft hyphen map to the break boundary at the end of this line,
1013+ // so use upstream affinity to keep the position on the hyphenated line.
1014+ return ui.TextPosition (
1015+ offset: lastVisualBlockInLine.textRange.end,
1016+ affinity: ui.TextAffinity .upstream,
1017+ );
1018+ }
9191019 return lastVisualBlockInLine == null
9201020 ? ui.TextPosition (offset: line.allLineTextRange.start, affinity: ui.TextAffinity .upstream)
9211021 : lastVisualBlockInLine.isLtr
@@ -942,6 +1042,20 @@ class TextLayout {
9421042 // We cannot assume clusters go sequentially because of bidi reshuffling
9431043 // but we don't care about the order because we only look for a cluster that contains the offset
9441044 for (final LineBlock visualBlock in line.visualBlocks) {
1045+ if (visualBlock is SoftHyphenBlock ) {
1046+ // A soft hyphen at a line break has no cluster; its glyph is the drawn hyphen.
1047+ if (visualBlock.textRange.start == codeUnitOffset) {
1048+ return ui.GlyphInfo (
1049+ visualBlock.advance.translate (
1050+ line.advance.left + line.formattingShift,
1051+ line.advance.top + line.fontBoundingBoxAscent,
1052+ ),
1053+ visualBlock.textRange,
1054+ visualBlock.isLtr ? ui.TextDirection .ltr : ui.TextDirection .rtl,
1055+ );
1056+ }
1057+ continue ;
1058+ }
9451059 for (
9461060 int start = visualBlock.clusterRange.start;
9471061 start < visualBlock.clusterRange.end;
@@ -1497,6 +1611,69 @@ class EllipsisBlock extends TextBlock {
14971611 );
14981612}
14991613
1614+ /// The hyphen drawn at a line break at a soft hyphen (U+00AD).
1615+ class SoftHyphen {
1616+ SoftHyphen (this .span, this .bidiLevel, this .textRange);
1617+
1618+ /// The hyphen string, shaped in the soft hyphen's style.
1619+ final TextSpan span;
1620+
1621+ /// The bidi level of the text the hyphen ends.
1622+ final int bidiLevel;
1623+
1624+ /// The soft hyphen's range in the paragraph text.
1625+ final ui.TextRange textRange;
1626+
1627+ late final double width = span.advanceWidth ()! ;
1628+ }
1629+
1630+ /// A block for the hyphen drawn at the end of a line that breaks at a soft
1631+ /// hyphen (U+00AD).
1632+ ///
1633+ /// The hyphen is shaped separately from the paragraph text, so the block owns
1634+ /// its clusters and covers no paragraph clusters. Its [textRange] is the soft
1635+ /// hyphen's, so that queries over the soft hyphen find the drawn hyphen, as in
1636+ /// SkParagraph.
1637+ class SoftHyphenBlock extends TextBlock {
1638+ factory SoftHyphenBlock (SoftHyphen softHyphen, double shiftFromLineStart) {
1639+ final List <TextCluster > clusters = softHyphen.span.extractClusters ();
1640+ final TextCluster firstVisualCluster = softHyphen.bidiLevel.isEven
1641+ ? clusters.first
1642+ : clusters.last;
1643+ return SoftHyphenBlock ._(
1644+ softHyphen,
1645+ clusters,
1646+ shiftFromLineStart,
1647+ firstVisualCluster.advance.left,
1648+ );
1649+ }
1650+
1651+ SoftHyphenBlock ._(
1652+ SoftHyphen softHyphen,
1653+ this .clusters,
1654+ double shiftFromLineStart,
1655+ double shiftFromSpanStart,
1656+ ) : super (
1657+ softHyphen.span,
1658+ softHyphen.bidiLevel,
1659+ ClusterRange .collapsed (0 ),
1660+ softHyphen.textRange,
1661+ // Relative to the hyphen's own span: the whole hyphen string.
1662+ ui.TextRange (start: 0 , end: softHyphen.span.text.length),
1663+ shiftFromLineStart,
1664+ shiftFromSpanStart,
1665+ );
1666+
1667+ final List <TextCluster > clusters;
1668+
1669+ @override
1670+ Iterable <(WebCluster , bool )> getTextClustersInVisualOrder (TextLayout layout) sync * {
1671+ for (final TextCluster cluster in isLtr ? clusters : clusters.reversed) {
1672+ yield (cluster, isLtr);
1673+ }
1674+ }
1675+ }
1676+
15001677class TextLine {
15011678 TextLine (
15021679 this .textClusterRange,
@@ -1523,7 +1700,8 @@ class TextLine {
15231700 // It was not implemented in SkParagraph either; kept it as is
15241701 unscaledAscent: fontBoundingBoxAscent,
15251702 height: advance.height,
1526- width: advance.width,
1703+ // Like SkParagraph, the width leaves out a hyphen at a soft hyphen break.
1704+ width: advance.width - softHyphenWidth,
15271705 // TODO(jlavrova): This should be set to `formattingShift`, right?
15281706 // `advance.left` is always zero.
15291707 left: advance.left,
@@ -1596,6 +1774,10 @@ class TextLine {
15961774 double formattingShift = 0.0 ; // For centered or right aligned text
15971775 late final double trailingSpacesWidth;
15981776
1777+ /// The width of the hyphen drawn at the end of the line, if it breaks at a
1778+ /// soft hyphen. It is included in [advance] .
1779+ double softHyphenWidth = 0.0 ;
1780+
15991781 double get fullWidth => advance.width + formattingShift + trailingSpacesWidth;
16001782
16011783 List <LineBlock > visualBlocks = < LineBlock > [];
0 commit comments