Skip to content

Commit

Permalink
CoreText fallback shaper ex
Browse files Browse the repository at this point in the history
More experimenting on top of https://github.com/rive-app/rive/pull/8556

This breaks some APIs that still need to be fixed up elsewhere.

The basic concept here is that when you request a system font you can tell it whether you're requesting to use it a Harfbuzz shaped system font or a CoreText (System) shaped system font.

We prioritize Harfbuzz first so that we have predictable performance and results across edit and runtime. To do this the fallback process now comes with an index. Iteration will keep happening until no font is returned (or all glyphs are found).

If the registered fallbacks look like this:
```
RiveFont.fallbackFonts = [
  UIFont(name: "PingFangSC-Semibold", size: 12)!,
  UIFont(name: "Hiragino Sans", size: 12)!
]
```

The fallback process will return:
```
iter 0: Ping Fang Harfbuzz Shaped
iter 1: Hiragino San Harfbuzz Shaped
iter 2: Ping Fang Coretext Shaped
iter 3: Hiragino Sans Coretext Shaped
```

We also use Coretext last as usually shaping with Coretext causes Apple's shaper to do its own fallbacks (it calls them cascades like css). So iter 2 (in the example above) will be the final iteration as all glyphs get filled via Apple's own fallbacks.

![CleanShot 2024-11-14 at 21 16 55@2x](https://github.com/user-attachments/assets/c12e158b-5b8e-41d4-8d9b-8184a316a079)

Diffs=
3eefba5039 CoreText fallback shaper ex (#8568)

Co-authored-by: David Skuza <[email protected]>
Co-authored-by: Luigi Rosso <[email protected]>
  • Loading branch information
3 people committed Nov 20, 2024
1 parent c9bed9a commit ec1e05a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
807f9b20ac7f040df6646b808bdb70ab84b7db57
3eefba5039d73329b31048b3bd839bfabc177bc0
4 changes: 2 additions & 2 deletions kotlin/src/main/cpp/include/helpers/font_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class FontHelper

static std::vector<uint8_t> getSystemFontBytes();

static rive::rcp<rive::Font> findFontFallback(
rive::Span<const rive::Unichar> missing);
static rive::rcp<rive::Font> findFontFallback(const rive::Unichar missing,
const uint32_t fallbackIndex);
};

} // namespace rive_android
Expand Down
4 changes: 3 additions & 1 deletion kotlin/src/main/cpp/include/models/dimensions_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class DimensionsHelper : public rive::Renderer
DimensionsHelper() :
m_Width(0.0f), m_Height(0.0f), m_ScaleX(1.0f), m_ScaleY(1.0f)
{}
~DimensionsHelper(){};
// clang-format off
~DimensionsHelper() {};
// clang-format on

float width() const { return m_Width; }
float height() const { return m_Height; }
Expand Down
8 changes: 6 additions & 2 deletions kotlin/src/main/cpp/src/bindings/bindings_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ extern "C"
{
unichars.push_back(rive::UTF::NextUTF8(&ptr));
}

rive::rcp<rive::Font> fallback = FontHelper::findFontFallback(unichars);
if (unichars.empty())
{
return false;
}
rive::rcp<rive::Font> fallback =
FontHelper::findFontFallback(unichars[0], 0);

return fallback != nullptr;
}
Expand Down
8 changes: 6 additions & 2 deletions kotlin/src/main/cpp/src/helpers/font_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,13 @@ std::vector<uint8_t> FontHelper::getSystemFontBytes()
return ByteArrayToUint8Vec(env, fontBytes.get());
}

rive::rcp<rive::Font> FontHelper::findFontFallback(
rive::Span<const rive::Unichar> missing)
rive::rcp<rive::Font> FontHelper::findFontFallback(const rive::Unichar missing,
const uint32_t fallbackIndex)
{
if (fallbackIndex > 0)
{
return nullptr;
}
for (const rive::rcp<rive::Font>& font : fallbackFonts)
{
bool found = font->hasGlyph(missing);
Expand Down

0 comments on commit ec1e05a

Please sign in to comment.