From 1028e1eafc9fea9585e1c9efcb6a37e0f4727557 Mon Sep 17 00:00:00 2001 From: Harry Terkelsen Date: Tue, 19 Nov 2019 12:37:28 -0800 Subject: [PATCH] Implement basic text rendering support in CanvasKit backend (flutter/engine#13903) * Implement basic text rendering support in CanvasKit backend * Update licenses * Address PR comments --- .../ci/licenses_golden/licenses_flutter | 1 + .../flutter/lib/web_ui/lib/src/engine.dart | 1 + .../lib/src/engine/compositor/fonts.dart | 70 +-- .../lib/src/engine/compositor/path.dart | 8 +- .../engine/compositor/recording_canvas.dart | 23 +- .../lib/src/engine/compositor/text.dart | 495 ++++++++++++++++++ .../lib/src/engine/compositor/util.dart | 25 + .../lib/web_ui/lib/src/ui/initialization.dart | 14 +- .../flutter/lib/web_ui/lib/src/ui/text.dart | 91 +++- 9 files changed, 634 insertions(+), 94 deletions(-) create mode 100644 engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 40add69d39..fe19287614 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -379,6 +379,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/rasterizer.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/recording_canvas.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/runtime_delegate.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/surface.dart +FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/text.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/util.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/vertices.dart FILE: ../../../flutter/lib/web_ui/lib/src/engine/compositor/viewport_metrics.dart diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine.dart b/engine/src/flutter/lib/web_ui/lib/src/engine.dart index 7a130e0909..c1b40aa0dc 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine.dart @@ -44,6 +44,7 @@ part 'engine/compositor/rasterizer.dart'; part 'engine/compositor/recording_canvas.dart'; part 'engine/compositor/runtime_delegate.dart'; part 'engine/compositor/surface.dart'; +part 'engine/compositor/text.dart'; part 'engine/compositor/util.dart'; part 'engine/compositor/vertices.dart'; part 'engine/compositor/viewport_metrics.dart'; diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart index 45707f61de..3c74dc4977 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/fonts.dart @@ -5,13 +5,14 @@ part of engine; class SkiaFontCollection { - final Map, js.JsObject>> - _registeredTypefaces = , js.JsObject>>{}; - - final List> _fontLoadingFutures = >[]; + final List> _loadingFontBuffers = >[]; Future ensureFontsLoaded() async { - await Future.wait(_fontLoadingFutures); + final List fontBuffers = + (await Future.wait(_loadingFontBuffers)) + .map((ByteBuffer buffer) => buffer.asUint8List()) + .toList(); + skFontMgr = canvasKit['SkFontMgr'].callMethod('FromData', fontBuffers); } Future registerFonts(AssetManager assetManager) async { @@ -48,63 +49,12 @@ class SkiaFontCollection { for (dynamic fontAssetItem in fontAssets) { final Map fontAsset = fontAssetItem; final String asset = fontAsset['asset']; - final Map descriptors = {}; - for (String descriptor in fontAsset.keys) { - if (descriptor != 'asset') { - descriptors[descriptor] = '${fontAsset[descriptor]}'; - } - } - _fontLoadingFutures.add(_registerFont( - family, assetManager.getAssetUrl(asset), descriptors)); + _loadingFontBuffers.add(html.window + .fetch(assetManager.getAssetUrl(asset)) + .then((dynamic fetchResult) => fetchResult.arrayBuffer())); } } } - Future _registerFont( - String family, String url, Map descriptors) async { - final dynamic fetchResult = await html.window.fetch(url); - final ByteBuffer resultBuffer = await fetchResult.arrayBuffer(); - final js.JsObject skTypeFace = skFontMgr.callMethod( - 'MakeTypefaceFromData', [resultBuffer.asUint8List()]); - _registeredTypefaces.putIfAbsent( - family, () => , js.JsObject>{}); - _registeredTypefaces[family][descriptors] = skTypeFace; - } - - js.JsObject getFont(String family, double size) { - if (_registeredTypefaces[family] == null) { - if (assertionsEnabled) { - html.window.console.warn('Using unregistered font: $family'); - } - return js.JsObject(canvasKit['SkFont'], [null, size]); - } - - // We don't attempt to find a Typeface matching the text style. Instead, we - // try to find the "default" typeface. The default typeface either has no - // descriptors, or only has a descriptor of font-weight 400 (the default). - final Map, js.JsObject> typefaces = - _registeredTypefaces[family]; - js.JsObject skTypeface; - - for (MapEntry, js.JsObject> entry - in typefaces.entries) { - final Map descriptors = entry.key; - if (descriptors.isEmpty || - (descriptors.length == 1 && descriptors['weight'] == '400')) { - skTypeface = entry.value; - break; - } - } - - // If we couldn't find a suitable default, just use any typeface in the - // family. - if (skTypeface == null) { - skTypeface = typefaces.values.first; - } - - return js.JsObject(canvasKit['SkFont'], [skTypeface, size]); - } - - final js.JsObject skFontMgr = - js.JsObject(canvasKit['SkFontMgr']['RefDefault']); + js.JsObject skFontMgr; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart index e501fb2cbb..da2b6599a8 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/path.dart @@ -41,10 +41,11 @@ class SkPath implements ui.Path { @override void addArc(ui.Rect oval, double startAngle, double sweepAngle) { + const double _toDegrees = 180.0 / math.pi; _skPath.callMethod('addArc', [ makeSkRect(oval), - startAngle, - sweepAngle, + startAngle * _toDegrees, + sweepAngle * _toDegrees, ]); } @@ -205,8 +206,7 @@ class SkPath implements ui.Path { @override ui.Rect getBounds() { final js.JsObject bounds = _skPath.callMethod('getBounds'); - return ui.Rect.fromLTRB( - bounds['fLeft'], bounds['fTop'], bounds['fRight'], bounds['fBottom']); + return fromSkRect(bounds); } @override diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/recording_canvas.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/recording_canvas.dart index 6cb4b33ef6..b4ec063c48 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/recording_canvas.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/recording_canvas.dart @@ -179,26 +179,11 @@ class SkRecordingCanvas implements RecordingCanvas { @override void drawParagraph(ui.Paragraph paragraph, ui.Offset offset) { - // TODO(het): This doesn't support most paragraph features. We are just - // creating a font from the family and size, and drawing it with - // ShapedText. - final EngineParagraph engineParagraph = paragraph; - final ParagraphGeometricStyle style = engineParagraph.geometricStyle; - final js.JsObject skFont = skiaFontCollection.getFont( - style.effectiveFontFamily, style.fontSize ?? 12.0); - final js.JsObject skShapedTextOpts = js.JsObject.jsify({ - 'font': skFont, - 'leftToRight': true, - 'text': engineParagraph.plainText, - 'width': engineParagraph.width + 1, - }); - final js.JsObject skShapedText = - js.JsObject(canvasKit['ShapedText'], [skShapedTextOpts]); - skCanvas.callMethod('drawText', [ - skShapedText, - offset.dx + engineParagraph._alignOffset, + final SkParagraph skParagraph = paragraph; + skCanvas.callMethod('drawParagraph', [ + skParagraph.skParagraph, + offset.dx, offset.dy, - makeSkPaint(engineParagraph._paint) ]); } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart new file mode 100644 index 0000000000..4286bd5ab2 --- /dev/null +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/text.dart @@ -0,0 +1,495 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +part of engine; + +// TODO(hterkelsen): Get rid of this once we can register font families with +// custom names. +/// CanvasKit does not yet allow us to specify family names when registering +/// fonts. CanvasKit reads the font name from the font's bytes. So, we map +/// some common family names to how they are registered in the Gallery app. +const Map _fontFamilyOverrides = { + 'Roboto': 'Google Sans', + 'GoogleSans': 'Google Sans', + 'GoogleSansDisplay': 'Google Sans Display', + 'MaterialIcons': 'Material Icons', + 'LibreFranklin': 'LibreFranklin', + 'AbrilFatface': 'Abril Fatface', + 'packages/cupertino_icons/CupertinoIcons': 'CupertinoIcons', + '.SF Pro Text': 'Google Sans', +}; + +class SkParagraphStyle implements ui.ParagraphStyle { + SkParagraphStyle({ + ui.TextAlign textAlign, + ui.TextDirection textDirection, + int maxLines, + String fontFamily, + double fontSize, + double height, + ui.FontWeight fontWeight, + ui.FontStyle fontStyle, + ui.StrutStyle strutStyle, + String ellipsis, + ui.Locale locale, + }) { + skParagraphStyle = toSkParagraphStyle( + textAlign, + textDirection, + maxLines, + fontFamily, + fontSize, + height, + fontWeight, + fontStyle, + ellipsis, + ); + assert(skParagraphStyle != null); + _textDirection = textDirection ?? ui.TextDirection.ltr; + _fontFamily = fontFamily; + } + + js.JsObject skParagraphStyle; + ui.TextDirection _textDirection; + String _fontFamily; + + static Map toSkTextStyle( + String fontFamily, + double fontSize, + ui.FontWeight fontWeight, + ui.FontStyle fontStyle, + ) { + final Map skTextStyle = {}; + if (fontWeight != null || fontStyle != null) { + skTextStyle['fontStyle'] = toSkFontStyle(fontWeight, fontStyle); + } + + if (fontSize != null) { + skTextStyle['fontSize'] = fontSize; + } + + if (fontFamily == null) { + fontFamily = 'Roboto'; + } + if (_fontFamilyOverrides.containsKey(fontFamily)) { + fontFamily = _fontFamilyOverrides[fontFamily]; + } + skTextStyle['fontFamilies'] = [fontFamily]; + + return skTextStyle; + } + + static js.JsObject toSkParagraphStyle( + ui.TextAlign textAlign, + ui.TextDirection textDirection, + int maxLines, + String fontFamily, + double fontSize, + double height, + ui.FontWeight fontWeight, + ui.FontStyle fontStyle, + String ellipsis, + ) { + final Map skParagraphStyle = {}; + + if (textAlign != null) { + switch (textAlign) { + case ui.TextAlign.left: + skParagraphStyle['textAlign'] = canvasKit['TextAlign']['Left']; + break; + case ui.TextAlign.right: + skParagraphStyle['textAlign'] = canvasKit['TextAlign']['Right']; + break; + case ui.TextAlign.center: + skParagraphStyle['textAlign'] = canvasKit['TextAlign']['Center']; + break; + case ui.TextAlign.justify: + skParagraphStyle['textAlign'] = canvasKit['TextAlign']['Justify']; + break; + case ui.TextAlign.start: + skParagraphStyle['textAlign'] = canvasKit['TextAlign']['Start']; + break; + case ui.TextAlign.end: + skParagraphStyle['textAlign'] = canvasKit['TextAlign']['End']; + break; + } + } + + if (textDirection != null) { + switch (textDirection) { + case ui.TextDirection.ltr: + skParagraphStyle['textDirection'] = canvasKit['TextDirection']['LTR']; + break; + case ui.TextDirection.rtl: + skParagraphStyle['textDirection'] = canvasKit['TextDirection']['RTL']; + break; + } + } + + if (height != null) { + skParagraphStyle['heightMultiplier'] = height; + } + + if (maxLines != null) { + skParagraphStyle['maxLines'] = maxLines; + } + + if (ellipsis != null) { + skParagraphStyle['ellipsis'] = ellipsis; + } + + skParagraphStyle['textStyle'] = + toSkTextStyle(fontFamily, fontSize, fontWeight, fontStyle); + + return canvasKit.callMethod( + 'ParagraphStyle', [js.JsObject.jsify(skParagraphStyle)]); + } +} + +class SkTextStyle implements ui.TextStyle { + js.JsObject skTextStyle; + + SkTextStyle({ + ui.Color color, + ui.TextDecoration decoration, + ui.Color decorationColor, + ui.TextDecorationStyle decorationStyle, + double decorationThickness, + ui.FontWeight fontWeight, + ui.FontStyle fontStyle, + ui.TextBaseline textBaseline, + String fontFamily, + List fontFamilyFallback, + double fontSize, + double letterSpacing, + double wordSpacing, + double height, + ui.Locale locale, + ui.Paint background, + ui.Paint foreground, + List shadows, + List fontFeatures, + }) { + final Map style = {}; + + if (background != null) { + style['backgroundColor'] = makeSkPaint(background); + } + + if (color != null) { + style['color'] = color.value; + } + + if (decoration != null) { + int decorationValue = canvasKit['NoDecoration']; + if (decoration.contains(ui.TextDecoration.underline)) { + decorationValue |= canvasKit['UnderlineDecoration']; + } + if (decoration.contains(ui.TextDecoration.overline)) { + decorationValue |= canvasKit['OverlineDecoration']; + } + if (decoration.contains(ui.TextDecoration.lineThrough)) { + decorationValue |= canvasKit['LineThroughDecoration']; + } + style['decoration'] = decorationValue; + } + + if (decorationThickness != null) { + style['decorationThickness'] = decorationThickness; + } + + if (fontSize != null) { + style['fontSize'] = fontSize; + } + + if (fontFamily == null) { + fontFamily = 'Roboto'; + } + + if (_fontFamilyOverrides.containsKey(fontFamily)) { + fontFamily = _fontFamilyOverrides[fontFamily]; + } + List fontFamilies = [fontFamily]; + if (fontFamilyFallback != null) { + fontFamilies.addAll(fontFamilies); + } + + style['fontFamilies'] = fontFamilies; + + if (fontWeight != null || fontStyle != null) { + style['fontStyle'] = toSkFontStyle(fontWeight, fontStyle); + } + + if (foreground != null) { + style['foreground'] = makeSkPaint(foreground); + } + + // TODO(hterkelsen): Add support for + // - decorationColor + // - decorationStyle + // - textBaseline + // - letterSpacing + // - wordSpacing + // - height + // - locale + // - shadows + // - fontFeatures + skTextStyle = canvasKit + .callMethod('TextStyle', [js.JsObject.jsify(style)]); + assert(skTextStyle != null); + } +} + +Map toSkFontStyle( + ui.FontWeight fontWeight, ui.FontStyle fontStyle) { + Map style = {}; + if (fontWeight != null) { + switch (fontWeight) { + case ui.FontWeight.w100: + style['weight'] = canvasKit['FontWeight']['Thin']; + break; + case ui.FontWeight.w200: + style['weight'] = canvasKit['FontWeight']['ExtraLight']; + break; + case ui.FontWeight.w300: + style['weight'] = canvasKit['FontWeight']['Light']; + break; + case ui.FontWeight.w400: + style['weight'] = canvasKit['FontWeight']['Normal']; + break; + case ui.FontWeight.w500: + style['weight'] = canvasKit['FontWeight']['Medium']; + break; + case ui.FontWeight.w600: + style['weight'] = canvasKit['FontWeight']['SemiBold']; + break; + case ui.FontWeight.w700: + style['weight'] = canvasKit['FontWeight']['Bold']; + break; + case ui.FontWeight.w800: + style['weight'] = canvasKit['FontWeight']['ExtraBold']; + break; + case ui.FontWeight.w900: + style['weight'] = canvasKit['FontWeight']['ExtraBlack']; + break; + } + } + + if (fontStyle != null) { + switch (fontStyle) { + case ui.FontStyle.normal: + style['slant'] = canvasKit['FontSlant']['Upright']; + break; + case ui.FontStyle.italic: + style['slant'] = canvasKit['FontSlant']['Italic']; + break; + } + } +} + +class SkParagraph implements ui.Paragraph { + SkParagraph(this.skParagraph, this._textDirection, this._fontFamily); + + final js.JsObject skParagraph; + final ui.TextDirection _textDirection; + final String _fontFamily; + + @override + double get alphabeticBaseline => + skParagraph.callMethod('getAlphabeticBaseline'); + + @override + bool get didExceedMaxLines => skParagraph.callMethod('didExceedMaxLines'); + + @override + double get height => skParagraph.callMethod('getHeight'); + + @override + double get ideographicBaseline => + skParagraph.callMethod('getIdeographicBaseline'); + + @override + double get longestLine => skParagraph.callMethod('getLongestLine'); + + @override + double get maxIntrinsicWidth => + skParagraph.callMethod('getMaxIntrinsicWidth'); + + @override + double get minIntrinsicWidth => + skParagraph.callMethod('getMinIntrinsicWidth'); + + @override + double get width => skParagraph.callMethod('getMaxWidth'); + + // TODO(hterkelsen): Implement placeholders once it's in CanvasKit + @override + List getBoxesForPlaceholders() { + return const []; + } + + @override + List getBoxesForRange( + int start, + int end, { + ui.BoxHeightStyle boxHeightStyle: ui.BoxHeightStyle.tight, + ui.BoxWidthStyle boxWidthStyle: ui.BoxWidthStyle.tight, + }) { + js.JsObject heightStyle; + switch (boxHeightStyle) { + case ui.BoxHeightStyle.tight: + heightStyle = canvasKit['RectHeightStyle']['Tight']; + break; + case ui.BoxHeightStyle.max: + heightStyle = canvasKit['RectHeightStyle']['Max']; + break; + default: + // TODO(hterkelsen): Support all height styles + html.window.console.warn( + 'We do not support $boxHeightStyle. Defaulting to BoxHeightStyle.tight'); + heightStyle = canvasKit['RectHeightStyle']['Tight']; + break; + } + + js.JsObject widthStyle; + switch (boxWidthStyle) { + case ui.BoxWidthStyle.tight: + widthStyle = canvasKit['RectWidthStyle']['Tight']; + break; + case ui.BoxWidthStyle.max: + widthStyle = canvasKit['RectWidthStyle']['Max']; + break; + } + + List skRects = + skParagraph.callMethod('getRectsForRange', [ + start, + end, + heightStyle, + widthStyle, + ]); + + List result = List(skRects.length); + + for (int i = 0; i < skRects.length; i++) { + final js.JsObject rect = skRects[i]; + result[i] = ui.TextBox.fromLTRBD( + rect['fLeft'], + rect['fTop'], + rect['fRight'], + rect['fBottom'], + _textDirection, + ); + } + + return result; + } + + @override + ui.TextPosition getPositionForOffset(ui.Offset offset) { + js.JsObject positionWithAffinity = + skParagraph.callMethod('getGlyphPositionAtCoordinate', [ + offset.dx, + offset.dy, + ]); + return fromPositionWithAffinity(positionWithAffinity); + } + + @override + ui.TextRange getWordBoundary(ui.TextPosition position) { + js.JsObject skRange = + skParagraph.callMethod('getWordBoundary', [position.offset]); + return ui.TextRange(start: skRange['start'], end: skRange['end']); + } + + @override + void layout(ui.ParagraphConstraints constraints) { + assert(constraints.width != null); + // TODO(het): CanvasKit throws an exception when laid out with + // a font that wasn't registered. + try { + skParagraph.callMethod('layout', [constraints.width]); + } catch (e) { + html.window.console.warn('CanvasKit threw an exception while laying ' + 'out the paragraph. The font was "$_fontFamily". Exception:\n$e'); + rethrow; + } + } + + @override + ui.TextRange getLineBoundary(ui.TextPosition position) { + // TODO(hterkelsen): Implement this when it's added to CanvasKit + throw UnimplementedError('getLineBoundary'); + } + + @override + List computeLineMetrics() { + // TODO(hterkelsen): Implement this when it's added to CanvasKit + throw UnimplementedError('computeLineMetrics'); + } +} + +class SkParagraphBuilder implements ui.ParagraphBuilder { + js.JsObject _paragraphBuilder; + ui.TextDirection _textDirection; + String _fontFamily; + + SkParagraphBuilder(ui.ParagraphStyle style) { + SkParagraphStyle skStyle = style; + _textDirection = skStyle._textDirection; + _fontFamily = skStyle._fontFamily; + _paragraphBuilder = canvasKit['ParagraphBuilder'].callMethod( + 'Make', + [ + skStyle.skParagraphStyle, + skiaFontCollection.skFontMgr, + ], + ); + } + + // TODO(hterkelsen): Implement placeholders. + @override + void addPlaceholder( + double width, + double height, + ui.PlaceholderAlignment alignment, { + double scale, + double baselineOffset, + ui.TextBaseline baseline, + }) { + throw UnimplementedError('addPlaceholder'); + } + + @override + void addText(String text) { + _paragraphBuilder.callMethod('addText', [text]); + } + + @override + ui.Paragraph build() { + final SkParagraph paragraph = SkParagraph( + _paragraphBuilder.callMethod('build'), _textDirection, _fontFamily); + _paragraphBuilder.callMethod('delete'); + _paragraphBuilder = null; + return paragraph; + } + + @override + int get placeholderCount => throw UnimplementedError('placeholderCount'); + + // TODO(hterkelsen): Implement this once CanvasKit exposes placeholders. + @override + List get placeholderScales => const []; + + @override + void pop() { + _paragraphBuilder.callMethod('pop'); + } + + @override + void pushStyle(ui.TextStyle style) { + final SkTextStyle skStyle = style; + _paragraphBuilder + .callMethod('pushStyle', [skStyle.skTextStyle]); + } +} diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/util.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/util.dart index 2ed77a6c62..0647b404af 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/util.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/compositor/util.dart @@ -23,6 +23,31 @@ js.JsObject makeSkRRect(ui.RRect rrect) { }); } +ui.Rect fromSkRect(js.JsObject skRect) { + return ui.Rect.fromLTRB( + skRect['fLeft'], + skRect['fTop'], + skRect['fRight'], + skRect['fBottom'], + ); +} + +ui.TextPosition fromPositionWithAffinity(js.JsObject positionWithAffinity) { + if (positionWithAffinity['affinity'] == canvasKit['Affinity']['Upstream']) { + return ui.TextPosition( + offset: positionWithAffinity['pos'], + affinity: ui.TextAffinity.upstream, + ); + } else { + assert(positionWithAffinity['affinity'] == + canvasKit['Affinity']['Downstream']); + return ui.TextPosition( + offset: positionWithAffinity['pos'], + affinity: ui.TextAffinity.downstream, + ); + } +} + js.JsArray makeSkPoint(ui.Offset point) { final js.JsArray skPoint = js.JsArray(); skPoint.length = 2; diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart index 041590bfeb..e72d34e8e1 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/initialization.dart @@ -22,9 +22,10 @@ Future webOnlyInitializePlatform({ assetManager ??= const engine.AssetManager(); await webOnlySetAssetManager(assetManager); - await _fontCollection.ensureFontsLoaded(); if (engine.experimentalUseSkia) { await engine.skiaFontCollection.ensureFontsLoaded(); + } else { + await _fontCollection.ensureFontsLoaded(); } _webOnlyIsInitialized = true; @@ -50,20 +51,21 @@ Future webOnlySetAssetManager(engine.AssetManager assetManager) async { if (engine.experimentalUseSkia) { engine.skiaFontCollection ??= engine.SkiaFontCollection(); + } else { + _fontCollection ??= engine.FontCollection(); + _fontCollection.clear(); } - _fontCollection ??= engine.FontCollection(); - _fontCollection.clear(); if (_assetManager != null) { - await _fontCollection.registerFonts(_assetManager); - if (engine.experimentalUseSkia) { await engine.skiaFontCollection.registerFonts(_assetManager); + } else { + await _fontCollection.registerFonts(_assetManager); } } - if (debugEmulateFlutterTesterEnvironment) { + if (debugEmulateFlutterTesterEnvironment && !engine.experimentalUseSkia) { _fontCollection.debugRegisterTestFonts(); } } diff --git a/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart b/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart index 3e2ad9cd5f..c3dc8edf31 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/ui/text.dart @@ -471,7 +471,53 @@ abstract class TextStyle { Paint foreground, List shadows, List fontFeatures, - }) = engine.EngineTextStyle; + }) { + if (engine.experimentalUseSkia) { + return engine.SkTextStyle( + color: color, + decoration: decoration, + decorationColor: decorationColor, + decorationStyle: decorationStyle, + decorationThickness: decorationThickness, + fontWeight: fontWeight, + fontStyle: fontStyle, + textBaseline: textBaseline, + fontFamily: fontFamily, + fontFamilyFallback: fontFamilyFallback, + fontSize: fontSize, + letterSpacing: letterSpacing, + wordSpacing: wordSpacing, + height: height, + locale: locale, + background: background, + foreground: foreground, + shadows: shadows, + fontFeatures: fontFeatures, + ); + } else { + return engine.EngineTextStyle( + color: color, + decoration: decoration, + decorationColor: decorationColor, + decorationStyle: decorationStyle, + decorationThickness: decorationThickness, + fontWeight: fontWeight, + fontStyle: fontStyle, + textBaseline: textBaseline, + fontFamily: fontFamily, + fontFamilyFallback: fontFamilyFallback, + fontSize: fontSize, + letterSpacing: letterSpacing, + wordSpacing: wordSpacing, + height: height, + locale: locale, + background: background, + foreground: foreground, + shadows: shadows, + fontFeatures: fontFeatures, + ); + } + } } /// An opaque object that determines the configuration used by @@ -543,7 +589,37 @@ abstract class ParagraphStyle { StrutStyle strutStyle, String ellipsis, Locale locale, - }) = engine.EngineParagraphStyle; + }) { + if (engine.experimentalUseSkia) { + return engine.SkParagraphStyle( + textAlign: textAlign, + textDirection: textDirection, + maxLines: maxLines, + fontFamily: fontFamily, + fontSize: fontSize, + height: height, + fontWeight: fontWeight, + fontStyle: fontStyle, + strutStyle: strutStyle, + ellipsis: ellipsis, + locale: locale, + ); + } else { + return engine.EngineParagraphStyle( + textAlign: textAlign, + textDirection: textDirection, + maxLines: maxLines, + fontFamily: fontFamily, + fontSize: fontSize, + height: height, + fontWeight: fontWeight, + fontStyle: fontStyle, + strutStyle: strutStyle, + ellipsis: ellipsis, + locale: locale, + ); + } + } } abstract class StrutStyle { @@ -1216,7 +1292,7 @@ abstract class Paragraph { /// have word breaks on both sides. In such cases, this method will return /// [offset, offset+1]. Word boundaries are defined more precisely in Unicode /// Standard Annex #29 http://www.unicode.org/reports/tr29/#Word_Boundaries - TextRange getWordBoundary(TextPosition position); + TextRange getWordBoundary(TextPosition position); /// Returns the [TextRange] of the line at the given [TextPosition]. /// @@ -1263,8 +1339,13 @@ abstract class Paragraph { abstract class ParagraphBuilder { /// Creates a [ParagraphBuilder] object, which is used to create a /// [Paragraph]. - factory ParagraphBuilder(ParagraphStyle style) => - engine.EngineParagraphBuilder(style); + factory ParagraphBuilder(ParagraphStyle style) { + if (engine.experimentalUseSkia) { + return engine.SkParagraphBuilder(style); + } else { + return engine.EngineParagraphBuilder(style); + } + } /// Applies the given style to the added text until [pop] is called. ///