CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-compose-components--components-resources

Resource management library for Compose Multiplatform applications providing type-safe access to images, strings, fonts, and drawable assets across all platforms.

Pending
Overview
Eval results
Files

font-resources.mddocs/

Font Resources

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.

Core Types

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 Functions

Font Creation

@Composable
expect fun Font(
    resource: FontResource,
    weight: FontWeight = FontWeight.Normal,
    style: FontStyle = FontStyle.Normal,
    variationSettings: FontVariation.Settings = FontVariation.Settings(weight, style)
): Font

Creates 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 load
  • weight - Font weight (Thin, Light, Normal, Medium, Bold, etc.)
  • style - Font style (Normal, Italic)
  • variationSettings - Custom font variation settings for variable fonts

Usage:

@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
    )
}

Variable Font Support

@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 Functions

Raw Font Bytes

suspend fun getFontResourceBytes(
    environment: ResourceEnvironment,
    resource: FontResource
): ByteArray

Retrieves 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 selection
  • resource - The font resource to load

Usage:

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)
}

Font Weights and Styles

Standard Font Weights

// 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 value

Font Styles

FontStyle.Normal        // Upright text
FontStyle.Italic        // Slanted text

Usage 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 Font Features

Font Variation Axes

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)
    )
}

Font Family Management

Creating Font Families

@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()
    }
}

Supported Font Formats

TrueType Fonts (TTF)

  • Wide compatibility across all platforms
  • Standard format for most fonts
  • Good performance and rendering quality

OpenType Fonts (OTF)

  • Advanced typography features
  • Better support for complex scripts
  • Enhanced glyph rendering

Variable Fonts

  • Single file contains multiple style variations
  • Efficient for reducing bundle size
  • Advanced customization through variation axes

File Extensions:

  • .ttf - TrueType Font
  • .otf - OpenType Font
  • .ttc - TrueType Collection
  • Variable fonts use same extensions with internal variation data

Platform-Specific Behavior

Android

  • Native font rendering with hardware acceleration
  • Support for all standard font formats
  • Automatic font fallback for missing glyphs

Desktop (JVM)

  • Skia-based font rendering for consistency
  • Cross-platform font metrics and appearance
  • Support for system font fallbacks

iOS

  • Core Text integration for optimal performance
  • Native font rendering with Apple's font stack
  • Excellent text rendering quality

Web (JS/Wasm)

  • Browser-based font rendering
  • Web font loading with proper fallbacks
  • Canvas text rendering for complex layouts

Performance Considerations

Font Loading

Best Practices:

  1. Preload frequently used fonts in app initialization
  2. Use font families efficiently - avoid loading unused weights/styles
  3. Consider variable fonts for multiple style variations
  4. Cache font objects when possible

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()
    }
}

Memory Management

  • Font objects are cached automatically
  • Unused fonts are eligible for garbage collection
  • Variable fonts are more memory-efficient than multiple static fonts

Error Handling

Common Exceptions:

  • MissingResourceException - Font file not found
  • IllegalArgumentException - Invalid font resource
  • Platform-specific font loading errors

Example 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
    )
}

Best Practices

  1. Organize fonts by family:

    font/
    ├── roboto_regular.ttf
    ├── roboto_bold.ttf
    ├── roboto_italic.ttf
    └── roboto_bold_italic.ttf
  2. Use descriptive naming:

    Font(Res.font.headline_bold)    // Good
    Font(Res.font.font1)           // Bad
  3. Consider font licensing:

    • Ensure fonts are properly licensed for distribution
    • Check license compatibility with your app's license
  4. Test across platforms:

    • Font rendering can vary between platforms
    • Test with actual content and different languages
  5. 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.
    )
  6. Use variable fonts when appropriate:

    • Single file for multiple variations
    • Smooth transitions between weights/styles
    • Better performance for complex typography needs

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-compose-components--components-resources

docs

font-resources.md

image-drawable-resources.md

index.md

plural-string-resources.md

resource-environment.md

string-array-resources.md

string-resources.md

tile.json