1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef CoreTextFontList_H
7 #define CoreTextFontList_H
9 #include <CoreFoundation/CoreFoundation.h>
11 #include "gfxPlatformFontList.h"
12 #include "gfxPlatformMac.h"
14 #include "mozilla/FontPropertyTypes.h"
15 #include "mozilla/gfx/UnscaledFontMac.h"
16 #include "mozilla/LookAndFeel.h"
17 #include "mozilla/MemoryReporting.h"
19 #include "nsRefPtrHashtable.h"
21 #include "nsUnicharUtils.h"
23 // Abstract base class for Core Text/Core Graphics-based platform font list,
24 // which is subclassed to create specific macOS and iOS variants.
26 // A single member of a font family (i.e. a single face, such as Times Italic).
27 class CTFontEntry final
: public gfxFontEntry
{
29 friend class CoreTextFontList
;
30 friend class gfxMacPlatformFontList
;
31 friend class gfxMacFont
;
33 CTFontEntry(const nsACString
& aPostscriptName
, WeightRange aWeight
,
34 bool aIsStandardFace
= false, double aSizeHint
= 0.0);
36 // for use with data fonts
37 CTFontEntry(const nsACString
& aPostscriptName
, CGFontRef aFontRef
,
38 WeightRange aWeight
, StretchRange aStretch
,
39 SlantStyleRange aStyle
, bool aIsDataUserFont
, bool aIsLocal
);
41 virtual ~CTFontEntry() { ::CGFontRelease(mFontRef
); }
43 gfxFontEntry
* Clone() const override
;
45 // Return a non-owning reference to our CGFont; caller must not release it.
46 // This will cause the fontEntry to create & retain a CGFont for the life
48 // Note that in the case of a broken font, this could return null.
49 CGFontRef
GetFontRef();
51 // Return a new reference to our CGFont. Caller is responsible to release
53 // (If the entry has a cached CGFont, this just bumps its refcount and
54 // returns it; if not, the instance returned will be owned solely by the
56 // Note that in the case of a broken font, this could return null.
57 CGFontRef
CreateOrCopyFontRef() MOZ_REQUIRES_SHARED(mLock
);
59 // override gfxFontEntry table access function to bypass table cache,
60 // use CGFontRef API to get direct access to system font data
61 hb_blob_t
* GetFontTable(uint32_t aTag
) override
;
63 void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
,
64 FontListSizes
* aSizes
) const override
;
66 nsresult
ReadCMAP(FontInfoData
* aFontInfoData
= nullptr) override
;
68 bool RequiresAATLayout() const { return mRequiresAAT
; }
70 bool HasVariations() override
;
71 void GetVariationAxes(
72 nsTArray
<gfxFontVariationAxis
>& aVariationAxes
) override
;
73 void GetVariationInstances(
74 nsTArray
<gfxFontVariationInstance
>& aInstances
) override
;
78 bool SupportsOpenTypeFeature(Script aScript
, uint32_t aFeatureTag
) override
;
81 gfxFont
* CreateFontInstance(const gfxFontStyle
* aFontStyle
) override
;
83 bool HasFontTable(uint32_t aTableTag
) override
;
85 static void DestroyBlobFunc(void* aUserData
);
87 CGFontRef mFontRef
MOZ_GUARDED_BY(mLock
); // owning reference
89 const double mSizeHint
;
91 bool mFontRefInitialized
MOZ_GUARDED_BY(mLock
);
93 mozilla::Atomic
<bool> mRequiresAAT
;
94 mozilla::Atomic
<bool> mIsCFF
;
95 mozilla::Atomic
<bool> mIsCFFInitialized
;
96 mozilla::Atomic
<bool> mHasVariations
;
97 mozilla::Atomic
<bool> mHasVariationsInitialized
;
98 mozilla::Atomic
<bool> mHasAATSmallCaps
;
99 mozilla::Atomic
<bool> mHasAATSmallCapsInitialized
;
101 // To work around Core Text's mishandling of the default value for 'opsz',
102 // we need to record whether the font has an a optical size axis, what its
103 // range and default values are, and a usable close-to-default alternative.
104 // (See bug 1457417 for details.)
105 // These fields are used by gfxMacFont, but stored in the font entry so
106 // that only a single font instance needs to inspect the available
108 gfxFontVariationAxis mOpszAxis
MOZ_GUARDED_BY(mLock
);
109 float mAdjustedDefaultOpsz
MOZ_GUARDED_BY(mLock
);
111 nsTHashtable
<nsUint32HashKey
> mAvailableTables
MOZ_GUARDED_BY(mLock
);
113 mozilla::ThreadSafeWeakPtr
<mozilla::gfx::UnscaledFontMac
> mUnscaledFont
;
116 class CTFontFamily
: public gfxFontFamily
{
118 CTFontFamily(const nsACString
& aName
, FontVisibility aVisibility
)
119 : gfxFontFamily(aName
, aVisibility
) {}
121 virtual ~CTFontFamily() = default;
123 void LocalizedName(nsACString
& aLocalizedName
) override
;
125 void FindStyleVariationsLocked(FontInfoData
* aFontInfoData
= nullptr)
126 MOZ_REQUIRES(mLock
) override
;
129 void AddFace(CTFontDescriptorRef aFace
) MOZ_REQUIRES(mLock
);
132 class CoreTextFontList
: public gfxPlatformFontList
{
133 using FontFamilyListEntry
= mozilla::dom::SystemFontListEntry
;
136 gfxFontFamily
* CreateFontFamily(const nsACString
& aName
,
137 FontVisibility aVisibility
) const override
;
139 static int32_t AppleWeightToCSSWeight(int32_t aAppleWeight
);
141 gfxFontEntry
* LookupLocalFont(nsPresContext
* aPresContext
,
142 const nsACString
& aFontName
,
143 WeightRange aWeightForEntry
,
144 StretchRange aStretchForEntry
,
145 SlantStyleRange aStyleForEntry
) override
;
147 gfxFontEntry
* MakePlatformFont(const nsACString
& aFontName
,
148 WeightRange aWeightForEntry
,
149 StretchRange aStretchForEntry
,
150 SlantStyleRange aStyleForEntry
,
151 const uint8_t* aFontData
,
152 uint32_t aLength
) override
;
154 bool FindAndAddFamiliesLocked(
155 nsPresContext
* aPresContext
, mozilla::StyleGenericFontFamily aGeneric
,
156 const nsACString
& aFamily
, nsTArray
<FamilyAndGeneric
>* aOutput
,
157 FindFamiliesFlags aFlags
, gfxFontStyle
* aStyle
= nullptr,
158 nsAtom
* aLanguage
= nullptr, gfxFloat aDevToCssSize
= 1.0)
159 MOZ_REQUIRES(mLock
) override
;
161 // Values for the entryType field in FontFamilyListEntry records passed
162 // from chrome to content process.
163 enum FontFamilyEntryType
{
164 kStandardFontFamily
= 0, // a standard installed font family
165 kSystemFontFamily
= 1, // name of 'system' font
167 void ReadSystemFontList(mozilla::dom::SystemFontList
*);
171 virtual ~CoreTextFontList();
173 // initialize font lists
174 nsresult
InitFontListForPlatform() MOZ_REQUIRES(mLock
) override
;
175 void InitSharedFontListForPlatform() MOZ_REQUIRES(mLock
) override
;
177 // handle commonly used fonts for which the name table should be loaded at
179 void PreloadNamesList() MOZ_REQUIRES(mLock
);
181 // initialize system fonts
182 virtual void InitSystemFontNames() = 0;
184 // Hooks for the macOS-specific "single face family" hack (Osaka-mono).
185 virtual void InitSingleFaceList() {}
186 virtual void InitAliasesForSingleFaceList() {}
188 virtual bool DeprecatedFamilyIsAvailable(const nsACString
& aName
) {
192 virtual FontVisibility
GetVisibilityForFamily(const nsACString
& aName
) const {
193 return FontVisibility::Unknown
;
196 // helper function to lookup in both hidden system fonts and normal fonts
197 gfxFontFamily
* FindSystemFontFamily(const nsACString
& aFamily
)
200 static void RegisteredFontsChangedNotificationCallback(
201 CFNotificationCenterRef center
, void* observer
, CFStringRef name
,
202 const void* object
, CFDictionaryRef userInfo
);
204 // attempt to use platform-specific fallback for the given character
205 // return null if no usable result found
206 gfxFontEntry
* PlatformGlobalFontFallback(nsPresContext
* aPresContext
,
209 const gfxFontStyle
* aMatchStyle
,
210 FontFamily
& aMatchedFamily
)
211 MOZ_REQUIRES(mLock
) override
;
213 bool UsesSystemFallback() override
{ return true; }
215 already_AddRefed
<FontInfoData
> CreateFontInfoData() override
;
217 // Add the specified family to mFontFamilies.
218 void AddFamily(CFStringRef aFamily
) MOZ_REQUIRES(mLock
);
220 void AddFamily(const nsACString
& aFamilyName
, FontVisibility aVisibility
)
223 static void ActivateFontsFromDir(
224 const nsACString
& aDir
,
225 nsTHashSet
<nsCStringHashKey
>* aLoadedFamilies
= nullptr);
227 gfxFontEntry
* CreateFontEntry(
228 mozilla::fontlist::Face
* aFace
,
229 const mozilla::fontlist::Family
* aFamily
) override
;
231 void GetFacesInitDataForFamily(
232 const mozilla::fontlist::Family
* aFamily
,
233 nsTArray
<mozilla::fontlist::Face::InitData
>& aFaces
,
234 bool aLoadCmaps
) const override
;
236 static void AddFaceInitData(
237 CTFontDescriptorRef aFontDesc
,
238 nsTArray
<mozilla::fontlist::Face::InitData
>& aFaces
, bool aLoadCmaps
);
240 void ReadFaceNamesForFamily(mozilla::fontlist::Family
* aFamily
,
241 bool aNeedFullnamePostscriptNames
)
242 MOZ_REQUIRES(mLock
) override
;
244 #ifdef MOZ_BUNDLED_FONTS
245 void ActivateBundledFonts();
248 // default font for use with system-wide font fallback
249 CTFontRef mDefaultFont
;
251 // Font family that -apple-system maps to
252 nsCString mSystemFontFamilyName
;
254 nsTArray
<nsCString
> mPreloadFonts
;
256 #ifdef MOZ_BUNDLED_FONTS
257 nsTHashSet
<nsCStringHashKey
> mBundledFamilies
;
261 #endif /* CoreTextFontList_H */