rob42
05/03/2024, 2:44 PMrob42
05/07/2024, 1:10 PMrob42
05/07/2024, 2:08 PMFontFamily
and FontFamily.Resolver
are sealed.
I have a hacky workaround, but I can only use font paths and can't look up fonts by identity:
@OptIn(ExperimentalTextApi::class)
fun fontStack(vararg fonts: Font): Font {
require(!fonts.any { it is SystemFont }) { "SystemFont implements its own fallback stack and cannot be used here"}
// Return the first resolvable font in the stack. Exceptions can be caught here, unlike if you were to try
// and use a missing font in a composition.
for (font in fonts) {
try {
fontResolver.resolve(fontFamily = font.toFontFamily(), fontWeight = font.weight, fontStyle = font.style)
return font
} catch (_: Exception) {}
}
// As a last resort, an invalid SystemFont will fall back internally to the platform default font.
return SystemFont(identity = "")
}
private val fontResolver by lazy { createFontFamilyResolver() }