refactor(apple): replace #ifdef checks in public interfaces with conditional no-op (#10136) 3560f39170

* fix(apple): remove #ifdef checks in public headers

Fixes an issue where framework consumers would also have to set
WITH_RIVE_TEXT or WITH_RIVE_AUDIO

* refactor(apple): remove #if(def) from public interfaces and no-op instead

* fix(apple): fix incorrect api usage

Co-authored-by: David Skuza <david@rive.app>
This commit is contained in:
dskuza
2025-07-09 16:39:13 +00:00
parent 8b8844e9a8
commit 08b2950cba
17 changed files with 67 additions and 63 deletions

View File

@@ -6,7 +6,6 @@
// Copyright © 2024 Rive. All rights reserved.
//
#ifdef WITH_RIVE_TEXT
#import <Foundation/Foundation.h>
#import <rive/text/font_hb.hpp>
#import <RiveRuntime/RiveRuntime-Swift.h>
@@ -43,4 +42,3 @@ NS_ASSUME_NONNULL_BEGIN
@end
NS_ASSUME_NONNULL_END
#endif

View File

@@ -1,4 +1,3 @@
#if WITH_RIVE_TEXT
//
// RiveFallbackFontDescriptor+UIKit.swift
// RiveRuntime
@@ -225,4 +224,3 @@ extension RiveNativeFont: RiveFontWidthProvider {
return Int(calculatedWidth)
}
}
#endif

View File

@@ -1,4 +1,3 @@
#if WITH_RIVE_TEXT
//
// RiveFallbackFontDescriptor.swift
// RiveRuntime
@@ -90,4 +89,3 @@ import SwiftUI
self.width = width
}
}
#endif // WITH_RIVE_TEXT

View File

@@ -1,4 +1,3 @@
#if WITH_RIVE_TEXT
//
// RiveSystemFontProvider.swift
// RiveRuntime
@@ -14,4 +13,3 @@ import Foundation
/// An array of possible fonts to use as fallback fonts.
@objc var fallbackFont: RiveNativeFont { get }
}
#endif // WITH_RIVE_TEXT

View File

