canvaskit: fontCollection.enableFontFallback() doesn't seem to work

80 views
Skip to first unread message

Alan Song

unread,
May 1, 2024, 8:16:35 PM5/1/24
to skia-discuss

When using paragraph builder built with `MakeFromFontCollection`, `enableFontFallback()` doesn't seem to perform font fallback when text style doesn't contain `fontFamilies` or contains non-registered fonts, and the paragraph cannot be drawn (its paragraph.getLineMetrics() is empty)


The expectation came from the comment for `enableFontFallback()`https://skia.googlesource.com/skia/+/81ccf3ab27ee093cfe8c6896e7b75fc263b221e2/modules/canvaskit/npm_build/types/index.d.ts#3267

> Enable fallback to dynamically discovered fonts for characters that are not handled by the text style's fonts.

If the "text styles's fonts" meant the `fontFamilies` option, then it seems to suggest that passing a nonexistent font name or not passing anything at all, would employ the `dynamically discovered fonts`.

Reproduction: https://jsfiddle.net/8nty3bwL/1/

The test script is copied from  from https://skia.googlesource.com/skia/+/81ccf3ab27ee093cfe8c6896e7b75fc263b221e2/modules/canvaskit/tests/paragraph_test.js#825 where `fontCollection.enableFontFallback()` is added, and `fontFamilies` is changed to a nonexistent font.

The workaround is to manually pass the fallback font names in the `fontFamilies` option, then font fallback works just fine - so maybe the comment was inaccurate?

line-metrics.png





jlav...@google.com

unread,
May 2, 2024, 12:44:51 PM5/2/24
to skia-discuss

In your code you are using TypefaceFontProvider as a default manager. 
There is a problem: in order to function as a fallback font manager the class should implement onMatchFamilyStyleCharacter method.
TypefaceFontProvider does not have that implementation, it returns null and so the algorithm does not work properly.
(We probably should fix it; I will make a change but not immediately).
Unfortunately, there is no work around this problem for now so you just have to wait.
I will create a bug which is supposed to be visible to public so you can track it: https://g-issues.skia.org/issues/338403014.

jlav...@google.com

unread,
May 2, 2024, 12:51:43 PM5/2/24
to skia-discuss
Correction: the right link is https://issues.skia.org/issues/338403014.

Alan Song

unread,
May 2, 2024, 2:22:41 PM5/2/24
to skia-discuss
Thank you so much for the quick reply! Good to know it'll be eventually fixed, for now I'll just manually pass in the all available font names to make fallback work. 
Reply all
Reply to author
Forward
0 new messages