Make an HBFont from a CTFontRef. By including ```rive/text/font_hb.hpp``` in iOS you'll get access to ```HBFont::FromSystem``` along with the ```HBFont::Decode``` (which we've been using so far to generate Font objects to put into the fallback list). You can get a UIFont with [this API](https://developer.apple.com/documentation/uikit/uifont/1619027-systemfontofsize?language=objc). You should be able to cast that to a CTFontRef: ``` CTFontRef ctFont = (__bridge CTFontRef)uiFont; auto fallbackFont = HBFont::FromSystem((void*)ctFont); ``` You can then use that in a fallback font procedure (like the example from the editor below) to load on demand/cache/provide from a list etc: https://github.com/rive-app/rive/blob/b5b930f88f18280afa44683c5756066abaa9b1dc/packages/rive_common/macos/rive_text/rive_text.cpp#L153-L175 Diffs= da1bb7745 Make an HBFont from a CTFontRef. (#7661) Co-authored-by: Luigi Rosso <luigi-rosso@users.noreply.github.com>
diff --git a/.rive_head b/.rive_head index fadf9ed..0ec88f5 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -1a5f273bb6534a395d1c3cdcc11a1ba3cd80a96c +da1bb77455deeafd48012a8eea5ea946a718fb77
diff --git a/build/premake5.lua b/build/premake5.lua index 52dd2a6..66c7d20 100644 --- a/build/premake5.lua +++ b/build/premake5.lua
@@ -128,6 +128,11 @@ objdir('%{cfg.system}_sim/obj/%{cfg.buildcfg}') end + filter('system:macosx or system:ios') + do + files({ '../src/text/font_hb_apple.mm' }) + end + filter({ 'system:android', 'configurations:release' }) do buildoptions({ '-flto=full' })
diff --git a/dependencies/premake5_harfbuzz.lua b/dependencies/premake5_harfbuzz.lua index 1123265..9c16173 100644 --- a/dependencies/premake5_harfbuzz.lua +++ b/dependencies/premake5_harfbuzz.lua
@@ -349,4 +349,10 @@ targetdir('%{cfg.system}/cache/arm64/bin/%{cfg.buildcfg}') objdir('%{cfg.system}/cache/arm64/obj/%{cfg.buildcfg}') end + + filter('system:macosx or system:ios') + do + defines({'HAVE_CORETEXT'}) + files({harfbuzz .. '/src/hb-coretext.cc'}) + end end
diff --git a/dependencies/premake5_harfbuzz_v2.lua b/dependencies/premake5_harfbuzz_v2.lua index 428aa15..9265555 100644 --- a/dependencies/premake5_harfbuzz_v2.lua +++ b/dependencies/premake5_harfbuzz_v2.lua
@@ -274,4 +274,10 @@ includedirs({ './' }) forceincludes({ 'rive_harfbuzz_renames.h' }) end + + filter('system:macosx or system:ios') + do + defines({'HAVE_CORETEXT'}) + files({harfbuzz .. '/src/hb-coretext.cc'}) + end end
diff --git a/dev/test/premake5.lua b/dev/test/premake5.lua index 318aeba..0cbd906 100644 --- a/dev/test/premake5.lua +++ b/dev/test/premake5.lua
@@ -62,4 +62,13 @@ }) forceincludes({ 'rive_yoga_renames.h' }) end + + filter({ 'system:macosx'} ) + do + links({ + 'Foundation.framework', + 'CoreGraphics.framework', + 'CoreText.framework' + }) + end end
diff --git a/include/rive/text/font_hb.hpp b/include/rive/text/font_hb.hpp index 9534611..ea112b6 100644 --- a/include/rive/text/font_hb.hpp +++ b/include/rive/text/font_hb.hpp
@@ -32,6 +32,7 @@ bool hasGlyph(rive::Span<const rive::Unichar>) const override; static rive::rcp<rive::Font> Decode(rive::Span<const uint8_t>); + static rive::rcp<rive::Font> FromSystem(void* systemFont); hb_font_t* font() const { return m_font; } private:
diff --git a/premake5_v2.lua b/premake5_v2.lua index 3d6c8aa..5abc30e 100644 --- a/premake5_v2.lua +++ b/premake5_v2.lua
@@ -140,6 +140,11 @@ architecture('x64') defines({ '_USE_MATH_DEFINES' }) end + + filter('system:macosx or system:ios') + do + files({ 'src/text/font_hb_apple.mm' }) + end end newoption({ @@ -177,4 +182,4 @@ newoption({ trigger = 'with_rive_layout', description = 'Compiles in layout features.', -}) \ No newline at end of file +})
diff --git a/src/text/font_hb.cpp b/src/text/font_hb.cpp index 8bd2324..4c47708 100644 --- a/src/text/font_hb.cpp +++ b/src/text/font_hb.cpp
@@ -48,6 +48,10 @@ return nullptr; } +#ifndef __APPLE__ +rive::rcp<rive::Font> HBFont::FromSystem(void* systemFont) { return nullptr; } +#endif + ////////////// constexpr int kStdScale = 2048;
diff --git a/src/text/font_hb_apple.mm b/src/text/font_hb_apple.mm new file mode 100644 index 0000000..0277d74 --- /dev/null +++ b/src/text/font_hb_apple.mm
@@ -0,0 +1,18 @@ +#include "rive/text_engine.hpp" + +#ifdef WITH_RIVE_TEXT +#include "rive/text/font_hb.hpp" +#import <CoreText/CoreText.h> + +#include "hb-coretext.h" + +rive::rcp<rive::Font> HBFont::FromSystem(void* systemFont) +{ + auto font = hb_coretext_font_create((CTFontRef)systemFont); + if (font) + { + return rive::rcp<rive::Font>(new HBFont(font)); + } + return nullptr; +} +#endif