Skip to content

feat: setting fallback fonts#152

Open
mfazekas wants to merge 19 commits intomainfrom
refactor/unified-font-source-api
Open

feat: setting fallback fonts#152
mfazekas wants to merge 19 commits intomainfrom
refactor/unified-font-source-api

Conversation

@mfazekas
Copy link
Collaborator

@mfazekas mfazekas commented Feb 20, 2026

Adds RiveFonts — font fallback API for react-native. Two-phase design: load fonts into opaque handles, then configure a weight-based fallback map. System font fallback is opt-in via a 'default' sentinel.

const regular = await RiveFonts.loadFont({ uri: 'https://…/font.ttf' });
const bold = await RiveFonts.loadFont('kanit_regular.ttf');
const cjk = await RiveFonts.loadFont({ name: 'Noto Sans CJK' });

// Custom fonts only — no system fallback
await RiveFonts.setFallbackFonts({
  default: [regular, cjk],
  700: [bold, cjk],
});

// Custom fonts + system fallback (explicit opt-in)
await RiveFonts.setFallbackFonts({
  default: [regular, cjk, 'default'],
});

// Reset
await RiveFonts.clearFallbackFonts();

How to test

Open the Font Fallback exerciser in the example app (Exercisers > Font Fallback).

1. Custom fonts only — select fonts, mount without "System Fallback" toggle — unsupported scripts show tofu.

2. Custom fonts + system fallback — select fonts, toggle "System Fallback" on, mount — unsupported scripts render via platform default.

3. Font ordering — toggles show selection order (1, 2, 3…) since fallback priority follows insertion order.

iOS Android
image image
iOS Android
image image

@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from 16688d6 to a53e063 Compare February 24, 2026 06:19
@mfazekas mfazekas changed the title feat: unified async RiveFonts.addFallbackFont(source) API feat: RiveFonts.addFallbackFont(source) API Feb 24, 2026
@edgarrii
Copy link

@mfazekas thank you!

@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from a53e063 to 64ab88b Compare February 24, 2026 14:39
@mfazekas mfazekas changed the title feat: RiveFonts.addFallbackFont(source) API feat: RiveFonts.addFallbackFonts(sources) API Feb 24, 2026
@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from 64ab88b to cfe8e4e Compare February 24, 2026 14:42
@mfazekas mfazekas marked this pull request as ready for review February 24, 2026 14:56
@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from 88a361d to ca4786f Compare February 27, 2026 06:47
@mfazekas mfazekas changed the title feat: RiveFonts.addFallbackFonts(sources) API refactor: two-phase font loading with weight-based fallback map Feb 27, 2026
@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from 42eb65e to 7ac409b Compare February 27, 2026 09:32
@mfazekas mfazekas changed the title refactor: two-phase font loading with weight-based fallback map feat: setting fallback fonts Feb 27, 2026
@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from 9624c4a to 2c28c78 Compare March 2, 2026 13:51
mfazekas added 12 commits March 2, 2026 14:55
Replace separate addFallbackFont/addFallbackFontFromResource/addFallbackFontFromURL
with a single addFallbackFont(source: FontSource) that accepts require() IDs,
URI objects, resource names, URLs, or raw ArrayBuffer bytes.

All methods are now async. Renamed native HybridObject from Rive to
RiveFontConfig to better reflect its purpose.
The font file existed in example/ios/ but wasn't referenced in the Xcode
project, so addFallbackFontFromResource couldn't find it at runtime.
Prevents "Cannot set value for property" error on initial render
when the riv file hasn't loaded yet.
…s API

Adds `{ name: string }` to FontSource for system fonts (e.g. `{ name: 'Thonburi' }` on iOS,
`{ name: 'serif' }` on Android). Renames `addFallbackFont` to `addFallbackFonts` accepting
an array to make font ordering explicit and avoid sequential awaits.
Separate font loading from configuration — loadFont() returns opaque
FallbackFont handles, setFallbackFonts() accepts a weight-keyed map.
System fonts always appended as last resort.
@mfazekas mfazekas force-pushed the refactor/unified-font-source-api branch from 2c28c78 to 36aadef Compare March 2, 2026 13:55
dskuza
dskuza previously approved these changes Mar 2, 2026
Copy link

@dskuza dskuza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the Apple side, this looks good to me. The one thing I would question (but it could be a followup) is whether we can add constants for the weights. You can see what Apple uses by viewing UIFont.Weight documentation, but it's also similar to this name mapping.

let requestedWeight = Int(weight.rawWeight)
let fonts = Self.fontsByWeight[requestedWeight] ?? Self.fontsByWeight[0] ?? []
var providers: [RiveFallbackFontProvider] = fonts
providers.append(RiveFallbackFontDescriptor())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the way of "guaranteeing" there's at least one fallback font in use (if all else fail)? We don't do this on iOS (there's only a default if you don't provide an explicit list). Not saying this is wrong, just something to call out.

Copy link
Contributor

@HayesGordon HayesGordon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants