Flutter Engine
The Flutter Engine
platform_mac.mm
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <TargetConditionals.h>
6
7#include "flutter/fml/platform/darwin/cf_utils.h"
8#include "flutter/fml/platform/darwin/platform_version.h"
11#include "txt/platform.h"
12#include "txt/platform_mac.h"
13
14#if TARGET_OS_EMBEDDED || TARGET_OS_SIMULATOR
15#include <UIKit/UIKit.h>
16#define FONT_CLASS UIFont
17#else // TARGET_OS_EMBEDDED
18#include <AppKit/AppKit.h>
19#define FONT_CLASS NSFont
20#endif // TARGET_OS_EMBEDDED
21
22// Apple system font larger than size 29 returns SFProDisplay typeface.
23static const CGFloat kSFProDisplayBreakPoint = 29;
24// Font name represents the "SF Pro Display" system font on Apple platforms.
25static const std::string kSFProDisplayName = "CupertinoSystemDisplay";
26// Font weight representing Regular
28
29namespace txt {
30
31const FourCharCode kWeightTag = 'wght';
32
33std::vector<std::string> GetDefaultFontFamilies() {
35 return {[FONT_CLASS systemFontOfSize:14].familyName.UTF8String};
36 } else {
37 return {"Helvetica"};
38 }
39}
40
41sk_sp<SkFontMgr> GetDefaultFontManager(uint32_t font_initialization_data) {
42 static sk_sp<SkFontMgr> mgr = SkFontMgr_New_CoreText(nullptr);
43 return mgr;
44}
45
46fml::CFRef<CTFontRef> MatchSystemUIFont(float desired_weight, float size) {
48 CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, size, nullptr));
49
50 if (desired_weight == kNormalWeightValue) {
51 return ct_font;
52 }
53
54 fml::CFRef<CFMutableDictionaryRef> variations(CFDictionaryCreateMutable(
55 kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks,
56 &kCFTypeDictionaryValueCallBacks));
57
58 auto add_axis_to_variations = [&variations](const FourCharCode tag,
59 float desired_value,
60 float normal_value) {
61 if (desired_value != normal_value) {
62 fml::CFRef<CFNumberRef> tag_number(
63 CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &tag));
64 fml::CFRef<CFNumberRef> value_number(CFNumberCreate(
65 kCFAllocatorDefault, kCFNumberFloatType, &desired_value));
66 CFDictionarySetValue(variations, tag_number, value_number);
67 }
68 };
69 add_axis_to_variations(kWeightTag, desired_weight, kNormalWeightValue);
70
71 fml::CFRef<CFMutableDictionaryRef> attributes(CFDictionaryCreateMutable(
72 kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks,
73 &kCFTypeDictionaryValueCallBacks));
74 CFDictionarySetValue(attributes, kCTFontVariationAttribute, variations);
75
77 CTFontDescriptorCreateWithAttributes(attributes));
78
80 CTFontCreateCopyWithAttributes(ct_font, size, nullptr, var_font_desc));
81}
82
83void RegisterSystemFonts(const DynamicFontManager& dynamic_font_manager) {
84 // iOS loads different system fonts when size is greater than 28 or lower
85 // than 17. The "familyName" property returned from CoreText stays the same
86 // despite the typeface is different.
87 //
88 // Below code manually loads and registers the larger font. The existing
89 // fallback correctly loads the smaller font. The code also iterates through
90 // the possible font weights from 100 - 900 to correctly load all of them, as
91 // a CTFont object for the large system font does not include all of the font
92 // weights by default.
93 //
94 // Darwin system fonts from 17 to 28 also have dynamic spacing based on sizes.
95 // These two fonts do not match the spacings when sizes are from 17 to 28.
96 // The spacing should be handled by the app or the framework.
97 //
98 // See https://www.wwdcnotes.com/notes/wwdc20/10175/ for Apple's document on
99 // this topic.
100 auto register_weighted_font = [&dynamic_font_manager](const int weight) {
101 sk_sp<SkTypeface> large_system_font_weighted = SkMakeTypefaceFromCTFont(
103 if (large_system_font_weighted) {
104 dynamic_font_manager.font_provider().RegisterTypeface(
105 large_system_font_weighted, kSFProDisplayName);
106 }
107 };
108 for (int i = 0; i < 8; i++) {
109 const int font_weight = i * 100;
110 register_weighted_font(font_weight);
111 }
112 // The value 780 returns a font weight of 800.
113 register_weighted_font(780);
114 // The value of 810 returns a font weight of 900.
115 register_weighted_font(810);
116}
117
118} // namespace txt
SK_API sk_sp< SkFontMgr > SkFontMgr_New_CoreText(CTFontCollectionRef)
TypefaceFontAssetProvider & font_provider() const
void RegisterTypeface(sk_sp< SkTypeface > typeface)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
bool IsPlatformVersionAtLeast(size_t major, size_t minor=0, size_t patch=0)
const FourCharCode kWeightTag
Definition: platform_mac.mm:31
std::vector< std::string > GetDefaultFontFamilies()
Definition: platform.cc:13
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition: platform.cc:17
void RegisterSystemFonts(const DynamicFontManager &dynamic_font_manager)
Definition: platform_mac.mm:83
fml::CFRef< CTFontRef > MatchSystemUIFont(float desired_weight, float size)
Definition: platform_mac.mm:46
#define FONT_CLASS
Definition: platform_mac.mm:19
static const std::string kSFProDisplayName
Definition: platform_mac.mm:25
float kNormalWeightValue
Definition: platform_mac.mm:27
static const CGFloat kSFProDisplayBreakPoint
Definition: platform_mac.mm:23