Hello! How can I load font resource in non-composa...
# compose-desktop
m
Hello! How can I load font resource in non-composable code using compose resources?
k
there is no way. the compose resources is for the compose if you would like to have resources in Kotlin multiplatform vote here: https://youtrack.jetbrains.com/issue/KT-49981
๐Ÿ‘ 1
๐Ÿ‘๐Ÿป 1
But you can use the compose resources library to load a font as a byte array
Res.readBytes("files/my-font.ttf")
and then create a Font by
Copy code
fun Font(
    identity: String,
    data: ByteArray,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal
): Font
Note: the method is not available on Android
m
Thanks! Will try that ๐Ÿ™Œ Is Android implementation impossible due to some platform specific issues or can I provide my own?
k
No. Android doesn't have an API to load fonts by byte array
only by a path to assets resource
m
Ok so that means I could store the byte array that I read with compose resources in some tmp file and then read it on android?
k
no. assets is not available to write
๐Ÿ‘ 1
if you have a font in the compose resource's dir then you can get a path as Res.getUri(path to font) and it will be an uri to assets on android
๐ŸŽ‰ 1
if you delete an uri prefix it will be an android specific path to assets
s
moko-resources is a pretty good library to handle this stuff for you. https://github.com/icerockdev/moko-resources
๐Ÿ™Œ 1