Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/ui/text.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ enum FontStyle {
italic,
}

String? _proxySystemFont(String? fontFamily, FontWeight? fontWeight) {
if (fontFamily != null && fontFamily == 'CupertinoSystemText' || fontFamily == 'CupertinoSystemDisplay') {
return fontFamily! + (fontWeight!= null ? fontWeight.toString().substring(11) :'');
}
return fontFamily;
}

/// The thickness of the glyphs used to draw the text.
///
/// Fonts are typically weighted on a 9-point scale, which, for historical
Expand Down Expand Up @@ -1742,7 +1749,7 @@ class TextStyle {
fontVariations,
),
_leadingDistribution = leadingDistribution,
_fontFamily = fontFamily ?? '',
_fontFamily = _proxySystemFont(fontFamily, fontWeight) ?? '',
_fontFamilyFallback = fontFamilyFallback,
_fontSize = fontSize,
_letterSpacing = letterSpacing,
Expand Down
3 changes: 3 additions & 0 deletions lib/ui/text/font_collection.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class FontCollection {
Dart_Handle callback,
const std::string& family_name);

// sk_sp<SkTypeface> FontCollection::matchTypeface(const SkString& familyName,
// SkFontStyle fontStyle) override;

private:
std::shared_ptr<txt::FontCollection> collection_;
sk_sp<txt::DynamicFontManager> dynamic_font_manager_;
Expand Down
15 changes: 14 additions & 1 deletion third_party/txt/src/txt/asset_font_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,24 @@ sk_sp<SkFontStyleSet> AssetFontManager::onMatchFamily(
sk_sp<SkTypeface> AssetFontManager::onMatchFamilyStyle(
const char familyName[],
const SkFontStyle& style) const {
// #if FML_OS_MACOSX || FML_OS_IOS
// if (strcmp(familyName, "CupertinoSystemText") == 0 || strcmp(familyName,
// "CupertinoSystemDisplay") == 0) { sk_sp<SkFontStyleSet> font_style_set =
// font_provider_->MatchFamily(std::string(familyName) + "w" +
// std::to_string(style.weight()));
const std::string local_font_name = std::string(familyName) + "w200";
sk_sp<SkFontStyleSet> font_style_set =
font_provider_->MatchFamily(std::string(familyName));
font_provider_->MatchFamily(std::string(local_font_name));
if (font_style_set == nullptr)
return nullptr;
return font_style_set->matchStyle(style);
// }
// #endif
// sk_sp<SkFontStyleSet> font_style_set =
// font_provider_->MatchFamily(std::string(familyName));
// if (font_style_set == nullptr)
// return nullptr;
// return font_style_set->matchStyle(style);
}

sk_sp<SkTypeface> AssetFontManager::onMatchFamilyStyleCharacter(
Expand Down
62 changes: 62 additions & 0 deletions third_party/txt/src/txt/platform_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was profiling allocations - and this seems to be leaking the CFNumbers, CFMutableDictionary and CTFontDescriptor. I don't see CFRelease calls anywhere? I don't think there's a ScopedCFTypeRef in fml so this will need to be released manually.

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand All @@ -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 =
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