NativeTextEngine

actual class NativeTextEngine
expect class NativeTextEngine

Native text engine for SDF glyph generation and text layout.

This engine uses Rust for high-performance text shaping (via rustybuzz) and SDF generation. It maintains a glyph atlas with LRU cache eviction.

Usage

val engine = NativeTextEngine()
engine.init(2048, 2048) // Initialize with atlas size
engine.loadFont(fontBytes) // Load primary font

// Process text and get layout
val layoutJson = engine.processText("Hello", 48f, 400f)

// Upload pending glyphs to atlas
if (engine.hasPendingUploads()) {
val uploads = parsePendingUploads(engine.getPendingUploads())
atlasManager.updateAtlas(uploads)
}

SDF Processing

Glyph data is pre-processed in Rust with alpha values:

  • A channel: normal text alpha (smoothstep at threshold 0.7)

  • G channel: shadow alpha (smoothstep falloff for glow effect)

actual class NativeTextEngine

Constructors

Link copied to clipboard
constructor()
expect constructor()
constructor()

Types

Companion
Link copied to clipboard
object Companion
object Companion

Functions

Link copied to clipboard
actual external fun clearFallbackFonts()
expect fun clearFallbackFonts()

Clears all loaded fallback fonts.

actual external fun clearFallbackFonts()
Link copied to clipboard
actual external fun destroy()
expect fun destroy()

Releases all resources and clears the glyph cache.

actual external fun destroy()
Link copied to clipboard
actual external fun getAtlasSize(): String
expect fun getAtlasSize(): String

Gets the current atlas size.

actual external fun getAtlasSize(): String
Link copied to clipboard
actual external fun getPendingUploads(): String

Gets pending glyph uploads as JSON. Each upload contains position, size, and RGBA pixel data (pre-processed SDF).

actual external fun getPendingUploads(): String
getPendingUploadsDirect
Link copied to clipboard
external fun getPendingUploadsDirect(buffer: ByteBuffer): Int
external fun getPendingUploadsDirect(buffer: ByteBuffer): Int
Link copied to clipboard
actual external fun hasPendingUploads(): Boolean

Checks if there are pending glyph uploads.

actual external fun hasPendingUploads(): Boolean
Link copied to clipboard
actual external fun init(atlasWidth: Int, atlasHeight: Int)
expect fun init(atlasWidth: Int, atlasHeight: Int)

Initializes the text engine with the given atlas dimensions. Must be called before any other operations.

actual external fun init(atlasWidth: Int, atlasHeight: Int)
Link copied to clipboard
actual external fun loadFallbackFont(bytes: ByteArray)
expect fun loadFallbackFont(bytes: ByteArray)

Loads a fallback font for missing glyphs. Fallback fonts are tried in order when the primary font doesn't contain a glyph.

actual external fun loadFallbackFont(bytes: ByteArray)
Link copied to clipboard
external fun loadFallbackFontFd(fd: Int): Boolean
Link copied to clipboard
fun NativeTextEngine.loadFallbackFontFromAsset(context: Context, assetPath: String): Boolean

Loads a fallback font from Android assets using zero-copy file descriptor. More memory efficient than loading entire font into ByteArray.

Link copied to clipboard

Loads a fallback font from a file path using zero-copy file descriptor.

Link copied to clipboard
actual external fun loadFont(bytes: ByteArray)
expect fun loadFont(bytes: ByteArray)

Loads the primary font for text rendering.

actual external fun loadFont(bytes: ByteArray)
Link copied to clipboard
actual external fun processText(text: String, sizeFn: Float, weight: Float): String
expect fun processText(text: String, sizeFn: Float, weight: Float = 400.0f): String

Processes text and returns layout information as JSON. This also generates SDF glyphs for any new characters.

actual external fun processText(text: String, sizeFn: Float, weight: Float): String
processTextDirect
Link copied to clipboard
external fun processTextDirect(text: String, sizePx: Float, weight: Float, buffer: ByteBuffer): Int
external fun processTextDirect(text: String, sizePx: Float, weight: Float, buffer: ByteBuffer): Int