Use new unresolvedCodePoints API from skia. (flutter/engine#41991)

NOTE: this works off of a skia CL that is not merged yet, so we shouldn't merge this until it actually lands in skia and rolls into the engine. See https://skia-review.googlesource.com/c/skia/+/695716
This commit is contained in:
Jackson Gardner
2023-05-16 11:33:12 -07:00
committed by GitHub
parent 6d1e5ca312
commit 148b626638
8 changed files with 112 additions and 40 deletions

View File

@@ -120,15 +120,19 @@ class FontFallbackManager {
registry.getMissingCodePoints(codePoints, fontFamilies);
if (missingCodePoints.isNotEmpty) {
_codePointsToCheckAgainstFallbackFonts.addAll(missingCodePoints);
if (!_scheduledCodePointCheck) {
_scheduledCodePointCheck = true;
_idleFuture = Future<void>.delayed(Duration.zero, () async {
_ensureFallbackFonts();
_scheduledCodePointCheck = false;
await downloadQueue.waitForIdle();
});
}
addMissingCodePoints(codePoints);
}
}
void addMissingCodePoints(List<int> codePoints) {
_codePointsToCheckAgainstFallbackFonts.addAll(codePoints);
if (!_scheduledCodePointCheck) {
_scheduledCodePointCheck = true;
_idleFuture = Future<void>.delayed(Duration.zero, () async {
_ensureFallbackFonts();
_scheduledCodePointCheck = false;
await downloadQueue.waitForIdle();
});
}
}
@@ -144,8 +148,7 @@ class FontFallbackManager {
}
final List<int> codePoints = _codePointsToCheckAgainstFallbackFonts.toList();
_codePointsToCheckAgainstFallbackFonts.clear();
final List<int> missingCodePoints = registry.getMissingCodePoints(codePoints, globalFontFallbacks);
findFontsForMissingCodePoints(missingCodePoints);
findFontsForMissingCodePoints(codePoints);
}
void registerFallbackFont(String family) {

View File

@@ -188,8 +188,9 @@ class SkwasmFontCollection implements FlutterFontCollection {
@override
void debugResetFallbackFonts() {
setDefaultFontFamilies(<String>[]);
setDefaultFontFamilies(<String>['Roboto']);
fontFallbackManager = FontFallbackManager(SkwasmFallbackRegistry(this));
fontCollectionClearCaches(handle);
}
}

View File

@@ -74,8 +74,9 @@ class SkwasmLineMetrics implements ui.LineMetrics {
class SkwasmParagraph implements ui.Paragraph {
SkwasmParagraph(this.handle);
ParagraphHandle handle;
final ParagraphHandle handle;
bool _isDisposed = false;
bool _hasCheckedForMissingCodePoints = false;
@override
double get width => paragraphGetWidth(handle);
@@ -102,8 +103,30 @@ class SkwasmParagraph implements ui.Paragraph {
bool get didExceedMaxLines => paragraphGetDidExceedMaxLines(handle);
@override
void layout(ui.ParagraphConstraints constraints) =>
void layout(ui.ParagraphConstraints constraints) {
paragraphLayout(handle, constraints.width);
if (!_hasCheckedForMissingCodePoints) {
_hasCheckedForMissingCodePoints = true;
final int missingCodePointCount = paragraphGetUnresolvedCodePoints(handle, nullptr, 0);
if (missingCodePointCount > 0) {
withStackScope((StackScope scope) {
final Pointer<Uint32> codePointBuffer = scope.allocUint32Array(missingCodePointCount);
final int returnedCodePointCount = paragraphGetUnresolvedCodePoints(
handle,
codePointBuffer,
missingCodePointCount
);
assert(missingCodePointCount == returnedCodePointCount);
renderer.fontCollection.fontFallbackManager!.addMissingCodePoints(
List<int>.generate(
missingCodePointCount,
(int index) => codePointBuffer[index]
)
);
});
}
}
}
List<ui.TextBox> _convertTextBoxList(TextBoxListHandle listHandle) {
final int length = textBoxListGetLength(listHandle);
@@ -562,30 +585,8 @@ class SkwasmParagraphBuilder implements ui.ParagraphBuilder {
placeholderScales.add(scale);
}
List<String> _getEffectiveFonts() {
final List<String> fallbackFonts = renderer.fontCollection.fontFallbackManager!.globalFontFallbacks;
final List<String>? currentFonts =
textStyleStack.isEmpty ? null : textStyleStack.last.style.fontFamilies;
if (currentFonts != null) {
return <String>[
...currentFonts,
...fallbackFonts,
];
} else if (style.defaultFontFamily != null) {
return <String>[
style.defaultFontFamily!,
...fallbackFonts,
];
} else {
return fallbackFonts;
}
}
@override
void addText(String text) {
renderer.fontCollection.fontFallbackManager!.ensureFontsSupportText(
text, _getEffectiveFonts()
);
final SkString16Handle stringHandle = skString16FromDartString(text);
paragraphBuilderAddText(handle, stringHandle);
skString16Free(stringHandle);

View File

@@ -50,3 +50,8 @@ external void fontCollectionRegisterTypeface(
TypefaceHandle typeface,
SkStringHandle fontName,
);
@Native<Void Function(
FontCollectionHandle
)>(symbol: 'fontCollection_clearCaches', isLeaf: true)
external void fontCollectionClearCaches(FontCollectionHandle handle);

View File

@@ -119,3 +119,21 @@ external TextBoxListHandle paragraphGetBoxesForRange(
@Native<TextBoxListHandle Function(ParagraphHandle)>(
symbol: 'paragraph_getBoxesForPlaceholders', isLeaf: true)
external TextBoxListHandle paragraphGetBoxesForPlaceholders(ParagraphHandle handle);
// Returns a list of the code points that were unable to be rendered with the
// selected fonts. The list is deduplicated, so each code point in the output
// is unique.
// If `nullptr` is passed in for `outCodePoints`, we simply return the count
// of the code points.
// Note: This must be called after the paragraph has been laid out at least
// once in order to get valid data.
@Native<Int Function(
ParagraphHandle,
Pointer<Uint32>,
Int,
)>(symbol: 'paragraph_getUnresolvedCodePoints', isLeaf: true)
external int paragraphGetUnresolvedCodePoints(
ParagraphHandle handle,
Pointer<Uint32> outCodePoints,
int outLength,
);

View File

@@ -84,3 +84,8 @@ SKWASM_EXPORT void fontCollection_registerTypeface(
collection->provider->registerTypeface(sk_sp<SkTypeface>(typeface));
}
}
SKWASM_EXPORT void fontCollection_clearCaches(
FlutterFontCollection* collection) {
collection->collection->clearCaches();
}

View File

@@ -118,3 +118,28 @@ SKWASM_EXPORT TextBoxList* paragraph_getBoxesForPlaceholders(
Paragraph* paragraph) {
return new TextBoxList{paragraph->getRectsForPlaceholders()};
}
// Returns a list of the code points that were unable to be rendered with the
// selected fonts. The list is deduplicated, so each code point in the output
// is unique.
// If `nullptr` is passed in for `outCodePoints`, we simply return the count
// of the code points.
// Note: This must be called after the paragraph has been laid out at least
// once in order to get valid data.
SKWASM_EXPORT int paragraph_getUnresolvedCodePoints(Paragraph* paragraph,
SkUnichar* outCodePoints,
int outLength) {
if (!outCodePoints) {
return paragraph->unresolvedCodepoints().size();
}
int outIndex = 0;
for (SkUnichar character : paragraph->unresolvedCodepoints()) {
if (outIndex < outLength) {
outCodePoints[outIndex] = character;
outIndex++;
} else {
break;
}
}
return outIndex;
}

View File

@@ -62,6 +62,7 @@ void testMain() {
ui.ParagraphStyle(),
);
pb.addText('مرحبا');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
@@ -100,6 +101,7 @@ void testMain() {
ui.ParagraphStyle(),
);
pb.addText('مرحبا');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
@@ -134,6 +136,7 @@ void testMain() {
ui.ParagraphStyle(),
);
pb.addText('Hello 😊');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
@@ -170,7 +173,10 @@ void testMain() {
() async {
// Try rendering text that requires fallback fonts, initially before the fonts are loaded.
ui.ParagraphBuilder(ui.ParagraphStyle()).addText('ヽಠ');
ui.ParagraphBuilder pb = ui.ParagraphBuilder(ui.ParagraphStyle());
pb.addText('ヽಠ');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
expect(
downloadedFontFamilies,
@@ -182,7 +188,9 @@ void testMain() {
// Do the same thing but this time with loaded fonts.
downloadedFontFamilies.clear();
ui.ParagraphBuilder(ui.ParagraphStyle()).addText('ヽಠ');
pb = ui.ParagraphBuilder(ui.ParagraphStyle());
pb.addText('ヽಠ');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
expect(downloadedFontFamilies, isEmpty);
});
@@ -190,7 +198,10 @@ void testMain() {
test('can find glyph for 2/3 symbol', () async {
// Try rendering text that requires fallback fonts, initially before the fonts are loaded.
ui.ParagraphBuilder(ui.ParagraphStyle()).addText('⅔');
ui.ParagraphBuilder pb = ui.ParagraphBuilder(ui.ParagraphStyle());
pb.addText('⅔');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
expect(
downloadedFontFamilies,
@@ -201,7 +212,10 @@ void testMain() {
// Do the same thing but this time with loaded fonts.
downloadedFontFamilies.clear();
ui.ParagraphBuilder(ui.ParagraphStyle()).addText('⅔');
pb = ui.ParagraphBuilder(ui.ParagraphStyle());
pb.addText('⅔');
pb.build().layout(const ui.ParagraphConstraints(width: 1000));
await renderer.fontCollection.fontFallbackManager!.debugWhenIdle();
expect(downloadedFontFamilies, isEmpty);
});