@@ -6,8 +6,6 @@
// Copyright © 2024 Rive. All rights reserved.
//
#ifdef WITH_RIVE_TEXT
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@@ -59,5 +57,3 @@ typedef NSArray<id<RiveFallbackFontProvider>>* _Nonnull (
@end
NS_ASSUME_NONNULL_END
#endif

View File

@@ -6,8 +6,6 @@
// Copyright © 2024 Rive. All rights reserved.
//
#ifdef WITH_RIVE_TEXT
#import "RiveFont.h"
#import "RiveFallbackFontCache.h"
#import <RiveRuntime/RiveRuntime-Swift.h>
@@ -113,6 +111,7 @@ static RiveFallbackFontsCallback _fallbackFontsCallback = nil;
static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
bool useSystemShaper)
{
#ifdef WITH_RIVE_TEXT
uint16_t weight = 400;
if ([font conformsToProtocol:@protocol(RiveWeightProvider)])
{
@@ -127,8 +126,12 @@ static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
CTFontRef ctFont = (__bridge CTFontRef)font;
return HBFont::FromSystem((void*)ctFont, useSystemShaper, weight, width);
#else
return nullptr;
#endif
}
#ifdef WITH_RIVE_TEXT
static rive::rcp<rive::Font> findFallbackFont(const rive::Unichar missing,
const uint32_t fallbackIndex,
const rive::Font* font)
@@ -191,6 +194,7 @@ static rive::rcp<rive::Font> findFallbackFont(const rive::Unichar missing,
return nullptr;
}
#endif
@implementation RiveFont
{
@@ -200,7 +204,9 @@ static rive::rcp<rive::Font> findFallbackFont(const rive::Unichar missing,
+ (void)load
{
#ifdef WITH_RIVE_TEXT
rive::Font::gFallbackProc = findFallbackFont;
#endif
_fallbackFontCache = [NSMutableDictionary dictionary];
}
@@ -223,6 +229,7 @@ static rive::rcp<rive::Font> findFallbackFont(const rive::Unichar missing,
+ (NSArray<id<RiveFallbackFontProvider>>*)fallbackFonts
{
#ifdef WITH_RIVE_TEXT
if (_fallbackFonts.count == 0)
{
return @[ [[RiveFallbackFontDescriptor alloc]
@@ -232,31 +239,39 @@ static rive::rcp<rive::Font> findFallbackFont(const rive::Unichar missing,
}
return _fallbackFonts;
#else
return @[];
#endif
}
+ (void)setFallbackFonts:
(nonnull NSArray<id<RiveFallbackFontProvider>>*)fallbackFonts
{
#ifdef WITH_RIVE_TEXT
// Set the user-specified fallbacks, and reset the cache.
_fallbackFonts = [fallbackFonts copy];
_fallbackFontCache = [NSMutableDictionary dictionary];
// "Reset" fallback fonts callback so that array can take priority
_fallbackFontsCallback = nil;
#endif
}
+ (void)setFallbackFontsCallback:(RiveFallbackFontsCallback)fallbackFontCallback
{
#ifdef WITH_RIVE_TEXT
// Set the user-specified fallback block, and reset the cache.
_fallbackFontsCallback = [fallbackFontCallback copy];
_fallbackFontCache = [NSMutableDictionary dictionary];
// "Reset" fallback fonts array so that callback can take priority
_fallbackFonts = nil;
#endif
}
+ (RiveFallbackFontsCallback)fallbackFontsCallback
{
#ifdef WITH_RIVE_TEXT
// If there is no user-specified block set, use our internal defaults.
if (_fallbackFontsCallback == nil)
{
@@ -270,8 +285,15 @@ static rive::rcp<rive::Font> findFallbackFont(const rive::Unichar missing,
}
return _fallbackFontsCallback;
#else
return ^NSArray<id<RiveFallbackFontProvider>>*(RiveFontStyle* style)
{
// Using this getter will always return a font.
// If no user-specified fonts were added, this
// returns a default.
return [RiveFont fallbackFonts];
};
#endif
}
@end
#endif

View File

@@ -12,7 +12,7 @@
#import <RiveRuntime/RiveRuntime-Swift.h>
#import <RenderContext.h>
#if WITH_RIVE_TEXT
#ifdef WITH_RIVE_TEXT
#import <CoreText/CTFont.h>
#import <rive/text/font_hb.hpp>
@@ -21,7 +21,7 @@
#endif
#endif
#if WITH_RIVE_TEXT
#ifdef WITH_RIVE_TEXT
static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
bool useSystemShaper)
{
@@ -82,7 +82,6 @@ static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
@end
#ifdef WITH_RIVE_AUDIO
@implementation RiveAudio
{
rive::rcp<rive::AudioSource>
@@ -106,7 +105,6 @@ static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
}
@end
#endif
/*
* RiveFactory
@@ -138,7 +136,6 @@ static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
rive::Span<const uint8_t>(bytes, [data length]))];
}
#ifdef WITH_RIVE_TEXT
- (RiveFont*)decodeFont:(nonnull NSData*)data
{
UInt8* bytes = (UInt8*)[data bytes];
@@ -150,24 +147,33 @@ static rive::rcp<rive::Font> riveFontFromNativeFont(id font,
#if TARGET_OS_IPHONE
- (RiveFont*)decodeUIFont:(UIFont*)font
{
#ifdef WITH_RIVE_TEXT
return [[RiveFont alloc] initWithFont:riveFontFromNativeFont(font, true)];
#else
return nil;
#endif
}
#else
- (RiveFont*)decodeNSFont:(NSFont*)font
{
#ifdef WITH_RIVE_TEXT
return [[RiveFont alloc] initWithFont:riveFontFromNativeFont(font, true)];
#else
return nil;
#endif
}
#endif
#endif
#ifdef WITH_RIVE_AUDIO
- (RiveAudio*)decodeAudio:(nonnull NSData*)data
{
#ifdef WITH_RIVE_AUDIO
UInt8* bytes = (UInt8*)[data bytes];
return [[RiveAudio alloc]
initWithAudio:instance->decodeAudio(
rive::Span<const uint8_t>(bytes, [data length]))];
}
#else
return nil;
#endif
}
@end

View File

@@ -96,7 +96,6 @@
@end
#ifdef WITH_RIVE_TEXT
@implementation RiveFontAsset
- (instancetype)initWithFileAsset:(const rive::FontAsset*)fileAsset
{
@@ -105,12 +104,12 @@
- (void)font:(RiveFont*)font
{
#ifdef WITH_RIVE_TEXT
((rive::FontAsset*)[self getInstance])->font([font instance]);
#endif
}
@end
#endif
#ifdef WITH_RIVE_AUDIO
@implementation RiveAudioAsset
- (instancetype)initWithFileAsset:(const rive::AudioAsset*)fileAsset
{
@@ -119,7 +118,8 @@
- (void)audio:(RiveAudio*)audio
{
#ifdef WITH_RIVE_AUDIO
((rive::AudioAsset*)[self getInstance])->audioSource([audio instance]);
#endif
}
@end
#endif

View File

@@ -54,10 +54,8 @@ NS_ASSUME_NONNULL_BEGIN
error:(NSError**)error;
- (RiveStateMachineInstance* __nullable)defaultStateMachine;
#if WITH_RIVE_TEXT
- (RiveTextValueRun* __nullable)textRun:(NSString*)name;
- (RiveTextValueRun* __nullable)textRun:(NSString*)name path:(NSString*)path;
#endif
- (void)advanceBy:(double)elapsedSeconds;
- (void)draw:(RiveRenderer*)renderer;

View File

@@ -25,27 +25,21 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable instancetype)initWithData:(NSData*)data;
@end
#ifdef WITH_RIVE_AUDIO
@interface RiveAudio : NSObject
@end
#endif
/*
* RiveFactory
*/
@interface RiveFactory : NSObject
#ifdef WITH_RIVE_TEXT
- (RiveFont*)decodeFont:(NSData*)data;
#if TARGET_OS_IPHONE || TARGET_OS_VISION || TARGET_OS_TV
- (RiveFont*)decodeUIFont:(UIFont*)data NS_SWIFT_NAME(decodeFont(_:));
- (nullable RiveFont*)decodeUIFont:(UIFont*)data NS_SWIFT_NAME(decodeFont(_:));
#else
- (RiveFont*)decodeNSFont:(NSFont*)data NS_SWIFT_NAME(decodeFont(_:));
#endif
- (nullable RiveFont*)decodeNSFont:(NSFont*)data NS_SWIFT_NAME(decodeFont(_:));
#endif
- (RiveRenderImage*)decodeImage:(NSData*)data;
#ifdef WITH_RIVE_AUDIO
- (RiveAudio*)decodeAudio:(NSData*)data;
#endif
- (nullable RiveAudio*)decodeAudio:(NSData*)data;
@end
NS_ASSUME_NONNULL_END

View File

@@ -13,9 +13,7 @@
@class RiveRenderImage;
@class RiveFont;
#ifdef WITH_RIVE_AUDIO
@class RiveAudio;
#endif
NS_ASSUME_NONNULL_BEGIN
@@ -47,14 +45,12 @@ NS_ASSUME_NONNULL_BEGIN
- (void)font:(RiveFont*)font;
@end
#ifdef WITH_RIVE_AUDIO
/*
* RiveAudioAsset
*/
@interface RiveAudioAsset : RiveFileAsset
- (void)audio:(RiveAudio*)audio;
@end
#endif
NS_ASSUME_NONNULL_END

View File

@@ -29,12 +29,8 @@
#import "rive/text/text_value_run.hpp"
#import "rive/event.hpp"
#import "rive/assets/image_asset.hpp"
#ifdef WITH_RIVE_TEXT
#import "rive/assets/font_asset.hpp"
#endif
#ifdef WITH_RIVE_AUDIO
#import "rive/assets/audio_asset.hpp"
#endif
#import "rive/assets/file_asset.hpp"
#import "rive/file_asset_loader.hpp"
#import "rive/viewmodel/runtime/viewmodel_instance_runtime.hpp"
@@ -143,23 +139,19 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithFileAsset:(const rive::ImageAsset*)fileAsset;
@end
#ifdef WITH_RIVE_TEXT
/*
* RiveFontAsset
*/
@interface RiveFontAsset ()
- (instancetype)initWithFileAsset:(const rive::FontAsset*)fileAsset;
@end
#endif
#ifdef WITH_RIVE_AUDIO
/*
* RiveAudioAsset
*/
@interface RiveAudioAsset ()
- (instancetype)initWithFileAsset:(const rive::AudioAsset*)fileAsset;
@end
#endif
/*
* RiveFileAsset
@@ -168,24 +160,20 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithFactory:(rive::Factory*)factory;
@end
#ifdef WITH_RIVE_TEXT
@interface RiveFont ()
- (instancetype)initWithFont:(rive::rcp<rive::Font>)font;
- (rive::rcp<rive::Font>)instance;
@end
#endif
@interface RiveRenderImage ()
- (instancetype)initWithImage:(rive::rcp<rive::RenderImage>)image;
- (rive::rcp<rive::RenderImage>)instance;
@end
#ifdef WITH_RIVE_AUDIO
@interface RiveAudio ()
- (instancetype)initWithAudio:(rive::rcp<rive::AudioSource>)audio;
- (rive::rcp<rive::AudioSource>)instance;
@end
#endif
@interface RiveDataBindingViewModel ()
- (instancetype)initWithViewModel:(rive::ViewModelRuntime*)viewModel;

View File

@@ -460,15 +460,16 @@ import Combine
setInput(inputName, value: Float(value), path: path)
}
#if WITH_RIVE_TEXT
/// Get a text value from a specified text run
/// - Parameters:
/// - textRunName: The name of a `Text Run` on the active Artboard
/// - Returns: String text value of the specified text run if applicable
@objc open func getTextRunValue(_ textRunName: String) -> String? {
#if WITH_RIVE_TEXT
if let textRun = riveModel?.artboard?.textRun(textRunName) {
return textRun.text()
}
#endif
return nil
}
@@ -478,9 +479,11 @@ import Combine
/// - path: The path to the nested text run.
/// - Returns: String text value of the specified text run if applicable
@objc open func getTextRunValue(_ textRunName: String, path: String) -> String? {
#if WITH_RIVE_TEXT
if let textRun = riveModel?.artboard?.textRun(textRunName, path: path) {
return textRun.text()
}
#endif
return nil
}
@@ -489,6 +492,7 @@ import Combine
/// - textRunName: The name of a `Text Run` on the active Artboard
/// - textValue: A String value for the text run
@objc open func setTextRunValue(_ textRunName: String, textValue: String) throws {
#if WITH_RIVE_TEXT
if let textRun = riveModel?.artboard?.textRun(textRunName) {
RiveLogger.log(viewModel: self, event: .textRun(textRunName, nil, textValue))
textRun.setText(textValue)
@@ -501,6 +505,7 @@ import Combine
RiveLogger.log(viewModel: self, event: .error(errorMessage))
throw RiveError.textValueRunError(errorMessage)
}
#endif
}
/// Set a text value for a specified text run
@@ -510,6 +515,7 @@ import Combine
/// - textValue: A String value for the text run
/// - Note: If the specified path is empty, the parent artboard will be used to find the text run.
@objc open func setTextRunValue(_ textRunName: String, path: String, textValue: String) throws {
#if WITH_RIVE_TEXT
if let textRun = riveModel?.artboard?.textRun(textRunName, path: path) {
RiveLogger.log(viewModel: self, event: .textRun(textRunName, path, textValue))
textRun.setText(textValue)
@@ -522,8 +528,8 @@ import Combine
RiveLogger.log(viewModel: self, event: .error(errorMessage))
throw RiveError.textValueRunError(errorMessage)
}
#endif
}
#endif
// TODO: Replace this with a more robust structure of the file's contents
@objc open func artboardNames() -> [String] {