diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart index 35d59eed67..84cfdb45fa 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/bitmap_canvas.dart @@ -661,10 +661,14 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking { } void _drawTextLine( - ParagraphGeometricStyle style, String line, double x, double y) { + ParagraphGeometricStyle style, + EngineLineMetrics line, + double x, + double y, + ) { final double letterSpacing = style.letterSpacing; if (letterSpacing == null || letterSpacing == 0.0) { - ctx.fillText(line, x, y); + ctx.fillText(line.text, x, y); } else { // When letter-spacing is set, we go through a more expensive code path // that renders each character separately with the correct spacing @@ -676,9 +680,9 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking { // would put 5px before each letter and 5px after it, but on the web, we // put no spacing before the letter and 10px after it. This is how the DOM // does it. - final int len = line.length; + final int len = line.text.length; for (int i = 0; i < len; i++) { - final String char = line[i]; + final String char = line.text[i]; ctx.fillText(char, x, y); x += letterSpacing + ctx.measureText(char).width; } @@ -692,8 +696,7 @@ class BitmapCanvas extends EngineCanvas with SaveStackTracking { final ParagraphGeometricStyle style = paragraph._geometricStyle; if (paragraph._drawOnCanvas && _childOverdraw == false) { - final List lines = - paragraph._lines ?? [paragraph._plainText]; + final List lines = paragraph._measurementResult.lines; final ui.PaintData backgroundPaint = paragraph._background?.webOnlyPaintData; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/measurement.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/measurement.dart index 20f8328e5d..e22fd5ea81 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/measurement.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/measurement.dart @@ -159,6 +159,12 @@ class RulerManager { /// in [DomTextMeasurementService], or a canvas-based approach in /// [CanvasTextMeasurementService]. abstract class TextMeasurementService { + /// Whether this service uses a canvas to make the text measurements. + /// + /// If [isCanvas] is false, it indicates that this service uses DOM elements + /// to make the text measurements. + bool get isCanvas; + /// Initializes the text measurement service with a specific /// [rulerCacheCapacity] that gets passed to the [RulerManager]. static void initialize({@required int rulerCacheCapacity}) { @@ -312,6 +318,9 @@ abstract class TextMeasurementService { /// needed for some cases that aren't yet supported in the canvas-based /// implementation such as letter-spacing, word-spacing, etc. class DomTextMeasurementService extends TextMeasurementService { + @override + final bool isCanvas = false; + /// The text measurement service singleton. static DomTextMeasurementService get instance => _instance ??= DomTextMeasurementService(); @@ -388,8 +397,11 @@ class DomTextMeasurementService extends TextMeasurementService { /// value. /// /// This method still needs to measure `minIntrinsicWidth`. - MeasurementResult _measureSingleLineParagraph(ParagraphRuler ruler, - ui.Paragraph paragraph, ui.ParagraphConstraints constraints) { + MeasurementResult _measureSingleLineParagraph( + ParagraphRuler ruler, + EngineParagraph paragraph, + ui.ParagraphConstraints constraints, + ) { final double width = constraints.width; final double minIntrinsicWidth = ruler.minIntrinsicDimensions.width; double maxIntrinsicWidth = ruler.singleLineDimensions.width; @@ -399,6 +411,20 @@ class DomTextMeasurementService extends TextMeasurementService { maxIntrinsicWidth = _applySubPixelRoundingHack(minIntrinsicWidth, maxIntrinsicWidth); final double ideographicBaseline = alphabeticBaseline * _baselineRatioHack; + + List lines; + if (paragraph._plainText != null) { + final double lineWidth = maxIntrinsicWidth; + lines = [ + EngineLineMetrics.withText( + paragraph._plainText, + hardBreak: true, + width: lineWidth, + lineNumber: 0, + ), + ]; + } + return MeasurementResult( constraints.width, isSingleLine: true, @@ -410,7 +436,7 @@ class DomTextMeasurementService extends TextMeasurementService { maxIntrinsicWidth: maxIntrinsicWidth, alphabeticBaseline: alphabeticBaseline, ideographicBaseline: ideographicBaseline, - lines: null, + lines: lines, ); } @@ -492,6 +518,9 @@ class DomTextMeasurementService extends TextMeasurementService { /// This is a faster implementation than [DomTextMeasurementService] and /// provides line breaks information that can be useful for multi-line text. class CanvasTextMeasurementService extends TextMeasurementService { + @override + final bool isCanvas = true; + /// The text measurement service singleton. static CanvasTextMeasurementService get instance => _instance ??= CanvasTextMeasurementService(); diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart index e093088a69..c4d7ab135e 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/paragraph.dart @@ -292,19 +292,21 @@ class EngineParagraph implements ui.Paragraph { /// - Paragraphs that have a non-null word-spacing. /// - Paragraphs with a background. bool get _drawOnCanvas { + if (_measurementResult.lines == null) { + return false; + } + bool canDrawTextOnCanvas; - if (TextMeasurementService.enableExperimentalCanvasImplementation) { - canDrawTextOnCanvas = _measurementResult.lines != null; + if (_measurementService.isCanvas) { + canDrawTextOnCanvas = true; } else { - canDrawTextOnCanvas = _measurementResult.isSingleLine && - _plainText != null && - _geometricStyle.ellipsis == null && - _geometricStyle.shadows == null; + canDrawTextOnCanvas = _geometricStyle.ellipsis == null; } return canDrawTextOnCanvas && _geometricStyle.decoration == null && - _geometricStyle.wordSpacing == null; + _geometricStyle.wordSpacing == null && + _geometricStyle.shadows == null; } /// Whether this paragraph has been laid out. diff --git a/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart b/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart index 3478124486..ee7b923c46 100644 --- a/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart +++ b/engine/src/flutter/lib/web_ui/test/text/measurement_test.dart @@ -155,11 +155,9 @@ void main() async { expect(result.maxIntrinsicWidth, 60); expect(result.minIntrinsicWidth, 30); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { - expect(result.lines, [ - line(' abc', hardBreak: true, width: 60.0, lineNumber: 0), - ]); - } + expect(result.lines, [ + line(' abc', hardBreak: true, width: 60.0, lineNumber: 0), + ]); // trailing whitespaces text = build(ahemStyle, 'abc '); @@ -167,10 +165,16 @@ void main() async { expect(result.maxIntrinsicWidth, 60); expect(result.minIntrinsicWidth, 30); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc ', hardBreak: true, width: 30.0, lineNumber: 0), ]); + } else { + // DOM-based measurement always includes trailing whitespace in the + // width, while Flutter and Canvas-based measurement don't. + expect(result.lines, [ + line('abc ', hardBreak: true, width: 60.0, lineNumber: 0), + ]); } // mixed whitespaces @@ -179,10 +183,16 @@ void main() async { expect(result.maxIntrinsicWidth, 100); expect(result.minIntrinsicWidth, 20); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line(' ab c ', hardBreak: true, width: 80.0, lineNumber: 0), ]); + } else { + // DOM-based measurement always includes trailing whitespace in the + // width, while Flutter and Canvas-based measurement don't. + expect(result.lines, [ + line(' ab c ', hardBreak: true, width: 100.0, lineNumber: 0), + ]); } // single whitespace @@ -191,10 +201,16 @@ void main() async { expect(result.maxIntrinsicWidth, 10); expect(result.minIntrinsicWidth, 0); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line(' ', hardBreak: true, width: 0.0, lineNumber: 0), ]); + } else { + // DOM-based measurement always includes trailing whitespace in the + // width, while Flutter and Canvas-based measurement don't. + expect(result.lines, [ + line(' ', hardBreak: true, width: 10.0, lineNumber: 0), + ]); } // whitespace only @@ -203,10 +219,16 @@ void main() async { expect(result.maxIntrinsicWidth, 50); expect(result.minIntrinsicWidth, 0); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line(' ', hardBreak: true, width: 0.0, lineNumber: 0), ]); + } else { + // DOM-based measurement always includes trailing whitespace in the + // width, while Flutter and Canvas-based measurement don't. + expect(result.lines, [ + line(' ', hardBreak: true, width: 50.0, lineNumber: 0), + ]); } }, ); @@ -223,11 +245,9 @@ void main() async { expect(result.minIntrinsicWidth, 50); expect(result.width, 50); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { - expect(result.lines, [ - line('12345', hardBreak: true, width: 50.0, lineNumber: 0), - ]); - } + expect(result.lines, [ + line('12345', hardBreak: true, width: 50.0, lineNumber: 0), + ]); }, ); @@ -238,18 +258,22 @@ void main() async { ui.ParagraphConstraints(width: 70); MeasurementResult result; - // The long text doesn't fit in 50px of width, so it needs to wrap. + // The long text doesn't fit in 70px of width, so it needs to wrap. result = instance.measure(build(ahemStyle, 'foo bar baz'), constraints); expect(result.isSingleLine, false); expect(result.maxIntrinsicWidth, 110); expect(result.minIntrinsicWidth, 30); expect(result.width, 70); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('foo bar ', hardBreak: false, width: 70.0, lineNumber: 0), line('baz', hardBreak: true, width: 30.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }, ); @@ -266,11 +290,15 @@ void main() async { expect(result.minIntrinsicWidth, 100); expect(result.width, 50); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('12345', hardBreak: false, width: 50.0, lineNumber: 0), line('67890', hardBreak: true, width: 50.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // The first word is force-broken twice. @@ -281,12 +309,16 @@ void main() async { expect(result.minIntrinsicWidth, 110); expect(result.width, 50); expect(result.height, 30); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abcde', hardBreak: false, width: 50.0, lineNumber: 0), line('fghij', hardBreak: false, width: 50.0, lineNumber: 1), line('k lm', hardBreak: true, width: 40.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Constraints aren't enough even for a single character. In this case, @@ -299,11 +331,15 @@ void main() async { expect(result.minIntrinsicWidth, 20); expect(result.width, 8); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('A', hardBreak: false, width: 10.0, lineNumber: 0), line('A', hardBreak: true, width: 10.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Extremely narrow constraints with new line in the middle. @@ -312,15 +348,17 @@ void main() async { expect(result.maxIntrinsicWidth, 20); expect(result.minIntrinsicWidth, 20); expect(result.width, 8); - if (instance is CanvasTextMeasurementService) { - // This can only be done correctly by the canvas-based implementation. - expect(result.height, 30); - + expect(result.height, 30); + if (instance.isCanvas) { expect(result.lines, [ line('A', hardBreak: false, width: 10.0, lineNumber: 0), line('A', hardBreak: true, width: 10.0, lineNumber: 1), line('A', hardBreak: true, width: 10.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Extremely narrow constraints with new line in the end. @@ -330,13 +368,17 @@ void main() async { expect(result.minIntrinsicWidth, 30); expect(result.width, 8); expect(result.height, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('A', hardBreak: false, width: 10.0, lineNumber: 0), line('A', hardBreak: false, width: 10.0, lineNumber: 1), line('A', hardBreak: true, width: 10.0, lineNumber: 2), line('', hardBreak: true, width: 0.0, lineNumber: 3), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }, ); @@ -353,11 +395,15 @@ void main() async { expect(result.minIntrinsicWidth, 20); expect(result.width, 50); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('12', hardBreak: true, width: 20.0, lineNumber: 0), line('34', hardBreak: true, width: 20.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }, ); @@ -370,12 +416,16 @@ void main() async { expect(result.maxIntrinsicWidth, 40); expect(result.minIntrinsicWidth, 40); expect(result.height, 30); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('', hardBreak: true, width: 0.0, lineNumber: 0), line('', hardBreak: true, width: 0.0, lineNumber: 1), line('1234', hardBreak: true, width: 40.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Empty lines in the middle. @@ -383,19 +433,23 @@ void main() async { expect(result.maxIntrinsicWidth, 30); expect(result.minIntrinsicWidth, 30); expect(result.height, 30); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('12', hardBreak: true, width: 20.0, lineNumber: 0), line('', hardBreak: true, width: 0.0, lineNumber: 1), line('345', hardBreak: true, width: 30.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Empty lines in the end. result = instance.measure(build(ahemStyle, '1234\n\n'), constraints); expect(result.maxIntrinsicWidth, 40); expect(result.minIntrinsicWidth, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { // This can only be done correctly in the canvas-based implementation. expect(result.height, 30); expect(result.lines, [ @@ -403,6 +457,10 @@ void main() async { line('', hardBreak: true, width: 0.0, lineNumber: 1), line('', hardBreak: true, width: 0.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }); @@ -419,11 +477,15 @@ void main() async { expect(result.width, double.infinity); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('123', hardBreak: true, width: 30.0, lineNumber: 0), line('456 789', hardBreak: true, width: 70.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }, ); @@ -480,54 +542,74 @@ void main() async { // Simple case. result = instance.measure(build(ahemStyle, 'abc de fghi'), constraints); expect(result.minIntrinsicWidth, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc ', hardBreak: false, width: 30.0, lineNumber: 0), line('de ', hardBreak: false, width: 20.0, lineNumber: 1), line('fghi', hardBreak: true, width: 40.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With new lines. result = instance.measure(build(ahemStyle, 'abcd\nef\nghi'), constraints); expect(result.minIntrinsicWidth, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abcd', hardBreak: true, width: 40.0, lineNumber: 0), line('ef', hardBreak: true, width: 20.0, lineNumber: 1), line('ghi', hardBreak: true, width: 30.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With trailing whitespace. result = instance.measure(build(ahemStyle, 'abcd efg'), constraints); expect(result.minIntrinsicWidth, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abcd ', hardBreak: false, width: 40.0, lineNumber: 0), line('efg', hardBreak: true, width: 30.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With trailing whitespace and new lines. result = instance.measure(build(ahemStyle, 'abc \ndefg'), constraints); expect(result.minIntrinsicWidth, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc ', hardBreak: true, width: 30.0, lineNumber: 0), line('defg', hardBreak: true, width: 40.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Very long text. result = instance.measure(build(ahemStyle, 'AAAAAAAAAAAA'), constraints); expect(result.minIntrinsicWidth, 120); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 0), line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 1), line('AA', hardBreak: true, width: 20.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }); @@ -537,64 +619,88 @@ void main() async { // Simple case. result = instance.measure(build(ahemStyle, 'abc de fghi'), constraints); expect(result.maxIntrinsicWidth, 110); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc ', hardBreak: false, width: 30.0, lineNumber: 0), line('de ', hardBreak: false, width: 20.0, lineNumber: 1), line('fghi', hardBreak: true, width: 40.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With new lines. result = instance.measure(build(ahemStyle, 'abcd\nef\nghi'), constraints); expect(result.maxIntrinsicWidth, 40); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abcd', hardBreak: true, width: 40.0, lineNumber: 0), line('ef', hardBreak: true, width: 20.0, lineNumber: 1), line('ghi', hardBreak: true, width: 30.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With long whitespace. result = instance.measure(build(ahemStyle, 'abcd efg'), constraints); expect(result.maxIntrinsicWidth, 100); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abcd ', hardBreak: false, width: 40.0, lineNumber: 0), line('efg', hardBreak: true, width: 30.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With trailing whitespace. result = instance.measure(build(ahemStyle, 'abc def '), constraints); expect(result.maxIntrinsicWidth, 100); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc ', hardBreak: false, width: 30.0, lineNumber: 0), line('def ', hardBreak: true, width: 30.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // With trailing whitespace and new lines. result = instance.measure(build(ahemStyle, 'abc \ndef '), constraints); expect(result.maxIntrinsicWidth, 60); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc ', hardBreak: true, width: 30.0, lineNumber: 0), line('def ', hardBreak: true, width: 30.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Very long text. result = instance.measure(build(ahemStyle, 'AAAAAAAAAAAA'), constraints); expect(result.maxIntrinsicWidth, 120); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 0), line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 1), line('AA', hardBreak: true, width: 20.0, lineNumber: 2), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }); @@ -619,10 +725,14 @@ void main() async { expect(result.minIntrinsicWidth, 480); expect(result.maxIntrinsicWidth, 480); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('AA...', hardBreak: false, width: 50.0, lineNumber: 0), ]); + } else { + // DOM-based measurement can't handle the ellipsis case very well. The + // text wraps into multiple lines instead. + expect(result.lines, isNull); } // The short prefix should make the text break into two lines, but the @@ -635,11 +745,15 @@ void main() async { expect(result.minIntrinsicWidth, 450); expect(result.maxIntrinsicWidth, 450); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('AAA', hardBreak: true, width: 30.0, lineNumber: 0), line('AA...', hardBreak: false, width: 50.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't handle the ellipsis case very well. The + // text wraps into multiple lines instead. + expect(result.lines, isNull); } // Tiny constraints. @@ -650,10 +764,14 @@ void main() async { expect(result.minIntrinsicWidth, 40); expect(result.maxIntrinsicWidth, 40); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('...', hardBreak: false, width: 30.0, lineNumber: 0), ]); + } else { + // DOM-based measurement can't handle the ellipsis case very well. The + // text wraps into multiple lines instead. + expect(result.lines, isNull); } // Tinier constraints (not enough for the ellipsis). @@ -663,12 +781,19 @@ void main() async { expect(result.minIntrinsicWidth, 40); expect(result.maxIntrinsicWidth, 40); expect(result.height, 10); - // TODO(flutter_web): https://github.com/flutter/flutter/issues/34346 - // if (instance is CanvasTextMeasurementService) { - // expect(result.lines, [ - // line('.', hardBreak: false, width: 10.0, lineNumber: 0), - // ]); - // } + if (instance.isCanvas) { + // TODO(flutter_web): https://github.com/flutter/flutter/issues/34346 + // expect(result.lines, [ + // line('.', hardBreak: false, width: 10.0, lineNumber: 0), + // ]); + expect(result.lines, [ + line('...', hardBreak: false, width: 30.0, lineNumber: 0), + ]); + } else { + // DOM-based measurement can't handle the ellipsis case very well. The + // text wraps into multiple lines instead. + expect(result.lines, isNull); + } }, ); @@ -685,22 +810,24 @@ void main() async { final ui.Paragraph oneline = build(maxlinesStyle, 'One line'); result = instance.measure(oneline, infiniteConstraints); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { - expect(result.lines, [ - line('One line', hardBreak: true, width: 80.0, lineNumber: 0), - ]); - } + expect(result.lines, [ + line('One line', hardBreak: true, width: 80.0, lineNumber: 0), + ]); // The height should respect max lines and be limited to two lines here. final ui.Paragraph threelines = build(maxlinesStyle, 'First\nSecond\nThird'); result = instance.measure(threelines, infiniteConstraints); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('First', hardBreak: true, width: 50.0, lineNumber: 0), line('Second', hardBreak: true, width: 60.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // The height should respect max lines and be limited to two lines here. @@ -710,11 +837,15 @@ void main() async { ); result = instance.measure(veryLong, constraints); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('Lorem ', hardBreak: false, width: 50.0, lineNumber: 0), line('ipsum ', hardBreak: false, width: 50.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Case when last line is a long unbreakable word. @@ -724,11 +855,15 @@ void main() async { ); result = instance.measure(veryLongLastLine, constraints); expect(result.height, 20); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('AAA ', hardBreak: false, width: 30.0, lineNumber: 0), line('AAAAA', hardBreak: false, width: 50.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }); @@ -757,30 +892,36 @@ void main() async { p = build(onelineStyle, 'abcdef'); result = instance.measure(p, constraints); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { - expect(result.lines, [ - line('abcdef', hardBreak: true, width: 60.0, lineNumber: 0), - ]); - } + expect(result.lines, [ + line('abcdef', hardBreak: true, width: 60.0, lineNumber: 0), + ]); // Simple overflow case. p = build(onelineStyle, 'abcd efg'); result = instance.measure(p, constraints); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('abc...', hardBreak: false, width: 60.0, lineNumber: 0), ]); + } else { + // DOM-based measurement can't handle the ellipsis case very well. The + // text wraps into multiple lines instead. + expect(result.lines, isNull); } // Another simple overflow case. p = build(onelineStyle, 'a bcde fgh'); result = instance.measure(p, constraints); expect(result.height, 10); - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.lines, [ line('a b...', hardBreak: false, width: 60.0, lineNumber: 0), ]); + } else { + // DOM-based measurement can't handle the ellipsis case very well. The + // text wraps into multiple lines instead. + expect(result.lines, isNull); } // The ellipsis is supposed to go on the second line, but because the @@ -788,26 +929,34 @@ void main() async { p = build(multilineStyle, 'abcdef ghijkl'); result = instance.measure(p, constraints); // This can only be done correctly in the canvas-based implementation. - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.height, 20); expect(result.lines, [ line('abcdef ', hardBreak: false, width: 60.0, lineNumber: 0), line('ghijkl', hardBreak: true, width: 60.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // But when the 2nd line is long enough, the ellipsis is shown. p = build(multilineStyle, 'abcd efghijkl'); result = instance.measure(p, constraints); // This can only be done correctly in the canvas-based implementation. - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.height, 20); expect(result.lines, [ line('abcd ', hardBreak: false, width: 40.0, lineNumber: 0), line('efg...', hardBreak: false, width: 60.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Even if the second line can be broken, we don't break it, we just @@ -815,39 +964,51 @@ void main() async { p = build(multilineStyle, 'abcde f gh ijk'); result = instance.measure(p, constraints); // This can only be done correctly in the canvas-based implementation. - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.height, 20); expect(result.lines, [ line('abcde ', hardBreak: false, width: 50.0, lineNumber: 0), line('f g...', hardBreak: false, width: 60.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // First line overflows but second line doesn't. p = build(multilineStyle, 'abcdefg hijk'); result = instance.measure(p, constraints); // This can only be done correctly in the canvas-based implementation. - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.height, 20); expect(result.lines, [ line('abcdef', hardBreak: false, width: 60.0, lineNumber: 0), line('g hijk', hardBreak: true, width: 60.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } // Both first and second lines overflow. p = build(multilineStyle, 'abcdefg hijklmnop'); result = instance.measure(p, constraints); // This can only be done correctly in the canvas-based implementation. - if (instance is CanvasTextMeasurementService) { + if (instance.isCanvas) { expect(result.height, 20); expect(result.lines, [ line('abcdef', hardBreak: false, width: 60.0, lineNumber: 0), line('g h...', hardBreak: false, width: 60.0, lineNumber: 1), ]); + } else { + // DOM-based measurement can't produce line metrics for multi-line + // paragraphs. + expect(result.lines, isNull); } }, );