Resource management library for Compose Multiplatform applications providing type-safe access to images, strings, fonts, and drawable assets across all platforms.
—
Font resources provide type-safe access to custom fonts with support for variable fonts, multiple weights, styles, and font variation settings. The library handles font loading across all Compose Multiplatform targets with platform-specific optimizations.
class FontResource(id: String, items: Set<ResourceItem>) : Resource(id, items)A font resource represents a font file (TTF, OTF, or variable font) that can be loaded and used in text rendering.
@Composable
expect fun Font(
resource: FontResource,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal,
variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style)
): FontCreates a Font object from a font resource with specified weight, style, and variation settings. This is a platform-specific expect function with different implementations for each target.
Parameters:
resource - The font resource to loadweight - Font weight (Thin, Light, Normal, Medium, Bold, etc.)style - Font style (Normal, Italic)variationSettings - Custom font variation settings for variable fontsUsage:
@Composable
fun CustomText() {
val regularFont = Font(
resource = Res.font.custom_font,
weight = FontWeight.Normal,
style = FontStyle.Normal
)
val boldFont = Font(
resource = Res.font.custom_font,
weight = FontWeight.Bold,
style = FontStyle.Normal
)
val fontFamily = FontFamily(regularFont, boldFont)
Text(
text = "Custom Typography",
fontFamily = fontFamily,
fontWeight = FontWeight.Bold
)
}@Composable
fun VariableFontExample() {
val variableFont = Font(
resource = Res.font.variable_font,
weight = FontWeight.Medium,
style = FontStyle.Normal,
variationSettings = FontVariation.Settings(
FontVariation.weight(500),
FontVariation.slant(-15f),
FontVariation.width(75f)
)
)
Text(
text = "Variable Font Text",
fontFamily = FontFamily(variableFont)
)
}suspend fun getFontResourceBytes(
environment: ResourceEnvironment,
resource: FontResource
): ByteArrayRetrieves the raw byte content of a font resource. Useful for custom font processing or integration with platform-specific font APIs.
Parameters:
environment - Resource environment for variant selectionresource - The font resource to loadUsage:
suspend fun installSystemFont(fontResource: FontResource) {
val environment = getSystemResourceEnvironment()
val fontBytes = getFontResourceBytes(environment, fontResource)
// Install font at system level (platform-specific)
systemFontManager.installFont(fontBytes)
}
suspend fun analyzeFontMetrics(fontResource: FontResource): FontMetrics {
val environment = getSystemResourceEnvironment()
val fontBytes = getFontResourceBytes(environment, fontResource)
return FontAnalyzer.analyze(fontBytes)
}// Available FontWeight values
FontWeight.Thin // 100
FontWeight.ExtraLight // 200
FontWeight.Light // 300
FontWeight.Normal // 400 (default)
FontWeight.Medium // 500
FontWeight.SemiBold // 600
FontWeight.Bold // 700
FontWeight.ExtraBold // 800
FontWeight.Black // 900
// Custom weights
FontWeight(350) // Custom weight valueFontStyle.Normal // Upright text
FontStyle.Italic // Slanted textUsage Examples:
@Composable
fun FontStyleExamples() {
val fontFamily = FontFamily(
Font(Res.font.my_font, FontWeight.Light, FontStyle.Normal),
Font(Res.font.my_font, FontWeight.Normal, FontStyle.Normal),
Font(Res.font.my_font, FontWeight.Bold, FontStyle.Normal),
Font(Res.font.my_font, FontWeight.Normal, FontStyle.Italic)
)
Column {
Text("Light", fontFamily = fontFamily, fontWeight = FontWeight.Light)
Text("Normal", fontFamily = fontFamily, fontWeight = FontWeight.Normal)
Text("Bold", fontFamily = fontFamily, fontWeight = FontWeight.Bold)
Text("Italic", fontFamily = fontFamily, fontStyle = FontStyle.Italic)
}
}Variable fonts support various axes for customization:
FontVariation.weight(value: Int) // Weight axis (wght)
FontVariation.width(value: Float) // Width axis (wdth)
FontVariation.slant(value: Float) // Slant axis (slnt)
FontVariation.italic(value: Float) // Italic axis (ital)
// Custom variation settings
FontVariation.Settings(
FontVariation.Setting("GRAD", 150f), // Custom axis
FontVariation.weight(600),
FontVariation.width(90f)
)Advanced Variable Font Usage:
@Composable
fun AdvancedVariableFont() {
val customSettings = FontVariation.Settings(
FontVariation.weight(450), // Semi-medium weight
FontVariation.width(85f), // Condensed width
FontVariation.slant(-8f), // Slight slant
FontVariation.Setting("GRAD", 100f) // Grade axis
)
val font = Font(
resource = Res.font.variable_font,
variationSettings = customSettings
)
Text(
text = "Advanced Typography",
fontFamily = FontFamily(font)
)
}@Composable
fun CompleteFontFamily() {
val fontFamily = FontFamily(
// Regular variants
Font(Res.font.my_font_regular, FontWeight.Normal, FontStyle.Normal),
Font(Res.font.my_font_italic, FontWeight.Normal, FontStyle.Italic),
// Bold variants
Font(Res.font.my_font_bold, FontWeight.Bold, FontStyle.Normal),
Font(Res.font.my_font_bold_italic, FontWeight.Bold, FontStyle.Italic),
// Light variants
Font(Res.font.my_font_light, FontWeight.Light, FontStyle.Normal),
Font(Res.font.my_font_light_italic, FontWeight.Light, FontStyle.Italic)
)
MaterialTheme(
typography = Typography(
h1 = TextStyle(fontFamily = fontFamily, fontWeight = FontWeight.Bold),
body1 = TextStyle(fontFamily = fontFamily, fontWeight = FontWeight.Normal),
caption = TextStyle(fontFamily = fontFamily, fontWeight = FontWeight.Light)
)
) {
// App content with custom typography
AppContent()
}
}File Extensions:
.ttf - TrueType Font.otf - OpenType Font.ttc - TrueType CollectionBest Practices:
Font Loading Example:
@Composable
fun OptimizedFontLoading() {
// Load fonts once and reuse
val fontFamily = remember {
FontFamily(
Font(Res.font.primary_regular, FontWeight.Normal),
Font(Res.font.primary_bold, FontWeight.Bold)
)
}
// Use throughout the composition
CompositionLocalProvider(LocalTextStyle provides TextStyle(fontFamily = fontFamily)) {
AppContent()
}
}Common Exceptions:
MissingResourceException - Font file not foundIllegalArgumentException - Invalid font resourceExample Error Handling:
@Composable
fun SafeFontLoading() {
val fontFamily = try {
FontFamily(Font(resource = Res.font.custom_font))
} catch (e: MissingResourceException) {
// Fallback to system font
FontFamily.Default
}
Text(
text = "Safe Font Text",
fontFamily = fontFamily
)
}Organize fonts by family:
font/
├── roboto_regular.ttf
├── roboto_bold.ttf
├── roboto_italic.ttf
└── roboto_bold_italic.ttfUse descriptive naming:
Font(Res.font.headline_bold) // Good
Font(Res.font.font1) // BadConsider font licensing:
Test across platforms:
Optimize font selection:
// Load only needed weights and styles
FontFamily(
Font(Res.font.app_font, FontWeight.Normal), // Most common
Font(Res.font.app_font, FontWeight.Bold) // For emphasis
// Skip unused weights like ExtraLight, Black, etc.
)Use variable fonts when appropriate:
Install with Tessl CLI
npx tessl i tessl/maven-org-jetbrains-compose-components--components-resources