@@ -17,7 +17,7 @@ import 'word_breaker.dart';
1717
1818const ui.Color _defaultTextColor = ui.Color (0xFFFF0000 );
1919
20- final String _placeholderChar = String .fromCharCode (0xFFFC );
20+ final String placeholderChar = String .fromCharCode (0xFFFC );
2121
2222/// A paragraph made up of a flat list of text spans and placeholders.
2323///
@@ -33,7 +33,6 @@ class CanvasParagraph implements ui.Paragraph {
3333 this .spans, {
3434 required this .paragraphStyle,
3535 required this .plainText,
36- required this .placeholderCount,
3736 required this .canDrawOnCanvas,
3837 }) : assert (spans.isNotEmpty);
3938
@@ -46,9 +45,6 @@ class CanvasParagraph implements ui.Paragraph {
4645 /// The full textual content of the paragraph.
4746 late String plainText;
4847
49- /// The number of placeholders in this paragraph.
50- final int placeholderCount;
51-
5248 /// Whether this paragraph can be drawn on a bitmap canvas.
5349 ///
5450 /// Some text features cannot be rendered into a 2D canvas and must use HTML,
@@ -87,8 +83,6 @@ class CanvasParagraph implements ui.Paragraph {
8783 /// Whether this paragraph has been laid out or not.
8884 bool isLaidOut = false ;
8985
90- bool get isRtl => paragraphStyle.effectiveTextDirection == ui.TextDirection .rtl;
91-
9286 ui.ParagraphConstraints ? _lastUsedConstraints;
9387
9488 late final TextLayoutService _layoutService = TextLayoutService (this );
@@ -138,27 +132,23 @@ class CanvasParagraph implements ui.Paragraph {
138132 _paintService.paint (canvas, offset);
139133 }
140134
141- /// Generates a flat string computed from all the spans of the paragraph.
142- String toPlainText () => plainText;
143-
144- DomHTMLElement ? _cachedDomElement;
135+ DomElement ? _cachedDomElement;
145136
146137 /// Returns a DOM element that represents the entire paragraph and its
147138 /// children.
148139 ///
149140 /// Generates a new DOM element on every invocation.
150- DomHTMLElement toDomElement () {
141+ DomElement toDomElement () {
151142 assert (isLaidOut);
152- final DomHTMLElement ? domElement = _cachedDomElement;
143+ final DomElement ? domElement = _cachedDomElement;
153144 if (domElement == null ) {
154145 return _cachedDomElement ?? = _createDomElement ();
155146 }
156- return domElement.cloneNode (true ) as DomHTMLElement ;
147+ return domElement.cloneNode (true ) as DomElement ;
157148 }
158149
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' );
162152
163153 // 1. Set paragraph-level styles.
164154
@@ -183,12 +173,8 @@ class CanvasParagraph implements ui.Paragraph {
183173 continue ;
184174 }
185175
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);
192178 _positionSpanElement (spanElement, line, fragment);
193179
194180 spanElement.appendText (text);
@@ -221,7 +207,6 @@ class CanvasParagraph implements ui.Paragraph {
221207
222208 @override
223209 ui.TextRange getWordBoundary (ui.TextPosition position) {
224- final String text = toPlainText ();
225210 final int characterPosition;
226211 switch (position.affinity) {
227212 case ui.TextAffinity .upstream:
@@ -231,8 +216,8 @@ class CanvasParagraph implements ui.Paragraph {
231216 characterPosition = position.offset;
232217 break ;
233218 }
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);
236221 return ui.TextRange (start: start, end: end);
237222 }
238223
@@ -288,51 +273,30 @@ void _positionSpanElement(DomElement element, ParagraphLine line, LayoutFragment
288273 ..lineHeight = '${boxRect .height }px' ;
289274}
290275
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.
309277///
310278/// Instead of keeping spans and styles in a tree hierarchy like the framework
311279/// does, we flatten the structure and resolve/merge all the styles from parent
312280/// 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
315285 /// text in the range between [start] and [end] .
316- FlatTextSpan ({
286+ ParagraphSpan ({
317287 required this .style,
318288 required this .start,
319289 required this .end,
320290 });
321291
322- @override
292+ /// The resolved style of the span.
323293 final EngineTextStyle style;
324294
325- @override
295+ /// The index of the beginning of the range of text represented by this span.
326296 final int start;
327297
328- @override
298+ /// The index of the end of the range of text represented by this span.
329299 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- }
336300}
337301
338302class PlaceholderSpan extends ParagraphPlaceholder implements ParagraphSpan {
@@ -622,16 +586,13 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
622586 double ? baselineOffset,
623587 ui.TextBaseline ? baseline,
624588 }) {
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-
628589 // Require a baseline to be specified if using a baseline-based alignment.
629590 assert (! (alignment == ui.PlaceholderAlignment .aboveBaseline ||
630591 alignment == ui.PlaceholderAlignment .belowBaseline ||
631592 alignment == ui.PlaceholderAlignment .baseline) || baseline != null );
632593
633594 final int start = _plainTextBuffer.length;
634- _plainTextBuffer.write (_placeholderChar );
595+ _plainTextBuffer.write (placeholderChar );
635596 final int end = _plainTextBuffer.length;
636597
637598 final EngineTextStyle style = _currentStyleNode.resolveStyle ();
@@ -674,7 +635,7 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
674635 final EngineTextStyle style = _currentStyleNode.resolveStyle ();
675636 _updateCanDrawOnCanvas (style);
676637
677- _spans.add (FlatTextSpan (style: style, start: start, end: end));
638+ _spans.add (ParagraphSpan (style: style, start: start, end: end));
678639 }
679640
680641 void _updateCanDrawOnCanvas (EngineTextStyle style) {
@@ -708,18 +669,15 @@ class CanvasParagraphBuilder implements ui.ParagraphBuilder {
708669 //
709670 // We want the paragraph to always have a non-empty list of spans to match
710671 // 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+ );
716675 }
717676
718677 return CanvasParagraph (
719678 _spans,
720679 paragraphStyle: _paragraphStyle,
721680 plainText: _plainTextBuffer.toString (),
722- placeholderCount: _placeholderCount,
723681 canDrawOnCanvas: _canDrawOnCanvas,
724682 );
725683 }
0 commit comments