Skip to content

Commit 3c348bc

Browse files
authored
[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.
1 parent 78940c1 commit 3c348bc

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@ private val emptyFontBase64 =
2929
@OptIn(ExperimentalEncodingApi::class)
3030
private val defaultEmptyFont by lazy { Font("org.jetbrains.compose.emptyFont", Base64.decode(emptyFontBase64)) }
3131

32+
private val fontCache = AsyncCache<String, Font>()
33+
3234
@Composable
3335
actual fun Font(resource: FontResource, weight: FontWeight, style: FontStyle): Font {
3436
val resourceReader = LocalResourceReader.currentOrPreview
3537
val fontFile by rememberResourceState(resource, weight, style, { defaultEmptyFont }) { env ->
3638
val path = resource.getResourceItemByEnvironment(env).path
37-
val fontBytes = resourceReader.read(path)
38-
Font(path, fontBytes, weight, style)
39+
val key = "$path:$weight:$style"
40+
fontCache.getOrLoad(key) {
41+
val fontBytes = resourceReader.read(path)
42+
Font(path, fontBytes, weight, style)
43+
}
3944
}
4045
return fontFile
4146
}

0 commit comments

Comments
 (0)