Roll CanvasKit to 0.17.3. (flutter/engine#20272)

* WIP on using TypefaceFontProvider

* Support pushPaintStyle
This commit is contained in:
Harry Terkelsen
2020-08-06 12:14:36 -07:00
committed by GitHub
parent dc700ae269
commit 5210bc1812
4 changed files with 80 additions and 82 deletions

View File

@@ -43,7 +43,8 @@ class CanvasKit {
external SkFontSlantEnum get FontSlant;
external SkAnimatedImage MakeAnimatedImageFromEncoded(Uint8List imageData);
external SkShaderNamespace get SkShader;
external SkMaskFilter MakeBlurMaskFilter(SkBlurStyle blurStyle, double sigma, bool respectCTM);
external SkMaskFilter MakeBlurMaskFilter(
SkBlurStyle blurStyle, double sigma, bool respectCTM);
external SkColorFilterNamespace get SkColorFilter;
external SkImageFilterNamespace get SkImageFilter;
external SkPath MakePathFromOp(SkPath path1, SkPath path2, SkPathOp pathOp);
@@ -57,7 +58,8 @@ class CanvasKit {
Uint16List? indices,
);
external SkParagraphBuilderNamespace get ParagraphBuilder;
external SkParagraphStyle ParagraphStyle(SkParagraphStyleProperties properties);
external SkParagraphStyle ParagraphStyle(
SkParagraphStyleProperties properties);
external SkTextStyle TextStyle(SkTextStyleProperties properties);
// Text decoration enum is embedded in the CanvasKit object itself.
@@ -68,7 +70,9 @@ class CanvasKit {
// End of text decoration enum.
external SkFontMgrNamespace get SkFontMgr;
external int GetWebGLContext(html.CanvasElement canvas, SkWebGLContextOptions options);
external TypefaceFontProviderNamespace get TypefaceFontProvider;
external int GetWebGLContext(
html.CanvasElement canvas, SkWebGLContextOptions options);
external SkGrContext MakeGrContext(int glContext);
external SkSurface MakeOnScreenGLSurface(
SkGrContext grContext,
@@ -569,7 +573,6 @@ SkStrokeJoin toSkStrokeJoin(ui.StrokeJoin strokeJoin) {
return _skStrokeJoins[strokeJoin.index];
}
@JS()
class SkFilterQualityEnum {
external SkFilterQuality get None;
@@ -594,7 +597,6 @@ SkFilterQuality toSkFilterQuality(ui.FilterQuality filterQuality) {
return _skFilterQualitys[filterQuality.index];
}
@JS()
class SkTileModeEnum {
external SkTileMode get Clamp;
@@ -620,6 +622,7 @@ SkTileMode toSkTileMode(ui.TileMode mode) {
@JS()
class SkAnimatedImage {
external int getFrameCount();
/// Returns duration in milliseconds.
external int getRepetitionCount();
external int decodeNextFrame();
@@ -675,9 +678,7 @@ class SkShaderNamespace {
}
@JS()
class SkShader {
}
class SkShader {}
// This needs to be bound to top-level because SkPaint is initialized
// with `new`. Also in Dart you can't write this:
@@ -873,6 +874,7 @@ Float32List _populateSkColor(SkFloat32List skColor, ui.Color color) {
Float32List toSharedSkColor1(ui.Color color) {
return _populateSkColor(_sharedSkColor1, color);
}
final SkFloat32List _sharedSkColor1 = mallocFloat32List(4);
/// Unpacks the [color] into CanvasKit-compatible representation stored
@@ -883,6 +885,7 @@ final SkFloat32List _sharedSkColor1 = mallocFloat32List(4);
Float32List toSharedSkColor2(ui.Color color) {
return _populateSkColor(_sharedSkColor2, color);
}
final SkFloat32List _sharedSkColor2 = mallocFloat32List(4);
/// Unpacks the [color] into CanvasKit-compatible representation stored
@@ -893,6 +896,7 @@ final SkFloat32List _sharedSkColor2 = mallocFloat32List(4);
Float32List toSharedSkColor3(ui.Color color) {
return _populateSkColor(_sharedSkColor3, color);
}
final SkFloat32List _sharedSkColor3 = mallocFloat32List(4);
Uint32List toSkIntColorList(List<ui.Color> colors) {
@@ -1392,20 +1396,26 @@ class SkParagraphBuilderNamespace {
SkParagraphStyle paragraphStyle,
SkFontMgr? fontManager,
);
external SkParagraphBuilder MakeFromFontProvider(
SkParagraphStyle paragraphStyle,
TypefaceFontProvider? fontManager,
);
}
@JS()
class SkParagraphBuilder {
external void addText(String text);
external void pushStyle(SkTextStyle textStyle);
external void pushPaintStyle(
SkTextStyle textStyle, SkPaint foreground, SkPaint background);
external void pop();
external SkParagraph build();
external void delete();
}
@JS()
class SkParagraphStyle {
}
class SkParagraphStyle {}
@JS()
@anonymous
@@ -1433,9 +1443,7 @@ class SkParagraphStyleProperties {
}
@JS()
class SkTextStyle {
}
class SkTextStyle {}
@JS()
@anonymous
@@ -1481,6 +1489,12 @@ class SkFontMgr {
external void delete();
}
@JS('window.flutter_canvas_kit.TypefaceFontProvider')
class TypefaceFontProvider extends SkFontMgr {
external TypefaceFontProvider();
external void registerFont(Uint8List font, String family);
}
@JS()
class SkParagraph {
external double getAlphabeticBaseline();
@@ -1519,7 +1533,7 @@ class SkTextRange {
}
@JS()
class SkVertices { }
class SkVertices {}
@JS()
@anonymous
@@ -1537,3 +1551,8 @@ class SkFontMgrNamespace {
// TODO(yjbanov): can this be made non-null? It returns null in our unit-tests right now.
external SkFontMgr? FromData(List<Uint8List> fonts);
}
@JS()
class TypefaceFontProviderNamespace {
external TypefaceFontProvider Make();
}

View File

@@ -22,26 +22,16 @@ class SkiaFontCollection {
/// Fonts which have been registered and loaded.
final List<_RegisteredFont?> _registeredFonts = <_RegisteredFont?>[];
/// A mapping from the name a font was registered with, to the family name
/// embedded in the font's bytes (the font's "actual" name).
///
/// For example, a font may be registered in Flutter assets with the name
/// "MaterialIcons", but if you read the family name out of the font's bytes
/// it is actually "Material Icons". Skia works with the actual names of the
/// fonts, so when we create a Skia Paragraph with Flutter font families, we
/// must convert them to their actual family name when we pass them to Skia.
final Map<String, String> fontFamilyOverrides = <String, String>{};
final Set<String?> registeredFamilies = <String?>{};
Future<void> ensureFontsLoaded() async {
await _loadFonts();
_computeFontFamilyOverrides();
final List<Uint8List> fontBuffers =
_registeredFonts.map<Uint8List>((f) => f!.bytes).toList();
fontProvider = canvasKit.TypefaceFontProvider.Make();
skFontMgr = canvasKit.SkFontMgr.FromData(fontBuffers);
for (var font in _registeredFonts) {
fontProvider!.registerFont(font!.bytes, font!.flutterFamily);
}
}
/// Loads all of the unloaded fonts in [_unloadedFonts] and adds them
@@ -51,29 +41,12 @@ class SkiaFontCollection {
return;
}
final List<_RegisteredFont?> loadedFonts = await Future.wait(_unloadedFonts);
final List<_RegisteredFont?> loadedFonts =
await Future.wait(_unloadedFonts);
_registeredFonts.addAll(loadedFonts.where((x) => x != null));
_unloadedFonts.clear();
}
void _computeFontFamilyOverrides() {
fontFamilyOverrides.clear();
for (_RegisteredFont? font in _registeredFonts) {
if (fontFamilyOverrides.containsKey(font!.flutterFamily)) {
if (fontFamilyOverrides[font.flutterFamily] != font.actualFamily) {
html.window.console.warn('Fonts in family ${font.flutterFamily} '
'have different actual family names.');
html.window.console.warn(
'Current actual family: ${fontFamilyOverrides[font.flutterFamily]}');
html.window.console.warn('New actual family: ${font.actualFamily}');
}
} else {
fontFamilyOverrides[font.flutterFamily] = font.actualFamily;
}
}
}
Future<void> loadFontFromList(Uint8List list, {String? fontFamily}) async {
String? actualFamily = _readActualFamilyName(list);
@@ -118,8 +91,9 @@ class SkiaFontCollection {
'There was a problem trying to load FontManifest.json');
}
for (Map<String, dynamic> fontFamily in fontManifest.cast<Map<String, dynamic>>()) {
final String? family = fontFamily['family'];
for (Map<String, dynamic> fontFamily
in fontManifest.cast<Map<String, dynamic>>()) {
final String family = fontFamily['family']!;
final List<dynamic> fontAssets = fontFamily['fonts'];
registeredFamilies.add(family);
@@ -141,10 +115,12 @@ class SkiaFontCollection {
}
}
Future<_RegisteredFont?> _registerFont(String url, String? family) async {
Future<_RegisteredFont?> _registerFont(String url, String family) async {
ByteBuffer buffer;
try {
buffer = await html.window.fetch(url).then(_getArrayBuffer as FutureOr<ByteBuffer> Function(dynamic));
buffer = await html.window
.fetch(url)
.then(_getArrayBuffer as FutureOr<ByteBuffer> Function(dynamic));
} catch (e) {
html.window.console.warn('Failed to load font $family at $url');
html.window.console.warn(e);
@@ -160,7 +136,7 @@ class SkiaFontCollection {
actualFamily = family;
}
return _RegisteredFont(bytes, family!, actualFamily!);
return _RegisteredFont(bytes, family, actualFamily!);
}
String? _readActualFamilyName(Uint8List bytes) {
@@ -178,6 +154,7 @@ class SkiaFontCollection {
}
SkFontMgr? skFontMgr;
TypefaceFontProvider? fontProvider;
}
/// Represents a font that has been registered.

View File

@@ -20,7 +20,7 @@ const bool canvasKitForceCpuOnly =
/// NPM, update this URL to `https://unpkg.com/canvaskit-wasm@0.34.0/bin/`.
const String canvasKitBaseUrl = String.fromEnvironment(
'FLUTTER_WEB_CANVASKIT_URL',
defaultValue: 'https://unpkg.com/canvaskit-wasm@0.17.2/bin/',
defaultValue: 'https://unpkg.com/canvaskit-wasm@0.17.3/bin/',
);
/// Initialize CanvasKit.

View File

@@ -20,17 +20,17 @@ class CkParagraphStyle implements ui.ParagraphStyle {
String? ellipsis,
ui.Locale? locale,
}) : skParagraphStyle = toSkParagraphStyle(
textAlign,
textDirection,
maxLines,
fontFamily,
fontSize,
height,
textHeightBehavior,
fontWeight,
fontStyle,
ellipsis,
) {
textAlign,
textDirection,
maxLines,
fontFamily,
fontSize,
height,
textHeightBehavior,
fontWeight,
fontStyle,
ellipsis,
) {
assert(skParagraphStyle != null);
_textDirection = textDirection ?? ui.TextDirection.ltr;
_fontFamily = fontFamily;
@@ -59,9 +59,6 @@ class CkParagraphStyle implements ui.ParagraphStyle {
!skiaFontCollection.registeredFamilies.contains(fontFamily)) {
fontFamily = 'Roboto';
}
if (skiaFontCollection.fontFamilyOverrides.containsKey(fontFamily)) {
fontFamily = skiaFontCollection.fontFamilyOverrides[fontFamily]!;
}
skTextStyle.fontFamilies = [fontFamily];
return skTextStyle;
@@ -114,6 +111,8 @@ class CkParagraphStyle implements ui.ParagraphStyle {
class CkTextStyle implements ui.TextStyle {
SkTextStyle skTextStyle;
CkPaint? background;
CkPaint? foreground;
factory CkTextStyle({
ui.Color? color,
@@ -173,9 +172,6 @@ class CkTextStyle implements ui.TextStyle {
fontFamily = 'Roboto';
}
if (skiaFontCollection.fontFamilyOverrides.containsKey(fontFamily)) {
fontFamily = skiaFontCollection.fontFamilyOverrides[fontFamily]!;
}
List<String> fontFamilies = <String>[fontFamily];
if (fontFamilyFallback != null &&
!fontFamilyFallback.every((font) => fontFamily == font)) {
@@ -202,14 +198,14 @@ class CkTextStyle implements ui.TextStyle {
// - locale
// - shadows
// - fontFeatures
return CkTextStyle._(canvasKit.TextStyle(properties));
return CkTextStyle._(
canvasKit.TextStyle(properties), foreground, background);
}
CkTextStyle._(this.skTextStyle);
CkTextStyle._(this.skTextStyle, this.foreground, this.background);
}
SkFontStyle toSkFontStyle(
ui.FontWeight? fontWeight, ui.FontStyle? fontStyle) {
SkFontStyle toSkFontStyle(ui.FontWeight? fontWeight, ui.FontStyle? fontStyle) {
final style = SkFontStyle();
if (fontWeight != null) {
style.weight = toSkFontWeight(fontWeight);
@@ -220,7 +216,8 @@ SkFontStyle toSkFontStyle(
return style;
}
class CkParagraph extends ResurrectableSkiaObject<SkParagraph> implements ui.Paragraph {
class CkParagraph extends ResurrectableSkiaObject<SkParagraph>
implements ui.Paragraph {
CkParagraph(
this._initialParagraph, this._paragraphStyle, this._paragraphCommands);
@@ -285,8 +282,7 @@ class CkParagraph extends ResurrectableSkiaObject<SkParagraph> implements ui.Par
bool get isResurrectionExpensive => true;
@override
double get alphabeticBaseline =>
skiaObject.getAlphabeticBaseline();
double get alphabeticBaseline => skiaObject.getAlphabeticBaseline();
@override
bool get didExceedMaxLines => skiaObject.didExceedMaxLines();
@@ -295,8 +291,7 @@ class CkParagraph extends ResurrectableSkiaObject<SkParagraph> implements ui.Par
double get height => skiaObject.getHeight();
@override
double get ideographicBaseline =>
skiaObject.getIdeographicBaseline();
double get ideographicBaseline => skiaObject.getIdeographicBaseline();
@override
double get longestLine => skiaObject.getLongestLine();
@@ -414,9 +409,9 @@ class CkParagraphBuilder implements ui.ParagraphBuilder {
CkParagraphBuilder(ui.ParagraphStyle style)
: _commands = <_ParagraphCommand>[],
_style = style as CkParagraphStyle,
_paragraphBuilder = canvasKit.ParagraphBuilder.Make(
_paragraphBuilder = canvasKit.ParagraphBuilder.MakeFromFontProvider(
style.skParagraphStyle,
skiaFontCollection.skFontMgr,
skiaFontCollection.fontProvider,
);
// TODO(hterkelsen): Implement placeholders.
@@ -468,7 +463,14 @@ class CkParagraphBuilder implements ui.ParagraphBuilder {
void pushStyle(ui.TextStyle style) {
final CkTextStyle skStyle = style as CkTextStyle;
_commands.add(_ParagraphCommand.pushStyle(skStyle));
_paragraphBuilder.pushStyle(skStyle.skTextStyle);
if (skStyle.foreground != null || skStyle.background != null) {
final SkPaint foreground = skStyle.foreground?.skiaObject ?? SkPaint();
final SkPaint background = skStyle.background?.skiaObject ?? SkPaint();
_paragraphBuilder.pushPaintStyle(
skStyle.skTextStyle, foreground, background);
} else {
_paragraphBuilder.pushStyle(skStyle.skTextStyle);
}
}
}