Implement basic text rendering support in CanvasKit backend (flutter/engine#13903)
* Implement basic text rendering support in CanvasKit backend * Update licenses * Address PR comments
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
part of engine;
|
||||
|
||||
class SkiaFontCollection {
|
||||
final Map<String, Map<Map<String, String>, js.JsObject>>
|
||||
_registeredTypefaces = <String, Map<Map<String, String>, js.JsObject>>{};
|
||||
|
||||
final List<Future<void>> _fontLoadingFutures = <Future<void>>[];
|
||||
final List<Future<ByteBuffer>> _loadingFontBuffers = <Future<ByteBuffer>>[];
|
||||
|
||||
Future<void> ensureFontsLoaded() async {
|
||||
await Future.wait(_fontLoadingFutures);
|
||||
final List<Uint8List> fontBuffers =
|
||||
(await Future.wait<ByteBuffer>(_loadingFontBuffers))
|
||||
.map((ByteBuffer buffer) => buffer.asUint8List())
|
||||
.toList();
|
||||
skFontMgr = canvasKit['SkFontMgr'].callMethod('FromData', fontBuffers);
|
||||
}
|
||||
|
||||
Future<void> registerFonts(AssetManager assetManager) async {
|
||||
@@ -48,63 +49,12 @@ class SkiaFontCollection {
|
||||
for (dynamic fontAssetItem in fontAssets) {
|
||||
final Map<String, dynamic> fontAsset = fontAssetItem;
|
||||
final String asset = fontAsset['asset'];
|
||||
final Map<String, String> descriptors = <String, String>{};
|
||||
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<void> _registerFont(
|
||||
String family, String url, Map<String, String> descriptors) async {
|
||||
final dynamic fetchResult = await html.window.fetch(url);
|
||||
final ByteBuffer resultBuffer = await fetchResult.arrayBuffer();
|
||||
final js.JsObject skTypeFace = skFontMgr.callMethod(
|
||||
'MakeTypefaceFromData', <Uint8List>[resultBuffer.asUint8List()]);
|
||||
_registeredTypefaces.putIfAbsent(
|
||||
family, () => <Map<String, String>, 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'], <dynamic>[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<Map<String, String>, js.JsObject> typefaces =
|
||||
_registeredTypefaces[family];
|
||||
js.JsObject skTypeface;
|
||||
|
||||
for (MapEntry<Map<String, String>, js.JsObject> entry
|
||||
in typefaces.entries) {
|
||||
final Map<String, String> 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'], <dynamic>[skTypeface, size]);
|
||||
}
|
||||
|
||||
final js.JsObject skFontMgr =
|
||||
js.JsObject(canvasKit['SkFontMgr']['RefDefault']);
|
||||
js.JsObject skFontMgr;
|
||||
}
|
||||
|
||||
@@ -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', <dynamic>[
|
||||
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
|
||||
|
||||
@@ -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(<String, dynamic>{
|
||||
'font': skFont,
|
||||
'leftToRight': true,
|
||||
'text': engineParagraph.plainText,
|
||||
'width': engineParagraph.width + 1,
|
||||
});
|
||||
final js.JsObject skShapedText =
|
||||
js.JsObject(canvasKit['ShapedText'], <js.JsObject>[skShapedTextOpts]);
|
||||
skCanvas.callMethod('drawText', <dynamic>[
|
||||
skShapedText,
|
||||
offset.dx + engineParagraph._alignOffset,
|
||||
final SkParagraph skParagraph = paragraph;
|
||||
skCanvas.callMethod('drawParagraph', <dynamic>[
|
||||
skParagraph.skParagraph,
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
makeSkPaint(engineParagraph._paint)
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String, String> _fontFamilyOverrides = <String, String>{
|
||||
'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<String, dynamic> toSkTextStyle(
|
||||
String fontFamily,
|
||||
double fontSize,
|
||||
ui.FontWeight fontWeight,
|
||||
ui.FontStyle fontStyle,
|
||||
) {
|
||||
final Map<String, dynamic> skTextStyle = <String, dynamic>{};
|
||||
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<String, dynamic> skParagraphStyle = <String, dynamic>{};
|
||||
|
||||
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>[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<String> fontFamilyFallback,
|
||||
double fontSize,
|
||||
double letterSpacing,
|
||||
double wordSpacing,
|
||||
double height,
|
||||
ui.Locale locale,
|
||||
ui.Paint background,
|
||||
ui.Paint foreground,
|
||||
List<ui.Shadow> shadows,
|
||||
List<ui.FontFeature> fontFeatures,
|
||||
}) {
|
||||
final Map<String, dynamic> style = <String, dynamic>{};
|
||||
|
||||
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<String> fontFamilies = <String>[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>[js.JsObject.jsify(style)]);
|
||||
assert(skTextStyle != null);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, js.JsObject> toSkFontStyle(
|
||||
ui.FontWeight fontWeight, ui.FontStyle fontStyle) {
|
||||
Map<String, js.JsObject> style = <String, js.JsObject>{};
|
||||
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<ui.TextBox> getBoxesForPlaceholders() {
|
||||
return const <ui.TextBox>[];
|
||||
}
|
||||
|
||||
@override
|
||||
List<ui.TextBox> 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<js.JsObject> skRects =
|
||||
skParagraph.callMethod('getRectsForRange', <dynamic>[
|
||||
start,
|
||||
end,
|
||||
heightStyle,
|
||||
widthStyle,
|
||||
]);
|
||||
|
||||
List<ui.TextBox> result = List<ui.TextBox>(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', <double>[
|
||||
offset.dx,
|
||||
offset.dy,
|
||||
]);
|
||||
return fromPositionWithAffinity(positionWithAffinity);
|
||||
}
|
||||
|
||||
@override
|
||||
ui.TextRange getWordBoundary(ui.TextPosition position) {
|
||||
js.JsObject skRange =
|
||||
skParagraph.callMethod('getWordBoundary', <int>[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', <double>[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<ui.LineMetrics> 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',
|
||||
<js.JsObject>[
|
||||
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', <String>[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<double> get placeholderScales => const <double>[];
|
||||
|
||||
@override
|
||||
void pop() {
|
||||
_paragraphBuilder.callMethod('pop');
|
||||
}
|
||||
|
||||
@override
|
||||
void pushStyle(ui.TextStyle style) {
|
||||
final SkTextStyle skStyle = style;
|
||||
_paragraphBuilder
|
||||
.callMethod('pushStyle', <js.JsObject>[skStyle.skTextStyle]);
|
||||
}
|
||||
}
|
||||
@@ -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<double> makeSkPoint(ui.Offset point) {
|
||||
final js.JsArray<double> skPoint = js.JsArray<double>();
|
||||
skPoint.length = 2;
|
||||
|
||||
@@ -22,9 +22,10 @@ Future<void> 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<void> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,7 +471,53 @@ abstract class TextStyle {
|
||||
Paint foreground,
|
||||
List<Shadow> shadows,
|
||||
List<FontFeature> 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.
|
||||
///
|
||||
|
||||
Reference in New Issue
Block a user