Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

Commit

Permalink
Various fixes in CanvasKit (flutter#16433)
Browse files Browse the repository at this point in the history
- Enable dynamically loaded fonts
- Fix addOval
- Fix getBoxesForRange for negative ranges
  • Loading branch information
Harry Terkelsen authored Feb 6, 2020
1 parent 03f639e commit 00904dd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
16 changes: 16 additions & 0 deletions lib/web_ui/lib/src/engine/compositor/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

part of engine;

// This URL was found by using the Google Fonts Developer API to find the URL
// for Roboto. The API warns that this URL is not stable. In order to update
// this, list out all of the fonts and find the URL for the regular
// Roboto font. The API reference is here:
// https://developers.google.com/fonts/docs/developer_api
const String _robotoUrl =
'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf';

/// Manages the fonts used in the Skia-based backend.
class SkiaFontCollection {
final List<Future<ByteBuffer>> _loadingFontBuffers = <Future<ByteBuffer>>[];
final List<Uint8List> _dynamicallyLoadedFonts = <Uint8List>[];

final Set<String> registeredFamilies = <String>{};

Expand All @@ -17,9 +24,18 @@ class SkiaFontCollection {
(await Future.wait<ByteBuffer>(_loadingFontBuffers))
.map((ByteBuffer buffer) => buffer.asUint8List())
.toList();
fontBuffers.addAll(_dynamicallyLoadedFonts);
skFontMgr = canvasKit['SkFontMgr'].callMethod('FromData', fontBuffers);
}

Future<void> loadFontFromList(Uint8List list, {String fontFamily}) async {
_dynamicallyLoadedFonts.add(list);
if (fontFamily != null) {
registeredFamilies.add(fontFamily);
}
await ensureFontsLoaded();
}

Future<void> registerFonts(AssetManager assetManager) async {
ByteData byteData;

Expand Down
6 changes: 5 additions & 1 deletion lib/web_ui/lib/src/engine/compositor/initialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ const bool experimentalUseSkia =
bool.fromEnvironment('FLUTTER_WEB_USE_SKIA', defaultValue: false);

/// The URL to use when downloading the CanvasKit script and associated wasm.
const String canvasKitBaseUrl = 'https://unpkg.com/canvaskit-wasm@0.11.0/bin/';
///
/// When CanvasKit pushes a new release to NPM, update this URL to reflect the
/// most recent version. For example, if CanvasKit releases version 0.34.0 to
/// NPM, update this URL to `https://unpkg.com/canvaskit-wasm@0.34.0/bin/`.
const String canvasKitBaseUrl = 'https://unpkg.com/canvaskit-wasm@0.12.0/bin/';

/// Initialize the Skia backend.
///
Expand Down
1 change: 1 addition & 0 deletions lib/web_ui/lib/src/engine/compositor/painting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SkPaint extends SkiaObject implements ui.Paint {

@override
ui.PaintingStyle get style => _style;

@override
set style(ui.PaintingStyle value) {
_style = value;
Expand Down
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/compositor/path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SkPath implements ui.Path {

@override
void addOval(ui.Rect oval) {
_skPath.callMethod('addOval', <dynamic>[makeSkRect(oval), true, 0]);
_skPath.callMethod('addOval', <dynamic>[makeSkRect(oval), false, 1]);
}

@override
Expand Down
19 changes: 17 additions & 2 deletions lib/web_ui/lib/src/engine/compositor/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class SkTextStyle implements ui.TextStyle {
}
List<String> fontFamilies = <String>[fontFamily];
if (fontFamilyFallback != null) {
fontFamilies.addAll(fontFamilies);
fontFamilies.addAll(fontFamilyFallback);
}

style['fontFamilies'] = fontFamilies;
Expand Down Expand Up @@ -343,6 +343,10 @@ class SkParagraph implements ui.Paragraph {
ui.BoxHeightStyle boxHeightStyle: ui.BoxHeightStyle.tight,
ui.BoxWidthStyle boxWidthStyle: ui.BoxWidthStyle.tight,
}) {
if (start < 0 || end < 0) {
return const <ui.TextBox>[];
}

js.JsObject heightStyle;
switch (boxHeightStyle) {
case ui.BoxHeightStyle.tight:
Expand Down Expand Up @@ -413,10 +417,21 @@ class SkParagraph implements ui.Paragraph {
@override
void layout(ui.ParagraphConstraints constraints) {
assert(constraints.width != null);

// Infinite width breaks layout, just use a very large number instead.
// TODO(het): Remove this once https://bugs.chromium.org/p/skia/issues/detail?id=9874
// is fixed.
double width;
const double largeFiniteWidth = 1000000;
if (constraints.width.isInfinite) {
width = largeFiniteWidth;
} else {
width = constraints.width;
}
// TODO(het): CanvasKit throws an exception when laid out with
// a font that wasn't registered.
try {
skParagraph.callMethod('layout', <double>[constraints.width]);
skParagraph.callMethod('layout', <double>[width]);
} catch (e) {
html.window.console.warn('CanvasKit threw an exception while laying '
'out the paragraph. The font was "$_fontFamily". Exception:\n$e');
Expand Down
12 changes: 9 additions & 3 deletions lib/web_ui/lib/src/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1587,9 +1587,15 @@ abstract class ParagraphBuilder {
/// * `fontFamily`: The family name used to identify the font in text styles.
/// If this is not provided, then the family name will be extracted from the font file.
Future<void> loadFontFromList(Uint8List list, {String fontFamily}) {
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
(_) => _sendFontChangeMessage()
);
if (engine.experimentalUseSkia) {
return engine.skiaFontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
(_) => _sendFontChangeMessage()
);
} else {
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
(_) => _sendFontChangeMessage()
);
}
}

final ByteData _fontChangeMessage = engine.JSONMessageCodec().encodeMessage(<String, dynamic>{'type': 'fontsChange'});
Expand Down

0 comments on commit 00904dd

Please sign in to comment.