From 1121d94ae5fa08d60d350ac5c8f7f0d7765c3374 Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Mon, 11 Jan 2021 15:34:01 -0800 Subject: [PATCH] [web] Apply font-family and other styles to the paragraph element (flutter/engine#23596) --- .../lib/src/engine/text/canvas_paragraph.dart | 14 ++--- .../lib/src/engine/text/font_collection.dart | 1 + .../lib/src/engine/text/layout_service.dart | 12 ++++ .../lib/web_ui/lib/src/engine/text/ruler.dart | 7 ++- .../text/canvas_paragraph_builder_test.dart | 59 +++++++++++++++++-- .../web_ui/test/text/font_loading_test.dart | 15 +++-- 6 files changed, 90 insertions(+), 18 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart index f61ca0c01f..66ff52ddf9 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/canvas_paragraph.dart @@ -127,21 +127,21 @@ class CanvasParagraph implements EngineParagraph { domRenderer.createElement('p') as html.HtmlElement; // 1. Set paragraph-level styles. + _applyParagraphStyleToElement(element: element, style: paragraphStyle); final html.CssStyleDeclaration cssStyle = element.style; - final ui.TextDirection direction = - paragraphStyle._textDirection ?? ui.TextDirection.ltr; - final ui.TextAlign align = paragraphStyle._textAlign ?? ui.TextAlign.start; cssStyle - ..direction = _textDirectionToCss(direction) - ..textAlign = textAlignToCssValue(align, direction) ..position = 'absolute' ..whiteSpace = 'pre-wrap' - ..overflowWrap = 'break-word' - ..overflow = 'hidden'; + ..overflowWrap = 'break-word'; + + if (paragraphStyle._maxLines != null || paragraphStyle._ellipsis != null) { + cssStyle..overflowY = 'hidden'; + } if (paragraphStyle._ellipsis != null && (paragraphStyle._maxLines == null || paragraphStyle._maxLines == 1)) { cssStyle + ..overflowX = 'hidden' ..whiteSpace = 'pre' ..textOverflow = 'ellipsis'; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart index 09e7d43e7f..3d09d8d393 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/font_collection.dart @@ -200,6 +200,7 @@ class FontManager { // loaded. They were measured using fallback font, so we should clear the // cache. TextMeasurementService.clearCache(); + Spanometer.clearRulersCache(); }, onError: (dynamic exception) { // Failures here will throw an html.DomException which confusingly // does not implement Exception or Error. Rethrow an Exception so it can diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart index d27aceba04..edd1938a5b 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/layout_service.dart @@ -1072,6 +1072,18 @@ class Spanometer { static Map _rulers = {}; + @visibleForTesting + static Map get rulers => _rulers; + + /// Clears the cache of rulers that are used for measuring text height and + /// baseline metrics. + static void clearRulersCache() { + _rulers.forEach((TextHeightStyle style, TextHeightRuler ruler) { + ruler.dispose(); + }); + _rulers.clear(); + } + String _cssFontString = ''; double? get letterSpacing => currentSpan.style._letterSpacing; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart index ee4ff2476b..8f597c47d2 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text/ruler.dart @@ -414,6 +414,11 @@ class TextHeightRuler { /// The height for this ruler's [textHeightStyle]. late final double height = _dimensions.height; + /// Disposes of this ruler and detaches it from the DOM tree. + void dispose() { + _host.remove(); + } + html.HtmlElement _createHost() { final html.DivElement host = html.DivElement(); host.style @@ -928,7 +933,7 @@ class ParagraphRuler { _singleLineHost.remove(); _minIntrinsicHost.remove(); _constrainedHost.remove(); - _textHeightRuler._host.remove(); + _textHeightRuler.dispose(); assert(() { _debugIsDisposed = true; return true; diff --git a/engine/src/flutter/lib/web_ui/test/text/canvas_paragraph_builder_test.dart b/engine/src/flutter/lib/web_ui/test/text/canvas_paragraph_builder_test.dart index bb6f197c40..78db0b0414 100644 --- a/engine/src/flutter/lib/web_ui/test/text/canvas_paragraph_builder_test.dart +++ b/engine/src/flutter/lib/web_ui/test/text/canvas_paragraph_builder_test.dart @@ -8,7 +8,8 @@ import 'package:test/test.dart'; import 'package:ui/src/engine.dart'; import 'package:ui/ui.dart'; -const String paragraphStyle = 'style="position: absolute; white-space: pre-wrap; overflow-wrap: break-word; overflow: hidden;"'; +const String paragraphStyle = + 'font-family: sans-serif; position: absolute; white-space: pre-wrap; overflow-wrap: break-word;'; const String defaultColor = 'color: rgb(255, 0, 0);'; const String defaultFontFamily = 'font-family: sans-serif;'; const String defaultFontSize = 'font-size: 14px;'; @@ -33,7 +34,11 @@ void testMain() { expect(paragraph.toPlainText(), 'Hello'); expect( paragraph.toDomElement().outerHtml, - '

Hello

', + '

' + '' + 'Hello' + '' + '

', ); expect(paragraph.spans, hasLength(1)); @@ -55,7 +60,7 @@ void testMain() { expect(paragraph.toPlainText(), 'Hello'); expect( paragraph.toDomElement().outerHtml, - '

Hello

', + '

Hello

', ); expect(paragraph.spans, hasLength(1)); @@ -63,6 +68,48 @@ void testMain() { expect(textSpan.style, styleWithDefaults()); }); + test('Sets correct styles for max-lines', () { + final EngineParagraphStyle style = EngineParagraphStyle(maxLines: 2); + final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style); + + builder.addText('Hello'); + + final CanvasParagraph paragraph = builder.build(); + expect(paragraph.paragraphStyle, style); + expect(paragraph.toPlainText(), 'Hello'); + expect( + paragraph.toDomElement().outerHtml, + '

' + '' + 'Hello' + '' + '

', + ); + }); + + test('Sets correct styles for ellipsis', () { + final EngineParagraphStyle style = EngineParagraphStyle(ellipsis: '...'); + final CanvasParagraphBuilder builder = CanvasParagraphBuilder(style); + + builder.addText('Hello'); + + final CanvasParagraph paragraph = builder.build(); + expect(paragraph.paragraphStyle, style); + expect(paragraph.toPlainText(), 'Hello'); + final String expectedParagraphStyle = paragraphStyle.replaceFirst( + 'white-space: pre-wrap', + 'white-space: pre', + ); + expect( + paragraph.toDomElement().outerHtml, + '

' + '' + 'Hello' + '' + '

', + ); + }); + test('Builds a single-span paragraph with complex styles', () { final EngineParagraphStyle style = EngineParagraphStyle(fontSize: 13.0, height: 1.5); @@ -80,7 +127,7 @@ void testMain() { expect(paragraph.toPlainText(), 'Hello'); expect( paragraph.toDomElement().outerHtml, - '

' + '

' '' 'Hello' '' @@ -116,7 +163,7 @@ void testMain() { expect(paragraph.toPlainText(), 'Hello world'); expect( paragraph.toDomElement().outerHtml, - '

' + '

' '' 'Hello' '' @@ -165,7 +212,7 @@ void testMain() { expect(paragraph.toPlainText(), 'Hello world!'); expect( paragraph.toDomElement().outerHtml, - '

' + '

' '' 'Hello' '' diff --git a/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart b/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart index d3e8de6c8b..548b317055 100644 --- a/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart +++ b/engine/src/flutter/lib/web_ui/test/text/font_loading_test.dart @@ -54,15 +54,21 @@ void testMain() async { test('loading font should clear measurement caches', () async { final ui.ParagraphStyle style = ui.ParagraphStyle(); - final DomParagraphBuilder builder = DomParagraphBuilder(style); final ui.ParagraphConstraints constraints = ui.ParagraphConstraints(width: 30.0); - builder.addText('test'); - final DomParagraph paragraph = builder.build(); + + final DomParagraphBuilder domBuilder = DomParagraphBuilder(style); + domBuilder.addText('test'); // Triggers the measuring and verifies the result has been cached. - paragraph.layout(constraints); + domBuilder.build().layout(constraints); expect(TextMeasurementService.rulerManager.rulers.length, 1); + final CanvasParagraphBuilder canvasBuilder = CanvasParagraphBuilder(style); + canvasBuilder.addText('test'); + // Triggers the measuring and verifies the ruler cache has been populated. + canvasBuilder.build().layout(constraints); + expect(Spanometer.rulers.length, 1); + // Now, loads a new font using loadFontFromList. This should clear the // cache final html.HttpRequest response = await html.HttpRequest.request( @@ -74,6 +80,7 @@ void testMain() async { // Verifies the font is loaded, and the cache is cleaned. expect(_containsFontFamily('Blehm'), true); expect(TextMeasurementService.rulerManager.rulers.length, 0); + expect(Spanometer.rulers.length, 0); }, // TODO(nurhan): https://github.com/flutter/flutter/issues/56702 // TODO(nurhan): https://github.com/flutter/flutter/issues/50770