diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:13:27 +0000 |
commit | 40a355a42d4a9444dc753c04c6608dade2f06a23 (patch) | |
tree | 871fc667d2de662f171103ce5ec067014ef85e61 /gfx/thebes/CoreTextFontList.cpp | |
parent | Adding upstream version 124.0.1. (diff) | |
download | firefox-adbda400be353e676059e335c3c0aaf99e719475.tar.xz firefox-adbda400be353e676059e335c3c0aaf99e719475.zip |
Adding upstream version 125.0.1.upstream/125.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'gfx/thebes/CoreTextFontList.cpp')
-rw-r--r-- | gfx/thebes/CoreTextFontList.cpp | 124 |
1 files changed, 116 insertions, 8 deletions
diff --git a/gfx/thebes/CoreTextFontList.cpp b/gfx/thebes/CoreTextFontList.cpp index 0c8c179e8e..e83435b638 100644 --- a/gfx/thebes/CoreTextFontList.cpp +++ b/gfx/thebes/CoreTextFontList.cpp @@ -35,6 +35,15 @@ using namespace mozilla; using namespace mozilla::gfx; +#ifdef MOZ_WIDGET_COCOA +// Building with newer macOS SDKs can cause a bunch of font-family names to be +// hidden from the Core Text API we use to enumerate available fonts. Because +// some content still benefits from having these names recognized, we forcibly +// include them in the list. Some day we might want to drop support for these. +# define USE_DEPRECATED_FONT_FAMILY_NAMES 1 +#endif + +#if USE_DEPRECATED_FONT_FAMILY_NAMES // List generated by diffing the arrays returned by // CTFontManagerCopyAvailableFontFamilyNames() when built with // MACOSX_DEPLOYMENT_TARGET=10.12 vs 11.0, to identify the font family names @@ -198,6 +207,7 @@ constexpr nsLiteralCString kDeprecatedFontFamilies[] = { "Superclarendon"_ns, "Times"_ns, }; +#endif // USE_DEPRECATED_FONT_FAMILY_NAMES static void GetStringForCFString(CFStringRef aSrc, nsAString& aDest) { auto len = CFStringGetLength(aSrc); @@ -497,7 +507,7 @@ CGFontRef CTFontEntry::CreateOrCopyFontRef() { return mFontRef; } - CrashReporter::AutoAnnotateCrashReport autoFontName( + CrashReporter::AutoRecordAnnotation autoFontName( CrashReporter::Annotation::FontName, mName); // Create a new CGFont; caller will own the only reference to it. @@ -666,7 +676,7 @@ bool CTFontEntry::SupportsOpenTypeFeature(Script aScript, return mHasAATSmallCaps; } - CrashReporter::AutoAnnotateCrashReport autoFontName( + CrashReporter::AutoRecordAnnotation autoFontName( CrashReporter::Annotation::FontName, FamilyName()); AutoCFRelease<CTFontRef> ctFont = @@ -907,6 +917,47 @@ void CTFontFamily::FindStyleVariationsLocked(FontInfoData* aFontInfoData) { AUTO_PROFILER_LABEL_DYNAMIC_NSCSTRING("CTFontFamily::FindStyleVariations", LAYOUT, mName); + if (mForSystemFont) { + MOZ_ASSERT(gfxPlatform::HasVariationFontSupport()); + + auto addToFamily = [&](CTFontRef aFont) MOZ_REQUIRES(mLock) { + AutoCFRelease<CFStringRef> psName = CTFontCopyPostScriptName(aFont); + nsAutoString nameUTF16; + nsAutoCString nameUTF8; + GetStringForCFString(psName, nameUTF16); + CopyUTF16toUTF8(nameUTF16, nameUTF8); + + auto* fe = + new CTFontEntry(nameUTF8, WeightRange(FontWeight::NORMAL), true, 0.0); + + // Set the appropriate style, assuming it may not have a variation range. + CTFontSymbolicTraits traits = CTFontGetSymbolicTraits(aFont); + fe->mStyleRange = SlantStyleRange((traits & kCTFontTraitItalic) + ? FontSlantStyle::ITALIC + : FontSlantStyle::NORMAL); + + // Set up weight (and width, if present) ranges. + fe->SetupVariationRanges(); + AddFontEntryLocked(fe); + }; + + addToFamily(mForSystemFont); + + // See if there is a corresponding italic face, and add it to the family. + AutoCFRelease<CTFontRef> italicFont = CTFontCreateCopyWithSymbolicTraits( + mForSystemFont, 0.0, nullptr, kCTFontTraitItalic, kCTFontTraitItalic); + if (italicFont != mForSystemFont) { + addToFamily(italicFont); + } + + CFRelease(mForSystemFont); + mForSystemFont = nullptr; + + SetHasStyles(true); + + return; + } + struct Context { CTFontFamily* family; const void* prevValue = nullptr; @@ -1134,11 +1185,13 @@ nsresult CoreTextFontList::InitFontListForPlatform() { (CFStringRef)CFArrayGetValueAtIndex(familyNames, i); AddFamily(familyName); } +#if USE_DEPRECATED_FONT_FAMILY_NAMES for (const auto& name : kDeprecatedFontFamilies) { if (DeprecatedFamilyIsAvailable(name)) { AddFamily(name, GetVisibilityForFamily(name)); } } +#endif } else { // Content process: use font list passed from the chrome process via // the GetXPCOMProcessAttributes message, because it's much faster than @@ -1196,8 +1249,11 @@ void CoreTextFontList::InitSharedFontListForPlatform() { AutoCFRelease<CFArrayRef> familyNames = CTFontManagerCopyAvailableFontFamilyNames(); nsTArray<fontlist::Family::InitData> families; - families.SetCapacity(CFArrayGetCount(familyNames) + - ArrayLength(kDeprecatedFontFamilies)); + families.SetCapacity(CFArrayGetCount(familyNames) +#if USE_DEPRECATED_FONT_FAMILY_NAMES + + ArrayLength(kDeprecatedFontFamilies) +#endif + ); for (CFIndex i = 0; i < CFArrayGetCount(familyNames); ++i) { nsAutoString name16; CFStringRef familyName = @@ -1209,6 +1265,7 @@ void CoreTextFontList::InitSharedFontListForPlatform() { families.AppendElement(fontlist::Family::InitData( key, name, fontlist::Family::kNoIndex, GetVisibilityForFamily(name))); } +#if USE_DEPRECATED_FONT_FAMILY_NAMES for (const nsACString& name : kDeprecatedFontFamilies) { if (DeprecatedFamilyIsAvailable(name)) { nsAutoCString key; @@ -1218,6 +1275,7 @@ void CoreTextFontList::InitSharedFontListForPlatform() { GetVisibilityForFamily(name))); } } +#endif SharedFontList()->SetFamilyNames(families); InitAliasesForSingleFaceList(); GetPrefsAndStartLoader(); @@ -1367,7 +1425,7 @@ gfxFontEntry* CoreTextFontList::LookupLocalFont( AutoLock lock(mLock); - CrashReporter::AutoAnnotateCrashReport autoFontName( + CrashReporter::AutoRecordAnnotation autoFontName( CrashReporter::Annotation::FontName, aFontName); AutoCFRelease<CFStringRef> faceName = CreateCFStringForString(aFontName); @@ -1444,7 +1502,7 @@ gfxFontEntry* CoreTextFontList::MakePlatformFont(const nsACString& aFontName, return nullptr; } - CrashReporter::AutoAnnotateCrashReport autoFontName( + CrashReporter::AutoRecordAnnotation autoFontName( CrashReporter::Annotation::FontName, aFontName); AutoCFRelease<CGDataProviderRef> provider = @@ -1504,7 +1562,7 @@ class CTFontInfo final : public FontInfoData { }; void CTFontInfo::LoadFontFamilyData(const nsACString& aFamilyName) { - CrashReporter::AutoAnnotateCrashReport autoFontName( + CrashReporter::AutoRecordAnnotation autoFontName( CrashReporter::Annotation::FontName, aFamilyName); // Prevent this from running concurrently with CGFont operations on the main // thread, because the macOS font cache is fragile with concurrent access. @@ -1726,7 +1784,7 @@ void CoreTextFontList::GetFacesInitDataForFamily( const fontlist::Family* aFamily, nsTArray<fontlist::Face::InitData>& aFaces, bool aLoadCmaps) const { auto name = aFamily->Key().AsString(SharedFontList()); - CrashReporter::AutoAnnotateCrashReport autoFontName( + CrashReporter::AutoRecordAnnotation autoFontName( CrashReporter::Annotation::FontName, name); struct Context { @@ -1805,6 +1863,56 @@ void CoreTextFontList::ReadFaceNamesForFamily( } } +static CFStringRef CopyRealFamilyName(CTFontRef aFont) { + AutoCFRelease<CFStringRef> psName = CTFontCopyPostScriptName(aFont); + AutoCFRelease<CGFontRef> cgFont = + CGFontCreateWithFontName(CFStringRef(psName)); + if (!cgFont) { + return CTFontCopyFamilyName(aFont); + } + AutoCFRelease<CTFontRef> ctFont = + CTFontCreateWithGraphicsFont(cgFont, 0.0, nullptr, nullptr); + if (!ctFont) { + return CTFontCopyFamilyName(aFont); + } + return CTFontCopyFamilyName(ctFont); +} + +void CoreTextFontList::InitSystemFontNames() { + // text font family + AutoCFRelease<CTFontRef> font = CTFontCreateUIFontForLanguage( + kCTFontUIFontSystem, 0.0, nullptr); // TODO: language + AutoCFRelease<CFStringRef> name = CopyRealFamilyName(font); + + nsAutoString familyName; + GetStringForCFString(name, familyName); + CopyUTF16toUTF8(familyName, mSystemFontFamilyName); + + // We store an in-process gfxFontFamily for the system font even if using the + // shared fontlist to manage "normal" fonts, because the hidden system fonts + // may be excluded from the font list altogether. This family will be + // populated based on the given NSFont. + RefPtr<gfxFontFamily> fam = new CTFontFamily(mSystemFontFamilyName, font); + if (fam) { + nsAutoCString key; + GenerateFontListKey(mSystemFontFamilyName, key); + mFontFamilies.InsertOrUpdate(key, std::move(fam)); + } +} + +FontFamily CoreTextFontList::GetDefaultFontForPlatform( + nsPresContext* aPresContext, const gfxFontStyle* aStyle, + nsAtom* aLanguage) { + AutoCFRelease<CTFontRef> font = CTFontCreateUIFontForLanguage( + kCTFontUIFontUser, 0.0, nullptr); // TODO: language + AutoCFRelease<CFStringRef> name = CTFontCopyFamilyName(font); + + nsAutoString familyName; + GetStringForCFString(name, familyName); + + return FindFamily(aPresContext, NS_ConvertUTF16toUTF8(familyName)); +} + #ifdef MOZ_BUNDLED_FONTS void CoreTextFontList::ActivateBundledFonts() { nsCOMPtr<nsIFile> localDir; |