diff --git a/.rive_head b/.rive_head index 1dc7e68..494c7ac 100644 --- a/.rive_head +++ b/.rive_head @@ -1 +1 @@ -48212b2bbd1f55028502f0669ea2be69c7a62694 +414c142f2c11ef3ad2cfa9fd0a1ed18dc7f9f98f diff --git a/Source/Renderer/RiveFactory.mm b/Source/Renderer/RiveFactory.mm index 173fec6..a69d221 100644 --- a/Source/Renderer/RiveFactory.mm +++ b/Source/Renderer/RiveFactory.mm @@ -19,27 +19,30 @@ static NSArray>* _fallbackFonts = nil; -static rive::rcp findFallbackFont(rive::Span missing) +static rive::rcp riveFontFromNativeFont(id font) { - // For each descriptor… - for (id fallback in RiveFont.fallbackFonts) + uint16_t weight = 400; + if ([font conformsToProtocol:@protocol(RiveWeightProvider)]) { - id fallbackFont = fallback.fallbackFont; + weight = [font riveWeightValue]; + } - uint16_t weight = 400; - if ([fallbackFont conformsToProtocol:@protocol(RiveWeightProvider)]) - { - weight = [fallbackFont riveWeightValue]; - } + uint8_t width = 100; + if ([font conformsToProtocol:@protocol(RiveFontWidthProvider)]) + { + width = [font riveFontWidthValue]; + } - uint8_t width = 100; - if ([fallbackFont conformsToProtocol:@protocol(RiveFontWidthProvider)]) - { - width = [fallbackFont riveFontWidthValue]; - } + CTFontRef ctFont = (__bridge CTFontRef)font; + return HBFont::FromSystem((void*)ctFont, weight, width); +} - CTFontRef ctFont = (__bridge CTFontRef)fallbackFont; - auto font = HBFont::FromSystem((void*)ctFont, weight, width); +static rive::rcp findFallbackFont(rive::Span missing) +{ + // For each descriptor… + for (id fallback in RiveFont.fallbackFonts) + { + auto font = riveFontFromNativeFont(fallback.fallbackFont); if (font->hasGlyph(missing)) { rive::rcp rcFont = rive::rcp(font); @@ -181,6 +184,18 @@ - (RiveFont*)decodeFont:(nonnull NSData*)data initWithFont:instance->decodeFont(rive::Span(bytes, [data length]))]; } +#if TARGET_OS_IPHONE +- (RiveFont*)decodeUIFont:(UIFont*)font +{ + return [[RiveFont alloc] initWithFont:riveFontFromNativeFont(font)]; +} +#else +- (RiveFont*)decodeNSFont:(NSFont*)font +{ + return [[RiveFont alloc] initWithFont:riveFontFromNativeFont(font)]; +} +#endif + - (RiveAudio*)decodeAudio:(nonnull NSData*)data { UInt8* bytes = (UInt8*)[data bytes]; diff --git a/Source/Renderer/include/RiveFactory.h b/Source/Renderer/include/RiveFactory.h index 5e94e51..ccfa4cf 100644 --- a/Source/Renderer/include/RiveFactory.h +++ b/Source/Renderer/include/RiveFactory.h @@ -40,6 +40,11 @@ NS_ASSUME_NONNULL_BEGIN */ @interface RiveFactory : NSObject - (RiveFont*)decodeFont:(NSData*)data; +#if TARGET_OS_IPHONE +- (RiveFont*)decodeUIFont:(UIFont*)data NS_SWIFT_NAME(decodeFont(_:)); +#else +- (RiveFont*)decodeNSFont:(NSFont*)data NS_SWIFT_NAME(decodeFont(_:)); +#endif - (RiveRenderImage*)decodeImage:(NSData*)data; - (RiveAudio*)decodeAudio:(NSData*)data; @end