[canvaskit] Only check missing glyphs against fallback fonts once per frame (flutter/engine#26776)
This commit is contained in:
@@ -7,7 +7,7 @@ import 'dart:convert';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:ui/src/engine.dart' show sendFontChangeMessage;
|
||||
import 'package:ui/src/engine.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
import '../util.dart';
|
||||
@@ -70,6 +70,151 @@ class FontFallbackData {
|
||||
|
||||
final Map<String, int> fontFallbackCounts = <String, int>{};
|
||||
|
||||
/// A list of code units to check against the global fallback fonts.
|
||||
final Set<int> _codeUnitsToCheckAgainstFallbackFonts = <int>{};
|
||||
|
||||
/// This is [true] if we have scheduled a check for missing code units.
|
||||
///
|
||||
/// We only do this once a frame, since checking if a font supports certain
|
||||
/// code units is very expensive.
|
||||
bool _scheduledCodeUnitCheck = false;
|
||||
|
||||
/// Determines if the given [text] contains any code points which are not
|
||||
/// supported by the current set of fonts.
|
||||
void ensureFontsSupportText(String text, List<String> fontFamilies) {
|
||||
// TODO(hterkelsen): Make this faster for the common case where the text
|
||||
// is supported by the given fonts.
|
||||
|
||||
// If the text is ASCII, then skip this check.
|
||||
bool isAscii = true;
|
||||
for (int i = 0; i < text.length; i++) {
|
||||
if (text.codeUnitAt(i) >= 160) {
|
||||
isAscii = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isAscii) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We have a cache of code units which are known to be covered by at least
|
||||
// one of our fallback fonts, and a cache of code units which are known not
|
||||
// to be covered by any fallback font. From the given text, construct a set
|
||||
// of code units which need to be checked.
|
||||
final Set<int> runesToCheck = <int>{};
|
||||
for (final int rune in text.runes) {
|
||||
// Filter out code units which ASCII, known to be covered, or known not
|
||||
// to be covered.
|
||||
if (!(rune < 160 ||
|
||||
knownCoveredCodeUnits.contains(rune) ||
|
||||
codeUnitsWithNoKnownFont.contains(rune))) {
|
||||
runesToCheck.add(rune);
|
||||
}
|
||||
}
|
||||
if (runesToCheck.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
final List<int> codeUnits = runesToCheck.toList();
|
||||
|
||||
List<SkFont> fonts = <SkFont>[];
|
||||
for (var font in fontFamilies) {
|
||||
List<SkFont>? typefacesForFamily =
|
||||
skiaFontCollection.familyToFontMap[font];
|
||||
if (typefacesForFamily != null) {
|
||||
fonts.addAll(typefacesForFamily);
|
||||
}
|
||||
}
|
||||
List<bool> codeUnitsSupported = List<bool>.filled(codeUnits.length, false);
|
||||
String testString = String.fromCharCodes(codeUnits);
|
||||
for (SkFont font in fonts) {
|
||||
Uint8List glyphs = font.getGlyphIDs(testString);
|
||||
assert(glyphs.length == codeUnitsSupported.length);
|
||||
for (int i = 0; i < glyphs.length; i++) {
|
||||
codeUnitsSupported[i] |= glyphs[i] != 0 || _isControlCode(codeUnits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (codeUnitsSupported.any((x) => !x)) {
|
||||
List<int> missingCodeUnits = <int>[];
|
||||
for (int i = 0; i < codeUnitsSupported.length; i++) {
|
||||
if (!codeUnitsSupported[i]) {
|
||||
missingCodeUnits.add(codeUnits[i]);
|
||||
}
|
||||
}
|
||||
_codeUnitsToCheckAgainstFallbackFonts.addAll(missingCodeUnits);
|
||||
if (!_scheduledCodeUnitCheck) {
|
||||
_scheduledCodeUnitCheck = true;
|
||||
// ignore: invalid_use_of_visible_for_testing_member
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.addPostFrameCallback(_ensureFallbackFonts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns [true] if [codepoint] is a Unicode control code.
|
||||
bool _isControlCode(int codepoint) {
|
||||
return codepoint < 32 || (codepoint > 127 && codepoint < 160);
|
||||
}
|
||||
|
||||
/// Checks the missing code units against the current set of fallback fonts
|
||||
/// and starts downloading new fallback fonts if the current set can't cover
|
||||
/// the code units.
|
||||
void _ensureFallbackFonts() {
|
||||
_scheduledCodeUnitCheck = false;
|
||||
// We don't know if the remaining code units are covered by our fallback
|
||||
// fonts. Check them and update the cache.
|
||||
List<int> codeUnits = _codeUnitsToCheckAgainstFallbackFonts.toList();
|
||||
_codeUnitsToCheckAgainstFallbackFonts.clear();
|
||||
List<bool> codeUnitsSupported = List<bool>.filled(codeUnits.length, false);
|
||||
String testString = String.fromCharCodes(codeUnits);
|
||||
|
||||
for (String font in globalFontFallbacks) {
|
||||
List<SkFont>? fontsForFamily = skiaFontCollection.familyToFontMap[font];
|
||||
if (fontsForFamily == null) {
|
||||
printWarning('A fallback font was registered but we '
|
||||
'cannot retrieve the typeface for it.');
|
||||
continue;
|
||||
}
|
||||
for (SkFont font in fontsForFamily) {
|
||||
Uint8List glyphs = font.getGlyphIDs(testString);
|
||||
assert(glyphs.length == codeUnitsSupported.length);
|
||||
for (int i = 0; i < glyphs.length; i++) {
|
||||
bool codeUnitSupported = glyphs[i] != 0;
|
||||
if (codeUnitSupported) {
|
||||
knownCoveredCodeUnits.add(codeUnits[i]);
|
||||
}
|
||||
codeUnitsSupported[i] |=
|
||||
codeUnitSupported || _isControlCode(codeUnits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Once we've checked every typeface for this family, check to see if
|
||||
// every code unit has been covered in order to avoid unnecessary checks.
|
||||
bool keepGoing = false;
|
||||
for (bool supported in codeUnitsSupported) {
|
||||
if (!supported) {
|
||||
keepGoing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!keepGoing) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// If we reached here, then there are some code units which aren't covered
|
||||
// by the global fallback fonts. Remove the ones which were covered and
|
||||
// try to find fallback fonts which cover them.
|
||||
for (int i = codeUnits.length - 1; i >= 0; i--) {
|
||||
if (codeUnitsSupported[i]) {
|
||||
codeUnits.removeAt(i);
|
||||
}
|
||||
}
|
||||
findFontsForMissingCodeunits(codeUnits);
|
||||
}
|
||||
|
||||
void registerFallbackFont(String family, Uint8List bytes) {
|
||||
final SkTypeface? typeface =
|
||||
canvasKit.FontMgr.RefDefault().MakeTypefaceFromData(bytes);
|
||||
@@ -89,11 +234,6 @@ class FontFallbackData {
|
||||
Future<void> findFontsForMissingCodeunits(List<int> codeUnits) async {
|
||||
final FontFallbackData data = FontFallbackData.instance;
|
||||
|
||||
// If all of the code units are known to have no Noto Font which covers them,
|
||||
// then just give up. We have already logged a warning.
|
||||
if (codeUnits.every((u) => data.codeUnitsWithNoKnownFont.contains(u))) {
|
||||
return;
|
||||
}
|
||||
Set<NotoFont> fonts = <NotoFont>{};
|
||||
Set<int> coveredCodeUnits = <int>{};
|
||||
Set<int> missingCodeUnits = <int>{};
|
||||
|
||||
@@ -29,8 +29,7 @@ class SkiaFontCollection {
|
||||
/// Fonts which have been registered and loaded.
|
||||
final List<RegisteredFont> _registeredFonts = <RegisteredFont>[];
|
||||
|
||||
final Map<String, List<SkTypeface>> familyToTypefaceMap =
|
||||
<String, List<SkTypeface>>{};
|
||||
final Map<String, List<SkFont>> familyToFontMap = <String, List<SkFont>>{};
|
||||
|
||||
Future<void> ensureFontsLoaded() async {
|
||||
await _loadFonts();
|
||||
@@ -40,20 +39,20 @@ class SkiaFontCollection {
|
||||
fontProvider = null;
|
||||
}
|
||||
fontProvider = canvasKit.TypefaceFontProvider.Make();
|
||||
familyToTypefaceMap.clear();
|
||||
familyToFontMap.clear();
|
||||
|
||||
for (var font in _registeredFonts) {
|
||||
fontProvider!.registerFont(font.bytes, font.family);
|
||||
familyToTypefaceMap
|
||||
.putIfAbsent(font.family, () => <SkTypeface>[])
|
||||
.add(font.typeface);
|
||||
familyToFontMap
|
||||
.putIfAbsent(font.family, () => <SkFont>[])
|
||||
.add(SkFont(font.typeface));
|
||||
}
|
||||
|
||||
for (var font in FontFallbackData.instance.registeredFallbackFonts) {
|
||||
fontProvider!.registerFont(font.bytes, font.family);
|
||||
familyToTypefaceMap
|
||||
.putIfAbsent(font.family, () => <SkTypeface>[])
|
||||
.add(font.typeface);
|
||||
familyToFontMap
|
||||
.putIfAbsent(font.family, () => <SkFont>[])
|
||||
.add(SkFont(font.typeface));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:ui/src/engine.dart' show frameReferences;
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
@@ -60,4 +61,10 @@ class Rasterizer {
|
||||
}
|
||||
frameReferences.clear();
|
||||
}
|
||||
|
||||
/// Forces the post-frame callbacks to run. Useful in tests.
|
||||
@visibleForTesting
|
||||
void debugRunPostFrameCallbacks() {
|
||||
_runPostFrameCallbacks();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:ui/ui.dart' as ui;
|
||||
|
||||
@@ -86,7 +84,8 @@ class CkParagraphStyle implements ui.ParagraphStyle {
|
||||
return skTextStyle;
|
||||
}
|
||||
|
||||
static SkStrutStyleProperties toSkStrutStyleProperties(ui.StrutStyle value, ui.TextHeightBehavior? paragraphHeightBehavior) {
|
||||
static SkStrutStyleProperties toSkStrutStyleProperties(
|
||||
ui.StrutStyle value, ui.TextHeightBehavior? paragraphHeightBehavior) {
|
||||
CkStrutStyle style = value as CkStrutStyle;
|
||||
final SkStrutStyleProperties skStrutStyle = SkStrutStyleProperties();
|
||||
skStrutStyle.fontFamilies =
|
||||
@@ -100,7 +99,9 @@ class CkParagraphStyle implements ui.ParagraphStyle {
|
||||
skStrutStyle.heightMultiplier = style._height;
|
||||
}
|
||||
|
||||
final ui.TextLeadingDistribution? effectiveLeadingDistribution = style._leadingDistribution ?? paragraphHeightBehavior?.leadingDistribution;
|
||||
final ui.TextLeadingDistribution? effectiveLeadingDistribution =
|
||||
style._leadingDistribution ??
|
||||
paragraphHeightBehavior?.leadingDistribution;
|
||||
switch (effectiveLeadingDistribution) {
|
||||
case null:
|
||||
break;
|
||||
@@ -172,7 +173,8 @@ class CkParagraphStyle implements ui.ParagraphStyle {
|
||||
}
|
||||
|
||||
if (strutStyle != null) {
|
||||
properties.strutStyle = toSkStrutStyleProperties(strutStyle, textHeightBehavior);
|
||||
properties.strutStyle =
|
||||
toSkStrutStyleProperties(strutStyle, textHeightBehavior);
|
||||
}
|
||||
|
||||
properties.textStyle = toSkTextStyleProperties(
|
||||
@@ -536,8 +538,7 @@ SkFontStyle toSkFontStyle(ui.FontWeight? fontWeight, ui.FontStyle? fontStyle) {
|
||||
/// [SynchronousSkiaObjectCache] is used that limits the number of live laid out
|
||||
/// paragraphs at any point in time within or outside the frame.
|
||||
class CkParagraph extends SkiaObject<SkParagraph> implements ui.Paragraph {
|
||||
CkParagraph(
|
||||
this._skParagraph, this._paragraphStyle, this._paragraphCommands);
|
||||
CkParagraph(this._skParagraph, this._paragraphStyle, this._paragraphCommands);
|
||||
|
||||
/// The result of calling `build()` on the JS CkParagraphBuilder.
|
||||
///
|
||||
@@ -609,7 +610,8 @@ class CkParagraph extends SkiaObject<SkParagraph> implements ui.Paragraph {
|
||||
_maxIntrinsicWidth = paragraph.getMaxIntrinsicWidth();
|
||||
_minIntrinsicWidth = paragraph.getMinIntrinsicWidth();
|
||||
_width = paragraph.getMaxWidth();
|
||||
_boxesForPlaceholders = skRectsToTextBoxes(paragraph.getRectsForPlaceholders());
|
||||
_boxesForPlaceholders =
|
||||
skRectsToTextBoxes(paragraph.getRectsForPlaceholders());
|
||||
} catch (e) {
|
||||
printWarning('CanvasKit threw an exception while laying '
|
||||
'out the paragraph. The font was "${_paragraphStyle._fontFamily}". '
|
||||
@@ -633,7 +635,8 @@ class CkParagraph extends SkiaObject<SkParagraph> implements ui.Paragraph {
|
||||
// lot of paragraphs are laid out _and_ rendered. To support
|
||||
// this use-case without blowing up memory usage we need this:
|
||||
// https://github.com/flutter/flutter/issues/81224
|
||||
static SynchronousSkiaObjectCache _paragraphCache = SynchronousSkiaObjectCache(500);
|
||||
static SynchronousSkiaObjectCache _paragraphCache =
|
||||
SynchronousSkiaObjectCache(500);
|
||||
|
||||
/// Marks this paragraph as having been used this frame.
|
||||
///
|
||||
@@ -902,176 +905,21 @@ class CkParagraphBuilder implements ui.ParagraphBuilder {
|
||||
return properties;
|
||||
}
|
||||
|
||||
/// Determines if the given [text] contains any code points which are not
|
||||
/// supported by the current set of fonts.
|
||||
void _ensureFontsSupportText(String text) {
|
||||
// TODO(hterkelsen): Make this faster for the common case where the text
|
||||
// is supported by the given fonts.
|
||||
|
||||
// A list of unique code units in the text.
|
||||
final List<int> codeUnits = text.runes.toSet().toList();
|
||||
|
||||
// First, check if every code unit in the text is known to be covered by one
|
||||
// of our global fallback fonts. We cache the set of code units covered by
|
||||
// the global fallback fonts since this set is growing monotonically over
|
||||
// the lifetime of the app.
|
||||
if (_checkIfGlobalFallbacksSupport(codeUnits)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Next, check if all of the remaining code units are ones which are known
|
||||
// to have no global font fallback. This means we know of no font we can
|
||||
// download which will cover the remaining code units. In this case we can
|
||||
// just skip the checks below, since we know there's nothing we can do to
|
||||
// cover the code units.
|
||||
if (_checkIfNoFallbackFontSupports(codeUnits)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// If the text is ASCII, then skip this check.
|
||||
bool isAscii = true;
|
||||
for (int i = 0; i < text.length; i++) {
|
||||
if (text.codeUnitAt(i) >= 160) {
|
||||
isAscii = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isAscii) {
|
||||
return;
|
||||
}
|
||||
|
||||
CkTextStyle style = _peekStyle();
|
||||
@override
|
||||
void addText(String text) {
|
||||
List<String> fontFamilies = <String>[];
|
||||
CkTextStyle style = _peekStyle();
|
||||
if (style.fontFamily != null) {
|
||||
fontFamilies.add(style.fontFamily!);
|
||||
}
|
||||
if (style.fontFamilyFallback != null) {
|
||||
fontFamilies.addAll(style.fontFamilyFallback!);
|
||||
}
|
||||
List<SkTypeface> typefaces = <SkTypeface>[];
|
||||
for (var font in fontFamilies) {
|
||||
List<SkTypeface>? typefacesForFamily =
|
||||
skiaFontCollection.familyToTypefaceMap[font];
|
||||
if (typefacesForFamily != null) {
|
||||
typefaces.addAll(typefacesForFamily);
|
||||
}
|
||||
}
|
||||
List<bool> codeUnitsSupported = List<bool>.filled(codeUnits.length, false);
|
||||
String testString = String.fromCharCodes(codeUnits);
|
||||
for (SkTypeface typeface in typefaces) {
|
||||
SkFont font = SkFont(typeface);
|
||||
Uint8List glyphs = font.getGlyphIDs(testString);
|
||||
assert(glyphs.length == codeUnitsSupported.length);
|
||||
for (int i = 0; i < glyphs.length; i++) {
|
||||
codeUnitsSupported[i] |= glyphs[i] != 0 || _isControlCode(codeUnits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (codeUnitsSupported.any((x) => !x)) {
|
||||
List<int> missingCodeUnits = <int>[];
|
||||
for (int i = 0; i < codeUnitsSupported.length; i++) {
|
||||
if (!codeUnitsSupported[i]) {
|
||||
missingCodeUnits.add(codeUnits[i]);
|
||||
}
|
||||
}
|
||||
findFontsForMissingCodeunits(missingCodeUnits);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns [true] if [codepoint] is a Unicode control code.
|
||||
bool _isControlCode(int codepoint) {
|
||||
return codepoint < 32 || (codepoint > 127 && codepoint < 160);
|
||||
}
|
||||
|
||||
/// Returns `true` if every code unit in [codeUnits] is covered by a global
|
||||
/// fallback font.
|
||||
///
|
||||
/// Calling this method has 2 side effects:
|
||||
/// 1. Updating the cache of known covered code units in the
|
||||
/// [FontFallbackData] instance.
|
||||
/// 2. Removing known covered code units from [codeUnits]. When the list
|
||||
/// is used again in [_ensureFontsSupportText]
|
||||
bool _checkIfGlobalFallbacksSupport(List<int> codeUnits) {
|
||||
final FontFallbackData fallbackData = FontFallbackData.instance;
|
||||
codeUnits.removeWhere((int codeUnit) =>
|
||||
fallbackData.knownCoveredCodeUnits.contains(codeUnit));
|
||||
if (codeUnits.isEmpty) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// We don't know if the remaining code units are covered by our fallback
|
||||
// fonts. Check them and update the cache.
|
||||
List<bool> codeUnitsSupported = List<bool>.filled(codeUnits.length, false);
|
||||
String testString = String.fromCharCodes(codeUnits);
|
||||
|
||||
for (String font in fallbackData.globalFontFallbacks) {
|
||||
List<SkTypeface>? typefacesForFamily =
|
||||
skiaFontCollection.familyToTypefaceMap[font];
|
||||
if (typefacesForFamily == null) {
|
||||
printWarning('A fallback font was registered but we '
|
||||
'cannot retrieve the typeface for it.');
|
||||
continue;
|
||||
}
|
||||
for (SkTypeface typeface in typefacesForFamily) {
|
||||
SkFont font = SkFont(typeface);
|
||||
Uint8List glyphs = font.getGlyphIDs(testString);
|
||||
assert(glyphs.length == codeUnitsSupported.length);
|
||||
for (int i = 0; i < glyphs.length; i++) {
|
||||
bool codeUnitSupported = glyphs[i] != 0;
|
||||
if (codeUnitSupported) {
|
||||
fallbackData.knownCoveredCodeUnits.add(codeUnits[i]);
|
||||
}
|
||||
codeUnitsSupported[i] |=
|
||||
codeUnitSupported || _isControlCode(codeUnits[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Once we've checked every typeface for this family, check to see if
|
||||
// every code unit has been covered in order to avoid unnecessary checks.
|
||||
bool keepGoing = false;
|
||||
for (bool supported in codeUnitsSupported) {
|
||||
if (!supported) {
|
||||
keepGoing = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!keepGoing) {
|
||||
// Every code unit is supported, clear [codeUnits] and return `true`.
|
||||
codeUnits.clear();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// If we reached here, then there are some code units which aren't covered
|
||||
// by the global fallback fonts. Remove the ones which were covered and
|
||||
// return false.
|
||||
for (int i = codeUnits.length - 1; i >= 0; i--) {
|
||||
if (codeUnitsSupported[i]) {
|
||||
codeUnits.removeAt(i);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Returns `true` if every code unit in [codeUnits] is known to not have any
|
||||
/// fallback font which can cover it.
|
||||
///
|
||||
/// This method has a side effect of removing every code unit from [codeUnits]
|
||||
/// which is known not to have a fallback font which covers it.
|
||||
bool _checkIfNoFallbackFontSupports(List<int> codeUnits) {
|
||||
final FontFallbackData fallbackData = FontFallbackData.instance;
|
||||
codeUnits.removeWhere((int codeUnit) =>
|
||||
fallbackData.codeUnitsWithNoKnownFont.contains(codeUnit));
|
||||
return codeUnits.isEmpty;
|
||||
}
|
||||
|
||||
@override
|
||||
void addText(String text) {
|
||||
_ensureFontsSupportText(text);
|
||||
FontFallbackData.instance.ensureFontsSupportText(text, fontFamilies);
|
||||
_commands.add(_ParagraphCommand.addText(text));
|
||||
_paragraphBuilder.addText(text);
|
||||
}
|
||||
|
||||
@override
|
||||
CkParagraph build() {
|
||||
final builtParagraph = _buildSkParagraph();
|
||||
|
||||
@@ -1344,6 +1344,8 @@ typedef PictureGenerator = CkPicture Function();
|
||||
Future<CkPicture> generatePictureWhenFontsStable(
|
||||
PictureGenerator generator) async {
|
||||
CkPicture picture = generator();
|
||||
// Fallback fonts start downloading as a post-frame callback.
|
||||
EnginePlatformDispatcher.instance.rasterizer!.debugRunPostFrameCallbacks();
|
||||
// Font downloading begins asynchronously so we inject a timer before checking the download queue.
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
while (notoDownloadQueue.isPending ||
|
||||
@@ -1351,6 +1353,7 @@ Future<CkPicture> generatePictureWhenFontsStable(
|
||||
await notoDownloadQueue.debugWhenIdle();
|
||||
await notoDownloadQueue.downloader.debugWhenIdle();
|
||||
picture = generator();
|
||||
EnginePlatformDispatcher.instance.rasterizer!.debugRunPostFrameCallbacks();
|
||||
// Dummy timer for the same reason as above.
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
}
|
||||
|
||||
@@ -96,6 +96,8 @@ void testMain() {
|
||||
);
|
||||
pb.addText('مرحبا');
|
||||
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.debugRunPostFrameCallbacks();
|
||||
await fontChangeCompleter.future;
|
||||
|
||||
expect(FontFallbackData.instance.globalFontFallbacks,
|
||||
@@ -160,6 +162,8 @@ void testMain() {
|
||||
);
|
||||
pb.addText('Hello 😊');
|
||||
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.debugRunPostFrameCallbacks();
|
||||
await fontChangeCompleter.future;
|
||||
|
||||
expect(FontFallbackData.instance.globalFontFallbacks,
|
||||
@@ -201,6 +205,8 @@ void testMain() {
|
||||
pb.addText('مرحبا');
|
||||
|
||||
// Flush microtasks and test that we didn't start any downloads.
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.debugRunPostFrameCallbacks();
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
|
||||
expect(notoDownloadQueue.isPending, isFalse);
|
||||
@@ -210,12 +216,17 @@ void testMain() {
|
||||
// Regression test for https://github.com/flutter/flutter/issues/75836
|
||||
// When we had this bug our font fallback resolution logic would end up in an
|
||||
// infinite loop and this test would freeze and time out.
|
||||
test('Can find fonts for two adjacent unmatched code units from different fonts', () async {
|
||||
final LoggingDownloader loggingDownloader = LoggingDownloader(NotoDownloader());
|
||||
test(
|
||||
'Can find fonts for two adjacent unmatched code units from different fonts',
|
||||
() async {
|
||||
final LoggingDownloader loggingDownloader =
|
||||
LoggingDownloader(NotoDownloader());
|
||||
notoDownloadQueue.downloader = loggingDownloader;
|
||||
// Try rendering text that requires fallback fonts, initially before the fonts are loaded.
|
||||
|
||||
CkParagraphBuilder(CkParagraphStyle()).addText('ヽಠ');
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.debugRunPostFrameCallbacks();
|
||||
await notoDownloadQueue.downloader.debugWhenIdle();
|
||||
expect(
|
||||
loggingDownloader.log,
|
||||
@@ -232,64 +243,77 @@ void testMain() {
|
||||
// Do the same thing but this time with loaded fonts.
|
||||
loggingDownloader.log.clear();
|
||||
CkParagraphBuilder(CkParagraphStyle()).addText('ヽಠ');
|
||||
EnginePlatformDispatcher.instance.rasterizer!
|
||||
.debugRunPostFrameCallbacks();
|
||||
await notoDownloadQueue.downloader.debugWhenIdle();
|
||||
expect(loggingDownloader.log, isEmpty);
|
||||
});
|
||||
|
||||
test('findMinimumFontsForCodeunits for all supported code units', () async {
|
||||
final LoggingDownloader loggingDownloader = LoggingDownloader(NotoDownloader());
|
||||
final LoggingDownloader loggingDownloader =
|
||||
LoggingDownloader(NotoDownloader());
|
||||
notoDownloadQueue.downloader = loggingDownloader;
|
||||
|
||||
// Collect all supported code units from all fallback fonts in the Noto
|
||||
// font tree.
|
||||
final Set<String> testedFonts = <String>{};
|
||||
final Set<int> supportedUniqueCodeUnits = <int>{};
|
||||
final IntervalTree<NotoFont> notoTree = FontFallbackData.instance.notoTree;
|
||||
final IntervalTree<NotoFont> notoTree =
|
||||
FontFallbackData.instance.notoTree;
|
||||
for (NotoFont font in notoTree.root.enumerateAllElements()) {
|
||||
testedFonts.add(font.name);
|
||||
for (CodeunitRange range in font.approximateUnicodeRanges) {
|
||||
for (int codeUnit = range.start; codeUnit < range.end; codeUnit += 1) {
|
||||
for (int codeUnit = range.start;
|
||||
codeUnit < range.end;
|
||||
codeUnit += 1) {
|
||||
supportedUniqueCodeUnits.add(codeUnit);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
expect(supportedUniqueCodeUnits.length, greaterThan(10000)); // sanity check
|
||||
expect(testedFonts, unorderedEquals(<String>{
|
||||
'Noto Sans',
|
||||
'Noto Sans Malayalam UI',
|
||||
'Noto Sans Armenian',
|
||||
'Noto Sans Georgian',
|
||||
'Noto Sans Hebrew',
|
||||
'Noto Naskh Arabic UI',
|
||||
'Noto Sans Devanagari UI',
|
||||
'Noto Sans Telugu UI',
|
||||
'Noto Sans Tamil UI',
|
||||
'Noto Sans Kannada UI',
|
||||
'Noto Sans Sinhala',
|
||||
'Noto Sans Gurmukhi UI',
|
||||
'Noto Sans Gujarati UI',
|
||||
'Noto Sans Bengali UI',
|
||||
'Noto Sans Thai UI',
|
||||
'Noto Sans Lao UI',
|
||||
'Noto Sans Myanmar UI',
|
||||
'Noto Sans Ethiopic',
|
||||
'Noto Sans Khmer UI',
|
||||
'Noto Sans SC',
|
||||
'Noto Sans JP',
|
||||
'Noto Sans TC',
|
||||
'Noto Sans HK',
|
||||
'Noto Sans KR',
|
||||
'Noto Sans Egyptian Hieroglyphs',
|
||||
}));
|
||||
expect(
|
||||
supportedUniqueCodeUnits.length, greaterThan(10000)); // sanity check
|
||||
expect(
|
||||
testedFonts,
|
||||
unorderedEquals(<String>{
|
||||
'Noto Sans',
|
||||
'Noto Sans Malayalam UI',
|
||||
'Noto Sans Armenian',
|
||||
'Noto Sans Georgian',
|
||||
'Noto Sans Hebrew',
|
||||
'Noto Naskh Arabic UI',
|
||||
'Noto Sans Devanagari UI',
|
||||
'Noto Sans Telugu UI',
|
||||
'Noto Sans Tamil UI',
|
||||
'Noto Sans Kannada UI',
|
||||
'Noto Sans Sinhala',
|
||||
'Noto Sans Gurmukhi UI',
|
||||
'Noto Sans Gujarati UI',
|
||||
'Noto Sans Bengali UI',
|
||||
'Noto Sans Thai UI',
|
||||
'Noto Sans Lao UI',
|
||||
'Noto Sans Myanmar UI',
|
||||
'Noto Sans Ethiopic',
|
||||
'Noto Sans Khmer UI',
|
||||
'Noto Sans SC',
|
||||
'Noto Sans JP',
|
||||
'Noto Sans TC',
|
||||
'Noto Sans HK',
|
||||
'Noto Sans KR',
|
||||
'Noto Sans Egyptian Hieroglyphs',
|
||||
}));
|
||||
|
||||
// Construct random paragraphs out of supported code units.
|
||||
final math.Random random = math.Random(0);
|
||||
final List<int> supportedCodeUnits = supportedUniqueCodeUnits.toList()..shuffle(random);
|
||||
final List<int> supportedCodeUnits = supportedUniqueCodeUnits.toList()
|
||||
..shuffle(random);
|
||||
const int paragraphLength = 3;
|
||||
|
||||
for (int batchStart = 0; batchStart < supportedCodeUnits.length; batchStart += paragraphLength) {
|
||||
final int batchEnd = math.min(batchStart + paragraphLength, supportedCodeUnits.length);
|
||||
for (int batchStart = 0;
|
||||
batchStart < supportedCodeUnits.length;
|
||||
batchStart += paragraphLength) {
|
||||
final int batchEnd =
|
||||
math.min(batchStart + paragraphLength, supportedCodeUnits.length);
|
||||
final Set<int> codeUnits = <int>{};
|
||||
for (int i = batchStart; i < batchEnd; i += 1) {
|
||||
codeUnits.add(supportedCodeUnits[i]);
|
||||
@@ -323,7 +347,8 @@ void testMain() {
|
||||
class TestDownloader extends NotoDownloader {
|
||||
static final Map<String, String> mockDownloads = <String, String>{};
|
||||
@override
|
||||
Future<String> downloadAsString(String url, {String? debugDescription}) async {
|
||||
Future<String> downloadAsString(String url,
|
||||
{String? debugDescription}) async {
|
||||
if (mockDownloads.containsKey(url)) {
|
||||
return mockDownloads[url]!;
|
||||
} else {
|
||||
|
||||
@@ -140,8 +140,10 @@ void _tests() {
|
||||
spy.fakeAsync.elapse(const Duration(seconds: 2));
|
||||
expect(
|
||||
spy.printLog,
|
||||
['Engine counters:\n'
|
||||
' TestSkDeletable created: 1\n'],
|
||||
[
|
||||
'Engine counters:\n'
|
||||
' TestSkDeletable created: 1\n'
|
||||
],
|
||||
);
|
||||
|
||||
expect(clone.box.debugGetStackTraces().length, 1);
|
||||
@@ -165,9 +167,11 @@ void _tests() {
|
||||
spy.fakeAsync.elapse(const Duration(seconds: 2));
|
||||
expect(
|
||||
spy.printLog,
|
||||
['Engine counters:\n'
|
||||
' TestSkDeletable created: 1\n'
|
||||
' TestSkDeletable deleted: 1\n'],
|
||||
[
|
||||
'Engine counters:\n'
|
||||
' TestSkDeletable created: 1\n'
|
||||
' TestSkDeletable deleted: 1\n'
|
||||
],
|
||||
);
|
||||
Instrumentation.enabled = false;
|
||||
});
|
||||
@@ -425,6 +429,11 @@ class FakeRasterizer implements Rasterizer {
|
||||
void setSkiaResourceCacheMaxBytes(int bytes) {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
@override
|
||||
void debugRunPostFrameCallbacks() {
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
||||
|
||||
class TestSelfManagedObject extends SkiaObject<TestSkDeletable> {
|
||||
|
||||
Reference in New Issue
Block a user