Browse Source

[resources] Add a font cache on non-android targets (#5109)

Add a font cache on non-android targets.
Android targets already have own font caching.

Fixes https://youtrack.jetbrains.com/issue/CMP-1477

## Release Notes
### Features - Resources
- To avoid constant reading raw font bytes on each Font usage on
non-android targets, there was added the font cache. Android has own
font cache inside the platform implementation.
pull/5098/head
Konstantin 4 months ago committed by GitHub
parent
commit
3c348bc215
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      components/resources/library/src/skikoMain/kotlin/org/jetbrains/compose/resources/FontResources.skiko.kt

5
components/resources/library/src/skikoMain/kotlin/org/jetbrains/compose/resources/FontResources.skiko.kt

@ -29,13 +29,18 @@ private val emptyFontBase64 =
@OptIn(ExperimentalEncodingApi::class) @OptIn(ExperimentalEncodingApi::class)
private val defaultEmptyFont by lazy { Font("org.jetbrains.compose.emptyFont", Base64.decode(emptyFontBase64)) } private val defaultEmptyFont by lazy { Font("org.jetbrains.compose.emptyFont", Base64.decode(emptyFontBase64)) }
private val fontCache = AsyncCache<String, Font>()
@Composable @Composable
actual fun Font(resource: FontResource, weight: FontWeight, style: FontStyle): Font { actual fun Font(resource: FontResource, weight: FontWeight, style: FontStyle): Font {
val resourceReader = LocalResourceReader.currentOrPreview val resourceReader = LocalResourceReader.currentOrPreview
val fontFile by rememberResourceState(resource, weight, style, { defaultEmptyFont }) { env -> val fontFile by rememberResourceState(resource, weight, style, { defaultEmptyFont }) { env ->
val path = resource.getResourceItemByEnvironment(env).path val path = resource.getResourceItemByEnvironment(env).path
val key = "$path:$weight:$style"
fontCache.getOrLoad(key) {
val fontBytes = resourceReader.read(path) val fontBytes = resourceReader.read(path)
Font(path, fontBytes, weight, style) Font(path, fontBytes, weight, style)
} }
}
return fontFile return fontFile
} }
Loading…
Cancel
Save