@@ -17,7 +17,7 @@ import 'word_breaker.dart';
17
17
18
18
const ui.Color _defaultTextColor = ui.Color (0xFFFF0000 );
19
19
20
- final String _placeholderChar = String .fromCharCode (0xFFFC );
20
+ final String placeholderChar = String .fromCharCode (0xFFFC );
21
21
22
22
/// A paragraph made up of a flat list of text spans and placeholders.
23
23
///
@@ -33,7 +33,6 @@ class CanvasParagraph implements ui.Paragraph {
33
33
this .spans, {
34
34
required this .paragraphStyle,
35
35
required this .plainText,
36
- required this .placeholderCount,
37
36
required this .canDrawOnCanvas,
38
37
}) : assert (spans.isNotEmpty);
39
38
@@ -46,9 +45,6 @@ class CanvasParagraph implements ui.Paragraph {
46
45
/// The full textual content of the paragraph.
47
46
late String plainText;
48
47
49
- /// The number of placeholders in this paragraph.
50
- final int placeholderCount;
51
-
52
48
/// Whether this paragraph can be drawn on a bitmap canvas.
53
49
///
54
50
/// Some text features cannot be rendered into a 2D canvas and must use HTML,
@@ -87,8 +83,6 @@ class CanvasParagraph implements ui.Paragraph {
87
83
/// Whether this paragraph has been laid out or not.
88
84
bool isLaidOut = false ;
89
85
90
- bool get isRtl => paragraphStyle.effectiveTextDirection == ui.TextDirection .rtl;
91
-
92
86
ui.ParagraphConstraints ? _lastUsedConstraints;
93
87
94
88
late final TextLayoutService _layoutService = TextLayoutService (this );
@@ -138,27 +132,23 @@ class CanvasParagraph implements ui.Paragraph {
138
132
_paintService.paint (canvas, offset);
139
133
}
140
134
141
- /// Generates a flat string computed from all the spans of the paragraph.
142
- String toPlainText () => plainText;
143
-
144
- DomHTMLElement ? _cachedDomElement;
135
+ DomElement ? _cachedDomElement;
145
136
146
137
/// Returns a DOM element that represents the entire paragraph and its
147
138
/// children.
148
139
///
149
140
/// Generates a new DOM element on every invocation.
150
- DomHTMLElement toDomElement () {
141
+ DomElement toDomElement () {
151
142
assert (isLaidOut);
152
- final DomHTMLElement ? domElement = _cachedDomElement;
143
+ final DomElement ? domElement = _cachedDomElement;
153
144
if (domElement == null ) {
154
145
return _cachedDomElement ?? = _createDomElement ();
155
146
}
156
- return domElement.cloneNode (true ) as DomHTMLElement ;
147
+ return domElement.cloneNode (true ) as DomElement ;
157
148
}
158
149
159
- DomHTMLElement _createDomElement () {
160
- final DomHTMLElement rootElement =
161
- domDocument.createElement ('flt-paragraph' ) as DomHTMLElement ;
150
+ DomElement _createDomElement () {
151
+ final DomElement rootElement = domDocument.createElement ('flt-paragraph' );
162
152
163
153
// 1. Set paragraph-level styles.
164
154
@@ -183,12 +173,8 @@ class CanvasParagraph implements ui.Paragraph {
183
173
continue ;
184
174
}
185
175
186
- final DomHTMLElement spanElement = domDocument.createElement ('flt-span' ) as DomHTMLElement ;
187
- applyTextStyleToElement (
188
- element: spanElement,
189
- style: fragment.style,
190
- isSpan: true ,
191
- );
176
+ final DomElement spanElement = domDocument.createElement ('flt-span' );
177
+ applyTextStyleToElement (element: spanElement, style: fragment.style);
192
178
_positionSpanElement (spanElement, line, fragment);
193
179
194
180
spanElement.appendText (text);
@@ -221,7 +207,6 @@ class CanvasParagraph implements ui.Paragraph {
221
207
222
208
@override
223
209
ui.TextRange getWordBoundary (ui.TextPosition position) {
224
- final String text = toPlainText ();
225
210
final int characterPosition;
226
211
switch (position.affinity) {
227
212
case ui.TextAffinity .upstream:
@@ -231,8 +216,8 @@ class CanvasParagraph implements ui.Paragraph {
231
216
characterPosition = position.offset;
232
217
break ;
233
218
}
234
- final int start = WordBreaker .prevBreakIndex (text , characterPosition + 1 );
235
- final int end = WordBreaker .nextBreakIndex (text , characterPosition);
219
+ final int start = WordBreaker .prevBreakIndex (plainText , characterPosition + 1 );
220
+ final int end = WordBreaker .nextBreakIndex (plainText , characterPosition);
236
221
return ui.TextRange (start: start, end: end);
237
222
}
238
223
@@ -288,51 +273,30 @@ void _positionSpanElement(DomElement element, ParagraphLine line, LayoutFragment
288
273
..lineHeight = '${boxRect .height }px' ;
289
274
}
290
275
291
- /// A common interface for all types of spans that make up a paragraph.
292
- ///
293
- /// These spans are stored as a flat list in the paragraph object.
294
- abstract class ParagraphSpan {
295
- /// The index of the beginning of the range of text represented by this span.
296
- int get start;
297
-
298
- /// The index of the end of the range of text represented by this span.
299
- int get end;
300
-
301
- /// The resolved style of the span.
302
- EngineTextStyle get style;
303
- }
304
-
305
- /// Represent a span of text in the paragraph.
306
- ///
307
- /// It's a "flat" text span as opposed to the framework text spans that are
308
- /// hierarchical.
276
+ /// Represents a span in the paragraph.
309
277
///
310
278
/// Instead of keeping spans and styles in a tree hierarchy like the framework
311
279
/// does, we flatten the structure and resolve/merge all the styles from parent
312
280
/// nodes.
313
- class FlatTextSpan implements ParagraphSpan {
314
- /// Creates a [FlatTextSpan] with the given [style] , representing the span of
281
+ ///
282
+ /// These spans are stored as a flat list in the paragraph object.
283
+ class ParagraphSpan {
284
+ /// Creates a [ParagraphSpan] with the given [style] , representing the span of
315
285
/// text in the range between [start] and [end] .
316
- FlatTextSpan ({
286
+ ParagraphSpan ({
317
287
required this .style,
318
288
required this .start,
319
289
required this .end,
320
290
});
321
291
322
- @override
292
+ /// The resolved style of the span.
323
293
final EngineTextStyle style;
324
294
325
- @override
295
+ /// The index of the beginning of the range of text represented by this span.
326
296
final int start;
327
297
328
- @override
298
+ /// The index of the end of the range of text represented by this span.
329
299
final int end;
330
-
331
- String textOf (CanvasParagraph paragraph) {
332
- final String text = paragraph.toPlainText ();
333
- assert (end <= text.length);
334
- return text.substring (start, end);
335
- }
336
300
}
337
301
338
302
class PlaceholderSpan extends ParagraphPlaceholder implements ParagraphSpan {
@@ -622,16 +586,13 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
622
586
double ? baselineOffset,
623
587
ui.TextBaseline ? baseline,
624
588
}) {
625
- // TODO(mdebbar): for measurement of placeholders, look at:
626
- // - https://github.com/flutter/engine/blob/c0f7e8acf9318d264ad6a235facd097de597ffcc/third_party/txt/src/txt/paragraph_txt.cc#L325-L350
627
-
628
589
// Require a baseline to be specified if using a baseline-based alignment.
629
590
assert (! (alignment == ui.PlaceholderAlignment .aboveBaseline ||
630
591
alignment == ui.PlaceholderAlignment .belowBaseline ||
631
592
alignment == ui.PlaceholderAlignment .baseline) || baseline != null );
632
593
633
594
final int start = _plainTextBuffer.length;
634
- _plainTextBuffer.write (_placeholderChar );
595
+ _plainTextBuffer.write (placeholderChar );
635
596
final int end = _plainTextBuffer.length;
636
597
637
598
final EngineTextStyle style = _currentStyleNode.resolveStyle ();
@@ -674,7 +635,7 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
674
635
final EngineTextStyle style = _currentStyleNode.resolveStyle ();
675
636
_updateCanDrawOnCanvas (style);
676
637
677
- _spans.add (FlatTextSpan (style: style, start: start, end: end));
638
+ _spans.add (ParagraphSpan (style: style, start: start, end: end));
678
639
}
679
640
680
641
void _updateCanDrawOnCanvas (EngineTextStyle style) {
@@ -708,18 +669,15 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
708
669
//
709
670
// We want the paragraph to always have a non-empty list of spans to match
710
671
// the expectations of the [LayoutFragmenter].
711
- _spans.add (FlatTextSpan (
712
- style: _rootStyleNode.resolveStyle (),
713
- start: 0 ,
714
- end: 0 ,
715
- ));
672
+ _spans.add (
673
+ ParagraphSpan (style: _rootStyleNode.resolveStyle (), start: 0 , end: 0 ),
674
+ );
716
675
}
717
676
718
677
return CanvasParagraph (
719
678
_spans,
720
679
paragraphStyle: _paragraphStyle,
721
680
plainText: _plainTextBuffer.toString (),
722
- placeholderCount: _placeholderCount,
723
681
canDrawOnCanvas: _canDrawOnCanvas,
724
682
);
725
683
}
0 commit comments