Get the correct iOS system font for each weight (flutter/engine#48937)
See [flutter/issue/139689](https://github.com/flutter/flutter/issues/139689) and [flutter/issue/132475](https://github.com/flutter/flutter/issues/132475) Registers all of the possible font weight values for "CupertinoSystemDisplay". The registering of "CupertinoSystemText" was removed, as the fallback will correctly show the right fonts, and this logic does not work for the smaller font due to Apple APIs removing the font weight. The font weights for 800 and 900 had to be added outside of the loop as they were coming back with values different from what was being put in. Before: <img width="250" alt="Screenshot 2023-12-06 at 1 17 11â¯PM" src="https://github.com/flutter/engine/assets/58190796/036231eb-d4e2-4c93-82de-fa62d7ddc7c1"> After: <img width="364" alt="Screenshot 2024-01-03 at 3 15 33â¯PM" src="https://github.com/flutter/engine/assets/58190796/69651f01-d694-4758-9fb5-acf50a1895c4"> [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -20,15 +20,15 @@
|
||||
|
||||
// Apple system font larger than size 29 returns SFProDisplay typeface.
|
||||
static const CGFloat kSFProDisplayBreakPoint = 29;
|
||||
// Apple system font smaller than size 16 returns SFProText typeface.
|
||||
static const CGFloat kSFProTextBreakPoint = 16;
|
||||
// Font name represents the "SF Pro Display" system font on Apple platforms.
|
||||
static const std::string kSFProDisplayName = "CupertinoSystemDisplay";
|
||||
// Font name represents the "SF Pro Text" system font on Apple platforms.
|
||||
static const std::string kSFProTextName = "CupertinoSystemText";
|
||||
// Font weight representing Regular
|
||||
float kNormalWeightValue = 400;
|
||||
|
||||
namespace txt {
|
||||
|
||||
const FourCharCode kWeightTag = 'wght';
|
||||
|
||||
std::vector<std::string> GetDefaultFontFamilies() {
|
||||
if (fml::IsPlatformVersionAtLeast(9)) {
|
||||
return {[FONT_CLASS systemFontOfSize:14].familyName.UTF8String};
|
||||
@@ -42,13 +42,52 @@ sk_sp<SkFontMgr> GetDefaultFontManager(uint32_t font_initialization_data) {
|
||||
return mgr;
|
||||
}
|
||||
|
||||
CTFontRef MatchSystemUIFont(float desired_weight, float size) {
|
||||
CTFontRef ct_font(
|
||||
CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, size, nullptr));
|
||||
|
||||
if (desired_weight == kNormalWeightValue) {
|
||||
return ct_font;
|
||||
}
|
||||
|
||||
CFMutableDictionaryRef variations(CFDictionaryCreateMutable(
|
||||
kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks,
|
||||
&kCFTypeDictionaryValueCallBacks));
|
||||
|
||||
auto add_axis_to_variations = [&variations](const FourCharCode tag,
|
||||
float desired_value,
|
||||
float normal_value) {
|
||||
if (desired_value != normal_value) {
|
||||
CFNumberRef tag_number(
|
||||
CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &tag));
|
||||
CFNumberRef value_number(CFNumberCreate(
|
||||
kCFAllocatorDefault, kCFNumberFloatType, &desired_value));
|
||||
CFDictionarySetValue(variations, tag_number, value_number);
|
||||
}
|
||||
};
|
||||
add_axis_to_variations(kWeightTag, desired_weight, kNormalWeightValue);
|
||||
|
||||
CFMutableDictionaryRef attributes(CFDictionaryCreateMutable(
|
||||
kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks,
|
||||
&kCFTypeDictionaryValueCallBacks));
|
||||
CFDictionarySetValue(attributes, kCTFontVariationAttribute, variations);
|
||||
|
||||
CTFontDescriptorRef var_font_desc(
|
||||
CTFontDescriptorCreateWithAttributes(attributes));
|
||||
|
||||
return CTFontCreateCopyWithAttributes(ct_font, size, nullptr, var_font_desc);
|
||||
}
|
||||
|
||||
void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) {
|
||||
// iOS loads different system fonts when size is greater than 28 or lower
|
||||
// than 17. The "familyName" property returned from CoreText stays the same
|
||||
// despite the typeface is different.
|
||||
//
|
||||
// Below code manually loads and registers them as two different fonts
|
||||
// so Flutter app can access them on macOS and iOS.
|
||||
// Below code manually loads and registers the larger font. The existing
|
||||
// fallback correctly loads the smaller font. The code also iterates through
|
||||
// the possible font weights from 100 - 900 to correctly load all of them, as
|
||||
// a CTFont object for the large system font does not include all of the font
|
||||
// weights by default.
|
||||
//
|
||||
// Darwin system fonts from 17 to 28 also have dynamic spacing based on sizes.
|
||||
// These two fonts do not match the spacings when sizes are from 17 to 28.
|
||||
@@ -56,20 +95,23 @@ void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) {
|
||||
//
|
||||
// See https://www.wwdcnotes.com/notes/wwdc20/10175/ for Apple's document on
|
||||
// this topic.
|
||||
sk_sp<SkTypeface> large_system_font = SkMakeTypefaceFromCTFont(
|
||||
(CTFontRef)CFAutorelease(CTFontCreateUIFontForLanguage(
|
||||
kCTFontUIFontSystem, kSFProDisplayBreakPoint, NULL)));
|
||||
if (large_system_font) {
|
||||
dynamic_font_manager.font_provider().RegisterTypeface(large_system_font,
|
||||
kSFProDisplayName);
|
||||
}
|
||||
sk_sp<SkTypeface> regular_system_font = SkMakeTypefaceFromCTFont(
|
||||
(CTFontRef)CFAutorelease(CTFontCreateUIFontForLanguage(
|
||||
kCTFontUIFontSystem, kSFProTextBreakPoint, NULL)));
|
||||
if (regular_system_font) {
|
||||
dynamic_font_manager.font_provider().RegisterTypeface(regular_system_font,
|
||||
kSFProTextName);
|
||||
auto register_weighted_font = [&dynamic_font_manager](const int weight) {
|
||||
sk_sp<SkTypeface> large_system_font_weighted =
|
||||
SkMakeTypefaceFromCTFont((CTFontRef)CFAutorelease(
|
||||
MatchSystemUIFont(weight, kSFProDisplayBreakPoint)));
|
||||
if (large_system_font_weighted) {
|
||||
dynamic_font_manager.font_provider().RegisterTypeface(
|
||||
large_system_font_weighted, kSFProDisplayName);
|
||||
}
|
||||
};
|
||||
for (int i = 0; i < 8; i++) {
|
||||
const int font_weight = i * 100;
|
||||
register_weighted_font(font_weight);
|
||||
}
|
||||
// The value 780 returns a font weight of 800.
|
||||
register_weighted_font(780);
|
||||
// The value of 810 returns a font weight of 900.
|
||||
register_weighted_font(810);
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
|
||||
@@ -21,13 +21,14 @@ class PlatformMacTests : public ::testing::Test {
|
||||
TEST_F(PlatformMacTests, RegisterSystemFonts) {
|
||||
DynamicFontManager dynamic_font_manager;
|
||||
RegisterSystemFonts(dynamic_font_manager);
|
||||
ASSERT_EQ(dynamic_font_manager.font_provider().GetFamilyCount(), 2ul);
|
||||
ASSERT_EQ(dynamic_font_manager.font_provider().GetFamilyCount(), 1ul);
|
||||
ASSERT_NE(dynamic_font_manager.font_provider().MatchFamily(
|
||||
"CupertinoSystemDisplay"),
|
||||
nullptr);
|
||||
ASSERT_NE(
|
||||
dynamic_font_manager.font_provider().MatchFamily("CupertinoSystemText"),
|
||||
nullptr);
|
||||
ASSERT_EQ(dynamic_font_manager.font_provider()
|
||||
.MatchFamily("CupertinoSystemDisplay")
|
||||
->count(),
|
||||
10);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
|
||||
Reference in New Issue
Block a user