Skip to content

Commit 3b21127

Browse files
committed
[web_paragraph] Map code units without a cluster to a cluster
Chrome gives some invisible characters, such as a soft hyphen (U+00AD) or a zero width space (U+200B), no text cluster. Their code units were left mapped to cluster 0, so a text range that included one could map to a cluster range that went backwards. Laying out a paragraph that ends with one of these characters failed the assertion in _TextClusterMapping.toTextRange. Map each of those code units to the cluster before it, or to the first cluster at the start of the text. A cluster range that starts at such a code unit then starts at the next cluster, so a span that starts with one covers only its own clusters.
1 parent 950bf4e commit 3b21127

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,23 @@ class TextLayout {
101101
allClusters.addAll(span.extractClusters());
102102
}
103103
allClusters.sort((a, b) => a.start.compareTo(b.start));
104+
// The browser gives some invisible characters, such as a soft hyphen (U+00AD) or a zero
105+
// width space (U+200B), no cluster of their own. Map each of those code units to the
106+
// cluster before it (or the first cluster, at the start of the text), so that text ranges
107+
// that include them still map to cluster ranges in order.
108+
var unmappedStart = 0;
104109
for (var i = 0; i < allClusters.length; ++i) {
105110
final WebCluster cluster = allClusters[i];
111+
for (var j = unmappedStart; j < cluster.start; ++j) {
112+
_mapping.add(textIndex: j, clusterIndex: math.max(i - 1, 0));
113+
}
106114
for (int j = cluster.start; j < cluster.end; ++j) {
107115
_mapping.add(textIndex: j, clusterIndex: i);
108116
}
117+
unmappedStart = math.max(unmappedStart, cluster.end);
118+
}
119+
for (var j = unmappedStart; j < paragraph.text.length; ++j) {
120+
_mapping.add(textIndex: j, clusterIndex: math.max(allClusters.length - 1, 0));
109121
}
110122

111123
// One more dummy element in the end to avoid extra checks
@@ -1077,7 +1089,13 @@ class _TextClusterMapping {
10771089
final int clusterIndex = toClusterIndex(start);
10781090
return ClusterRange.collapsed(clusterIndex);
10791091
}
1080-
return ClusterRange(start: toClusterIndex(start), end: toClusterIndex(end - 1) + 1);
1092+
int startClusterIndex = toClusterIndex(start);
1093+
if (_clusters[startClusterIndex].end <= start) {
1094+
// `start` has no cluster of its own and is mapped to the cluster before it, so the range
1095+
// starts at the next cluster.
1096+
startClusterIndex += 1;
1097+
}
1098+
return ClusterRange(start: startClusterIndex, end: toClusterIndex(end - 1) + 1);
10811099
}
10821100

10831101
ui.TextRange toTextRange(ClusterRange clusterRange) {

‎engine/src/flutter/lib/web_ui/test/webparagraph/paragraph_edge_cases_test.dart‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,4 +833,41 @@ Future<void> testMain() async {
833833
},
834834
);
835835
});
836+
837+
group('Paragraph characters without a cluster', () {
838+
// The browser gives some invisible characters no cluster of their own.
839+
for (final (String name, String text) in <(String, String)>[
840+
('soft hyphen', 'ab cd\u00AD'),
841+
('zero width space', 'ab cd\u200B'),
842+
]) {
843+
test('Layout handles a trailing $name', () {
844+
final builder = ui.ParagraphBuilder(ahemStyle);
845+
builder.addText(text);
846+
final ui.Paragraph paragraph = builder.build();
847+
848+
expect(() => paragraph.layout(const ui.ParagraphConstraints(width: 40)), returnsNormally);
849+
expect(paragraph.numberOfLines, 2);
850+
});
851+
}
852+
853+
for (final (String name, String character) in <(String, String)>[
854+
('soft hyphen', '\u00AD'),
855+
('zero width space', '\u200B'),
856+
]) {
857+
test('A span that starts with a $name covers only its own text', () {
858+
final builder = ui.ParagraphBuilder(ahemStyle);
859+
builder.addText('abcd');
860+
builder.pushStyle(ui.TextStyle(color: const ui.Color(0xFF0000FF)));
861+
builder.addText('${character}ef');
862+
builder.pop();
863+
final ui.Paragraph paragraph = builder.build();
864+
paragraph.layout(const ui.ParagraphConstraints(width: 1000));
865+
866+
final ui.TextBox abcd = paragraph.getBoxesForRange(0, 4).single;
867+
final List<ui.TextBox> ef = paragraph.getBoxesForRange(5, 7);
868+
expect(ef, hasLength(1));
869+
expect(ef.single.left, closeTo(abcd.right, 0.01));
870+
});
871+
}
872+
});
836873
}

0 commit comments

Comments
 (0)