{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

FontFamily.Resolver

public sealed interface 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 methods

abstract void
preload(@NonNull FontFamily fontFamily)

Preloading resolves and caches all fonts reachable in a FontFamily.

abstract @NonNull State<@NonNull Object>
resolve(
    FontFamily fontFamily,
    @NonNull FontWeight fontWeight,
    @NonNull FontStyle fontStyle,
    @NonNull FontSynthesis fontSynthesis
)

Resolves a typeface using any appropriate logic for the FontFamily.

Extension functions

default final @NonNull State<@NonNull Typeface>
FontFamilyResolverKt.resolveAsTypeface(
    @NonNull FontFamily.Resolver receiver,
    FontFamily fontFamily,
    @NonNull FontWeight fontWeight,
    @NonNull FontStyle fontStyle,
    @NonNull FontSynthesis fontSynthesis
)

Resolve a font to an Android Typeface

Public methods

preload

abstract void preload(@NonNull FontFamily fontFamily)

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
@NonNull FontFamily fontFamily

the family to resolve all fonts from

Throws
kotlin.IllegalStateException

if any reachable font fails to load

resolve

abstract @NonNull State<@NonNull Objectresolve(
    FontFamily fontFamily,
    @NonNull FontWeight fontWeight,
    @NonNull FontStyle fontStyle,
    @NonNull FontSynthesis fontSynthesis
)

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

family to resolve. If null will use FontFamily.Default

@NonNull FontWeight fontWeight

desired font weight

@NonNull FontStyle fontStyle

desired font style

@NonNull FontSynthesis fontSynthesis

configuration for font synthesis

Returns
@NonNull State<@NonNull Object>

platform-specific Typeface such as android.graphics.Typeface

Throws
kotlin.IllegalStateException

if the FontFamily cannot resolve a to a typeface

Extension functions

FontFamilyResolverKt.resolveAsTypeface

default final @NonNull State<@NonNull TypefaceFontFamilyResolverKt.resolveAsTypeface(
    @NonNull FontFamily.Resolver receiver,
    FontFamily fontFamily,
    @NonNull FontWeight fontWeight,
    @NonNull FontStyle fontStyle,
    @NonNull FontSynthesis fontSynthesis
)

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

fontFamily to resolve from

@NonNull FontWeight fontWeight

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

@NonNull FontStyle fontStyle

italic or upright text, to resolve in fontFamily

@NonNull FontSynthesis fontSynthesis

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.