FontFamily.Resolver


Main interface for resolving FontFamily into a platform-specific typeface for use in Compose-based applications.

Fonts are loaded via Resolver.resolve from a FontFamily and a type request, and return a platform-specific typeface.

Fonts may be preloaded by calling Resolver.preload to avoid text reflow when async fonts load.

Summary

Public functions

suspend Unit
preload(fontFamily: FontFamily)

Preloading resolves and caches all fonts reachable in a FontFamily.

Cmn
State<Any>
resolve(
    fontFamily: FontFamily?,
    fontWeight: FontWeight,
    fontStyle: FontStyle,
    fontSynthesis: FontSynthesis
)

Resolves a typeface using any appropriate logic for the FontFamily.

Cmn

Extension functions

State<Typeface>
FontFamily.Resolver.resolveAsTypeface(
    fontFamily: FontFamily?,
    fontWeight: FontWeight,
    fontStyle: FontStyle,
    fontSynthesis: FontSynthesis
)

Resolve a font to an Android Typeface

android

Public functions

preload

suspend fun preload(fontFamily: FontFamily): Unit

Preloading resolves and caches all fonts reachable in a FontFamily.

It checks the cache first, and if there is a miss, it will fetch from the network.

Fonts are consider reachable if they are the first entry in the fallback chain for any call to resolve.

This method will suspend until:

  1. All FontLoadingStrategy.Async fonts that are reachable have completed loading, or failed to load

  2. All reachable fonts in the fallback chain have been loaded and inserted into the cache

After returning, all fonts with FontLoadingStrategy.Async and FontLoadingStrategy.OptionalLocal will be permanently cached. In contrast to resolve this method will throw when a reachable FontLoadingStrategy.Async font fails to resolve.

All fonts with FontLoadingStrategy.Blocking will be cached with normal eviction rules.

Parameters
fontFamily: FontFamily

the family to resolve all fonts from

Throws
kotlin.IllegalStateException

if any reachable font fails to load

resolve

fun resolve(
    fontFamily: FontFamily? = null,
    fontWeight: FontWeight = FontWeight.Normal,
    fontStyle: FontStyle = FontStyle.Normal,
    fontSynthesis: FontSynthesis = FontSynthesis.All
): State<Any>

Resolves a typeface using any appropriate logic for the FontFamily.

FontListFontFamily will always resolve using fallback chains and load using Font.ResourceLoader.

Platform specific FontFamily will resolve according to platform behavior, as documented for each FontFamily.

Parameters
fontFamily: FontFamily? = null

family to resolve. If null will use FontFamily.Default

fontWeight: FontWeight = FontWeight.Normal

desired font weight

fontStyle: FontStyle = FontStyle.Normal

desired font style

fontSynthesis: FontSynthesis = FontSynthesis.All

configuration for font synthesis

Returns
State<Any>

platform-specific Typeface such as android.graphics.Typeface

Throws
kotlin.IllegalStateException

if the FontFamily cannot resolve a to a typeface

Extension functions

resolveAsTypeface

fun FontFamily.Resolver.resolveAsTypeface(
    fontFamily: FontFamily? = null,
    fontWeight: FontWeight = FontWeight.Normal,
    fontStyle: FontStyle = FontStyle.Normal,
    fontSynthesis: FontSynthesis = FontSynthesis.All
): State<Typeface>

Resolve a font to an Android Typeface

On Android, font resolution always produces an android.graphics.Typeface.

This convenience method converts State to State to avoid casting the result.

Parameters
fontFamily: FontFamily? = null

fontFamily to resolve from

fontWeight: FontWeight = FontWeight.Normal

font weight to resolve in fontFamily, will use closest match if not exact

fontStyle: FontStyle = FontStyle.Normal

italic or upright text, to resolve in fontFamily

fontSynthesis: FontSynthesis = FontSynthesis.All

allow font synthesis if fontFamily or fontStyle don't have an exact match. This will allow "fake bold" (drawing with too wide a brush) and "fake italic" (drawing then skewing) to be applied when no exact match is present for the weight and style.