AndroidFont.TypefaceLoader


Loader for loading an AndroidFont and producing an android.graphics.Typeface.

This interface is not intended to be used by application developers for text display. To load a typeface for display use FontFamily.Resolver.

TypefaceLoader allows the introduction of new types of font descriptors for use in FontListFontFamily. A TypefaceLoader allows a new subclass of AndroidFont to be used by normal compose text rendering methods.

Examples of new types of fonts that TypefaceLoader can add:

During resolution from FontFamily.Resolver, an AndroidFont subclass will be queried for an appropriate loader.

The loader attached to an instance of an AndroidFont is only required to be able to load that instance, though it is advised to create one loader for all instances of the same subclass and share them between AndroidFont instances to avoid allocations or allow caching.

Summary

Public functions

suspend Typeface?
awaitLoad(context: Context, font: AndroidFont)

Asynchronously load the font, from either local or remote sources such that it will cause text reflow when loading completes.

android
Typeface?
loadBlocking(context: Context, font: AndroidFont)

Immediately load the font in a blocking manner such that it will be available this frame.

android

Public functions

awaitLoad

suspend fun awaitLoad(context: Context, font: AndroidFont): Typeface?

Asynchronously load the font, from either local or remote sources such that it will cause text reflow when loading completes.

This method will be called on a UI-critical thread, and should not block the thread for font loading from sources slower than the local filesystem. More expensive loads should dispatch to an appropriate thread.

This method is always called in a timeout context and must return it's final value within 15 seconds. If the Typeface is not resolved within 15 seconds, the async load is cancelled and considered a permanent failure. Implementations should use structured concurrency to cooperatively cancel work.

Compose does not know what resources are required to satisfy a font load. Subclasses implementing FontLoadingStrategy.Async behavior should ensure requests are de-duped for the same resource.

It is possible for awaitLoad to be called for the same instance of AndroidFont in parallel. Implementations should support parallel concurrent loads, or de-dup.

Parameters
context: Context

current Android context for loading the font

font: AndroidFont

the font to load which contains this loader as AndroidFont.typefaceLoader

Returns
Typeface?

android.graphics.Typeface for loaded font, or null if not available

loadBlocking

fun loadBlocking(context: Context, font: AndroidFont): Typeface?

Immediately load the font in a blocking manner such that it will be available this frame.

This method will be called on a UI-critical thread, however it has been determined that this font is required for the current frame. This method is allowed to perform small amounts of I/O to load a font file from disk.

This method should never perform expensive I/O operations, such as loading from a remote source. If expensive operations are required to complete the font, this method may choose to throw. Note that this method will never be called for fonts with FontLoadingStrategy.Async.

It is possible for loadBlocking to be called for the same instance of AndroidFont in parallel. Implementations should support parallel concurrent loads, or de-dup.

Parameters
context: Context

current Android context for loading the font

font: AndroidFont

the font to load which contains this loader as AndroidFont.typefaceLoader

Returns
Typeface?

android.graphics.Typeface for loaded font, or null if the font fails to load