Skip to content

Commit 00904dd

Browse files
author
Harry Terkelsen
authored
Various fixes in CanvasKit (flutter#16433)
- Enable dynamically loaded fonts - Fix addOval - Fix getBoxesForRange for negative ranges
1 parent 03f639e commit 00904dd

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

lib/web_ui/lib/src/engine/compositor/fonts.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@
44

55
part of engine;
66

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

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

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

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

31+
Future<void> loadFontFromList(Uint8List list, {String fontFamily}) async {
32+
_dynamicallyLoadedFonts.add(list);
33+
if (fontFamily != null) {
34+
registeredFamilies.add(fontFamily);
35+
}
36+
await ensureFontsLoaded();
37+
}
38+
2339
Future<void> registerFonts(AssetManager assetManager) async {
2440
ByteData byteData;
2541

lib/web_ui/lib/src/engine/compositor/initialization.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const bool experimentalUseSkia =
99
bool.fromEnvironment('FLUTTER_WEB_USE_SKIA', defaultValue: false);
1010

1111
/// The URL to use when downloading the CanvasKit script and associated wasm.
12-
const String canvasKitBaseUrl = 'https://unpkg.com/[email protected]/bin/';
12+
///
13+
/// When CanvasKit pushes a new release to NPM, update this URL to reflect the
14+
/// most recent version. For example, if CanvasKit releases version 0.34.0 to
15+
/// NPM, update this URL to `https://unpkg.com/[email protected]/bin/`.
16+
const String canvasKitBaseUrl = 'https://unpkg.com/[email protected]/bin/';
1317

1418
/// Initialize the Skia backend.
1519
///

lib/web_ui/lib/src/engine/compositor/painting.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class SkPaint extends SkiaObject implements ui.Paint {
3030

3131
@override
3232
ui.PaintingStyle get style => _style;
33+
3334
@override
3435
set style(ui.PaintingStyle value) {
3536
_style = value;

lib/web_ui/lib/src/engine/compositor/path.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class SkPath implements ui.Path {
5656

5757
@override
5858
void addOval(ui.Rect oval) {
59-
_skPath.callMethod('addOval', <dynamic>[makeSkRect(oval), true, 0]);
59+
_skPath.callMethod('addOval', <dynamic>[makeSkRect(oval), false, 1]);
6060
}
6161

6262
@override

lib/web_ui/lib/src/engine/compositor/text.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ class SkTextStyle implements ui.TextStyle {
218218
}
219219
List<String> fontFamilies = <String>[fontFamily];
220220
if (fontFamilyFallback != null) {
221-
fontFamilies.addAll(fontFamilies);
221+
fontFamilies.addAll(fontFamilyFallback);
222222
}
223223

224224
style['fontFamilies'] = fontFamilies;
@@ -343,6 +343,10 @@ class SkParagraph implements ui.Paragraph {
343343
ui.BoxHeightStyle boxHeightStyle: ui.BoxHeightStyle.tight,
344344
ui.BoxWidthStyle boxWidthStyle: ui.BoxWidthStyle.tight,
345345
}) {
346+
if (start < 0 || end < 0) {
347+
return const <ui.TextBox>[];
348+
}
349+
346350
js.JsObject heightStyle;
347351
switch (boxHeightStyle) {
348352
case ui.BoxHeightStyle.tight:
@@ -413,10 +417,21 @@ class SkParagraph implements ui.Paragraph {
413417
@override
414418
void layout(ui.ParagraphConstraints constraints) {
415419
assert(constraints.width != null);
420+
421+
// Infinite width breaks layout, just use a very large number instead.
422+
// TODO(het): Remove this once https://bugs.chromium.org/p/skia/issues/detail?id=9874
423+
// is fixed.
424+
double width;
425+
const double largeFiniteWidth = 1000000;
426+
if (constraints.width.isInfinite) {
427+
width = largeFiniteWidth;
428+
} else {
429+
width = constraints.width;
430+
}
416431
// TODO(het): CanvasKit throws an exception when laid out with
417432
// a font that wasn't registered.
418433
try {
419-
skParagraph.callMethod('layout', <double>[constraints.width]);
434+
skParagraph.callMethod('layout', <double>[width]);
420435
} catch (e) {
421436
html.window.console.warn('CanvasKit threw an exception while laying '
422437
'out the paragraph. The font was "$_fontFamily". Exception:\n$e');

lib/web_ui/lib/src/ui/text.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,9 +1587,15 @@ abstract class ParagraphBuilder {
15871587
/// * `fontFamily`: The family name used to identify the font in text styles.
15881588
/// If this is not provided, then the family name will be extracted from the font file.
15891589
Future<void> loadFontFromList(Uint8List list, {String fontFamily}) {
1590-
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
1591-
(_) => _sendFontChangeMessage()
1592-
);
1590+
if (engine.experimentalUseSkia) {
1591+
return engine.skiaFontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
1592+
(_) => _sendFontChangeMessage()
1593+
);
1594+
} else {
1595+
return _fontCollection.loadFontFromList(list, fontFamily: fontFamily).then(
1596+
(_) => _sendFontChangeMessage()
1597+
);
1598+
}
15931599
}
15941600

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

0 commit comments

Comments
 (0)