|
20 | 20 |
|
21 | 21 | // Apple system font larger than size 29 returns SFProDisplay typeface. |
22 | 22 | static const CGFloat kSFProDisplayBreakPoint = 29; |
23 | | -// Apple system font smaller than size 16 returns SFProText typeface. |
24 | | -static const CGFloat kSFProTextBreakPoint = 16; |
25 | 23 | // Font name represents the "SF Pro Display" system font on Apple platforms. |
26 | 24 | static const std::string kSFProDisplayName = "CupertinoSystemDisplay"; |
27 | | -// Font name represents the "SF Pro Text" system font on Apple platforms. |
28 | | -static const std::string kSFProTextName = "CupertinoSystemText"; |
| 25 | +// Font weight representing Regular |
| 26 | +float kNormalWeightValue = 400; |
29 | 27 |
|
30 | 28 | namespace txt { |
31 | 29 |
|
| 30 | +const FourCharCode kWeightTag = 'wght'; |
| 31 | + |
32 | 32 | std::vector<std::string> GetDefaultFontFamilies() { |
33 | 33 | if (fml::IsPlatformVersionAtLeast(9)) { |
34 | 34 | return {[FONT_CLASS systemFontOfSize:14].familyName.UTF8String}; |
|
42 | 42 | return mgr; |
43 | 43 | } |
44 | 44 |
|
| 45 | +CTFontRef MatchSystemUIFont(float desired_weight, float size) { |
| 46 | + CTFontRef ct_font( |
| 47 | + CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, size, nullptr)); |
| 48 | + |
| 49 | + if (desired_weight == kNormalWeightValue) { |
| 50 | + return ct_font; |
| 51 | + } |
| 52 | + |
| 53 | + CFMutableDictionaryRef variations(CFDictionaryCreateMutable( |
| 54 | + kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, |
| 55 | + &kCFTypeDictionaryValueCallBacks)); |
| 56 | + |
| 57 | + auto add_axis_to_variations = [&variations](const FourCharCode tag, |
| 58 | + float desired_value, |
| 59 | + float normal_value) { |
| 60 | + if (desired_value != normal_value) { |
| 61 | + CFNumberRef tag_number( |
| 62 | + CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &tag)); |
| 63 | + CFNumberRef value_number(CFNumberCreate( |
| 64 | + kCFAllocatorDefault, kCFNumberFloatType, &desired_value)); |
| 65 | + CFDictionarySetValue(variations, tag_number, value_number); |
| 66 | + } |
| 67 | + }; |
| 68 | + add_axis_to_variations(kWeightTag, desired_weight, kNormalWeightValue); |
| 69 | + |
| 70 | + CFMutableDictionaryRef attributes(CFDictionaryCreateMutable( |
| 71 | + kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, |
| 72 | + &kCFTypeDictionaryValueCallBacks)); |
| 73 | + CFDictionarySetValue(attributes, kCTFontVariationAttribute, variations); |
| 74 | + |
| 75 | + CTFontDescriptorRef var_font_desc( |
| 76 | + CTFontDescriptorCreateWithAttributes(attributes)); |
| 77 | + |
| 78 | + return CTFontCreateCopyWithAttributes(ct_font, size, nullptr, var_font_desc); |
| 79 | +} |
| 80 | + |
45 | 81 | void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) { |
46 | 82 | // iOS loads different system fonts when size is greater than 28 or lower |
47 | 83 | // than 17. The "familyName" property returned from CoreText stays the same |
48 | 84 | // despite the typeface is different. |
49 | 85 | // |
50 | | - // Below code manually loads and registers them as two different fonts |
51 | | - // so Flutter app can access them on macOS and iOS. |
| 86 | + // Below code manually loads and registers the larger font. The existing |
| 87 | + // fallback correctly loads the smaller font. The code also iterates through |
| 88 | + // the possible font weights from 100 - 900 to correctly load all of them, as |
| 89 | + // a CTFont object for the large system font does not include all of the font |
| 90 | + // weights by default. |
52 | 91 | // |
53 | 92 | // Darwin system fonts from 17 to 28 also have dynamic spacing based on sizes. |
54 | 93 | // These two fonts do not match the spacings when sizes are from 17 to 28. |
55 | 94 | // The spacing should be handled by the app or the framework. |
56 | 95 | // |
57 | 96 | // See https://www.wwdcnotes.com/notes/wwdc20/10175/ for Apple's document on |
58 | 97 | // this topic. |
59 | | - sk_sp<SkTypeface> large_system_font = SkMakeTypefaceFromCTFont( |
60 | | - (CTFontRef)CFAutorelease(CTFontCreateUIFontForLanguage( |
61 | | - kCTFontUIFontSystem, kSFProDisplayBreakPoint, NULL))); |
62 | | - if (large_system_font) { |
63 | | - dynamic_font_manager.font_provider().RegisterTypeface(large_system_font, |
64 | | - kSFProDisplayName); |
65 | | - } |
66 | | - sk_sp<SkTypeface> regular_system_font = SkMakeTypefaceFromCTFont( |
67 | | - (CTFontRef)CFAutorelease(CTFontCreateUIFontForLanguage( |
68 | | - kCTFontUIFontSystem, kSFProTextBreakPoint, NULL))); |
69 | | - if (regular_system_font) { |
70 | | - dynamic_font_manager.font_provider().RegisterTypeface(regular_system_font, |
71 | | - kSFProTextName); |
| 98 | + auto register_weighted_font = [&dynamic_font_manager](const int weight) { |
| 99 | + sk_sp<SkTypeface> large_system_font_weighted = |
| 100 | + SkMakeTypefaceFromCTFont((CTFontRef)CFAutorelease( |
| 101 | + MatchSystemUIFont(weight, kSFProDisplayBreakPoint))); |
| 102 | + if (large_system_font_weighted) { |
| 103 | + dynamic_font_manager.font_provider().RegisterTypeface( |
| 104 | + large_system_font_weighted, kSFProDisplayName); |
| 105 | + } |
| 106 | + }; |
| 107 | + for (int i = 0; i < 8; i++) { |
| 108 | + const int font_weight = i * 100; |
| 109 | + register_weighted_font(font_weight); |
72 | 110 | } |
| 111 | + // The value 780 returns a font weight of 800. |
| 112 | + register_weighted_font(780); |
| 113 | + // The value of 810 returns a font weight of 900. |
| 114 | + register_weighted_font(810); |
73 | 115 | } |
74 | 116 |
|
75 | 117 | } // namespace txt |
0 commit comments