Skip to content

Commit

Permalink
Make an HBFont from a CTFontRef.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
luigi-rosso and luigi-rosso committed Jul 25, 2024
1 parent 9f0db02 commit d42be0a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1a5f273bb6534a395d1c3cdcc11a1ba3cd80a96c
da1bb77455deeafd48012a8eea5ea946a718fb77
5 changes: 5 additions & 0 deletions build/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ do
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' })
Expand Down
6 changes: 6 additions & 0 deletions dependencies/premake5_harfbuzz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,10 @@ do
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
6 changes: 6 additions & 0 deletions dependencies/premake5_harfbuzz_v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,10 @@ do
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
9 changes: 9 additions & 0 deletions dev/test/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,13 @@ do
})
forceincludes({ 'rive_yoga_renames.h' })
end

filter({ 'system:macosx'} )
do
links({
'Foundation.framework',
'CoreGraphics.framework',
'CoreText.framework'
})
end
end
1 change: 1 addition & 0 deletions include/rive/text/font_hb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class HBFont : public rive::Font
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:
Expand Down
7 changes: 6 additions & 1 deletion premake5_v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ do
architecture('x64')
defines({ '_USE_MATH_DEFINES' })
end

filter('system:macosx or system:ios')
do
files({ 'src/text/font_hb_apple.mm' })
end
end

newoption({
Expand Down Expand Up @@ -177,4 +182,4 @@ newoption({
newoption({
trigger = 'with_rive_layout',
description = 'Compiles in layout features.',
})
})
4 changes: 4 additions & 0 deletions src/text/font_hb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ rive::rcp<rive::Font> HBFont::Decode(rive::Span<const uint8_t> span)
return nullptr;
}

#ifndef __APPLE__
rive::rcp<rive::Font> HBFont::FromSystem(void* systemFont) { return nullptr; }
#endif

//////////////

constexpr int kStdScale = 2048;
Expand Down
18 changes: 18 additions & 0 deletions src/text/font_hb_apple.mm
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d42be0a

Please sign in to comment.