Cdm2883
December 22, 2024, 6:34am
1
Font rendering in Compose Multiplatform has always been something of a puzzle to me.
I’m looking for a way to make it possible to use one or more of the other fonts layer by layer to try to draw a certain glyph when it doesn’t exist in the main font. Something like IDEA’s:
I searched for Use new skiko API to provide font fallbacks by eymar · Pull Request #1400 · JetBrains/compose-multiplatform-core · GitHub but it doesn’t seem to work for my current request for “multiple FontFamilies”. I’ve looked at the code related to LocalFontFamilyResolverImpl
, and I can’t seem to find any interfaces related to glyph lookups.Does anyone know what to do?
1 Like
+1!!! I’ve also always wanted this.
Cdm2883
December 28, 2024, 5:14am
3
In fact, I did a curveball a long time ago and recently rewrote it.
package vip.cdms.orecompose.utils
import androidx.compose.runtime.Composable
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.withStyle
typealias FontFamilyFallback = Pair<FontFamily, (Char) -> Boolean>
val LocalFontsFallback = staticCompositionLocalOf<Array<FontFamilyFallback>?> { null }
@Composable
fun CharSequence.localFontsFallback() = LocalFontsFallback.current
?.takeIf { isNotEmpty() }?.let { fontsFallback(*it) }
?: if (this is AnnotatedString) this else AnnotatedString(this.toString())
fun CharSequence.fontsFallback(vararg fonts: FontFamilyFallback) =
charStyle(*Array(fonts.size) { i -> SpanStyle(fontFamily = fonts[i].first) to fonts[i].second })
This file has been truncated. show original
But performance is a big issue, and it’s very intrusive, and maybe there are potential compatibility issues.
1 Like