diff --git a/android/src/main/java/com/margelo/nitro/rive/Rive.kt b/android/src/main/java/com/margelo/nitro/rive/HybridDefaultFallbackFont.kt similarity index 54% rename from android/src/main/java/com/margelo/nitro/rive/Rive.kt rename to android/src/main/java/com/margelo/nitro/rive/HybridDefaultFallbackFont.kt index 84ae2e99..f7f5152b 100644 --- a/android/src/main/java/com/margelo/nitro/rive/Rive.kt +++ b/android/src/main/java/com/margelo/nitro/rive/HybridDefaultFallbackFont.kt @@ -5,8 +5,4 @@ import com.facebook.proguard.annotations.DoNotStrip @Keep @DoNotStrip -class Rive : HybridRiveSpec() { - override fun multiply(a: Double, b: Double): Double { - return a * b - } -} +class HybridDefaultFallbackFont : HybridFallbackFontSpec() diff --git a/android/src/main/java/com/margelo/nitro/rive/HybridFallbackFont.kt b/android/src/main/java/com/margelo/nitro/rive/HybridFallbackFont.kt new file mode 100644 index 00000000..9b3cc18e --- /dev/null +++ b/android/src/main/java/com/margelo/nitro/rive/HybridFallbackFont.kt @@ -0,0 +1,10 @@ +package com.margelo.nitro.rive + +import androidx.annotation.Keep +import com.facebook.proguard.annotations.DoNotStrip + +@Keep +@DoNotStrip +class HybridFallbackFont( + val fontBytes: ByteArray +) : HybridFallbackFontSpec() diff --git a/android/src/main/java/com/margelo/nitro/rive/HybridRiveFontConfig.kt b/android/src/main/java/com/margelo/nitro/rive/HybridRiveFontConfig.kt new file mode 100644 index 00000000..8245a31d --- /dev/null +++ b/android/src/main/java/com/margelo/nitro/rive/HybridRiveFontConfig.kt @@ -0,0 +1,136 @@ +package com.margelo.nitro.rive + +import android.util.Log +import androidx.annotation.Keep +import app.rive.runtime.kotlin.fonts.FontFallbackStrategy +import app.rive.runtime.kotlin.fonts.FontHelper +import app.rive.runtime.kotlin.fonts.Fonts +import com.facebook.proguard.annotations.DoNotStrip +import com.margelo.nitro.NitroModules +import com.margelo.nitro.core.ArrayBuffer +import com.margelo.nitro.core.Promise +import java.net.URL +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +@Keep +@DoNotStrip +class HybridRiveFontConfig : HybridRiveFontConfigSpec() { + companion object { + private const val TAG = "RiveFonts" + private const val DEFAULT_WEIGHT = 0 + private val fontsByWeight: MutableMap> = + java.util.Collections.synchronizedMap(mutableMapOf()) + + private fun resetFontCache() { + try { + FontFallbackStrategy.cppResetFontCache() + } catch (e: Exception) { + Log.e(TAG, "Failed to reset font cache: ${e.message}") + } + } + } + + override fun loadFontFromURL(url: String): Promise { + return Promise.async { + val fontBytes = withContext(Dispatchers.IO) { + URL(url).readBytes() + } + HybridFallbackFont(fontBytes) + } + } + + override fun loadFontFromResource(resource: String): HybridFallbackFontSpec { + val context = NitroModules.applicationContext + ?: throw Error("Application context not available") + + val resName = resource.substringBeforeLast(".") + + val rawId = context.resources.getIdentifier(resName, "raw", context.packageName) + if (rawId != 0) { + val fontBytes = context.resources.openRawResource(rawId).use { it.readBytes() } + return HybridFallbackFont(fontBytes) + } + + val fontId = context.resources.getIdentifier(resName, "font", context.packageName) + if (fontId != 0) { + val fontBytes = context.resources.openRawResource(fontId).use { it.readBytes() } + return HybridFallbackFont(fontBytes) + } + + val assetPaths = listOf("fonts/$resource", resource) + for (assetPath in assetPaths) { + try { + val fontBytes = context.assets.open(assetPath).use { it.readBytes() } + return HybridFallbackFont(fontBytes) + } catch (_: Exception) { + } + } + + throw Error("Font resource not found: $resource (checked res/raw, res/font, assets/fonts)") + } + + override fun loadFontFromBytes(bytes: ArrayBuffer): HybridFallbackFontSpec { + val buffer = bytes.getBuffer(false) + val byteArray = ByteArray(buffer.remaining()) + buffer.get(byteArray) + return HybridFallbackFont(byteArray) + } + + override fun loadFontByName(name: String): HybridFallbackFontSpec { + val fonts = FontHelper.getFallbackFonts(Fonts.FontOpts(familyName = name)) + if (fonts.isEmpty()) { + throw Error("System font not found: $name") + } + val fontBytes = FontHelper.getFontBytes(fonts.first()) + ?: throw Error("Could not read font bytes for: $name") + return HybridFallbackFont(fontBytes) + } + + override fun getSystemDefaultFont(): HybridFallbackFontSpec { + return HybridDefaultFallbackFont() + } + + override fun setFontsForWeight(weight: Double, fonts: Array) { + val key = weight.toInt() + synchronized(fontsByWeight) { + fontsByWeight[key] = fonts.toList() + } + } + + override fun applyFallbackFonts(): Promise { + return Promise.async { + FontFallbackStrategy.stylePicker = object : FontFallbackStrategy { + override fun getFont(weight: Fonts.Weight): List { + val requestedWeight = weight.weight + val specs = synchronized(fontsByWeight) { + fontsByWeight[requestedWeight] ?: fontsByWeight[DEFAULT_WEIGHT] ?: emptyList() + } + return specs.mapNotNull { spec -> + when (spec) { + is HybridDefaultFallbackFont -> + FontHelper.getFallbackFontBytes(Fonts.FontOpts(weight = weight)) + is HybridFallbackFont -> + spec.fontBytes + else -> { + Log.e(TAG, "Unknown fallback font spec type: ${spec::class.simpleName}") + null + } + } + } + } + } + resetFontCache() + } + } + + override fun clearFallbackFonts(): Promise { + return Promise.async { + synchronized(fontsByWeight) { + fontsByWeight.clear() + } + FontFallbackStrategy.stylePicker = null + resetFontCache() + } + } +} diff --git a/example/android/app/src/main/res/raw/kanit_regular.ttf b/example/android/app/src/main/res/raw/kanit_regular.ttf new file mode 100644 index 00000000..e9bc0a2f Binary files /dev/null and b/example/android/app/src/main/res/raw/kanit_regular.ttf differ diff --git a/example/assets/rive/fallback_fonts.riv b/example/assets/rive/fallback_fonts.riv new file mode 100644 index 00000000..bfaabf2f Binary files /dev/null and b/example/assets/rive/fallback_fonts.riv differ diff --git a/example/assets/rive/style_fallback_fonts.riv b/example/assets/rive/style_fallback_fonts.riv new file mode 100644 index 00000000..9748dc26 Binary files /dev/null and b/example/assets/rive/style_fallback_fonts.riv differ diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index a87eb944..0aa1d96f 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1904,7 +1904,7 @@ PODS: - ReactCommon/turbomodule/core - RNWorklets - Yoga - - RNRive (0.2.4): + - RNRive (0.2.5): - DoubleConversion - glog - hermes-engine @@ -2330,7 +2330,7 @@ SPEC CHECKSUMS: RNCPicker: 28c076ae12a1056269ec0305fe35fac3086c477d RNGestureHandler: 6b39f4e43e4b3a0fb86de9531d090ff205a011d5 RNReanimated: 66b68ebe3baf7ec9e716bd059d700726f250d344 - RNRive: 5abb7e84bce9e6a0fe3ede1b366c442ac67dda75 + RNRive: acc0d2356fbb2f65dce95b25cc154bdd0c8ceecc RNWorklets: b1faafefb82d9f29c4018404a0fb33974b494a7b SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748 Yoga: 9f110fc4b7aa538663cba3c14cbb1c335f43c13f diff --git a/example/ios/RiveExample.xcodeproj/project.pbxproj b/example/ios/RiveExample.xcodeproj/project.pbxproj index ade84b2a..86c5844b 100644 --- a/example/ios/RiveExample.xcodeproj/project.pbxproj +++ b/example/ios/RiveExample.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 761780ED2CA45674006654EE /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 761780EC2CA45674006654EE /* AppDelegate.swift */; }; 81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; }; F8EBA76E2DD7CE540010BBD0 /* rewards.riv in Resources */ = {isa = PBXBuildFile; fileRef = F8EBA76D2DD7CE540010BBD0 /* rewards.riv */; }; + F8EBA7712DD7CF000010BBD0 /* kanit_regular.ttf in Resources */ = {isa = PBXBuildFile; fileRef = F8EBA7702DD7CF000010BBD0 /* kanit_regular.ttf */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -27,6 +28,7 @@ E0AD05C932EEB95D292EBDBA /* Pods-RiveExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RiveExample.release.xcconfig"; path = "Target Support Files/Pods-RiveExample/Pods-RiveExample.release.xcconfig"; sourceTree = ""; }; ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; F8EBA76D2DD7CE540010BBD0 /* rewards.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = rewards.riv; sourceTree = ""; }; + F8EBA7702DD7CF000010BBD0 /* kanit_regular.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; path = kanit_regular.ttf; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFileSystemSynchronizedRootGroup section */ @@ -60,6 +62,7 @@ 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */, 13B07FB81A68108700A75B9A /* PrivacyInfo.xcprivacy */, F8EBA76D2DD7CE540010BBD0 /* rewards.riv */, + F8EBA7702DD7CF000010BBD0 /* kanit_regular.ttf */, ); name = RiveExample; sourceTree = ""; @@ -175,6 +178,7 @@ 81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */, 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */, F8EBA76E2DD7CE540010BBD0 /* rewards.riv in Resources */, + F8EBA7712DD7CF000010BBD0 /* kanit_regular.ttf in Resources */, 35B325D75078933C519F2ACC /* PrivacyInfo.xcprivacy in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/example/ios/kanit_regular.ttf b/example/ios/kanit_regular.ttf new file mode 100644 index 00000000..e9bc0a2f Binary files /dev/null and b/example/ios/kanit_regular.ttf differ diff --git a/example/src/exercisers/FontFallbackExample.tsx b/example/src/exercisers/FontFallbackExample.tsx new file mode 100644 index 00000000..84ecf81d --- /dev/null +++ b/example/src/exercisers/FontFallbackExample.tsx @@ -0,0 +1,572 @@ +import { useState, useMemo, useEffect } from 'react'; +import { + View, + Text, + StyleSheet, + TouchableOpacity, + ScrollView, + ActivityIndicator, + TextInput, +} from 'react-native'; +import { + Fit, + RiveView, + RiveFonts, + useRive, + useRiveFile, + useRiveString, + type FontSource, + type FallbackFont, +} from '@rive-app/react-native'; +import { type Metadata } from '../shared/metadata'; + +const TEXT_PROPERTY = 'text'; + +const FONTS: Record< + string, + { label: string; sublabel: string; source: FontSource } +> = { + kanitBundled: { + label: 'Kanit (Bundled)', + sublabel: 'From native resources — 173KB', + source: 'kanit_regular.ttf', + }, + kanit: { + label: 'Kanit (URL)', + sublabel: 'Geometric Thai+Latin — 173KB', + source: { + uri: 'https://raw.githubusercontent.com/google/fonts/main/ofl/kanit/Kanit-Regular.ttf', + }, + }, + notoSerifThai: { + label: 'Noto Serif Thai', + sublabel: 'Serif Thai — 292KB', + source: { + uri: 'https://raw.githubusercontent.com/google/fonts/main/ofl/notoserifthai/NotoSerifThai%5Bwdth%2Cwght%5D.ttf', + }, + }, + thonburiSystem: { + label: 'Thonburi (System)', + sublabel: 'Built-in Thai font — iOS only', + source: { name: 'Thonburi' }, + }, + serifSystem: { + label: 'serif (System)', + sublabel: 'Noto Serif — Android only', + source: { name: 'serif' }, + }, +}; + +type FontKey = keyof typeof FONTS; + +type LogEntry = { + message: string; + type: 'success' | 'error' | 'info'; + timestamp: number; +}; + +export default function FontFallbackExample() { + const [mounted, setMounted] = useState(false); + const [selectedFonts, setSelectedFonts] = useState([]); + const [loadedFonts, setLoadedFonts] = useState>( + new Map() + ); + const [systemFallback, setSystemFallback] = useState(false); + const [loadingFont, setLoadingFont] = useState(null); + const [log, setLog] = useState([]); + const [inputText, setInputText] = useState('ABC 你好 สวัสดี'); + + const addLog = (message: string, type: LogEntry['type'] = 'info') => { + setLog((prev) => [...prev, { message, type, timestamp: Date.now() }]); + }; + + const toggleFont = async (key: FontKey) => { + if (mounted) return; + const font = FONTS[key]!; + const isSelected = selectedFonts.includes(key); + + if (isSelected) { + setSelectedFonts((prev) => prev.filter((k) => k !== key)); + setLoadedFonts((prev) => { + const next = new Map(prev); + next.delete(key); + return next; + }); + addLog(`Removed ${font.label} from selection`, 'info'); + return; + } + + setLoadingFont(key); + try { + const handle = await RiveFonts.loadFont(font.source); + addLog(`Loaded ${font.label}`, 'success'); + setSelectedFonts((prev) => [...prev, key]); + setLoadedFonts((prev) => new Map(prev).set(key, handle)); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + addLog(`${font.label}: ${msg}`, 'error'); + console.error(`FontFallback ${font.label}:`, err); + } finally { + setLoadingFont(null); + } + }; + + const handleMount = async () => { + try { + const entries: FallbackFont[] = selectedFonts + .map((key) => loadedFonts.get(key)) + .filter((f): f is FallbackFont => f != null); + if (systemFallback) { + entries.push(RiveFonts.systemFallback()); + } + await RiveFonts.setFallbackFonts({ default: entries }); + } catch (err) { + console.error('setFallbackFonts:', err); + } + setMounted(true); + const parts = selectedFonts.map((k) => FONTS[k]!.label); + if (systemFallback) parts.push('System Fallback'); + addLog( + `Mounted with: ${parts.length > 0 ? parts.join(', ') : 'no fonts configured'}`, + 'info' + ); + }; + + const handleReset = async () => { + await RiveFonts.clearFallbackFonts(); + setSelectedFonts([]); + setLoadedFonts(new Map()); + setSystemFallback(false); + setMounted(false); + addLog('Cleared fonts & unmounted view', 'info'); + }; + + return ( + + {mounted ? ( + + ) : ( + + Configure Fallback Fonts + + Select custom fonts below, then mount the view. Font lookups are + cached at startup — to change fonts, unmount and remount. + + + )} + + + {mounted && ( + <> + + + + + + + + + + )} + + + {mounted ? 'Active Fonts' : 'Select Fonts'} + + + {(Object.keys(FONTS) as FontKey[]).map((key) => ( + toggleFont(key)} + /> + ))} + + setSystemFallback((prev) => !prev)} + /> + + {mounted ? ( + + ) : ( + + )} + + {log.length > 0 && ( + + + Log + setLog([])}> + Clear + + + {log.map((entry) => ( + + {entry.message} + + ))} + + )} + + + ); +} + +function MountedView({ text }: { text: string }) { + const { riveViewRef, setHybridRef } = useRive(); + const { riveFile, isLoading, error } = useRiveFile( + // https://rive.app/marketplace/26480-49641-simple-test-text-property/ + require('../../assets/rive/font_fallback.riv') + ); + const viewModel = useMemo( + () => riveFile?.defaultArtboardViewModel(), + [riveFile] + ); + const instance = useMemo( + () => viewModel?.createDefaultInstance(), + [viewModel] + ); + + const { setValue: setRiveText, error: textError } = useRiveString( + TEXT_PROPERTY, + instance + ); + + if (textError) { + console.error('Failed to bind text property:', textError); + } + + useEffect(() => { + setRiveText(text); + riveViewRef?.playIfNeeded(); + }, [text, setRiveText, riveViewRef]); + + if (isLoading) { + return ( + + + + ); + } + + if (error || !riveFile || !instance) { + return ( + + + {error || 'Failed to set up view model'} + + + ); + } + + return ( + + + + ); +} + +function FontToggle({ + label, + sublabel, + order, + disabled, + loading, + onPress, +}: { + label: string; + sublabel: string; + order?: number; + disabled: boolean; + loading: boolean; + onPress: () => void; +}) { + const selected = order != null; + return ( + + {loading ? ( + + ) : ( + <> + + + {label} + + {sublabel} + + + {selected ? `${order}` : ''} + + + )} + + ); +} + +function Preset({ + label, + text, + onPress, +}: { + label: string; + text: string; + onPress: (text: string) => void; +}) { + return ( + onPress(text)}> + {label} + + ); +} + +function ActionButton({ + label, + onPress, + destructive, +}: { + label: string; + onPress: () => void; + destructive?: boolean; +}) { + return ( + + {label} + + ); +} + +FontFallbackExample.metadata = { + name: 'Font Fallback', + description: + 'Tests RiveFonts API — configure custom fallback fonts before mounting the Rive view', +} satisfies Metadata; + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#fff', + }, + setupContainer: { + height: 120, + backgroundColor: '#f0f0f0', + justifyContent: 'center', + alignItems: 'center', + padding: 20, + }, + setupTitle: { + fontSize: 18, + fontWeight: '700', + color: '#333', + marginBottom: 8, + }, + setupHint: { + fontSize: 13, + color: '#666', + textAlign: 'center', + }, + riveContainer: { + height: 120, + backgroundColor: '#f0f0f0', + justifyContent: 'center', + alignItems: 'center', + }, + rive: { + width: '100%', + height: '100%', + }, + errorText: { + color: 'red', + textAlign: 'center', + padding: 20, + }, + controls: { + flex: 1, + }, + controlsContent: { + padding: 16, + gap: 10, + paddingBottom: 40, + }, + input: { + borderWidth: 1, + borderColor: '#ccc', + borderRadius: 10, + padding: 12, + fontSize: 16, + backgroundColor: '#fafafa', + }, + presets: { + flexDirection: 'row', + flexWrap: 'wrap', + gap: 8, + }, + preset: { + backgroundColor: '#e8e8e8', + borderRadius: 8, + paddingHorizontal: 12, + paddingVertical: 6, + }, + presetText: { + fontSize: 13, + color: '#333', + }, + sectionTitle: { + fontSize: 14, + fontWeight: '600', + color: '#333', + marginTop: 6, + }, + fontToggle: { + flexDirection: 'row', + alignItems: 'center', + borderWidth: 1, + borderColor: '#ddd', + borderRadius: 10, + padding: 14, + backgroundColor: '#fafafa', + minHeight: 56, + }, + fontToggleSelected: { + borderColor: '#007AFF', + backgroundColor: '#F0F7FF', + }, + fontToggleDisabled: { + opacity: 0.6, + }, + fontToggleInfo: { + flex: 1, + }, + fontToggleLabel: { + fontSize: 16, + fontWeight: '600', + color: '#333', + }, + fontToggleLabelSelected: { + color: '#007AFF', + }, + fontToggleSublabel: { + fontSize: 12, + color: '#888', + marginTop: 2, + }, + fontToggleCheck: { + fontSize: 18, + color: '#007AFF', + fontWeight: '700', + width: 24, + textAlign: 'center', + }, + button: { + backgroundColor: '#007AFF', + borderRadius: 10, + padding: 14, + alignItems: 'center', + minHeight: 50, + justifyContent: 'center', + }, + buttonDestructive: { + backgroundColor: '#FF3B30', + }, + buttonText: { + color: '#fff', + fontSize: 16, + fontWeight: '600', + textAlign: 'center', + }, + logContainer: { + marginTop: 6, + backgroundColor: '#1a1a2e', + borderRadius: 10, + padding: 12, + }, + logHeader: { + flexDirection: 'row', + justifyContent: 'space-between', + alignItems: 'center', + marginBottom: 8, + }, + logTitle: { + color: '#888', + fontSize: 12, + fontWeight: '600', + textTransform: 'uppercase', + }, + clearLog: { + color: '#007AFF', + fontSize: 12, + }, + logEntry: { + color: '#ccc', + fontSize: 13, + fontFamily: 'monospace', + paddingVertical: 2, + }, + logSuccess: { + color: '#4CD964', + }, + logError: { + color: '#FF3B30', + }, +}); diff --git a/ios/HybridDefaultFallbackFont.swift b/ios/HybridDefaultFallbackFont.swift new file mode 100644 index 00000000..035356e4 --- /dev/null +++ b/ios/HybridDefaultFallbackFont.swift @@ -0,0 +1,3 @@ +import NitroModules + +class HybridDefaultFallbackFont: HybridFallbackFontSpec {} diff --git a/ios/HybridFallbackFont.swift b/ios/HybridFallbackFont.swift new file mode 100644 index 00000000..f8c5f03c --- /dev/null +++ b/ios/HybridFallbackFont.swift @@ -0,0 +1,10 @@ +import NitroModules + +class HybridFallbackFont: HybridFallbackFontSpec { + let font: UIFont + + init(font: UIFont) { + self.font = font + super.init() + } +} diff --git a/ios/HybridRiveFontConfig.swift b/ios/HybridRiveFontConfig.swift new file mode 100644 index 00000000..3425a5a3 --- /dev/null +++ b/ios/HybridRiveFontConfig.swift @@ -0,0 +1,109 @@ +import CoreText +import NitroModules +import RiveRuntime + +class HybridRiveFontConfig: HybridRiveFontConfigSpec { + private static let defaultWeight = 0 + private static var fontsByWeight: [Int: [any HybridFallbackFontSpec]] = [:] + + func loadFontFromURL(url: String) throws -> Promise<(any HybridFallbackFontSpec)> { + return Promise.async { + guard let parsedURL = URL(string: url) else { + throw RuntimeError.error(withMessage: "Invalid font URL: \(url)") + } + let (data, _) = try await URLSession.shared.data(from: parsedURL) + let font = try Self.createUIFont(from: data) + return HybridFallbackFont(font: font) + } + } + + func loadFontFromResource(resource: String) throws -> (any HybridFallbackFontSpec) { + let nsResource = resource as NSString + let name = nsResource.deletingPathExtension + let ext = nsResource.pathExtension.isEmpty ? nil : nsResource.pathExtension + + guard let path = Bundle.main.path(forResource: name, ofType: ext) else { + throw RuntimeError.error(withMessage: "Font resource not found: \(resource)") + } + let data = try Data(contentsOf: URL(fileURLWithPath: path)) + let font = try Self.createUIFont(from: data) + return HybridFallbackFont(font: font) + } + + func loadFontFromBytes(bytes: ArrayBuffer) throws -> (any HybridFallbackFontSpec) { + let data = bytes.toData(copyIfNeeded: true) + let font = try Self.createUIFont(from: data) + return HybridFallbackFont(font: font) + } + + func loadFontByName(name: String) throws -> (any HybridFallbackFontSpec) { + guard let font = UIFont(name: name, size: UIFont.systemFontSize) else { + throw RuntimeError.error(withMessage: "System font not found: \(name)") + } + return HybridFallbackFont(font: font) + } + + func getSystemDefaultFont() throws -> (any HybridFallbackFontSpec) { + return HybridDefaultFallbackFont() + } + + func setFontsForWeight(weight: Double, fonts: [(any HybridFallbackFontSpec)]) throws { + let key = Int(weight) + Self.fontsByWeight[key] = fonts + } + + func applyFallbackFonts() throws -> Promise { + return Promise.async { + _ = RiveFont.self + RiveFont.fallbackFontsCallback = { weight in + let requestedWeight = Int(weight.rawWeight) + let specs = Self.fontsByWeight[requestedWeight] ?? Self.fontsByWeight[Self.defaultWeight] ?? [] + return specs.compactMap { spec in + if spec is HybridDefaultFallbackFont { + return RiveFallbackFontDescriptor() + } else if let fallbackFont = spec as? HybridFallbackFont { + return fallbackFont.font + } else { + print("[RiveFonts] Unknown fallback font spec type: \(type(of: spec))") + return nil + } + } + } + } + } + + func clearFallbackFonts() throws -> Promise { + return Promise.async { + Self.fontsByWeight.removeAll() + RiveFont.fallbackFontsCallback = { _ in [RiveFallbackFontDescriptor()] } + RiveFont.fallbackFonts = [RiveFallbackFontDescriptor()] + } + } + + private static func createUIFont(from data: Data) throws -> UIFont { + guard let provider = CGDataProvider(data: data as CFData), + let cgFont = CGFont(provider) + else { + throw RuntimeError.error(withMessage: "Failed to decode font data") + } + + var error: Unmanaged? + if !CTFontManagerRegisterGraphicsFont(cgFont, &error) { + let cfError = error?.takeRetainedValue() + let domain = cfError.map { CFErrorGetDomain($0) as String } ?? "" + if domain != kCTFontManagerErrorDomain as String { + throw RuntimeError.error( + withMessage: "Failed to register font: \(cfError?.localizedDescription ?? "unknown error")" + ) + } + } + + guard let fontName = cgFont.postScriptName as String? else { + throw RuntimeError.error(withMessage: "Failed to get font name from data") + } + guard let font = UIFont(name: fontName, size: UIFont.systemFontSize) else { + throw RuntimeError.error(withMessage: "Failed to create UIFont for: \(fontName)") + } + return font + } +} diff --git a/ios/Rive.swift b/ios/Rive.swift deleted file mode 100644 index 2901590f..00000000 --- a/ios/Rive.swift +++ /dev/null @@ -1,5 +0,0 @@ -class Rive: HybridRiveSpec { - public func multiply(a: Double, b: Double) throws -> Double { - return a * b - } -} diff --git a/nitro.json b/nitro.json index f60b2005..e1e89ae8 100644 --- a/nitro.json +++ b/nitro.json @@ -9,9 +9,9 @@ "androidCxxLibName": "rive" }, "autolinking": { - "Rive": { - "swift": "Rive", - "kotlin": "Rive" + "RiveFontConfig": { + "swift": "HybridRiveFontConfig", + "kotlin": "HybridRiveFontConfig" }, "RiveFileFactory": { "swift": "HybridRiveFileFactory", diff --git a/nitrogen/generated/android/c++/JHybridFallbackFontSpec.cpp b/nitrogen/generated/android/c++/JHybridFallbackFontSpec.cpp new file mode 100644 index 00000000..d3e62d0d --- /dev/null +++ b/nitrogen/generated/android/c++/JHybridFallbackFontSpec.cpp @@ -0,0 +1,55 @@ +/// +/// JHybridFallbackFontSpec.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include "JHybridFallbackFontSpec.hpp" + + + + + +namespace margelo::nitro::rive { + + jni::local_ref JHybridFallbackFontSpec::initHybrid(jni::alias_ref jThis) { + return makeCxxInstance(jThis); + } + + void JHybridFallbackFontSpec::registerNatives() { + registerHybrid({ + makeNativeMethod("initHybrid", JHybridFallbackFontSpec::initHybrid), + }); + } + + size_t JHybridFallbackFontSpec::getExternalMemorySize() noexcept { + static const auto method = javaClassStatic()->getMethod("getMemorySize"); + return method(_javaPart); + } + + bool JHybridFallbackFontSpec::equals(const std::shared_ptr& other) { + if (auto otherCast = std::dynamic_pointer_cast(other)) { + return _javaPart == otherCast->_javaPart; + } + return false; + } + + void JHybridFallbackFontSpec::dispose() noexcept { + static const auto method = javaClassStatic()->getMethod("dispose"); + method(_javaPart); + } + + std::string JHybridFallbackFontSpec::toString() { + static const auto method = javaClassStatic()->getMethod("toString"); + auto javaString = method(_javaPart); + return javaString->toStdString(); + } + + // Properties + + + // Methods + + +} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/android/c++/JHybridRiveSpec.hpp b/nitrogen/generated/android/c++/JHybridFallbackFontSpec.hpp similarity index 66% rename from nitrogen/generated/android/c++/JHybridRiveSpec.hpp rename to nitrogen/generated/android/c++/JHybridFallbackFontSpec.hpp index 633677a6..52a871dc 100644 --- a/nitrogen/generated/android/c++/JHybridRiveSpec.hpp +++ b/nitrogen/generated/android/c++/JHybridFallbackFontSpec.hpp @@ -1,5 +1,5 @@ /// -/// HybridRiveSpec.hpp +/// HybridFallbackFontSpec.hpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -9,7 +9,7 @@ #include #include -#include "HybridRiveSpec.hpp" +#include "HybridFallbackFontSpec.hpp" @@ -18,22 +18,22 @@ namespace margelo::nitro::rive { using namespace facebook; - class JHybridRiveSpec: public jni::HybridClass, - public virtual HybridRiveSpec { + class JHybridFallbackFontSpec: public jni::HybridClass, + public virtual HybridFallbackFontSpec { public: - static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/rive/HybridRiveSpec;"; + static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/rive/HybridFallbackFontSpec;"; static jni::local_ref initHybrid(jni::alias_ref jThis); static void registerNatives(); protected: // C++ constructor (called from Java via `initHybrid()`) - explicit JHybridRiveSpec(jni::alias_ref jThis) : - HybridObject(HybridRiveSpec::TAG), + explicit JHybridFallbackFontSpec(jni::alias_ref jThis) : + HybridObject(HybridFallbackFontSpec::TAG), HybridBase(jThis), _javaPart(jni::make_global(jThis)) {} public: - ~JHybridRiveSpec() override { + ~JHybridFallbackFontSpec() override { // Hermes GC can destroy JS objects on a non-JNI Thread. jni::ThreadScope::WithClassLoader([&] { _javaPart.reset(); }); } @@ -45,7 +45,7 @@ namespace margelo::nitro::rive { std::string toString() override; public: - inline const jni::global_ref& getJavaPart() const noexcept { + inline const jni::global_ref& getJavaPart() const noexcept { return _javaPart; } @@ -55,12 +55,12 @@ namespace margelo::nitro::rive { public: // Methods - double multiply(double a, double b) override; + private: friend HybridBase; using HybridBase::HybridBase; - jni::global_ref _javaPart; + jni::global_ref _javaPart; }; } // namespace margelo::nitro::rive diff --git a/nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.cpp b/nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.cpp new file mode 100644 index 00000000..62759763 --- /dev/null +++ b/nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.cpp @@ -0,0 +1,143 @@ +/// +/// JHybridRiveFontConfigSpec.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include "JHybridRiveFontConfigSpec.hpp" + +// Forward declaration of `HybridFallbackFontSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridFallbackFontSpec; } + +#include +#include "HybridFallbackFontSpec.hpp" +#include +#include +#include "JHybridFallbackFontSpec.hpp" +#include +#include +#include +#include +#include + +namespace margelo::nitro::rive { + + jni::local_ref JHybridRiveFontConfigSpec::initHybrid(jni::alias_ref jThis) { + return makeCxxInstance(jThis); + } + + void JHybridRiveFontConfigSpec::registerNatives() { + registerHybrid({ + makeNativeMethod("initHybrid", JHybridRiveFontConfigSpec::initHybrid), + }); + } + + size_t JHybridRiveFontConfigSpec::getExternalMemorySize() noexcept { + static const auto method = javaClassStatic()->getMethod("getMemorySize"); + return method(_javaPart); + } + + bool JHybridRiveFontConfigSpec::equals(const std::shared_ptr& other) { + if (auto otherCast = std::dynamic_pointer_cast(other)) { + return _javaPart == otherCast->_javaPart; + } + return false; + } + + void JHybridRiveFontConfigSpec::dispose() noexcept { + static const auto method = javaClassStatic()->getMethod("dispose"); + method(_javaPart); + } + + std::string JHybridRiveFontConfigSpec::toString() { + static const auto method = javaClassStatic()->getMethod("toString"); + auto javaString = method(_javaPart); + return javaString->toStdString(); + } + + // Properties + + + // Methods + std::shared_ptr>> JHybridRiveFontConfigSpec::loadFontFromURL(const std::string& url) { + static const auto method = javaClassStatic()->getMethod(jni::alias_ref /* url */)>("loadFontFromURL"); + auto __result = method(_javaPart, jni::make_jstring(url)); + return [&]() { + auto __promise = Promise>::create(); + __result->cthis()->addOnResolvedListener([=](const jni::alias_ref& __boxedResult) { + auto __result = jni::static_ref_cast(__boxedResult); + __promise->resolve(__result->cthis()->shared_cast()); + }); + __result->cthis()->addOnRejectedListener([=](const jni::alias_ref& __throwable) { + jni::JniException __jniError(__throwable); + __promise->reject(std::make_exception_ptr(__jniError)); + }); + return __promise; + }(); + } + std::shared_ptr JHybridRiveFontConfigSpec::loadFontFromResource(const std::string& resource) { + static const auto method = javaClassStatic()->getMethod(jni::alias_ref /* resource */)>("loadFontFromResource"); + auto __result = method(_javaPart, jni::make_jstring(resource)); + return __result->cthis()->shared_cast(); + } + std::shared_ptr JHybridRiveFontConfigSpec::loadFontFromBytes(const std::shared_ptr& bytes) { + static const auto method = javaClassStatic()->getMethod(jni::alias_ref /* bytes */)>("loadFontFromBytes"); + auto __result = method(_javaPart, JArrayBuffer::wrap(bytes)); + return __result->cthis()->shared_cast(); + } + std::shared_ptr JHybridRiveFontConfigSpec::loadFontByName(const std::string& name) { + static const auto method = javaClassStatic()->getMethod(jni::alias_ref /* name */)>("loadFontByName"); + auto __result = method(_javaPart, jni::make_jstring(name)); + return __result->cthis()->shared_cast(); + } + std::shared_ptr JHybridRiveFontConfigSpec::getSystemDefaultFont() { + static const auto method = javaClassStatic()->getMethod()>("getSystemDefaultFont"); + auto __result = method(_javaPart); + return __result->cthis()->shared_cast(); + } + void JHybridRiveFontConfigSpec::setFontsForWeight(double weight, const std::vector>& fonts) { + static const auto method = javaClassStatic()->getMethod> /* fonts */)>("setFontsForWeight"); + method(_javaPart, weight, [&]() { + size_t __size = fonts.size(); + jni::local_ref> __array = jni::JArrayClass::newArray(__size); + for (size_t __i = 0; __i < __size; __i++) { + const auto& __element = fonts[__i]; + auto __elementJni = std::dynamic_pointer_cast(__element)->getJavaPart(); + __array->setElement(__i, __elementJni.get()); + } + return __array; + }()); + } + std::shared_ptr> JHybridRiveFontConfigSpec::applyFallbackFonts() { + static const auto method = javaClassStatic()->getMethod()>("applyFallbackFonts"); + auto __result = method(_javaPart); + return [&]() { + auto __promise = Promise::create(); + __result->cthis()->addOnResolvedListener([=](const jni::alias_ref& /* unit */) { + __promise->resolve(); + }); + __result->cthis()->addOnRejectedListener([=](const jni::alias_ref& __throwable) { + jni::JniException __jniError(__throwable); + __promise->reject(std::make_exception_ptr(__jniError)); + }); + return __promise; + }(); + } + std::shared_ptr> JHybridRiveFontConfigSpec::clearFallbackFonts() { + static const auto method = javaClassStatic()->getMethod()>("clearFallbackFonts"); + auto __result = method(_javaPart); + return [&]() { + auto __promise = Promise::create(); + __result->cthis()->addOnResolvedListener([=](const jni::alias_ref& /* unit */) { + __promise->resolve(); + }); + __result->cthis()->addOnRejectedListener([=](const jni::alias_ref& __throwable) { + jni::JniException __jniError(__throwable); + __promise->reject(std::make_exception_ptr(__jniError)); + }); + return __promise; + }(); + } + +} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.hpp b/nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.hpp new file mode 100644 index 00000000..ff10b319 --- /dev/null +++ b/nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.hpp @@ -0,0 +1,73 @@ +/// +/// HybridRiveFontConfigSpec.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#include +#include +#include "HybridRiveFontConfigSpec.hpp" + + + + +namespace margelo::nitro::rive { + + using namespace facebook; + + class JHybridRiveFontConfigSpec: public jni::HybridClass, + public virtual HybridRiveFontConfigSpec { + public: + static auto constexpr kJavaDescriptor = "Lcom/margelo/nitro/rive/HybridRiveFontConfigSpec;"; + static jni::local_ref initHybrid(jni::alias_ref jThis); + static void registerNatives(); + + protected: + // C++ constructor (called from Java via `initHybrid()`) + explicit JHybridRiveFontConfigSpec(jni::alias_ref jThis) : + HybridObject(HybridRiveFontConfigSpec::TAG), + HybridBase(jThis), + _javaPart(jni::make_global(jThis)) {} + + public: + ~JHybridRiveFontConfigSpec() override { + // Hermes GC can destroy JS objects on a non-JNI Thread. + jni::ThreadScope::WithClassLoader([&] { _javaPart.reset(); }); + } + + public: + size_t getExternalMemorySize() noexcept override; + bool equals(const std::shared_ptr& other) override; + void dispose() noexcept override; + std::string toString() override; + + public: + inline const jni::global_ref& getJavaPart() const noexcept { + return _javaPart; + } + + public: + // Properties + + + public: + // Methods + std::shared_ptr>> loadFontFromURL(const std::string& url) override; + std::shared_ptr loadFontFromResource(const std::string& resource) override; + std::shared_ptr loadFontFromBytes(const std::shared_ptr& bytes) override; + std::shared_ptr loadFontByName(const std::string& name) override; + std::shared_ptr getSystemDefaultFont() override; + void setFontsForWeight(double weight, const std::vector>& fonts) override; + std::shared_ptr> applyFallbackFonts() override; + std::shared_ptr> clearFallbackFonts() override; + + private: + friend HybridBase; + using HybridBase::HybridBase; + jni::global_ref _javaPart; + }; + +} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/android/c++/JHybridRiveSpec.cpp b/nitrogen/generated/android/c++/JHybridRiveSpec.cpp deleted file mode 100644 index f1fcd7a4..00000000 --- a/nitrogen/generated/android/c++/JHybridRiveSpec.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/// -/// JHybridRiveSpec.cpp -/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. -/// https://github.com/mrousavy/nitro -/// Copyright © Marc Rousavy @ Margelo -/// - -#include "JHybridRiveSpec.hpp" - - - - - -namespace margelo::nitro::rive { - - jni::local_ref JHybridRiveSpec::initHybrid(jni::alias_ref jThis) { - return makeCxxInstance(jThis); - } - - void JHybridRiveSpec::registerNatives() { - registerHybrid({ - makeNativeMethod("initHybrid", JHybridRiveSpec::initHybrid), - }); - } - - size_t JHybridRiveSpec::getExternalMemorySize() noexcept { - static const auto method = javaClassStatic()->getMethod("getMemorySize"); - return method(_javaPart); - } - - bool JHybridRiveSpec::equals(const std::shared_ptr& other) { - if (auto otherCast = std::dynamic_pointer_cast(other)) { - return _javaPart == otherCast->_javaPart; - } - return false; - } - - void JHybridRiveSpec::dispose() noexcept { - static const auto method = javaClassStatic()->getMethod("dispose"); - method(_javaPart); - } - - std::string JHybridRiveSpec::toString() { - static const auto method = javaClassStatic()->getMethod("toString"); - auto javaString = method(_javaPart); - return javaString->toStdString(); - } - - // Properties - - - // Methods - double JHybridRiveSpec::multiply(double a, double b) { - static const auto method = javaClassStatic()->getMethod("multiply"); - auto __result = method(_javaPart, a, b); - return __result; - } - -} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveSpec.kt b/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridFallbackFontSpec.kt similarity index 78% rename from nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveSpec.kt rename to nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridFallbackFontSpec.kt index 826a3b32..615dc0c1 100644 --- a/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveSpec.kt +++ b/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridFallbackFontSpec.kt @@ -1,5 +1,5 @@ /// -/// HybridRiveSpec.kt +/// HybridFallbackFontSpec.kt /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -13,8 +13,8 @@ import com.facebook.proguard.annotations.DoNotStrip import com.margelo.nitro.core.HybridObject /** - * A Kotlin class representing the Rive HybridObject. - * Implement this abstract class to create Kotlin-based instances of Rive. + * A Kotlin class representing the FallbackFont HybridObject. + * Implement this abstract class to create Kotlin-based instances of FallbackFont. */ @DoNotStrip @Keep @@ -23,7 +23,7 @@ import com.margelo.nitro.core.HybridObject "RedundantSuppression", "RedundantUnitReturnType", "SimpleRedundantLet", "LocalVariableName", "PropertyName", "PrivatePropertyName", "FunctionName" ) -abstract class HybridRiveSpec: HybridObject() { +abstract class HybridFallbackFontSpec: HybridObject() { @DoNotStrip private var mHybridData: HybridData = initHybrid() @@ -38,20 +38,18 @@ abstract class HybridRiveSpec: HybridObject() { // Default implementation of `HybridObject.toString()` override fun toString(): String { - return "[HybridObject Rive]" + return "[HybridObject FallbackFont]" } // Properties // Methods - @DoNotStrip - @Keep - abstract fun multiply(a: Double, b: Double): Double + private external fun initHybrid(): HybridData companion object { - protected const val TAG = "HybridRiveSpec" + protected const val TAG = "HybridFallbackFontSpec" } } diff --git a/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveFontConfigSpec.kt b/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveFontConfigSpec.kt new file mode 100644 index 00000000..401502ee --- /dev/null +++ b/nitrogen/generated/android/kotlin/com/margelo/nitro/rive/HybridRiveFontConfigSpec.kt @@ -0,0 +1,87 @@ +/// +/// HybridRiveFontConfigSpec.kt +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +package com.margelo.nitro.rive + +import androidx.annotation.Keep +import com.facebook.jni.HybridData +import com.facebook.proguard.annotations.DoNotStrip +import com.margelo.nitro.core.Promise +import com.margelo.nitro.core.ArrayBuffer +import com.margelo.nitro.core.HybridObject + +/** + * A Kotlin class representing the RiveFontConfig HybridObject. + * Implement this abstract class to create Kotlin-based instances of RiveFontConfig. + */ +@DoNotStrip +@Keep +@Suppress( + "KotlinJniMissingFunction", "unused", + "RedundantSuppression", "RedundantUnitReturnType", "SimpleRedundantLet", + "LocalVariableName", "PropertyName", "PrivatePropertyName", "FunctionName" +) +abstract class HybridRiveFontConfigSpec: HybridObject() { + @DoNotStrip + private var mHybridData: HybridData = initHybrid() + + init { + super.updateNative(mHybridData) + } + + override fun updateNative(hybridData: HybridData) { + mHybridData = hybridData + super.updateNative(hybridData) + } + + // Default implementation of `HybridObject.toString()` + override fun toString(): String { + return "[HybridObject RiveFontConfig]" + } + + // Properties + + + // Methods + @DoNotStrip + @Keep + abstract fun loadFontFromURL(url: String): Promise + + @DoNotStrip + @Keep + abstract fun loadFontFromResource(resource: String): HybridFallbackFontSpec + + @DoNotStrip + @Keep + abstract fun loadFontFromBytes(bytes: ArrayBuffer): HybridFallbackFontSpec + + @DoNotStrip + @Keep + abstract fun loadFontByName(name: String): HybridFallbackFontSpec + + @DoNotStrip + @Keep + abstract fun getSystemDefaultFont(): HybridFallbackFontSpec + + @DoNotStrip + @Keep + abstract fun setFontsForWeight(weight: Double, fonts: Array): Unit + + @DoNotStrip + @Keep + abstract fun applyFallbackFonts(): Promise + + @DoNotStrip + @Keep + abstract fun clearFallbackFonts(): Promise + + private external fun initHybrid(): HybridData + + companion object { + protected const val TAG = "HybridRiveFontConfigSpec" + } +} diff --git a/nitrogen/generated/android/rive+autolinking.cmake b/nitrogen/generated/android/rive+autolinking.cmake index 6b89147c..020c674e 100644 --- a/nitrogen/generated/android/rive+autolinking.cmake +++ b/nitrogen/generated/android/rive+autolinking.cmake @@ -34,9 +34,10 @@ target_sources( ../nitrogen/generated/android/riveOnLoad.cpp # Shared Nitrogen C++ sources ../nitrogen/generated/shared/c++/HybridBindableArtboardSpec.cpp - ../nitrogen/generated/shared/c++/HybridRiveSpec.cpp ../nitrogen/generated/shared/c++/HybridRiveFileSpec.cpp ../nitrogen/generated/shared/c++/HybridRiveFileFactorySpec.cpp + ../nitrogen/generated/shared/c++/HybridFallbackFontSpec.cpp + ../nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.cpp ../nitrogen/generated/shared/c++/HybridRiveImageSpec.cpp ../nitrogen/generated/shared/c++/HybridRiveImageFactorySpec.cpp ../nitrogen/generated/shared/c++/HybridRiveRuntimeSpec.cpp @@ -56,9 +57,10 @@ target_sources( ../nitrogen/generated/shared/c++/HybridViewModelArtboardPropertySpec.cpp # Android-specific Nitrogen C++ sources ../nitrogen/generated/android/c++/JHybridBindableArtboardSpec.cpp - ../nitrogen/generated/android/c++/JHybridRiveSpec.cpp ../nitrogen/generated/android/c++/JHybridRiveFileSpec.cpp ../nitrogen/generated/android/c++/JHybridRiveFileFactorySpec.cpp + ../nitrogen/generated/android/c++/JHybridFallbackFontSpec.cpp + ../nitrogen/generated/android/c++/JHybridRiveFontConfigSpec.cpp ../nitrogen/generated/android/c++/JHybridRiveImageSpec.cpp ../nitrogen/generated/android/c++/JHybridRiveImageFactorySpec.cpp ../nitrogen/generated/android/c++/JHybridRiveRuntimeSpec.cpp diff --git a/nitrogen/generated/android/riveOnLoad.cpp b/nitrogen/generated/android/riveOnLoad.cpp index 25e1473d..5bd9eb58 100644 --- a/nitrogen/generated/android/riveOnLoad.cpp +++ b/nitrogen/generated/android/riveOnLoad.cpp @@ -16,9 +16,10 @@ #include #include "JHybridBindableArtboardSpec.hpp" -#include "JHybridRiveSpec.hpp" #include "JHybridRiveFileSpec.hpp" #include "JHybridRiveFileFactorySpec.hpp" +#include "JHybridFallbackFontSpec.hpp" +#include "JHybridRiveFontConfigSpec.hpp" #include "JHybridRiveImageSpec.hpp" #include "JHybridRiveImageFactorySpec.hpp" #include "JHybridRiveRuntimeSpec.hpp" @@ -54,9 +55,10 @@ int initialize(JavaVM* vm) { return facebook::jni::initialize(vm, [] { // Register native JNI methods margelo::nitro::rive::JHybridBindableArtboardSpec::registerNatives(); - margelo::nitro::rive::JHybridRiveSpec::registerNatives(); margelo::nitro::rive::JHybridRiveFileSpec::registerNatives(); margelo::nitro::rive::JHybridRiveFileFactorySpec::registerNatives(); + margelo::nitro::rive::JHybridFallbackFontSpec::registerNatives(); + margelo::nitro::rive::JHybridRiveFontConfigSpec::registerNatives(); margelo::nitro::rive::JHybridRiveImageSpec::registerNatives(); margelo::nitro::rive::JHybridRiveImageFactorySpec::registerNatives(); margelo::nitro::rive::JHybridRiveRuntimeSpec::registerNatives(); @@ -83,9 +85,9 @@ int initialize(JavaVM* vm) { // Register Nitro Hybrid Objects HybridObjectRegistry::registerHybridObjectConstructor( - "Rive", + "RiveFontConfig", []() -> std::shared_ptr { - static DefaultConstructableObject object("com/margelo/nitro/rive/Rive"); + static DefaultConstructableObject object("com/margelo/nitro/rive/HybridRiveFontConfig"); auto instance = object.create(); return instance->cthis()->shared(); } diff --git a/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.cpp b/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.cpp index ee02c6de..89ae6cb9 100644 --- a/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.cpp +++ b/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.cpp @@ -9,12 +9,13 @@ // Include C++ implementation defined types #include "HybridBindableArtboardSpecSwift.hpp" +#include "HybridFallbackFontSpecSwift.hpp" #include "HybridRiveFileFactorySpecSwift.hpp" #include "HybridRiveFileSpecSwift.hpp" +#include "HybridRiveFontConfigSpecSwift.hpp" #include "HybridRiveImageFactorySpecSwift.hpp" #include "HybridRiveImageSpecSwift.hpp" #include "HybridRiveRuntimeSpecSwift.hpp" -#include "HybridRiveSpecSwift.hpp" #include "HybridRiveViewSpecSwift.hpp" #include "HybridViewModelArtboardPropertySpecSwift.hpp" #include "HybridViewModelBooleanPropertySpecSwift.hpp" @@ -49,22 +50,6 @@ namespace margelo::nitro::rive::bridge::swift { return swiftPart.toUnsafe(); } - // pragma MARK: std::shared_ptr - std::shared_ptr create_std__shared_ptr_HybridRiveSpec_(void* NON_NULL swiftUnsafePointer) noexcept { - RNRive::HybridRiveSpec_cxx swiftPart = RNRive::HybridRiveSpec_cxx::fromUnsafe(swiftUnsafePointer); - return std::make_shared(swiftPart); - } - void* NON_NULL get_std__shared_ptr_HybridRiveSpec_(std__shared_ptr_HybridRiveSpec_ cppType) { - std::shared_ptr swiftWrapper = std::dynamic_pointer_cast(cppType); - #ifdef NITRO_DEBUG - if (swiftWrapper == nullptr) [[unlikely]] { - throw std::runtime_error("Class \"HybridRiveSpec\" is not implemented in Swift!"); - } - #endif - RNRive::HybridRiveSpec_cxx& swiftPart = swiftWrapper->getSwiftPart(); - return swiftPart.toUnsafe(); - } - // pragma MARK: std::shared_ptr std::shared_ptr create_std__shared_ptr_HybridViewModelSpec_(void* NON_NULL swiftUnsafePointer) noexcept { RNRive::HybridViewModelSpec_cxx swiftPart = RNRive::HybridViewModelSpec_cxx::fromUnsafe(swiftUnsafePointer); @@ -145,6 +130,54 @@ namespace margelo::nitro::rive::bridge::swift { return swiftPart.toUnsafe(); } + // pragma MARK: std::shared_ptr + std::shared_ptr create_std__shared_ptr_HybridFallbackFontSpec_(void* NON_NULL swiftUnsafePointer) noexcept { + RNRive::HybridFallbackFontSpec_cxx swiftPart = RNRive::HybridFallbackFontSpec_cxx::fromUnsafe(swiftUnsafePointer); + return std::make_shared(swiftPart); + } + void* NON_NULL get_std__shared_ptr_HybridFallbackFontSpec_(std__shared_ptr_HybridFallbackFontSpec_ cppType) { + std::shared_ptr swiftWrapper = std::dynamic_pointer_cast(cppType); + #ifdef NITRO_DEBUG + if (swiftWrapper == nullptr) [[unlikely]] { + throw std::runtime_error("Class \"HybridFallbackFontSpec\" is not implemented in Swift!"); + } + #endif + RNRive::HybridFallbackFontSpec_cxx& swiftPart = swiftWrapper->getSwiftPart(); + return swiftPart.toUnsafe(); + } + + // pragma MARK: std::function& /* result */)> + Func_void_std__shared_ptr_HybridFallbackFontSpec_ create_Func_void_std__shared_ptr_HybridFallbackFontSpec_(void* NON_NULL swiftClosureWrapper) noexcept { + auto swiftClosure = RNRive::Func_void_std__shared_ptr_HybridFallbackFontSpec_::fromUnsafe(swiftClosureWrapper); + return [swiftClosure = std::move(swiftClosure)](const std::shared_ptr& result) mutable -> void { + swiftClosure.call(result); + }; + } + + // pragma MARK: std::function + Func_void create_Func_void(void* NON_NULL swiftClosureWrapper) noexcept { + auto swiftClosure = RNRive::Func_void::fromUnsafe(swiftClosureWrapper); + return [swiftClosure = std::move(swiftClosure)]() mutable -> void { + swiftClosure.call(); + }; + } + + // pragma MARK: std::shared_ptr + std::shared_ptr create_std__shared_ptr_HybridRiveFontConfigSpec_(void* NON_NULL swiftUnsafePointer) noexcept { + RNRive::HybridRiveFontConfigSpec_cxx swiftPart = RNRive::HybridRiveFontConfigSpec_cxx::fromUnsafe(swiftUnsafePointer); + return std::make_shared(swiftPart); + } + void* NON_NULL get_std__shared_ptr_HybridRiveFontConfigSpec_(std__shared_ptr_HybridRiveFontConfigSpec_ cppType) { + std::shared_ptr swiftWrapper = std::dynamic_pointer_cast(cppType); + #ifdef NITRO_DEBUG + if (swiftWrapper == nullptr) [[unlikely]] { + throw std::runtime_error("Class \"HybridRiveFontConfigSpec\" is not implemented in Swift!"); + } + #endif + RNRive::HybridRiveFontConfigSpec_cxx& swiftPart = swiftWrapper->getSwiftPart(); + return swiftPart.toUnsafe(); + } + // pragma MARK: std::function& /* result */)> Func_void_std__shared_ptr_HybridRiveImageSpec_ create_Func_void_std__shared_ptr_HybridRiveImageSpec_(void* NON_NULL swiftClosureWrapper) noexcept { auto swiftClosure = RNRive::Func_void_std__shared_ptr_HybridRiveImageSpec_::fromUnsafe(swiftClosureWrapper); @@ -169,14 +202,6 @@ namespace margelo::nitro::rive::bridge::swift { return swiftPart.toUnsafe(); } - // pragma MARK: std::function - Func_void create_Func_void(void* NON_NULL swiftClosureWrapper) noexcept { - auto swiftClosure = RNRive::Func_void::fromUnsafe(swiftClosureWrapper); - return [swiftClosure = std::move(swiftClosure)]() mutable -> void { - swiftClosure.call(); - }; - } - // pragma MARK: std::shared_ptr std::shared_ptr create_std__shared_ptr_HybridRiveRuntimeSpec_(void* NON_NULL swiftUnsafePointer) noexcept { RNRive::HybridRiveRuntimeSpec_cxx swiftPart = RNRive::HybridRiveRuntimeSpec_cxx::fromUnsafe(swiftUnsafePointer); diff --git a/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.hpp b/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.hpp index 425cbd31..6ccaab64 100644 --- a/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.hpp +++ b/nitrogen/generated/ios/RNRive-Swift-Cxx-Bridge.hpp @@ -22,18 +22,20 @@ namespace margelo::nitro::rive { enum class DataBindMode; } namespace margelo::nitro::rive { enum class Fit; } // Forward declaration of `HybridBindableArtboardSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridBindableArtboardSpec; } +// Forward declaration of `HybridFallbackFontSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridFallbackFontSpec; } // Forward declaration of `HybridRiveFileFactorySpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveFileFactorySpec; } // Forward declaration of `HybridRiveFileSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveFileSpec; } +// Forward declaration of `HybridRiveFontConfigSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridRiveFontConfigSpec; } // Forward declaration of `HybridRiveImageFactorySpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveImageFactorySpec; } // Forward declaration of `HybridRiveImageSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveImageSpec; } // Forward declaration of `HybridRiveRuntimeSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveRuntimeSpec; } -// Forward declaration of `HybridRiveSpec` to properly resolve imports. -namespace margelo::nitro::rive { class HybridRiveSpec; } // Forward declaration of `HybridRiveViewSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveViewSpec; } // Forward declaration of `HybridViewModelArtboardPropertySpec` to properly resolve imports. @@ -76,18 +78,20 @@ namespace margelo::nitro::rive { struct UnifiedRiveEvent; } // Forward declarations of Swift defined types // Forward declaration of `HybridBindableArtboardSpec_cxx` to properly resolve imports. namespace RNRive { class HybridBindableArtboardSpec_cxx; } +// Forward declaration of `HybridFallbackFontSpec_cxx` to properly resolve imports. +namespace RNRive { class HybridFallbackFontSpec_cxx; } // Forward declaration of `HybridRiveFileFactorySpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveFileFactorySpec_cxx; } // Forward declaration of `HybridRiveFileSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveFileSpec_cxx; } +// Forward declaration of `HybridRiveFontConfigSpec_cxx` to properly resolve imports. +namespace RNRive { class HybridRiveFontConfigSpec_cxx; } // Forward declaration of `HybridRiveImageFactorySpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveImageFactorySpec_cxx; } // Forward declaration of `HybridRiveImageSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveImageSpec_cxx; } // Forward declaration of `HybridRiveRuntimeSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveRuntimeSpec_cxx; } -// Forward declaration of `HybridRiveSpec_cxx` to properly resolve imports. -namespace RNRive { class HybridRiveSpec_cxx; } // Forward declaration of `HybridRiveViewSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveViewSpec_cxx; } // Forward declaration of `HybridViewModelArtboardPropertySpec_cxx` to properly resolve imports. @@ -123,12 +127,13 @@ namespace RNRive { class HybridViewModelTriggerPropertySpec_cxx; } #include "DataBindMode.hpp" #include "Fit.hpp" #include "HybridBindableArtboardSpec.hpp" +#include "HybridFallbackFontSpec.hpp" #include "HybridRiveFileFactorySpec.hpp" #include "HybridRiveFileSpec.hpp" +#include "HybridRiveFontConfigSpec.hpp" #include "HybridRiveImageFactorySpec.hpp" #include "HybridRiveImageSpec.hpp" #include "HybridRiveRuntimeSpec.hpp" -#include "HybridRiveSpec.hpp" #include "HybridRiveViewSpec.hpp" #include "HybridViewModelArtboardPropertySpec.hpp" #include "HybridViewModelBooleanPropertySpec.hpp" @@ -178,27 +183,6 @@ namespace margelo::nitro::rive::bridge::swift { using std__weak_ptr_HybridBindableArtboardSpec_ = std::weak_ptr; inline std__weak_ptr_HybridBindableArtboardSpec_ weakify_std__shared_ptr_HybridBindableArtboardSpec_(const std::shared_ptr& strong) noexcept { return strong; } - // pragma MARK: std::shared_ptr - /** - * Specialized version of `std::shared_ptr`. - */ - using std__shared_ptr_HybridRiveSpec_ = std::shared_ptr; - std::shared_ptr create_std__shared_ptr_HybridRiveSpec_(void* NON_NULL swiftUnsafePointer) noexcept; - void* NON_NULL get_std__shared_ptr_HybridRiveSpec_(std__shared_ptr_HybridRiveSpec_ cppType); - - // pragma MARK: std::weak_ptr - using std__weak_ptr_HybridRiveSpec_ = std::weak_ptr; - inline std__weak_ptr_HybridRiveSpec_ weakify_std__shared_ptr_HybridRiveSpec_(const std::shared_ptr& strong) noexcept { return strong; } - - // pragma MARK: Result - using Result_double_ = Result; - inline Result_double_ create_Result_double_(double value) noexcept { - return Result::withValue(std::move(value)); - } - inline Result_double_ create_Result_double_(const std::exception_ptr& error) noexcept { - return Result::withError(error); - } - // pragma MARK: std::optional /** * Specialized version of `std::optional`. @@ -480,59 +464,61 @@ namespace margelo::nitro::rive::bridge::swift { return Result>>>::withError(error); } - // pragma MARK: std::shared_ptr>> + // pragma MARK: std::shared_ptr /** - * Specialized version of `std::shared_ptr>>`. + * Specialized version of `std::shared_ptr`. */ - using std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec___ = std::shared_ptr>>; - inline std::shared_ptr>> create_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec___() noexcept { - return Promise>::create(); + using std__shared_ptr_HybridFallbackFontSpec_ = std::shared_ptr; + std::shared_ptr create_std__shared_ptr_HybridFallbackFontSpec_(void* NON_NULL swiftUnsafePointer) noexcept; + void* NON_NULL get_std__shared_ptr_HybridFallbackFontSpec_(std__shared_ptr_HybridFallbackFontSpec_ cppType); + + // pragma MARK: std::weak_ptr + using std__weak_ptr_HybridFallbackFontSpec_ = std::weak_ptr; + inline std__weak_ptr_HybridFallbackFontSpec_ weakify_std__shared_ptr_HybridFallbackFontSpec_(const std::shared_ptr& strong) noexcept { return strong; } + + // pragma MARK: std::shared_ptr>> + /** + * Specialized version of `std::shared_ptr>>`. + */ + using std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec___ = std::shared_ptr>>; + inline std::shared_ptr>> create_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec___() noexcept { + return Promise>::create(); } - inline PromiseHolder> wrap_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec___(std::shared_ptr>> promise) noexcept { - return PromiseHolder>(std::move(promise)); + inline PromiseHolder> wrap_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec___(std::shared_ptr>> promise) noexcept { + return PromiseHolder>(std::move(promise)); } - // pragma MARK: std::function& /* result */)> + // pragma MARK: std::function& /* result */)> /** - * Specialized version of `std::function&)>`. + * Specialized version of `std::function&)>`. */ - using Func_void_std__shared_ptr_HybridRiveImageSpec_ = std::function& /* result */)>; + using Func_void_std__shared_ptr_HybridFallbackFontSpec_ = std::function& /* result */)>; /** - * Wrapper class for a `std::function& / * result * /)>`, this can be used from Swift. + * Wrapper class for a `std::function& / * result * /)>`, this can be used from Swift. */ - class Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper final { + class Func_void_std__shared_ptr_HybridFallbackFontSpec__Wrapper final { public: - explicit Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper(std::function& /* result */)>&& func): _function(std::make_unique& /* result */)>>(std::move(func))) {} - inline void call(std::shared_ptr result) const noexcept { + explicit Func_void_std__shared_ptr_HybridFallbackFontSpec__Wrapper(std::function& /* result */)>&& func): _function(std::make_unique& /* result */)>>(std::move(func))) {} + inline void call(std::shared_ptr result) const noexcept { _function->operator()(result); } private: - std::unique_ptr& /* result */)>> _function; + std::unique_ptr& /* result */)>> _function; } SWIFT_NONCOPYABLE; - Func_void_std__shared_ptr_HybridRiveImageSpec_ create_Func_void_std__shared_ptr_HybridRiveImageSpec_(void* NON_NULL swiftClosureWrapper) noexcept; - inline Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper wrap_Func_void_std__shared_ptr_HybridRiveImageSpec_(Func_void_std__shared_ptr_HybridRiveImageSpec_ value) noexcept { - return Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper(std::move(value)); + Func_void_std__shared_ptr_HybridFallbackFontSpec_ create_Func_void_std__shared_ptr_HybridFallbackFontSpec_(void* NON_NULL swiftClosureWrapper) noexcept; + inline Func_void_std__shared_ptr_HybridFallbackFontSpec__Wrapper wrap_Func_void_std__shared_ptr_HybridFallbackFontSpec_(Func_void_std__shared_ptr_HybridFallbackFontSpec_ value) noexcept { + return Func_void_std__shared_ptr_HybridFallbackFontSpec__Wrapper(std::move(value)); } - // pragma MARK: std::shared_ptr + // pragma MARK: std::vector> /** - * Specialized version of `std::shared_ptr`. + * Specialized version of `std::vector>`. */ - using std__shared_ptr_HybridRiveImageFactorySpec_ = std::shared_ptr; - std::shared_ptr create_std__shared_ptr_HybridRiveImageFactorySpec_(void* NON_NULL swiftUnsafePointer) noexcept; - void* NON_NULL get_std__shared_ptr_HybridRiveImageFactorySpec_(std__shared_ptr_HybridRiveImageFactorySpec_ cppType); - - // pragma MARK: std::weak_ptr - using std__weak_ptr_HybridRiveImageFactorySpec_ = std::weak_ptr; - inline std__weak_ptr_HybridRiveImageFactorySpec_ weakify_std__shared_ptr_HybridRiveImageFactorySpec_(const std::shared_ptr& strong) noexcept { return strong; } - - // pragma MARK: Result>>> - using Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____ = Result>>>; - inline Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____ create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____(const std::shared_ptr>>& value) noexcept { - return Result>>>::withValue(value); - } - inline Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____ create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____(const std::exception_ptr& error) noexcept { - return Result>>>::withError(error); + using std__vector_std__shared_ptr_HybridFallbackFontSpec__ = std::vector>; + inline std::vector> create_std__vector_std__shared_ptr_HybridFallbackFontSpec__(size_t size) noexcept { + std::vector> vector; + vector.reserve(size); + return vector; } // pragma MARK: std::shared_ptr> @@ -569,17 +555,35 @@ namespace margelo::nitro::rive::bridge::swift { return Func_void_Wrapper(std::move(value)); } - // pragma MARK: std::shared_ptr + // pragma MARK: std::shared_ptr /** - * Specialized version of `std::shared_ptr`. + * Specialized version of `std::shared_ptr`. */ - using std__shared_ptr_HybridRiveRuntimeSpec_ = std::shared_ptr; - std::shared_ptr create_std__shared_ptr_HybridRiveRuntimeSpec_(void* NON_NULL swiftUnsafePointer) noexcept; - void* NON_NULL get_std__shared_ptr_HybridRiveRuntimeSpec_(std__shared_ptr_HybridRiveRuntimeSpec_ cppType); + using std__shared_ptr_HybridRiveFontConfigSpec_ = std::shared_ptr; + std::shared_ptr create_std__shared_ptr_HybridRiveFontConfigSpec_(void* NON_NULL swiftUnsafePointer) noexcept; + void* NON_NULL get_std__shared_ptr_HybridRiveFontConfigSpec_(std__shared_ptr_HybridRiveFontConfigSpec_ cppType); - // pragma MARK: std::weak_ptr - using std__weak_ptr_HybridRiveRuntimeSpec_ = std::weak_ptr; - inline std__weak_ptr_HybridRiveRuntimeSpec_ weakify_std__shared_ptr_HybridRiveRuntimeSpec_(const std::shared_ptr& strong) noexcept { return strong; } + // pragma MARK: std::weak_ptr + using std__weak_ptr_HybridRiveFontConfigSpec_ = std::weak_ptr; + inline std__weak_ptr_HybridRiveFontConfigSpec_ weakify_std__shared_ptr_HybridRiveFontConfigSpec_(const std::shared_ptr& strong) noexcept { return strong; } + + // pragma MARK: Result>>> + using Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____ = Result>>>; + inline Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____ create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____(const std::shared_ptr>>& value) noexcept { + return Result>>>::withValue(value); + } + inline Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____ create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____(const std::exception_ptr& error) noexcept { + return Result>>>::withError(error); + } + + // pragma MARK: Result> + using Result_std__shared_ptr_HybridFallbackFontSpec__ = Result>; + inline Result_std__shared_ptr_HybridFallbackFontSpec__ create_Result_std__shared_ptr_HybridFallbackFontSpec__(const std::shared_ptr& value) noexcept { + return Result>::withValue(value); + } + inline Result_std__shared_ptr_HybridFallbackFontSpec__ create_Result_std__shared_ptr_HybridFallbackFontSpec__(const std::exception_ptr& error) noexcept { + return Result>::withError(error); + } // pragma MARK: Result>> using Result_std__shared_ptr_Promise_void___ = Result>>; @@ -590,6 +594,73 @@ namespace margelo::nitro::rive::bridge::swift { return Result>>::withError(error); } + // pragma MARK: std::shared_ptr>> + /** + * Specialized version of `std::shared_ptr>>`. + */ + using std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec___ = std::shared_ptr>>; + inline std::shared_ptr>> create_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec___() noexcept { + return Promise>::create(); + } + inline PromiseHolder> wrap_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec___(std::shared_ptr>> promise) noexcept { + return PromiseHolder>(std::move(promise)); + } + + // pragma MARK: std::function& /* result */)> + /** + * Specialized version of `std::function&)>`. + */ + using Func_void_std__shared_ptr_HybridRiveImageSpec_ = std::function& /* result */)>; + /** + * Wrapper class for a `std::function& / * result * /)>`, this can be used from Swift. + */ + class Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper final { + public: + explicit Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper(std::function& /* result */)>&& func): _function(std::make_unique& /* result */)>>(std::move(func))) {} + inline void call(std::shared_ptr result) const noexcept { + _function->operator()(result); + } + private: + std::unique_ptr& /* result */)>> _function; + } SWIFT_NONCOPYABLE; + Func_void_std__shared_ptr_HybridRiveImageSpec_ create_Func_void_std__shared_ptr_HybridRiveImageSpec_(void* NON_NULL swiftClosureWrapper) noexcept; + inline Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper wrap_Func_void_std__shared_ptr_HybridRiveImageSpec_(Func_void_std__shared_ptr_HybridRiveImageSpec_ value) noexcept { + return Func_void_std__shared_ptr_HybridRiveImageSpec__Wrapper(std::move(value)); + } + + // pragma MARK: std::shared_ptr + /** + * Specialized version of `std::shared_ptr`. + */ + using std__shared_ptr_HybridRiveImageFactorySpec_ = std::shared_ptr; + std::shared_ptr create_std__shared_ptr_HybridRiveImageFactorySpec_(void* NON_NULL swiftUnsafePointer) noexcept; + void* NON_NULL get_std__shared_ptr_HybridRiveImageFactorySpec_(std__shared_ptr_HybridRiveImageFactorySpec_ cppType); + + // pragma MARK: std::weak_ptr + using std__weak_ptr_HybridRiveImageFactorySpec_ = std::weak_ptr; + inline std__weak_ptr_HybridRiveImageFactorySpec_ weakify_std__shared_ptr_HybridRiveImageFactorySpec_(const std::shared_ptr& strong) noexcept { return strong; } + + // pragma MARK: Result>>> + using Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____ = Result>>>; + inline Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____ create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____(const std::shared_ptr>>& value) noexcept { + return Result>>>::withValue(value); + } + inline Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____ create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridRiveImageSpec____(const std::exception_ptr& error) noexcept { + return Result>>>::withError(error); + } + + // pragma MARK: std::shared_ptr + /** + * Specialized version of `std::shared_ptr`. + */ + using std__shared_ptr_HybridRiveRuntimeSpec_ = std::shared_ptr; + std::shared_ptr create_std__shared_ptr_HybridRiveRuntimeSpec_(void* NON_NULL swiftUnsafePointer) noexcept; + void* NON_NULL get_std__shared_ptr_HybridRiveRuntimeSpec_(std__shared_ptr_HybridRiveRuntimeSpec_ cppType); + + // pragma MARK: std::weak_ptr + using std__weak_ptr_HybridRiveRuntimeSpec_ = std::weak_ptr; + inline std__weak_ptr_HybridRiveRuntimeSpec_ weakify_std__shared_ptr_HybridRiveRuntimeSpec_(const std::shared_ptr& strong) noexcept { return strong; } + // pragma MARK: std::optional /** * Specialized version of `std::optional`. @@ -895,6 +966,15 @@ namespace margelo::nitro::rive::bridge::swift { return Result>>::withError(error); } + // pragma MARK: Result + using Result_double_ = Result; + inline Result_double_ create_Result_double_(double value) noexcept { + return Result::withValue(std::move(value)); + } + inline Result_double_ create_Result_double_(const std::exception_ptr& error) noexcept { + return Result::withError(error); + } + // pragma MARK: Result using Result_bool_ = Result; inline Result_bool_ create_Result_bool_(bool value) noexcept { diff --git a/nitrogen/generated/ios/RNRive-Swift-Cxx-Umbrella.hpp b/nitrogen/generated/ios/RNRive-Swift-Cxx-Umbrella.hpp index ab1751e6..b7531b3d 100644 --- a/nitrogen/generated/ios/RNRive-Swift-Cxx-Umbrella.hpp +++ b/nitrogen/generated/ios/RNRive-Swift-Cxx-Umbrella.hpp @@ -22,18 +22,20 @@ namespace margelo::nitro::rive { enum class DataBindMode; } namespace margelo::nitro::rive { enum class Fit; } // Forward declaration of `HybridBindableArtboardSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridBindableArtboardSpec; } +// Forward declaration of `HybridFallbackFontSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridFallbackFontSpec; } // Forward declaration of `HybridRiveFileFactorySpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveFileFactorySpec; } // Forward declaration of `HybridRiveFileSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveFileSpec; } +// Forward declaration of `HybridRiveFontConfigSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridRiveFontConfigSpec; } // Forward declaration of `HybridRiveImageFactorySpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveImageFactorySpec; } // Forward declaration of `HybridRiveImageSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveImageSpec; } // Forward declaration of `HybridRiveRuntimeSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveRuntimeSpec; } -// Forward declaration of `HybridRiveSpec` to properly resolve imports. -namespace margelo::nitro::rive { class HybridRiveSpec; } // Forward declaration of `HybridRiveViewSpec` to properly resolve imports. namespace margelo::nitro::rive { class HybridRiveViewSpec; } // Forward declaration of `HybridViewModelArtboardPropertySpec` to properly resolve imports. @@ -81,12 +83,13 @@ namespace margelo::nitro::rive { struct UnifiedRiveEvent; } #include "DataBindMode.hpp" #include "Fit.hpp" #include "HybridBindableArtboardSpec.hpp" +#include "HybridFallbackFontSpec.hpp" #include "HybridRiveFileFactorySpec.hpp" #include "HybridRiveFileSpec.hpp" +#include "HybridRiveFontConfigSpec.hpp" #include "HybridRiveImageFactorySpec.hpp" #include "HybridRiveImageSpec.hpp" #include "HybridRiveRuntimeSpec.hpp" -#include "HybridRiveSpec.hpp" #include "HybridRiveViewSpec.hpp" #include "HybridViewModelArtboardPropertySpec.hpp" #include "HybridViewModelBooleanPropertySpec.hpp" @@ -130,18 +133,20 @@ namespace margelo::nitro::rive { struct UnifiedRiveEvent; } // Forward declarations of Swift defined types // Forward declaration of `HybridBindableArtboardSpec_cxx` to properly resolve imports. namespace RNRive { class HybridBindableArtboardSpec_cxx; } +// Forward declaration of `HybridFallbackFontSpec_cxx` to properly resolve imports. +namespace RNRive { class HybridFallbackFontSpec_cxx; } // Forward declaration of `HybridRiveFileFactorySpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveFileFactorySpec_cxx; } // Forward declaration of `HybridRiveFileSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveFileSpec_cxx; } +// Forward declaration of `HybridRiveFontConfigSpec_cxx` to properly resolve imports. +namespace RNRive { class HybridRiveFontConfigSpec_cxx; } // Forward declaration of `HybridRiveImageFactorySpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveImageFactorySpec_cxx; } // Forward declaration of `HybridRiveImageSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveImageSpec_cxx; } // Forward declaration of `HybridRiveRuntimeSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveRuntimeSpec_cxx; } -// Forward declaration of `HybridRiveSpec_cxx` to properly resolve imports. -namespace RNRive { class HybridRiveSpec_cxx; } // Forward declaration of `HybridRiveViewSpec_cxx` to properly resolve imports. namespace RNRive { class HybridRiveViewSpec_cxx; } // Forward declaration of `HybridViewModelArtboardPropertySpec_cxx` to properly resolve imports. diff --git a/nitrogen/generated/ios/RNRiveAutolinking.mm b/nitrogen/generated/ios/RNRiveAutolinking.mm index ed0e32a6..263fbb58 100644 --- a/nitrogen/generated/ios/RNRiveAutolinking.mm +++ b/nitrogen/generated/ios/RNRiveAutolinking.mm @@ -10,7 +10,7 @@ #import "RNRive-Swift-Cxx-Umbrella.hpp" #import -#include "HybridRiveSpecSwift.hpp" +#include "HybridRiveFontConfigSpecSwift.hpp" #include "HybridRiveFileFactorySpecSwift.hpp" #include "HybridRiveFileSpecSwift.hpp" #include "HybridRiveViewSpecSwift.hpp" @@ -27,9 +27,9 @@ + (void) load { using namespace margelo::nitro::rive; HybridObjectRegistry::registerHybridObjectConstructor( - "Rive", + "RiveFontConfig", []() -> std::shared_ptr { - std::shared_ptr hybridObject = RNRive::RNRiveAutolinking::createRive(); + std::shared_ptr hybridObject = RNRive::RNRiveAutolinking::createRiveFontConfig(); return hybridObject; } ); diff --git a/nitrogen/generated/ios/RNRiveAutolinking.swift b/nitrogen/generated/ios/RNRiveAutolinking.swift index 35432ce8..658b9bb4 100644 --- a/nitrogen/generated/ios/RNRiveAutolinking.swift +++ b/nitrogen/generated/ios/RNRiveAutolinking.swift @@ -12,16 +12,16 @@ import NitroModules public final class RNRiveAutolinking { public typealias bridge = margelo.nitro.rive.bridge.swift - public static func createRive() -> bridge.std__shared_ptr_HybridRiveSpec_ { - let hybridObject = Rive() - return { () -> bridge.std__shared_ptr_HybridRiveSpec_ in + public static func createRiveFontConfig() -> bridge.std__shared_ptr_HybridRiveFontConfigSpec_ { + let hybridObject = HybridRiveFontConfig() + return { () -> bridge.std__shared_ptr_HybridRiveFontConfigSpec_ in let __cxxWrapped = hybridObject.getCxxWrapper() return __cxxWrapped.getCxxPart() }() } - public static func isRiveRecyclable() -> Bool { - return Rive.self is any RecyclableView.Type + public static func isRiveFontConfigRecyclable() -> Bool { + return HybridRiveFontConfig.self is any RecyclableView.Type } public static func createRiveFileFactory() -> bridge.std__shared_ptr_HybridRiveFileFactorySpec_ { diff --git a/nitrogen/generated/ios/c++/HybridRiveSpecSwift.cpp b/nitrogen/generated/ios/c++/HybridFallbackFontSpecSwift.cpp similarity index 73% rename from nitrogen/generated/ios/c++/HybridRiveSpecSwift.cpp rename to nitrogen/generated/ios/c++/HybridFallbackFontSpecSwift.cpp index 06217812..2a81a925 100644 --- a/nitrogen/generated/ios/c++/HybridRiveSpecSwift.cpp +++ b/nitrogen/generated/ios/c++/HybridFallbackFontSpecSwift.cpp @@ -1,11 +1,11 @@ /// -/// HybridRiveSpecSwift.cpp +/// HybridFallbackFontSpecSwift.cpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo /// -#include "HybridRiveSpecSwift.hpp" +#include "HybridFallbackFontSpecSwift.hpp" namespace margelo::nitro::rive { } // namespace margelo::nitro::rive diff --git a/nitrogen/generated/ios/c++/HybridRiveSpecSwift.hpp b/nitrogen/generated/ios/c++/HybridFallbackFontSpecSwift.hpp similarity index 51% rename from nitrogen/generated/ios/c++/HybridRiveSpecSwift.hpp rename to nitrogen/generated/ios/c++/HybridFallbackFontSpecSwift.hpp index 705422d2..c75ab171 100644 --- a/nitrogen/generated/ios/c++/HybridRiveSpecSwift.hpp +++ b/nitrogen/generated/ios/c++/HybridFallbackFontSpecSwift.hpp @@ -1,5 +1,5 @@ /// -/// HybridRiveSpecSwift.hpp +/// HybridFallbackFontSpecSwift.hpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -7,10 +7,10 @@ #pragma once -#include "HybridRiveSpec.hpp" +#include "HybridFallbackFontSpec.hpp" -// Forward declaration of `HybridRiveSpec_cxx` to properly resolve imports. -namespace RNRive { class HybridRiveSpec_cxx; } +// Forward declaration of `HybridFallbackFontSpec_cxx` to properly resolve imports. +namespace RNRive { class HybridFallbackFontSpec_cxx; } @@ -21,25 +21,25 @@ namespace RNRive { class HybridRiveSpec_cxx; } namespace margelo::nitro::rive { /** - * The C++ part of HybridRiveSpec_cxx.swift. + * The C++ part of HybridFallbackFontSpec_cxx.swift. * - * HybridRiveSpecSwift (C++) accesses HybridRiveSpec_cxx (Swift), and might + * HybridFallbackFontSpecSwift (C++) accesses HybridFallbackFontSpec_cxx (Swift), and might * contain some additional bridging code for C++ <> Swift interop. * * Since this obviously introduces an overhead, I hope at some point in - * the future, HybridRiveSpec_cxx can directly inherit from the C++ class HybridRiveSpec + * the future, HybridFallbackFontSpec_cxx can directly inherit from the C++ class HybridFallbackFontSpec * to simplify the whole structure and memory management. */ - class HybridRiveSpecSwift: public virtual HybridRiveSpec { + class HybridFallbackFontSpecSwift: public virtual HybridFallbackFontSpec { public: // Constructor from a Swift instance - explicit HybridRiveSpecSwift(const RNRive::HybridRiveSpec_cxx& swiftPart): - HybridObject(HybridRiveSpec::TAG), + explicit HybridFallbackFontSpecSwift(const RNRive::HybridFallbackFontSpec_cxx& swiftPart): + HybridObject(HybridFallbackFontSpec::TAG), _swiftPart(swiftPart) { } public: // Get the Swift part - inline RNRive::HybridRiveSpec_cxx& getSwiftPart() noexcept { + inline RNRive::HybridFallbackFontSpec_cxx& getSwiftPart() noexcept { return _swiftPart; } @@ -48,7 +48,7 @@ namespace margelo::nitro::rive { return _swiftPart.getMemorySize(); } bool equals(const std::shared_ptr& other) override { - if (auto otherCast = std::dynamic_pointer_cast(other)) { + if (auto otherCast = std::dynamic_pointer_cast(other)) { return _swiftPart.equals(otherCast->_swiftPart); } return false; @@ -66,17 +66,10 @@ namespace margelo::nitro::rive { public: // Methods - inline double multiply(double a, double b) override { - auto __result = _swiftPart.multiply(std::forward(a), std::forward(b)); - if (__result.hasError()) [[unlikely]] { - std::rethrow_exception(__result.error()); - } - auto __value = std::move(__result.value()); - return __value; - } + private: - RNRive::HybridRiveSpec_cxx _swiftPart; + RNRive::HybridFallbackFontSpec_cxx _swiftPart; }; } // namespace margelo::nitro::rive diff --git a/nitrogen/generated/ios/c++/HybridRiveFontConfigSpecSwift.cpp b/nitrogen/generated/ios/c++/HybridRiveFontConfigSpecSwift.cpp new file mode 100644 index 00000000..404ef2b7 --- /dev/null +++ b/nitrogen/generated/ios/c++/HybridRiveFontConfigSpecSwift.cpp @@ -0,0 +1,11 @@ +/// +/// HybridRiveFontConfigSpecSwift.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include "HybridRiveFontConfigSpecSwift.hpp" + +namespace margelo::nitro::rive { +} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/ios/c++/HybridRiveFontConfigSpecSwift.hpp b/nitrogen/generated/ios/c++/HybridRiveFontConfigSpecSwift.hpp new file mode 100644 index 00000000..d056e08a --- /dev/null +++ b/nitrogen/generated/ios/c++/HybridRiveFontConfigSpecSwift.hpp @@ -0,0 +1,145 @@ +/// +/// HybridRiveFontConfigSpecSwift.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#include "HybridRiveFontConfigSpec.hpp" + +// Forward declaration of `HybridRiveFontConfigSpec_cxx` to properly resolve imports. +namespace RNRive { class HybridRiveFontConfigSpec_cxx; } + +// Forward declaration of `HybridFallbackFontSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridFallbackFontSpec; } +// Forward declaration of `ArrayBufferHolder` to properly resolve imports. +namespace NitroModules { class ArrayBufferHolder; } + +#include +#include "HybridFallbackFontSpec.hpp" +#include +#include +#include +#include +#include + +#include "RNRive-Swift-Cxx-Umbrella.hpp" + +namespace margelo::nitro::rive { + + /** + * The C++ part of HybridRiveFontConfigSpec_cxx.swift. + * + * HybridRiveFontConfigSpecSwift (C++) accesses HybridRiveFontConfigSpec_cxx (Swift), and might + * contain some additional bridging code for C++ <> Swift interop. + * + * Since this obviously introduces an overhead, I hope at some point in + * the future, HybridRiveFontConfigSpec_cxx can directly inherit from the C++ class HybridRiveFontConfigSpec + * to simplify the whole structure and memory management. + */ + class HybridRiveFontConfigSpecSwift: public virtual HybridRiveFontConfigSpec { + public: + // Constructor from a Swift instance + explicit HybridRiveFontConfigSpecSwift(const RNRive::HybridRiveFontConfigSpec_cxx& swiftPart): + HybridObject(HybridRiveFontConfigSpec::TAG), + _swiftPart(swiftPart) { } + + public: + // Get the Swift part + inline RNRive::HybridRiveFontConfigSpec_cxx& getSwiftPart() noexcept { + return _swiftPart; + } + + public: + inline size_t getExternalMemorySize() noexcept override { + return _swiftPart.getMemorySize(); + } + bool equals(const std::shared_ptr& other) override { + if (auto otherCast = std::dynamic_pointer_cast(other)) { + return _swiftPart.equals(otherCast->_swiftPart); + } + return false; + } + void dispose() noexcept override { + _swiftPart.dispose(); + } + std::string toString() override { + return _swiftPart.toString(); + } + + public: + // Properties + + + public: + // Methods + inline std::shared_ptr>> loadFontFromURL(const std::string& url) override { + auto __result = _swiftPart.loadFontFromURL(url); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + inline std::shared_ptr loadFontFromResource(const std::string& resource) override { + auto __result = _swiftPart.loadFontFromResource(resource); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + inline std::shared_ptr loadFontFromBytes(const std::shared_ptr& bytes) override { + auto __result = _swiftPart.loadFontFromBytes(ArrayBufferHolder(bytes)); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + inline std::shared_ptr loadFontByName(const std::string& name) override { + auto __result = _swiftPart.loadFontByName(name); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + inline std::shared_ptr getSystemDefaultFont() override { + auto __result = _swiftPart.getSystemDefaultFont(); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + inline void setFontsForWeight(double weight, const std::vector>& fonts) override { + auto __result = _swiftPart.setFontsForWeight(std::forward(weight), fonts); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + } + inline std::shared_ptr> applyFallbackFonts() override { + auto __result = _swiftPart.applyFallbackFonts(); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + inline std::shared_ptr> clearFallbackFonts() override { + auto __result = _swiftPart.clearFallbackFonts(); + if (__result.hasError()) [[unlikely]] { + std::rethrow_exception(__result.error()); + } + auto __value = std::move(__result.value()); + return __value; + } + + private: + RNRive::HybridRiveFontConfigSpec_cxx _swiftPart; + }; + +} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_HybridFallbackFontSpec_.swift b/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_HybridFallbackFontSpec_.swift new file mode 100644 index 00000000..b7dc1138 --- /dev/null +++ b/nitrogen/generated/ios/swift/Func_void_std__shared_ptr_HybridFallbackFontSpec_.swift @@ -0,0 +1,51 @@ +/// +/// Func_void_std__shared_ptr_HybridFallbackFontSpec_.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +import Foundation +import NitroModules + +/** + * Wraps a Swift `(_ value: (any HybridFallbackFontSpec)) -> Void` as a class. + * This class can be used from C++, e.g. to wrap the Swift closure as a `std::function`. + */ +public final class Func_void_std__shared_ptr_HybridFallbackFontSpec_ { + public typealias bridge = margelo.nitro.rive.bridge.swift + + private let closure: (_ value: (any HybridFallbackFontSpec)) -> Void + + public init(_ closure: @escaping (_ value: (any HybridFallbackFontSpec)) -> Void) { + self.closure = closure + } + + @inline(__always) + public func call(value: bridge.std__shared_ptr_HybridFallbackFontSpec_) -> Void { + self.closure({ () -> any HybridFallbackFontSpec in + let __unsafePointer = bridge.get_std__shared_ptr_HybridFallbackFontSpec_(value) + let __instance = HybridFallbackFontSpec_cxx.fromUnsafe(__unsafePointer) + return __instance.getHybridFallbackFontSpec() + }()) + } + + /** + * Casts this instance to a retained unsafe raw pointer. + * This acquires one additional strong reference on the object! + */ + @inline(__always) + public func toUnsafe() -> UnsafeMutableRawPointer { + return Unmanaged.passRetained(self).toOpaque() + } + + /** + * Casts an unsafe pointer to a `Func_void_std__shared_ptr_HybridFallbackFontSpec_`. + * The pointer has to be a retained opaque `Unmanaged`. + * This removes one strong reference from the object! + */ + @inline(__always) + public static func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> Func_void_std__shared_ptr_HybridFallbackFontSpec_ { + return Unmanaged.fromOpaque(pointer).takeRetainedValue() + } +} diff --git a/nitrogen/generated/ios/swift/HybridFallbackFontSpec.swift b/nitrogen/generated/ios/swift/HybridFallbackFontSpec.swift new file mode 100644 index 00000000..85663357 --- /dev/null +++ b/nitrogen/generated/ios/swift/HybridFallbackFontSpec.swift @@ -0,0 +1,56 @@ +/// +/// HybridFallbackFontSpec.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +import Foundation +import NitroModules + +/// See ``HybridFallbackFontSpec`` +public protocol HybridFallbackFontSpec_protocol: HybridObject { + // Properties + + + // Methods + +} + +public extension HybridFallbackFontSpec_protocol { + /// Default implementation of ``HybridObject.toString`` + func toString() -> String { + return "[HybridObject FallbackFont]" + } +} + +/// See ``HybridFallbackFontSpec`` +open class HybridFallbackFontSpec_base { + private weak var cxxWrapper: HybridFallbackFontSpec_cxx? = nil + public init() { } + public func getCxxWrapper() -> HybridFallbackFontSpec_cxx { + #if DEBUG + guard self is any HybridFallbackFontSpec else { + fatalError("`self` is not a `HybridFallbackFontSpec`! Did you accidentally inherit from `HybridFallbackFontSpec_base` instead of `HybridFallbackFontSpec`?") + } + #endif + if let cxxWrapper = self.cxxWrapper { + return cxxWrapper + } else { + let cxxWrapper = HybridFallbackFontSpec_cxx(self as! any HybridFallbackFontSpec) + self.cxxWrapper = cxxWrapper + return cxxWrapper + } + } +} + +/** + * A Swift base-protocol representing the FallbackFont HybridObject. + * Implement this protocol to create Swift-based instances of FallbackFont. + * ```swift + * class HybridFallbackFont : HybridFallbackFontSpec { + * // ... + * } + * ``` + */ +public typealias HybridFallbackFontSpec = HybridFallbackFontSpec_protocol & HybridFallbackFontSpec_base diff --git a/nitrogen/generated/ios/swift/HybridRiveSpec_cxx.swift b/nitrogen/generated/ios/swift/HybridFallbackFontSpec_cxx.swift similarity index 64% rename from nitrogen/generated/ios/swift/HybridRiveSpec_cxx.swift rename to nitrogen/generated/ios/swift/HybridFallbackFontSpec_cxx.swift index 84ef93e3..9376705e 100644 --- a/nitrogen/generated/ios/swift/HybridRiveSpec_cxx.swift +++ b/nitrogen/generated/ios/swift/HybridFallbackFontSpec_cxx.swift @@ -1,5 +1,5 @@ /// -/// HybridRiveSpec_cxx.swift +/// HybridFallbackFontSpec_cxx.swift /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -9,7 +9,7 @@ import Foundation import NitroModules /** - * A class implementation that bridges HybridRiveSpec over to C++. + * A class implementation that bridges HybridFallbackFontSpec over to C++. * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined. * * Also, some Swift types need to be bridged with special handling: @@ -17,7 +17,7 @@ import NitroModules * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper * - Throwing methods need to be wrapped with a Result type, as exceptions cannot be propagated to C++ */ -open class HybridRiveSpec_cxx { +open class HybridFallbackFontSpec_cxx { /** * The Swift <> C++ bridge's namespace (`margelo::nitro::rive::bridge::swift`) * from `RNRive-Swift-Cxx-Bridge.hpp`. @@ -26,30 +26,30 @@ open class HybridRiveSpec_cxx { public typealias bridge = margelo.nitro.rive.bridge.swift /** - * Holds an instance of the `HybridRiveSpec` Swift protocol. + * Holds an instance of the `HybridFallbackFontSpec` Swift protocol. */ - private var __implementation: any HybridRiveSpec + private var __implementation: any HybridFallbackFontSpec /** * Holds a weak pointer to the C++ class that wraps the Swift class. */ - private var __cxxPart: bridge.std__weak_ptr_HybridRiveSpec_ + private var __cxxPart: bridge.std__weak_ptr_HybridFallbackFontSpec_ /** - * Create a new `HybridRiveSpec_cxx` that wraps the given `HybridRiveSpec`. + * Create a new `HybridFallbackFontSpec_cxx` that wraps the given `HybridFallbackFontSpec`. * All properties and methods bridge to C++ types. */ - public init(_ implementation: any HybridRiveSpec) { + public init(_ implementation: any HybridFallbackFontSpec) { self.__implementation = implementation self.__cxxPart = .init() /* no base class */ } /** - * Get the actual `HybridRiveSpec` instance this class wraps. + * Get the actual `HybridFallbackFontSpec` instance this class wraps. */ @inline(__always) - public func getHybridRiveSpec() -> any HybridRiveSpec { + public func getHybridFallbackFontSpec() -> any HybridFallbackFontSpec { return __implementation } @@ -62,25 +62,25 @@ open class HybridRiveSpec_cxx { } /** - * Casts an unsafe pointer to a `HybridRiveSpec_cxx`. - * The pointer has to be a retained opaque `Unmanaged`. + * Casts an unsafe pointer to a `HybridFallbackFontSpec_cxx`. + * The pointer has to be a retained opaque `Unmanaged`. * This removes one strong reference from the object! */ - public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridRiveSpec_cxx { - return Unmanaged.fromOpaque(pointer).takeRetainedValue() + public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridFallbackFontSpec_cxx { + return Unmanaged.fromOpaque(pointer).takeRetainedValue() } /** * Gets (or creates) the C++ part of this Hybrid Object. - * The C++ part is a `std::shared_ptr`. + * The C++ part is a `std::shared_ptr`. */ - public func getCxxPart() -> bridge.std__shared_ptr_HybridRiveSpec_ { + public func getCxxPart() -> bridge.std__shared_ptr_HybridFallbackFontSpec_ { let cachedCxxPart = self.__cxxPart.lock() if Bool(fromCxx: cachedCxxPart) { return cachedCxxPart } else { - let newCxxPart = bridge.create_std__shared_ptr_HybridRiveSpec_(self.toUnsafe()) - __cxxPart = bridge.weakify_std__shared_ptr_HybridRiveSpec_(newCxxPart) + let newCxxPart = bridge.create_std__shared_ptr_HybridFallbackFontSpec_(self.toUnsafe()) + __cxxPart = bridge.weakify_std__shared_ptr_HybridFallbackFontSpec_(newCxxPart) return newCxxPart } } @@ -100,7 +100,7 @@ open class HybridRiveSpec_cxx { * Compares this object with the given [other] object for reference equality. */ @inline(__always) - public func equals(other: HybridRiveSpec_cxx) -> Bool { + public func equals(other: HybridFallbackFontSpec_cxx) -> Bool { return self.__implementation === other.__implementation } @@ -125,15 +125,5 @@ open class HybridRiveSpec_cxx { // Methods - @inline(__always) - public final func multiply(a: Double, b: Double) -> bridge.Result_double_ { - do { - let __result = try self.__implementation.multiply(a: a, b: b) - let __resultCpp = __result - return bridge.create_Result_double_(__resultCpp) - } catch (let __error) { - let __exceptionPtr = __error.toCpp() - return bridge.create_Result_double_(__exceptionPtr) - } - } + } diff --git a/nitrogen/generated/ios/swift/HybridRiveFontConfigSpec.swift b/nitrogen/generated/ios/swift/HybridRiveFontConfigSpec.swift new file mode 100644 index 00000000..e6af995c --- /dev/null +++ b/nitrogen/generated/ios/swift/HybridRiveFontConfigSpec.swift @@ -0,0 +1,63 @@ +/// +/// HybridRiveFontConfigSpec.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +import Foundation +import NitroModules + +/// See ``HybridRiveFontConfigSpec`` +public protocol HybridRiveFontConfigSpec_protocol: HybridObject { + // Properties + + + // Methods + func loadFontFromURL(url: String) throws -> Promise<(any HybridFallbackFontSpec)> + func loadFontFromResource(resource: String) throws -> (any HybridFallbackFontSpec) + func loadFontFromBytes(bytes: ArrayBuffer) throws -> (any HybridFallbackFontSpec) + func loadFontByName(name: String) throws -> (any HybridFallbackFontSpec) + func getSystemDefaultFont() throws -> (any HybridFallbackFontSpec) + func setFontsForWeight(weight: Double, fonts: [(any HybridFallbackFontSpec)]) throws -> Void + func applyFallbackFonts() throws -> Promise + func clearFallbackFonts() throws -> Promise +} + +public extension HybridRiveFontConfigSpec_protocol { + /// Default implementation of ``HybridObject.toString`` + func toString() -> String { + return "[HybridObject RiveFontConfig]" + } +} + +/// See ``HybridRiveFontConfigSpec`` +open class HybridRiveFontConfigSpec_base { + private weak var cxxWrapper: HybridRiveFontConfigSpec_cxx? = nil + public init() { } + public func getCxxWrapper() -> HybridRiveFontConfigSpec_cxx { + #if DEBUG + guard self is any HybridRiveFontConfigSpec else { + fatalError("`self` is not a `HybridRiveFontConfigSpec`! Did you accidentally inherit from `HybridRiveFontConfigSpec_base` instead of `HybridRiveFontConfigSpec`?") + } + #endif + if let cxxWrapper = self.cxxWrapper { + return cxxWrapper + } else { + let cxxWrapper = HybridRiveFontConfigSpec_cxx(self as! any HybridRiveFontConfigSpec) + self.cxxWrapper = cxxWrapper + return cxxWrapper + } + } +} + +/** + * A Swift base-protocol representing the RiveFontConfig HybridObject. + * Implement this protocol to create Swift-based instances of RiveFontConfig. + * ```swift + * class HybridRiveFontConfig : HybridRiveFontConfigSpec { + * // ... + * } + * ``` + */ +public typealias HybridRiveFontConfigSpec = HybridRiveFontConfigSpec_protocol & HybridRiveFontConfigSpec_base diff --git a/nitrogen/generated/ios/swift/HybridRiveFontConfigSpec_cxx.swift b/nitrogen/generated/ios/swift/HybridRiveFontConfigSpec_cxx.swift new file mode 100644 index 00000000..c4d6b44a --- /dev/null +++ b/nitrogen/generated/ios/swift/HybridRiveFontConfigSpec_cxx.swift @@ -0,0 +1,262 @@ +/// +/// HybridRiveFontConfigSpec_cxx.swift +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +import Foundation +import NitroModules + +/** + * A class implementation that bridges HybridRiveFontConfigSpec over to C++. + * In C++, we cannot use Swift protocols - so we need to wrap it in a class to make it strongly defined. + * + * Also, some Swift types need to be bridged with special handling: + * - Enums need to be wrapped in Structs, otherwise they cannot be accessed bi-directionally (Swift bug: https://github.com/swiftlang/swift/issues/75330) + * - Other HybridObjects need to be wrapped/unwrapped from the Swift TCxx wrapper + * - Throwing methods need to be wrapped with a Result type, as exceptions cannot be propagated to C++ + */ +open class HybridRiveFontConfigSpec_cxx { + /** + * The Swift <> C++ bridge's namespace (`margelo::nitro::rive::bridge::swift`) + * from `RNRive-Swift-Cxx-Bridge.hpp`. + * This contains specialized C++ templates, and C++ helper functions that can be accessed from Swift. + */ + public typealias bridge = margelo.nitro.rive.bridge.swift + + /** + * Holds an instance of the `HybridRiveFontConfigSpec` Swift protocol. + */ + private var __implementation: any HybridRiveFontConfigSpec + + /** + * Holds a weak pointer to the C++ class that wraps the Swift class. + */ + private var __cxxPart: bridge.std__weak_ptr_HybridRiveFontConfigSpec_ + + /** + * Create a new `HybridRiveFontConfigSpec_cxx` that wraps the given `HybridRiveFontConfigSpec`. + * All properties and methods bridge to C++ types. + */ + public init(_ implementation: any HybridRiveFontConfigSpec) { + self.__implementation = implementation + self.__cxxPart = .init() + /* no base class */ + } + + /** + * Get the actual `HybridRiveFontConfigSpec` instance this class wraps. + */ + @inline(__always) + public func getHybridRiveFontConfigSpec() -> any HybridRiveFontConfigSpec { + return __implementation + } + + /** + * Casts this instance to a retained unsafe raw pointer. + * This acquires one additional strong reference on the object! + */ + public func toUnsafe() -> UnsafeMutableRawPointer { + return Unmanaged.passRetained(self).toOpaque() + } + + /** + * Casts an unsafe pointer to a `HybridRiveFontConfigSpec_cxx`. + * The pointer has to be a retained opaque `Unmanaged`. + * This removes one strong reference from the object! + */ + public class func fromUnsafe(_ pointer: UnsafeMutableRawPointer) -> HybridRiveFontConfigSpec_cxx { + return Unmanaged.fromOpaque(pointer).takeRetainedValue() + } + + /** + * Gets (or creates) the C++ part of this Hybrid Object. + * The C++ part is a `std::shared_ptr`. + */ + public func getCxxPart() -> bridge.std__shared_ptr_HybridRiveFontConfigSpec_ { + let cachedCxxPart = self.__cxxPart.lock() + if Bool(fromCxx: cachedCxxPart) { + return cachedCxxPart + } else { + let newCxxPart = bridge.create_std__shared_ptr_HybridRiveFontConfigSpec_(self.toUnsafe()) + __cxxPart = bridge.weakify_std__shared_ptr_HybridRiveFontConfigSpec_(newCxxPart) + return newCxxPart + } + } + + + + /** + * Get the memory size of the Swift class (plus size of any other allocations) + * so the JS VM can properly track it and garbage-collect the JS object if needed. + */ + @inline(__always) + public var memorySize: Int { + return MemoryHelper.getSizeOf(self.__implementation) + self.__implementation.memorySize + } + + /** + * Compares this object with the given [other] object for reference equality. + */ + @inline(__always) + public func equals(other: HybridRiveFontConfigSpec_cxx) -> Bool { + return self.__implementation === other.__implementation + } + + /** + * Call dispose() on the Swift class. + * This _may_ be called manually from JS. + */ + @inline(__always) + public func dispose() { + self.__implementation.dispose() + } + + /** + * Call toString() on the Swift class. + */ + @inline(__always) + public func toString() -> String { + return self.__implementation.toString() + } + + // Properties + + + // Methods + @inline(__always) + public final func loadFontFromURL(url: std.string) -> bridge.Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____ { + do { + let __result = try self.__implementation.loadFontFromURL(url: String(url)) + let __resultCpp = { () -> bridge.std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec___ in + let __promise = bridge.create_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec___() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec___(__promise) + __result + .then({ __result in __promiseHolder.resolve({ () -> bridge.std__shared_ptr_HybridFallbackFontSpec_ in + let __cxxWrapped = __result.getCxxWrapper() + return __cxxWrapped.getCxxPart() + }()) }) + .catch({ __error in __promiseHolder.reject(__error.toCpp()) }) + return __promise + }() + return bridge.create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_Promise_std__shared_ptr_HybridFallbackFontSpec____(__exceptionPtr) + } + } + + @inline(__always) + public final func loadFontFromResource(resource: std.string) -> bridge.Result_std__shared_ptr_HybridFallbackFontSpec__ { + do { + let __result = try self.__implementation.loadFontFromResource(resource: String(resource)) + let __resultCpp = { () -> bridge.std__shared_ptr_HybridFallbackFontSpec_ in + let __cxxWrapped = __result.getCxxWrapper() + return __cxxWrapped.getCxxPart() + }() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__exceptionPtr) + } + } + + @inline(__always) + public final func loadFontFromBytes(bytes: ArrayBuffer) -> bridge.Result_std__shared_ptr_HybridFallbackFontSpec__ { + do { + let __result = try self.__implementation.loadFontFromBytes(bytes: bytes) + let __resultCpp = { () -> bridge.std__shared_ptr_HybridFallbackFontSpec_ in + let __cxxWrapped = __result.getCxxWrapper() + return __cxxWrapped.getCxxPart() + }() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__exceptionPtr) + } + } + + @inline(__always) + public final func loadFontByName(name: std.string) -> bridge.Result_std__shared_ptr_HybridFallbackFontSpec__ { + do { + let __result = try self.__implementation.loadFontByName(name: String(name)) + let __resultCpp = { () -> bridge.std__shared_ptr_HybridFallbackFontSpec_ in + let __cxxWrapped = __result.getCxxWrapper() + return __cxxWrapped.getCxxPart() + }() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__exceptionPtr) + } + } + + @inline(__always) + public final func getSystemDefaultFont() -> bridge.Result_std__shared_ptr_HybridFallbackFontSpec__ { + do { + let __result = try self.__implementation.getSystemDefaultFont() + let __resultCpp = { () -> bridge.std__shared_ptr_HybridFallbackFontSpec_ in + let __cxxWrapped = __result.getCxxWrapper() + return __cxxWrapped.getCxxPart() + }() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_HybridFallbackFontSpec__(__exceptionPtr) + } + } + + @inline(__always) + public final func setFontsForWeight(weight: Double, fonts: bridge.std__vector_std__shared_ptr_HybridFallbackFontSpec__) -> bridge.Result_void_ { + do { + try self.__implementation.setFontsForWeight(weight: weight, fonts: fonts.map({ __item in { () -> any HybridFallbackFontSpec in + let __unsafePointer = bridge.get_std__shared_ptr_HybridFallbackFontSpec_(__item) + let __instance = HybridFallbackFontSpec_cxx.fromUnsafe(__unsafePointer) + return __instance.getHybridFallbackFontSpec() + }() })) + return bridge.create_Result_void_() + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_void_(__exceptionPtr) + } + } + + @inline(__always) + public final func applyFallbackFonts() -> bridge.Result_std__shared_ptr_Promise_void___ { + do { + let __result = try self.__implementation.applyFallbackFonts() + let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in + let __promise = bridge.create_std__shared_ptr_Promise_void__() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise) + __result + .then({ __result in __promiseHolder.resolve() }) + .catch({ __error in __promiseHolder.reject(__error.toCpp()) }) + return __promise + }() + return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr) + } + } + + @inline(__always) + public final func clearFallbackFonts() -> bridge.Result_std__shared_ptr_Promise_void___ { + do { + let __result = try self.__implementation.clearFallbackFonts() + let __resultCpp = { () -> bridge.std__shared_ptr_Promise_void__ in + let __promise = bridge.create_std__shared_ptr_Promise_void__() + let __promiseHolder = bridge.wrap_std__shared_ptr_Promise_void__(__promise) + __result + .then({ __result in __promiseHolder.resolve() }) + .catch({ __error in __promiseHolder.reject(__error.toCpp()) }) + return __promise + }() + return bridge.create_Result_std__shared_ptr_Promise_void___(__resultCpp) + } catch (let __error) { + let __exceptionPtr = __error.toCpp() + return bridge.create_Result_std__shared_ptr_Promise_void___(__exceptionPtr) + } + } +} diff --git a/nitrogen/generated/ios/swift/HybridRiveSpec.swift b/nitrogen/generated/ios/swift/HybridRiveSpec.swift deleted file mode 100644 index 8ca7f1e2..00000000 --- a/nitrogen/generated/ios/swift/HybridRiveSpec.swift +++ /dev/null @@ -1,56 +0,0 @@ -/// -/// HybridRiveSpec.swift -/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. -/// https://github.com/mrousavy/nitro -/// Copyright © Marc Rousavy @ Margelo -/// - -import Foundation -import NitroModules - -/// See ``HybridRiveSpec`` -public protocol HybridRiveSpec_protocol: HybridObject { - // Properties - - - // Methods - func multiply(a: Double, b: Double) throws -> Double -} - -public extension HybridRiveSpec_protocol { - /// Default implementation of ``HybridObject.toString`` - func toString() -> String { - return "[HybridObject Rive]" - } -} - -/// See ``HybridRiveSpec`` -open class HybridRiveSpec_base { - private weak var cxxWrapper: HybridRiveSpec_cxx? = nil - public init() { } - public func getCxxWrapper() -> HybridRiveSpec_cxx { - #if DEBUG - guard self is any HybridRiveSpec else { - fatalError("`self` is not a `HybridRiveSpec`! Did you accidentally inherit from `HybridRiveSpec_base` instead of `HybridRiveSpec`?") - } - #endif - if let cxxWrapper = self.cxxWrapper { - return cxxWrapper - } else { - let cxxWrapper = HybridRiveSpec_cxx(self as! any HybridRiveSpec) - self.cxxWrapper = cxxWrapper - return cxxWrapper - } - } -} - -/** - * A Swift base-protocol representing the Rive HybridObject. - * Implement this protocol to create Swift-based instances of Rive. - * ```swift - * class HybridRive : HybridRiveSpec { - * // ... - * } - * ``` - */ -public typealias HybridRiveSpec = HybridRiveSpec_protocol & HybridRiveSpec_base diff --git a/nitrogen/generated/shared/c++/HybridRiveSpec.cpp b/nitrogen/generated/shared/c++/HybridFallbackFontSpec.cpp similarity index 69% rename from nitrogen/generated/shared/c++/HybridRiveSpec.cpp rename to nitrogen/generated/shared/c++/HybridFallbackFontSpec.cpp index 9309c469..4ed3104b 100644 --- a/nitrogen/generated/shared/c++/HybridRiveSpec.cpp +++ b/nitrogen/generated/shared/c++/HybridFallbackFontSpec.cpp @@ -1,20 +1,20 @@ /// -/// HybridRiveSpec.cpp +/// HybridFallbackFontSpec.cpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo /// -#include "HybridRiveSpec.hpp" +#include "HybridFallbackFontSpec.hpp" namespace margelo::nitro::rive { - void HybridRiveSpec::loadHybridMethods() { + void HybridFallbackFontSpec::loadHybridMethods() { // load base methods/properties HybridObject::loadHybridMethods(); // load custom methods/properties registerHybrids(this, [](Prototype& prototype) { - prototype.registerHybridMethod("multiply", &HybridRiveSpec::multiply); + }); } diff --git a/nitrogen/generated/shared/c++/HybridRiveSpec.hpp b/nitrogen/generated/shared/c++/HybridFallbackFontSpec.hpp similarity index 63% rename from nitrogen/generated/shared/c++/HybridRiveSpec.hpp rename to nitrogen/generated/shared/c++/HybridFallbackFontSpec.hpp index 8874aed1..33172c5e 100644 --- a/nitrogen/generated/shared/c++/HybridRiveSpec.hpp +++ b/nitrogen/generated/shared/c++/HybridFallbackFontSpec.hpp @@ -1,5 +1,5 @@ /// -/// HybridRiveSpec.hpp +/// HybridFallbackFontSpec.hpp /// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. /// https://github.com/mrousavy/nitro /// Copyright © Marc Rousavy @ Margelo @@ -22,25 +22,25 @@ namespace margelo::nitro::rive { using namespace margelo::nitro; /** - * An abstract base class for `Rive` - * Inherit this class to create instances of `HybridRiveSpec` in C++. + * An abstract base class for `FallbackFont` + * Inherit this class to create instances of `HybridFallbackFontSpec` in C++. * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual. * @example * ```cpp - * class HybridRive: public HybridRiveSpec { + * class HybridFallbackFont: public HybridFallbackFontSpec { * public: - * HybridRive(...): HybridObject(TAG) { ... } + * HybridFallbackFont(...): HybridObject(TAG) { ... } * // ... * }; * ``` */ - class HybridRiveSpec: public virtual HybridObject { + class HybridFallbackFontSpec: public virtual HybridObject { public: // Constructor - explicit HybridRiveSpec(): HybridObject(TAG) { } + explicit HybridFallbackFontSpec(): HybridObject(TAG) { } // Destructor - ~HybridRiveSpec() override = default; + ~HybridFallbackFontSpec() override = default; public: // Properties @@ -48,7 +48,7 @@ namespace margelo::nitro::rive { public: // Methods - virtual double multiply(double a, double b) = 0; + protected: // Hybrid Setup @@ -56,7 +56,7 @@ namespace margelo::nitro::rive { protected: // Tag for logging - static constexpr auto TAG = "Rive"; + static constexpr auto TAG = "FallbackFont"; }; } // namespace margelo::nitro::rive diff --git a/nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.cpp b/nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.cpp new file mode 100644 index 00000000..111806a2 --- /dev/null +++ b/nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.cpp @@ -0,0 +1,28 @@ +/// +/// HybridRiveFontConfigSpec.cpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#include "HybridRiveFontConfigSpec.hpp" + +namespace margelo::nitro::rive { + + void HybridRiveFontConfigSpec::loadHybridMethods() { + // load base methods/properties + HybridObject::loadHybridMethods(); + // load custom methods/properties + registerHybrids(this, [](Prototype& prototype) { + prototype.registerHybridMethod("loadFontFromURL", &HybridRiveFontConfigSpec::loadFontFromURL); + prototype.registerHybridMethod("loadFontFromResource", &HybridRiveFontConfigSpec::loadFontFromResource); + prototype.registerHybridMethod("loadFontFromBytes", &HybridRiveFontConfigSpec::loadFontFromBytes); + prototype.registerHybridMethod("loadFontByName", &HybridRiveFontConfigSpec::loadFontByName); + prototype.registerHybridMethod("getSystemDefaultFont", &HybridRiveFontConfigSpec::getSystemDefaultFont); + prototype.registerHybridMethod("setFontsForWeight", &HybridRiveFontConfigSpec::setFontsForWeight); + prototype.registerHybridMethod("applyFallbackFonts", &HybridRiveFontConfigSpec::applyFallbackFonts); + prototype.registerHybridMethod("clearFallbackFonts", &HybridRiveFontConfigSpec::clearFallbackFonts); + }); + } + +} // namespace margelo::nitro::rive diff --git a/nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.hpp b/nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.hpp new file mode 100644 index 00000000..a6df0aaa --- /dev/null +++ b/nitrogen/generated/shared/c++/HybridRiveFontConfigSpec.hpp @@ -0,0 +1,75 @@ +/// +/// HybridRiveFontConfigSpec.hpp +/// This file was generated by nitrogen. DO NOT MODIFY THIS FILE. +/// https://github.com/mrousavy/nitro +/// Copyright © Marc Rousavy @ Margelo +/// + +#pragma once + +#if __has_include() +#include +#else +#error NitroModules cannot be found! Are you sure you installed NitroModules properly? +#endif + +// Forward declaration of `HybridFallbackFontSpec` to properly resolve imports. +namespace margelo::nitro::rive { class HybridFallbackFontSpec; } + +#include +#include "HybridFallbackFontSpec.hpp" +#include +#include +#include +#include + +namespace margelo::nitro::rive { + + using namespace margelo::nitro; + + /** + * An abstract base class for `RiveFontConfig` + * Inherit this class to create instances of `HybridRiveFontConfigSpec` in C++. + * You must explicitly call `HybridObject`'s constructor yourself, because it is virtual. + * @example + * ```cpp + * class HybridRiveFontConfig: public HybridRiveFontConfigSpec { + * public: + * HybridRiveFontConfig(...): HybridObject(TAG) { ... } + * // ... + * }; + * ``` + */ + class HybridRiveFontConfigSpec: public virtual HybridObject { + public: + // Constructor + explicit HybridRiveFontConfigSpec(): HybridObject(TAG) { } + + // Destructor + ~HybridRiveFontConfigSpec() override = default; + + public: + // Properties + + + public: + // Methods + virtual std::shared_ptr>> loadFontFromURL(const std::string& url) = 0; + virtual std::shared_ptr loadFontFromResource(const std::string& resource) = 0; + virtual std::shared_ptr loadFontFromBytes(const std::shared_ptr& bytes) = 0; + virtual std::shared_ptr loadFontByName(const std::string& name) = 0; + virtual std::shared_ptr getSystemDefaultFont() = 0; + virtual void setFontsForWeight(double weight, const std::vector>& fonts) = 0; + virtual std::shared_ptr> applyFallbackFonts() = 0; + virtual std::shared_ptr> clearFallbackFonts() = 0; + + protected: + // Hybrid Setup + void loadHybridMethods() override; + + protected: + // Tag for logging + static constexpr auto TAG = "RiveFontConfig"; + }; + +} // namespace margelo::nitro::rive diff --git a/src/core/RiveFonts.ts b/src/core/RiveFonts.ts new file mode 100644 index 00000000..a3a552ab --- /dev/null +++ b/src/core/RiveFonts.ts @@ -0,0 +1,95 @@ +import { NitroModules } from 'react-native-nitro-modules'; +import { Image } from 'react-native'; +import type { + RiveFontConfig, + FallbackFont, +} from '../specs/RiveFontConfig.nitro'; + +const RiveFontConfigInternal = + NitroModules.createHybridObject('RiveFontConfig'); + +/** + * A font source that can be: + * - `number` — a Metro `require('./Font.ttf')` asset ID + * - `{ uri: string }` — a URI object (http/https URL, file:// path, or resource name) + * - `string` — a native resource name (e.g. "kanit_regular.ttf") or URL + * - `{ name: string }` — a system font name (e.g. "Arial", "sans-serif") + * - `ArrayBuffer` — raw font bytes (TTF/OTF) + */ +export type FontSource = + | number + | { uri: string } + | { name: string } + | string + | ArrayBuffer; + +export type FontWeight = number | 'default'; + +export type FallbackFontMap = Partial>; + +const DEFAULT_WEIGHT = 0; + +function resolveWeight(key: string): number { + return key === 'default' ? DEFAULT_WEIGHT : Number(key); +} + +export namespace RiveFonts { + export async function loadFont(source: FontSource): Promise { + if (source instanceof ArrayBuffer) { + return RiveFontConfigInternal.loadFontFromBytes(source); + } + + if (typeof source === 'number') { + const resolved = Image.resolveAssetSource(source); + if (!resolved?.uri) { + throw new Error( + `Invalid font asset: could not resolve require() ID ${source}. Ensure 'ttf' is in metro.config.js assetExts.` + ); + } + return loadFontByURI(resolved.uri); + } + + if (typeof source === 'object' && 'name' in source) { + return RiveFontConfigInternal.loadFontByName(source.name); + } + + if (typeof source === 'object' && 'uri' in source) { + return loadFontByURI(source.uri); + } + + if (typeof source === 'string') { + if (/^https?:\/\//.test(source) || /^file:\/\//.test(source)) { + return RiveFontConfigInternal.loadFontFromURL(source); + } + return RiveFontConfigInternal.loadFontFromResource(source); + } + + throw new Error(`Invalid font source: ${String(source)}`); + } + + export function systemFallback(): FallbackFont { + return RiveFontConfigInternal.getSystemDefaultFont(); + } + + export async function setFallbackFonts( + fontsByWeight: FallbackFontMap + ): Promise { + for (const [key, fonts] of Object.entries(fontsByWeight)) { + if (fonts) { + RiveFontConfigInternal.setFontsForWeight(resolveWeight(key), fonts); + } + } + await RiveFontConfigInternal.applyFallbackFonts(); + } + + export async function clearFallbackFonts(): Promise { + return RiveFontConfigInternal.clearFallbackFonts(); + } +} + +function loadFontByURI(uri: string): Promise | FallbackFont { + if (/^https?:\/\//.test(uri) || /^file:\/\//.test(uri)) { + return RiveFontConfigInternal.loadFontFromURL(uri); + } + return RiveFontConfigInternal.loadFontFromResource(uri); +} diff --git a/src/index.tsx b/src/index.tsx index b4bad0b6..28756ac0 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -39,6 +39,13 @@ export { Alignment } from './core/Alignment'; export { RiveFileFactory } from './core/RiveFile'; export { RiveImages } from './core/RiveImages'; export type { RiveImage } from './specs/RiveImage.nitro'; +export { + RiveFonts, + type FontSource, + type FontWeight, + type FallbackFontMap, +} from './core/RiveFonts'; +export type { FallbackFont } from './specs/RiveFontConfig.nitro'; export { RiveColor } from './core/RiveColor'; export { type RiveEvent, RiveEventType } from './core/Events'; export { type RiveError, RiveErrorType } from './core/Errors'; diff --git a/src/specs/Rive.nitro.ts b/src/specs/Rive.nitro.ts deleted file mode 100644 index c126a78d..00000000 --- a/src/specs/Rive.nitro.ts +++ /dev/null @@ -1,6 +0,0 @@ -import type { HybridObject } from 'react-native-nitro-modules'; - -export interface Rive - extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> { - multiply(a: number, b: number): number; -} diff --git a/src/specs/RiveFontConfig.nitro.ts b/src/specs/RiveFontConfig.nitro.ts new file mode 100644 index 00000000..0e5fe549 --- /dev/null +++ b/src/specs/RiveFontConfig.nitro.ts @@ -0,0 +1,16 @@ +import type { HybridObject } from 'react-native-nitro-modules'; + +export interface FallbackFont + extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> {} + +export interface RiveFontConfig + extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> { + loadFontFromURL(url: string): Promise; + loadFontFromResource(resource: string): FallbackFont; + loadFontFromBytes(bytes: ArrayBuffer): FallbackFont; + loadFontByName(name: string): FallbackFont; + getSystemDefaultFont(): FallbackFont; + setFontsForWeight(weight: number, fonts: FallbackFont[]): void; + applyFallbackFonts(): Promise; + clearFallbackFonts(): Promise; +}