Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fallback font support for iOS and macOS
This builds on top of #7661 and adds an iOS / macOS API for setting fallback fonts. At a high-level, this adds a class property to `RiveFont` for getting / setting the fallback font(s) **based on the system** (or _optionally_, a `UIFont/NSFont`)(accessible in Objective-C and Swift via `RiveFont.fallbackFonts`). This property is an array of a objects conforming to a new protocol: `RiveFallbackFontProvider` (platform-agnostic). By default, if no fallbacks are set, or an empty array is set, the default system font of regular weight will be used. In terms of naming, the `RiveFallbackFontDescriptorDesign` and `RiveFallbackFontDescriptorWeight` types each have cases that mirror those available in 1st party Apple APIs, so that usage and expectations are similar across our APIs, as well as those provided by UIKit / AppKit. ## Example Usage ```swift RiveFont.fallbackFonts = [ RiveFallbackFontDescriptor(systemDesign: .default, weight: .bold), RiveFallbackFontDescriptor(systemDesign: .monospaced, weight: .ultraLight), UIFont.systemFont(ofSize: 20, weight: .bold) ] ``` ## RiveFallbackFontProvider `RiveFallbackFontProvider` is a protocol that defines the interface for types that can be used to return system fonts, or any font that can be used as a fallback. `RiveFallbackFontDescriptor` and `UIFont/NSFont` conform to this protocol; both can be used to define fallback fonts. ## RiveFallbackFontDescriptor `RiveFallbackFontDescriptor` is a platform-agnostic way of defining the _type_ of system font you want to request as a fallback, if necessary. It contains a couple of properties: `design` and `weight`. These are used in conjunction with each other to start with and update a system font (generated by `[UI/NS]Font.systemFont(ofSize:weight:)`, potentially matching on more than one font. ## Unit Tests Unit tests have been written to verify that `design` and `weight` create different fonts, based on the provided values. The tests at a high-level are the same: for each case of both properties, check that there is at least one matching font. For each property, check that each font name is unique. On iOS, the font names are unique based on system design _and_ weight. I felt this was better than asserting against a specific font name, in case Apple changes that from under our feet. Additionally, what the default system fallback is set to is also tested. ## IRL Testing This was tested by creating a riv file that contained a text run whose font was exported containing only the glyphs used, and setting the text run to some text that did not use the exported glyphs. Tested on: - [x] iOS (Simulator) - [x] iPadOS - [x] macOS Diffs= a4e15fb7b Add fallback font support for iOS and macOS (#7690) Co-authored-by: David Skuza <[email protected]>
- Loading branch information