Skip to content

Commit

Permalink
[resources] Add a font cache on non-android targets (#5109)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
terrakok authored Jul 23, 2024
1 parent 78940c1 commit 3c348bc
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,18 @@ private val emptyFontBase64 =
@OptIn(ExperimentalEncodingApi::class)
private val defaultEmptyFont by lazy { Font("org.jetbrains.compose.emptyFont", Base64.decode(emptyFontBase64)) }

private val fontCache = AsyncCache<String, Font>()

@Composable
actual fun Font(resource: FontResource, weight: FontWeight, style: FontStyle): Font {
val resourceReader = LocalResourceReader.currentOrPreview
val fontFile by rememberResourceState(resource, weight, style, { defaultEmptyFont }) { env ->
val path = resource.getResourceItemByEnvironment(env).path
val fontBytes = resourceReader.read(path)
Font(path, fontBytes, weight, style)
val key = "$path:$weight:$style"
fontCache.getOrLoad(key) {
val fontBytes = resourceReader.read(path)
Font(path, fontBytes, weight, style)
}
}
return fontFile
}

0 comments on commit 3c348bc

Please sign in to comment.