Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new skiko API to provide font fallbacks #1400

Merged
merged 4 commits into from
Jun 20, 2024
Merged

Conversation

eymar
Copy link
Collaborator

@eymar eymar commented Jun 11, 2024

TODO:

Description:

This PR updates the implementation details of FontCache in skikoMain:

  • It relies on new skiko API to ensure that manually preloaded and registered fonts can be used as a last fallback, when other fallbacks didn't work out
  • Update the k/wasm demo, so it preloads the emojis font

Fixes:

With this change, it's possible to mix different characters in one Text without composing the text using AnnotatedString.
Although, when the most of glyphs are known to be from a particular font, it's more performant to specify the font explicitly when using Text(...) component or via Theme.

Usage example:

The fallbacks won't work automatically, the fallback fonts should be preloaded like this:

val fontFamilyResolver = LocalFontFamilyResolver.current

...

LaunchedEffect(Unit) {
      val notoEmojisBytes = loadEmojisFontAsBytes() // loadRes(notoColorEmoji).toByteArray()
      val fontFamily = FontFamily(listOf(Font("NotoColorEmoji", notoEmojisBytes)))
      fontFamilyResolver.preload(fontFamily)
      fontsLoaded.value = true
}

Testing

  • Open compose multiplatform core demo: (./gradlew :compose:mpp:demo:wasmJsBrowserRun)
  • Go to Components -> Text Field -> Emojis
  • Both text fields should show the emojis
Screenshot 2024-06-11 at 11 22 56

This should be tested by QA

Release Notes

Fixes - Web

  • Allow preloading the fallback fonts. This enables the usage of emojis and other unicode characters without manually composing the Text with AnnotatedString.

@eymar eymar requested review from igordmn and Schahen June 11, 2024 09:30
private val registered: MutableSet<String> = HashSet()
private val typefacesCache: Cache<String, SkTypeface> = ExpireAfterAccessCache(
60_000_000_000 // 1 minute
)

init {
fonts.setDefaultFontManager(FontMgr.default)
fonts.setDefaultFontManager(FontMgr.defaultWithFallbackFontProvider(fontProvider))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It affects all targets.
In most cases desktop and ios won't be affected, because the default font manager can provide the fallbacks, and therefore the new logic in skiko won't be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants