-
Notifications
You must be signed in to change notification settings - Fork 6k
Use new unresolvedCodePoints
API from skia.
#41991
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
) | ||
Comment on lines
+121
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is going to be a common occurrence, consider making an extension method for it: extension PointerToList<T extends NativeType> on Pointer<T> {
List<T> toList(int length) {
return List<T>.generate(length, (int index) => this[index]);
}
} but when I tried this, the analyzer didn't like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think probably the reason this is failing is because the |
||
); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand what this third There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(); | ||
} | ||
Comment on lines
+132
to
+134
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, it looks like you are handling the case of |
||
int outIndex = 0; | ||
for (SkUnichar character : paragraph->unresolvedCodepoints()) { | ||
if (outIndex < outLength) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see. Hmm this is a bit confusing and could use a better name or an API change. For example, it might be a good idea to split this into 2 APIs:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A boolean is actually not good enough, we want the API to return the length of the buffer needed to hold all the code points so we can allocate the correct amount. This pattern is actually pretty common for C APIs, where the user of the API passes in |
||
outCodePoints[outIndex] = character; | ||
outIndex++; | ||
} else { | ||
break; | ||
} | ||
} | ||
return outIndex; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quick question: is it possible for
paragraphGetUnresolvedCodePoints
to return a different result for the same paragraph later? E.g. when extra fallback fonts are downloaded.I guess what I'm really asking is whether there's ever a case where we need to reset
paragraphGetUnresolvedCodePoints
to false.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe it can. The reason is that you can't actually change the list of font families on the text styles or paragraph style once it is a paragraph, that's specified at the
ParagraphBuilder
stage. Once you've already created the paragraph, the only way to add more fallback fonts to it is by actually rebuilding the paragraph with new text styles.