-
Notifications
You must be signed in to change notification settings - Fork 6k
Get the correct iOS system font for each weight #48937
Changes from 4 commits
971a196
176ad4c
f481127
2751004
70327ca
ffc14b6
562afbe
18c7e6d
bab6f20
b46459b
c163e5e
41de08d
47caba5
8b78c32
a3f3346
a528a57
0b6fc08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,13 @@ | |
| 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,6 +46,42 @@ | |
| 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); | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was profiling allocations - and this seems to be leaking the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @knopp could you file an issue for that so it doesn't get lost?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this has been already fixed when clang tidy started complaining about missing CFRelease.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you know which release will contain the memory leak fix? |
||
| 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 | ||
|
|
@@ -63,13 +103,35 @@ void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) { | |
| dynamic_font_manager.font_provider().RegisterTypeface(large_system_font, | ||
| kSFProDisplayName); | ||
| } | ||
| for (int i = 0; i < 9; i++) { | ||
| const int font_weight = i * 100; | ||
| sk_sp<SkTypeface> large_system_font_weighted = | ||
MitchellGoodwin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| SkMakeTypefaceFromCTFont((CTFontRef)CFAutorelease( | ||
| MatchSystemUIFont(font_weight, kSFProDisplayBreakPoint))); | ||
| if (large_system_font_weighted) { | ||
| dynamic_font_manager.font_provider().RegisterTypeface( | ||
| large_system_font_weighted, | ||
| kSFProDisplayName + "w" + std::to_string(font_weight + 100)); | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| for (int i = 0; i < 9; i++) { | ||
| const int font_weight = i * 100; | ||
| sk_sp<SkTypeface> large_system_font_weighted = | ||
| SkMakeTypefaceFromCTFont((CTFontRef)CFAutorelease( | ||
| MatchSystemUIFont(font_weight, kSFProTextBreakPoint))); | ||
| if (large_system_font_weighted) { | ||
| dynamic_font_manager.font_provider().RegisterTypeface( | ||
| large_system_font_weighted, | ||
| kSFProTextName + "w" + std::to_string(font_weight + 100)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace txt | ||
Uh oh!
There was an error while loading. Please reload this page.