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 GFXPLATFORMFONTLIST_H_
7 #define GFXPLATFORMFONTLIST_H_
9 #include "nsClassHashtable.h"
10 #include "nsTHashMap.h"
11 #include "nsTHashSet.h"
12 #include "nsRefPtrHashtable.h"
13 #include "nsTHashtable.h"
15 #include "gfxFontUtils.h"
16 #include "gfxFontInfoLoader.h"
18 #include "gfxFontConstants.h"
19 #include "gfxPlatform.h"
20 #include "SharedFontList.h"
22 #include "base/process.h"
23 #include "nsIMemoryReporter.h"
24 #include "mozilla/Attributes.h"
25 #include "mozilla/EnumeratedArray.h"
26 #include "mozilla/FontPropertyTypes.h"
27 #include "mozilla/MemoryReporting.h"
28 #include "mozilla/RangedArray.h"
29 #include "mozilla/RecursiveMutex.h"
30 #include "mozilla/ipc/SharedMemory.h"
31 #include "nsLanguageAtomService.h"
37 } // namespace mozilla
39 class CharMapHashKey
: public PLDHashEntryHdr
{
41 typedef gfxCharacterMap
* KeyType
;
42 typedef const gfxCharacterMap
* KeyTypePointer
;
44 explicit CharMapHashKey(const gfxCharacterMap
* aCharMap
)
45 : mCharMap(const_cast<gfxCharacterMap
*>(aCharMap
)) {
46 MOZ_COUNT_CTOR(CharMapHashKey
);
48 CharMapHashKey(const CharMapHashKey
& toCopy
) : mCharMap(toCopy
.mCharMap
) {
49 MOZ_COUNT_CTOR(CharMapHashKey
);
51 MOZ_COUNTED_DTOR(CharMapHashKey
)
53 gfxCharacterMap
* GetKey() const { return mCharMap
.get(); }
55 bool KeyEquals(const gfxCharacterMap
* aCharMap
) const {
56 MOZ_ASSERT(!aCharMap
->mBuildOnTheFly
&& !mCharMap
->mBuildOnTheFly
,
57 "custom cmap used in shared cmap hashtable");
58 // cmaps built on the fly never match
59 if (aCharMap
->mHash
!= mCharMap
->mHash
) {
62 return mCharMap
->Equals(aCharMap
);
65 static const gfxCharacterMap
* KeyToPointer(gfxCharacterMap
* aCharMap
) {
68 static PLDHashNumber
HashKey(const gfxCharacterMap
* aCharMap
) {
69 return aCharMap
->mHash
;
72 enum { ALLOW_MEMMOVE
= true };
75 friend class gfxPlatformFontList
;
77 // gfxCharacterMap::Release() will notify us when the refcount of a
78 // charmap drops to 1; at that point, we'll lock the cache, check if
79 // the charmap is owned by the cache and this is still the only ref,
80 // and if so, delete it.
81 RefPtr
<gfxCharacterMap
> mCharMap
;
85 * A helper class used to create a SharedBitSet instance in a FontList's shared
86 * memory, while ensuring that we avoid bloating memory by avoiding creating
87 * duplicate instances.
89 class ShmemCharMapHashEntry final
: public PLDHashEntryHdr
{
91 typedef const gfxSparseBitSet
* KeyType
;
92 typedef const gfxSparseBitSet
* KeyTypePointer
;
95 * Creation from a gfxSparseBitSet creates not only the ShmemCharMapHashEntry
96 * itself, but also a SharedBitSet in shared memory.
97 * Only the parent process creates and manages these entries.
99 explicit ShmemCharMapHashEntry(const gfxSparseBitSet
* aCharMap
);
101 ShmemCharMapHashEntry(ShmemCharMapHashEntry
&&) = default;
102 ShmemCharMapHashEntry
& operator=(ShmemCharMapHashEntry
&&) = default;
105 * Return a shared-memory Pointer that refers to the wrapped SharedBitSet.
106 * This can be passed to content processes to give them access to the same
107 * SharedBitSet as the parent stored.
109 mozilla::fontlist::Pointer
GetCharMap() const { return mCharMap
; }
111 bool KeyEquals(KeyType aCharMap
) const {
112 // mHash is a 32-bit Adler checksum of the bitset; if it doesn't match we
113 // can immediately reject it as non-matching, but if it is equal we still
114 // need to do a full equality check below.
115 if (mHash
!= aCharMap
->GetChecksum()) {
119 return mCharMap
.ToPtr
<const SharedBitSet
>(mList
)->Equals(aCharMap
);
122 static KeyTypePointer
KeyToPointer(KeyType aCharMap
) { return aCharMap
; }
123 static PLDHashNumber
HashKey(KeyType aCharMap
) {
124 return aCharMap
->GetChecksum();
127 enum { ALLOW_MEMMOVE
= false }; // because of the Pointer member
130 // charMaps are stored in the shared memory that FontList objects point to,
131 // and are never deleted until the FontList (all referencing font lists,
132 // actually) have gone away.
133 mozilla::fontlist::FontList
* mList
;
134 mozilla::fontlist::Pointer mCharMap
;
138 // gfxPlatformFontList is an abstract class for the global font list on the
139 // system; concrete subclasses for each platform implement the actual interface
140 // to the system fonts. This class exists because we cannot rely on the platform
141 // font-finding APIs to behave in sensible/similar ways, particularly with rich,
142 // complex OpenType families, so we do our own font family/style management here
145 // Much of this is based on the old gfxQuartzFontCache, but adapted for use on
148 struct FontListSizes
{
149 uint32_t mFontListSize
; // size of the font list and dependent objects
150 // (font family and face names, etc), but NOT
151 // including the font table cache and the cmaps
153 mFontTableCacheSize
; // memory used for the gfxFontEntry table caches
154 uint32_t mCharMapsSize
; // memory used for cmap coverage info
155 uint32_t mLoaderSize
; // memory used for (platform-specific) loader
156 uint32_t mSharedSize
; // shared-memory use (reported by parent only)
159 class gfxUserFontSet
;
161 class gfxPlatformFontList
: public gfxFontInfoLoader
{
162 friend class InitOtherFamilyNamesRunnable
;
165 typedef mozilla::StretchRange StretchRange
;
166 typedef mozilla::SlantStyleRange SlantStyleRange
;
167 typedef mozilla::WeightRange WeightRange
;
168 typedef mozilla::intl::Script Script
;
170 using AutoLock
= mozilla::RecursiveMutexAutoLock
;
171 using AutoUnlock
= mozilla::RecursiveMutexAutoUnlock
;
173 // Class used to hold cached copies of the font-name prefs, so that they can
174 // be accessed from non-main-thread callers who are not allowed to touch the
175 // Preferences service.
176 class FontPrefs final
{
178 using HashMap
= nsTHashMap
<nsCStringHashKey
, nsCString
>;
181 ~FontPrefs() = default;
183 FontPrefs(const FontPrefs
& aOther
) = delete;
184 FontPrefs
& operator=(const FontPrefs
& aOther
) = delete;
186 // Lookup the font.name.<foo> or font.name-list.<foo> pref for a given
187 // generic+langgroup pair.
188 bool LookupName(const nsACString
& aPref
, nsACString
& aValue
) const;
189 bool LookupNameList(const nsACString
& aPref
, nsACString
& aValue
) const;
191 // Does the font.name-list.emoji pref have a user-set value?
192 bool EmojiHasUserValue() const { return mEmojiHasUserValue
; }
194 // Expose iterators over all the defined prefs of each type.
195 HashMap::ConstIterator
NameIter() const { return mFontName
.ConstIter(); }
196 HashMap::ConstIterator
NameListIter() const {
197 return mFontNameList
.ConstIter();
201 static constexpr char kNamePrefix
[] = "font.name.";
202 static constexpr char kNameListPrefix
[] = "font.name-list.";
207 HashMap mFontNameList
;
208 bool mEmojiHasUserValue
= false;
211 // For font family lists loaded from user preferences (prefs such as
212 // font.name-list.<generic>.<langGroup>) that map CSS generics to
213 // platform-specific font families.
214 typedef nsTArray
<FontFamily
> PrefFontList
;
216 // Return the global font-list singleton, or NULL if aMustInitialize is false
217 // and it has not yet been fully initialized.
218 static gfxPlatformFontList
* PlatformFontList(bool aMustInitialize
= true) {
219 if (!aMustInitialize
&&
220 !(sPlatformFontList
&& sPlatformFontList
->IsInitialized())) {
223 // If there is a font-list initialization thread, we need to let it run
224 // to completion before the font list can be used for anything else.
225 if (sInitFontListThread
) {
226 // If we're currently on the initialization thread, just continue;
227 // otherwise wait for it to finish.
228 if (IsInitFontListThread()) {
229 return sPlatformFontList
;
231 PR_JoinThread(sInitFontListThread
);
232 sInitFontListThread
= nullptr;
233 // If font-list initialization failed, the thread will have cleared
234 // the static sPlatformFontList pointer; we cannot proceed without any
236 if (!sPlatformFontList
) {
237 MOZ_CRASH("Could not initialize gfxPlatformFontList");
240 if (!sPlatformFontList
->IsInitialized()) {
241 if (!sPlatformFontList
->InitFontList()) {
242 MOZ_CRASH("Could not initialize gfxPlatformFontList");
245 return sPlatformFontList
;
248 void GetMissingFonts(nsTArray
<nsCString
>& aMissingFonts
);
249 void GetMissingFonts(nsCString
& aMissingFonts
);
251 static bool Initialize(gfxPlatformFontList
* aList
);
253 static void Shutdown() {
254 // Ensure any font-list initialization thread is finished before we delete
255 // the platform fontlist singleton, which that thread may try to use.
256 if (sInitFontListThread
&& !IsInitFontListThread()) {
257 PR_JoinThread(sInitFontListThread
);
258 sInitFontListThread
= nullptr;
260 delete sPlatformFontList
;
261 sPlatformFontList
= nullptr;
264 bool IsInitialized() const { return mFontlistInitCount
; }
266 virtual ~gfxPlatformFontList();
268 // Initialize font lists; return true on success, false if something fails.
271 void FontListChanged();
274 * Gathers (from a platform's underlying font system) the information needed
275 * to initialize a fontlist::Family with its Face members.
277 virtual void GetFacesInitDataForFamily(
278 const mozilla::fontlist::Family
* aFamily
,
279 nsTArray
<mozilla::fontlist::Face::InitData
>& aFaces
,
280 bool aLoadCmaps
) const {}
282 virtual void GetFontList(nsAtom
* aLangGroup
, const nsACString
& aGenericFamily
,
283 nsTArray
<nsString
>& aListOfFonts
);
285 // Pass false to notify content that the shared font list has been modified
286 // but not completely invalidated.
287 void UpdateFontList(bool aFullRebuild
= true);
289 void ClearLangGroupPrefFonts() {
290 AutoLock
lock(mLock
);
291 ClearLangGroupPrefFontsLocked();
293 virtual void ClearLangGroupPrefFontsLocked() MOZ_REQUIRES(mLock
);
295 void GetFontFamilyList(nsTArray
<RefPtr
<gfxFontFamily
>>& aFamilyArray
);
297 already_AddRefed
<gfxFont
> SystemFindFontForChar(
298 nsPresContext
* aPresContext
, uint32_t aCh
, uint32_t aNextCh
,
299 Script aRunScript
, eFontPresentation aPresentation
,
300 const gfxFontStyle
* aStyle
, FontVisibility
* aVisibility
);
302 // Flags to control optional behaviors in FindAndAddFamilies. The sense
303 // of the bit flags have been chosen such that the default parameter of
304 // FindFamiliesFlags(0) in FindFamily will give the most commonly-desired
305 // behavior, and only a few callsites need to explicitly pass other values.
306 enum class FindFamiliesFlags
{
307 // If set, "other" (e.g. localized) family names should be loaded
308 // immediately; if clear, InitOtherFamilyNames is allowed to defer
309 // loading to avoid blocking.
310 eForceOtherFamilyNamesLoading
= 1 << 0,
312 // If set, FindAndAddFamilies should not check for legacy "styled
313 // family" names to add to the font list. This is used to avoid
314 // a recursive search when using FindFamily to find a potential base
315 // family name for a styled variant.
316 eNoSearchForLegacyFamilyNames
= 1 << 1,
318 // If set, FindAndAddFamilies will not add a missing entry to
320 eNoAddToNamesMissedWhenSearching
= 1 << 2,
322 // If set, the family name was quoted and so must not be treated as a CSS
324 eQuotedFamilyName
= 1 << 3,
326 // If set, "hidden" font families (like ".SF NS Text" on macOS) are
327 // searched in addition to standard user-visible families.
328 eSearchHiddenFamilies
= 1 << 4,
331 // Find family(ies) matching aFamily and append to the aOutput array
332 // (there may be multiple results in the case of fontconfig aliases, etc).
333 // Return true if any match was found and appended, false if none.
334 bool FindAndAddFamilies(
335 nsPresContext
* aPresContext
, mozilla::StyleGenericFontFamily aGeneric
,
336 const nsACString
& aFamily
, nsTArray
<FamilyAndGeneric
>* aOutput
,
337 FindFamiliesFlags aFlags
, gfxFontStyle
* aStyle
= nullptr,
338 nsAtom
* aLanguage
= nullptr, gfxFloat aDevToCssSize
= 1.0);
340 virtual bool FindAndAddFamiliesLocked(
341 nsPresContext
* aPresContext
, mozilla::StyleGenericFontFamily aGeneric
,
342 const nsACString
& aFamily
, nsTArray
<FamilyAndGeneric
>* aOutput
,
343 FindFamiliesFlags aFlags
, gfxFontStyle
* aStyle
= nullptr,
344 nsAtom
* aLanguage
= nullptr, gfxFloat aDevToCssSize
= 1.0)
347 gfxFontEntry
* FindFontForFamily(nsPresContext
* aPresContext
,
348 const nsACString
& aFamily
,
349 const gfxFontStyle
* aStyle
);
351 mozilla::fontlist::FontList
* SharedFontList() const {
352 return mSharedFontList
.get();
355 // Create a handle for a single shmem block (identified by index) ready to
356 // be shared to the given processId.
357 void ShareFontListShmBlockToProcess(uint32_t aGeneration
, uint32_t aIndex
,
358 base::ProcessId aPid
,
359 mozilla::ipc::SharedMemory::Handle
* aOut
);
361 // Populate the array aBlocks with the complete list of shmem handles ready
362 // to be shared to the given processId.
363 void ShareFontListToProcess(
364 nsTArray
<mozilla::ipc::SharedMemory::Handle
>* aBlocks
,
365 base::ProcessId aPid
);
367 void ShmBlockAdded(uint32_t aGeneration
, uint32_t aIndex
,
368 mozilla::ipc::SharedMemory::Handle aHandle
);
370 mozilla::ipc::SharedMemory::Handle
ShareShmBlockToProcess(
371 uint32_t aIndex
, base::ProcessId aPid
);
373 void SetCharacterMap(uint32_t aGeneration
, uint32_t aFamilyIndex
, bool aAlias
,
374 uint32_t aFaceIndex
, const gfxSparseBitSet
& aMap
);
376 void SetupFamilyCharMap(uint32_t aGeneration
, uint32_t aIndex
, bool aAlias
);
378 // Start the async cmap loading process, if not already under way, from the
379 // given family index. (For use in any process that needs font lookups.)
380 void StartCmapLoadingFromFamily(uint32_t aStartIndex
);
382 // [Parent] Handle request from content process to start cmap loading.
383 void StartCmapLoading(uint32_t aGeneration
, uint32_t aStartIndex
);
385 void CancelLoadCmapsTask() {
386 if (mLoadCmapsRunnable
) {
387 mLoadCmapsRunnable
->Cancel();
388 mLoadCmapsRunnable
= nullptr;
392 // Populate aFamily with face records, and if aLoadCmaps is true, also load
393 // their character maps (rather than leaving this to be done lazily).
394 // Note that even when aFamily->IsInitialized() is true, it can make sense
395 // to call InitializeFamily again if passing aLoadCmaps=true, in order to
396 // ensure cmaps are loaded.
397 [[nodiscard
]] bool InitializeFamily(mozilla::fontlist::Family
* aFamily
,
398 bool aLoadCmaps
= false);
399 void InitializeFamily(uint32_t aGeneration
, uint32_t aFamilyIndex
,
402 // name lookup table methods
404 void AddOtherFamilyNames(gfxFontFamily
* aFamilyEntry
,
405 const nsTArray
<nsCString
>& aOtherFamilyNames
);
407 void AddFullname(gfxFontEntry
* aFontEntry
, const nsCString
& aFullname
) {
408 AutoLock
lock(mLock
);
409 AddFullnameLocked(aFontEntry
, aFullname
);
411 void AddFullnameLocked(gfxFontEntry
* aFontEntry
, const nsCString
& aFullname
)
414 void AddPostscriptName(gfxFontEntry
* aFontEntry
,
415 const nsCString
& aPostscriptName
) {
416 AutoLock
lock(mLock
);
417 AddPostscriptNameLocked(aFontEntry
, aPostscriptName
);
419 void AddPostscriptNameLocked(gfxFontEntry
* aFontEntry
,
420 const nsCString
& aPostscriptName
)
423 bool NeedFullnamePostscriptNames() { return mExtraNames
!= nullptr; }
426 * Read PSName and FullName of the given face, for src:local lookup,
427 * returning true if actually implemented and succeeded.
429 virtual bool ReadFaceNames(const mozilla::fontlist::Family
* aFamily
,
430 const mozilla::fontlist::Face
* aFace
,
431 nsCString
& aPSName
, nsCString
& aFullName
) {
435 // initialize localized family names
436 bool InitOtherFamilyNames(bool aDeferOtherFamilyNamesLoading
);
437 bool InitOtherFamilyNames(uint32_t aGeneration
, bool aDefer
);
439 // pure virtual functions, to be provided by concrete subclasses
441 // get the system default font family
442 FontFamily
GetDefaultFont(nsPresContext
* aPresContext
,
443 const gfxFontStyle
* aStyle
);
444 FontFamily
GetDefaultFontLocked(nsPresContext
* aPresContext
,
445 const gfxFontStyle
* aStyle
)
448 // get the "ultimate" default font, for use if the font list is otherwise
449 // unusable (e.g. in the middle of being updated)
450 gfxFontEntry
* GetDefaultFontEntry() {
451 AutoLock
lock(mLock
);
452 return mDefaultFontEntry
.get();
456 * Look up a font by name on the host platform.
458 * Note that the style attributes (weight, stretch, style) are NOT used in
459 * selecting the platform font, which is looked up by name only; these are
460 * values to be recorded in the new font entry.
462 virtual gfxFontEntry
* LookupLocalFont(nsPresContext
* aPresContext
,
463 const nsACString
& aFontName
,
464 WeightRange aWeightForEntry
,
465 StretchRange aStretchForEntry
,
466 SlantStyleRange aStyleForEntry
) = 0;
469 * Create a new platform font from downloaded data (@font-face).
471 * Note that the style attributes (weight, stretch, style) are NOT related
472 * (necessarily) to any values within the font resource itself; these are
473 * values to be recorded in the new font entry and used for face selection,
474 * in place of whatever inherent style attributes the resource may have.
476 * This method takes ownership of the data block passed in as aFontData,
477 * and must ensure it is free()'d when no longer required.
479 virtual gfxFontEntry
* MakePlatformFont(const nsACString
& aFontName
,
480 WeightRange aWeightForEntry
,
481 StretchRange aStretchForEntry
,
482 SlantStyleRange aStyleForEntry
,
483 const uint8_t* aFontData
,
484 uint32_t aLength
) = 0;
486 // get the standard family name on the platform for a given font name
487 // (platforms may override, eg Mac)
488 virtual bool GetStandardFamilyName(const nsCString
& aFontName
,
489 nsACString
& aFamilyName
);
491 // Get the localized family name for a given font family.
492 bool GetLocalizedFamilyName(const FontFamily
& aFamily
,
493 nsACString
& aFamilyName
);
495 // get the default font name which is available on the system from
496 // font.name-list.*. if there are no available fonts in the pref,
497 // returns an empty FamilyAndGeneric record.
498 FamilyAndGeneric
GetDefaultFontFamily(const nsACString
& aLangGroup
,
499 const nsACString
& aGenericFamily
);
501 virtual void AddSizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf
,
502 FontListSizes
* aSizes
) const;
503 virtual void AddSizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
,
504 FontListSizes
* aSizes
) const;
506 mozilla::fontlist::Pointer
GetShmemCharMap(const gfxSparseBitSet
* aCmap
) {
507 AutoLock
lock(mLock
);
508 return GetShmemCharMapLocked(aCmap
);
510 mozilla::fontlist::Pointer
GetShmemCharMapLocked(const gfxSparseBitSet
* aCmap
)
513 // Search for existing cmap that matches the input; return the input if no
515 already_AddRefed
<gfxCharacterMap
> FindCharMap(gfxCharacterMap
* aCmap
);
517 // Remove the cmap from the shared cmap set if it holds the only remaining
518 // reference to the object.
519 void MaybeRemoveCmap(gfxCharacterMap
* aCharMap
);
521 // Keep track of userfont sets to notify when global fontlist changes occur.
522 void AddUserFontSet(gfxUserFontSet
* aUserFontSet
) {
523 AutoLock
lock(mLock
);
524 mUserFontSetList
.Insert(aUserFontSet
);
527 void RemoveUserFontSet(gfxUserFontSet
* aUserFontSet
) {
528 AutoLock
lock(mLock
);
529 mUserFontSetList
.Remove(aUserFontSet
);
532 static const gfxFontEntry::ScriptRange sComplexScriptRanges
[];
534 void GetFontlistInitInfo(uint32_t& aNumInits
, uint32_t& aLoaderState
) {
535 aNumInits
= mFontlistInitCount
;
536 aLoaderState
= (uint32_t)mState
;
539 virtual void AddGenericFonts(nsPresContext
* aPresContext
,
540 mozilla::StyleGenericFontFamily aGenericType
,
542 nsTArray
<FamilyAndGeneric
>& aFamilyList
);
545 * Given a Face from the shared font list, return a gfxFontEntry usable
546 * by the current process. This returns a cached entry if available,
547 * otherwise it calls the (platform-specific) CreateFontEntry method to
548 * make one, and adds it to the cache.
550 gfxFontEntry
* GetOrCreateFontEntry(mozilla::fontlist::Face
* aFace
,
551 const mozilla::fontlist::Family
* aFamily
) {
552 AutoLock
lock(mLock
);
553 return GetOrCreateFontEntryLocked(aFace
, aFamily
);
555 gfxFontEntry
* GetOrCreateFontEntryLocked(
556 mozilla::fontlist::Face
* aFace
, const mozilla::fontlist::Family
* aFamily
)
559 const FontPrefs
* GetFontPrefs() const MOZ_REQUIRES(mLock
) {
560 return mFontPrefs
.get();
563 bool EmojiPrefHasUserValue() const {
564 AutoLock
lock(mLock
);
565 return mFontPrefs
->EmojiHasUserValue();
568 PrefFontList
* GetPrefFontsLangGroup(
569 nsPresContext
* aPresContext
, mozilla::StyleGenericFontFamily aGenericType
,
570 eFontPrefLang aPrefLang
) {
571 AutoLock
lock(mLock
);
572 return GetPrefFontsLangGroupLocked(aPresContext
, aGenericType
, aPrefLang
);
574 PrefFontList
* GetPrefFontsLangGroupLocked(
575 nsPresContext
* aPresContext
, mozilla::StyleGenericFontFamily aGenericType
,
576 eFontPrefLang aPrefLang
) MOZ_REQUIRES(mLock
);
578 // in some situations, need to make decisions about ambiguous characters, may
579 // need to look at multiple pref langs
580 void GetLangPrefs(eFontPrefLang aPrefLangs
[], uint32_t& aLen
,
581 eFontPrefLang aCharLang
, eFontPrefLang aPageLang
);
583 // convert a lang group to enum constant (i.e. "zh-TW" ==>
584 // eFontPrefLang_ChineseTW)
585 static eFontPrefLang
GetFontPrefLangFor(const char* aLang
);
587 // convert a lang group atom to enum constant
588 static eFontPrefLang
GetFontPrefLangFor(nsAtom
* aLang
);
590 // convert an enum constant to a lang group atom
591 static nsAtom
* GetLangGroupForPrefLang(eFontPrefLang aLang
);
593 // convert a enum constant to lang group string (i.e. eFontPrefLang_ChineseTW
595 static const char* GetPrefLangName(eFontPrefLang aLang
);
597 // map a char code to a font language for Preferences
598 static eFontPrefLang
GetFontPrefLangFor(uint32_t aCh
);
600 // returns true if a pref lang is CJK
601 static bool IsLangCJK(eFontPrefLang aLang
);
603 // helper method to add a pref lang to an array, if not already in array
604 static void AppendPrefLang(eFontPrefLang aPrefLangs
[], uint32_t& aLen
,
605 eFontPrefLang aAddLang
);
607 // default serif/sans-serif choice based on font.default.xxx prefs
608 mozilla::StyleGenericFontFamily
GetDefaultGeneric(eFontPrefLang aLang
);
610 // Returns true if the font family whitelist is not empty. In this case we
611 // ignore the "CSS visibility level"; only the given fonts are present in
612 // the browser's font list.
613 bool IsFontFamilyWhitelistActive() const {
614 return mFontFamilyWhitelistActive
;
617 static void FontWhitelistPrefChanged(const char* aPref
, void* aClosure
);
619 bool AddWithLegacyFamilyName(const nsACString
& aLegacyName
,
620 gfxFontEntry
* aFontEntry
,
621 FontVisibility aVisibility
);
623 static const char* GetGenericName(
624 mozilla::StyleGenericFontFamily aGenericType
);
626 bool SkipFontFallbackForChar(FontVisibility aVisibility
, uint32_t aCh
) const {
627 AutoLock
lock(mLock
);
628 return mCodepointsWithNoFonts
[aVisibility
].test(aCh
);
631 // Return whether the given font-family record should be visible to CSS,
632 // in a context with the given FontVisibility setting.
633 bool IsVisibleToCSS(const gfxFontFamily
& aFamily
,
634 FontVisibility aVisibility
) const;
635 bool IsVisibleToCSS(const mozilla::fontlist::Family
& aFamily
,
636 FontVisibility aVisibility
) const;
638 // (Re-)initialize the set of codepoints that we know cannot be rendered.
639 void InitializeCodepointsWithNoFonts() MOZ_REQUIRES(mLock
);
641 // If using the shared font list, returns a generation count that is
642 // incremented if/when the platform list is reinitialized (e.g. because
643 // fonts are installed/removed while the browser is running), such that
644 // existing references to shared font family or face objects and character
645 // maps will no longer be valid.
646 // (The legacy (non-shared) list just returns 0 here.)
647 uint32_t GetGeneration() const;
649 // Sometimes we need to know if we're on the InitFontList startup thread.
650 static bool IsInitFontListThread() {
651 return PR_GetCurrentThread() == sInitFontListThread
;
654 bool IsKnownIconFontFamily(const nsAtom
* aFamilyName
) const;
655 void LoadIconFontOverrideList();
657 void Lock() MOZ_CAPABILITY_ACQUIRE(mLock
) { mLock
.Lock(); }
658 void Unlock() MOZ_CAPABILITY_RELEASE(mLock
) { mLock
.Unlock(); }
660 // This is only public because some external callers want to be able to
661 // assert about the locked status.
662 mutable mozilla::RecursiveMutex mLock
;
665 virtual nsTArray
<std::pair
<const char**, uint32_t>>
666 GetFilteredPlatformFontLists() = 0;
668 friend class mozilla::fontlist::FontList
;
669 friend class InitOtherFamilyNamesForStylo
;
672 static bool FamilyInList(const nsACString
& aName
, const char* (&aList
)[N
]) {
673 return FamilyInList(aName
, aList
, N
);
675 static bool FamilyInList(const nsACString
& aName
, const char* aList
[],
678 // Check list is correctly sorted (in debug build only; no-op on release).
680 static void CheckFamilyList(const char* (&aList
)[N
]) {
681 CheckFamilyList(aList
, N
);
683 static void CheckFamilyList(const char* aList
[], size_t aCount
);
685 class InitOtherFamilyNamesRunnable
: public mozilla::CancelableRunnable
{
687 InitOtherFamilyNamesRunnable()
688 : CancelableRunnable(
689 "gfxPlatformFontList::InitOtherFamilyNamesRunnable"),
690 mIsCanceled(false) {}
692 NS_IMETHOD
Run() override
{
697 gfxPlatformFontList
* fontList
= gfxPlatformFontList::PlatformFontList();
702 fontList
->InitOtherFamilyNamesInternal(true);
707 nsresult
Cancel() override
{
717 class MemoryReporter final
: public nsIMemoryReporter
{
718 ~MemoryReporter() = default;
722 NS_DECL_NSIMEMORYREPORTER
725 class PrefName final
: public nsAutoCString
{
726 void Init(const nsACString
& aGeneric
, const nsACString
& aLangGroup
) {
728 if (!aLangGroup
.IsEmpty()) {
735 PrefName(const nsACString
& aGeneric
, const nsACString
& aLangGroup
) {
736 Init(aGeneric
, aLangGroup
);
739 PrefName(const char* aGeneric
, const char* aLangGroup
) {
740 Init(nsDependentCString(aGeneric
), nsDependentCString(aLangGroup
));
743 PrefName(const char* aGeneric
, nsAtom
* aLangGroup
) {
745 Init(nsDependentCString(aGeneric
), nsAtomCString(aLangGroup
));
747 Init(nsDependentCString(aGeneric
), nsAutoCString());
752 explicit gfxPlatformFontList(bool aNeedFullnamePostscriptNames
= true);
754 static gfxPlatformFontList
* sPlatformFontList
;
757 * Convenience method to return the first matching family (if any) as found
758 * by FindAndAddFamilies(). The family will be initialized (synchronously)
759 * if this has not already been done, so the returned pointer, if non-null,
762 mozilla::fontlist::Family
* FindSharedFamily(
763 nsPresContext
* aPresContext
, const nsACString
& aFamily
,
764 FindFamiliesFlags aFlags
= FindFamiliesFlags(0),
765 gfxFontStyle
* aStyle
= nullptr, nsAtom
* aLanguage
= nullptr,
766 gfxFloat aDevToCssSize
= 1.0) MOZ_REQUIRES(mLock
);
768 gfxFontFamily
* FindUnsharedFamily(
769 nsPresContext
* aPresContext
, const nsACString
& aFamily
,
770 FindFamiliesFlags aFlags
= FindFamiliesFlags(0),
771 gfxFontStyle
* aStyle
= nullptr, nsAtom
* aLanguage
= nullptr,
772 gfxFloat aDevToCssSize
= 1.0) MOZ_REQUIRES(mLock
) {
773 if (SharedFontList()) {
776 AutoTArray
<FamilyAndGeneric
, 1> families
;
777 if (FindAndAddFamiliesLocked(
778 aPresContext
, mozilla::StyleGenericFontFamily::None
, aFamily
,
779 &families
, aFlags
, aStyle
, aLanguage
, aDevToCssSize
)) {
780 return families
[0].mFamily
.mUnshared
;
785 FontFamily
FindFamily(nsPresContext
* aPresContext
, const nsACString
& aFamily
,
786 FindFamiliesFlags aFlags
= FindFamiliesFlags(0),
787 gfxFontStyle
* aStyle
= nullptr,
788 nsAtom
* aLanguage
= nullptr,
789 gfxFloat aDevToCssSize
= 1.0) MOZ_REQUIRES(mLock
) {
790 if (SharedFontList()) {
791 return FontFamily(FindSharedFamily(aPresContext
, aFamily
, aFlags
, aStyle
,
792 aLanguage
, aDevToCssSize
));
794 return FontFamily(FindUnsharedFamily(aPresContext
, aFamily
, aFlags
, aStyle
,
795 aLanguage
, aDevToCssSize
));
798 // Lookup family name in global family list without substitutions or
799 // localized family name lookup. Used for common font fallback families.
800 gfxFontFamily
* FindFamilyByCanonicalName(const nsACString
& aFamily
)
801 MOZ_REQUIRES(mLock
) {
803 gfxFontFamily
* familyEntry
;
804 GenerateFontListKey(aFamily
, key
);
805 if ((familyEntry
= mFontFamilies
.GetWeak(key
))) {
806 return CheckFamily(familyEntry
);
811 // returns default font for a given character, null otherwise
812 already_AddRefed
<gfxFont
> CommonFontFallback(nsPresContext
* aPresContext
,
813 uint32_t aCh
, uint32_t aNextCh
,
815 eFontPresentation aPresentation
,
816 const gfxFontStyle
* aMatchStyle
,
817 FontFamily
& aMatchedFamily
)
820 // Search fonts system-wide for a given character, null if not found.
821 already_AddRefed
<gfxFont
> GlobalFontFallback(
822 nsPresContext
* aPresContext
, uint32_t aCh
, uint32_t aNextCh
,
823 Script aRunScript
, eFontPresentation aPresentation
,
824 const gfxFontStyle
* aMatchStyle
, uint32_t& aCmapCount
,
825 FontFamily
& aMatchedFamily
) MOZ_REQUIRES(mLock
);
827 // Platform-specific implementation of global font fallback, if any;
828 // this may return nullptr in which case the default cmap-based fallback
829 // will be performed.
830 virtual gfxFontEntry
* PlatformGlobalFontFallback(
831 nsPresContext
* aPresContext
, const uint32_t aCh
, Script aRunScript
,
832 const gfxFontStyle
* aMatchStyle
, FontFamily
& aMatchedFamily
) {
836 // whether system-based font fallback is used or not
837 // if system fallback is used, no need to load all cmaps
838 virtual bool UsesSystemFallback() { return false; }
840 void AppendCJKPrefLangs(eFontPrefLang aPrefLangs
[], uint32_t& aLen
,
841 eFontPrefLang aCharLang
, eFontPrefLang aPageLang
)
844 // verifies that a family contains a non-zero font count
845 gfxFontFamily
* CheckFamily(gfxFontFamily
* aFamily
) MOZ_REQUIRES(mLock
);
847 // initialize localized family names
848 void InitOtherFamilyNamesInternal(bool aDeferOtherFamilyNamesLoading
);
849 void CancelInitOtherFamilyNamesTask();
851 void AddToMissedNames(const nsCString
& aKey
) MOZ_REQUIRES(mLock
);
853 // search through font families, looking for a given name, initializing
854 // facename lists along the way. first checks all families with names
855 // close to face name, then searchs all families if not found.
856 gfxFontEntry
* SearchFamiliesForFaceName(const nsACString
& aFaceName
)
859 // helper method for finding fullname/postscript names in facename lists
860 gfxFontEntry
* FindFaceName(const nsACString
& aFaceName
) MOZ_REQUIRES(mLock
);
862 // look up a font by name, for cases where platform font list
863 // maintains explicit mappings of fullname/psname ==> font
864 virtual gfxFontEntry
* LookupInFaceNameLists(const nsACString
& aFaceName
)
867 gfxFontEntry
* LookupInSharedFaceNameList(nsPresContext
* aPresContext
,
868 const nsACString
& aFaceName
,
869 WeightRange aWeightForEntry
,
870 StretchRange aStretchForEntry
,
871 SlantStyleRange aStyleForEntry
)
874 // Add an entry for aName to the local names table, but only if it is not
875 // already present, or aName and aData.mFamilyName look like a better match
876 // than the existing entry.
877 void MaybeAddToLocalNameTable(
878 const nsACString
& aName
,
879 const mozilla::fontlist::LocalFaceRec::InitData
& aData
)
882 // load the bad underline blocklist from pref.
883 void LoadBadUnderlineList();
885 // This version of the function will not modify aKeyName
886 void GenerateFontListKey(const nsACString
& aKeyName
, nsACString
& aResult
);
887 // This version of the function WILL modify aKeyName
888 void GenerateFontListKey(nsACString
& aKeyName
);
890 virtual void GetFontFamilyNames(nsTArray
<nsCString
>& aFontFamilyNames
)
893 // helper function to map lang to lang group
894 nsAtom
* GetLangGroup(nsAtom
* aLanguage
);
896 // gfxFontInfoLoader overrides, used to load in font cmaps
897 void InitLoader() MOZ_REQUIRES(mLock
) override
;
898 bool LoadFontInfo() override
;
899 void CleanupLoader() override
;
901 void ForceGlobalReflowLocked(
902 gfxPlatform::NeedsReframe aNeedsReframe
,
903 gfxPlatform::BroadcastToChildren aBroadcastToChildren
=
904 gfxPlatform::BroadcastToChildren::Yes
) MOZ_REQUIRES(mLock
);
906 // read the loader initialization prefs, and start it
907 void GetPrefsAndStartLoader();
909 // If aForgetLocalFaces is true, all gfxFontEntries for src:local fonts must
910 // be discarded (not potentially reused to satisfy the rebuilt rules),
911 // because they may no longer be valid.
912 void RebuildLocalFonts(bool aForgetLocalFaces
= false) MOZ_REQUIRES(mLock
);
914 void ResolveGenericFontNames(nsPresContext
* aPresContext
,
915 mozilla::StyleGenericFontFamily aGenericType
,
916 eFontPrefLang aPrefLang
,
917 PrefFontList
* aGenericFamilies
)
920 void ResolveEmojiFontNames(nsPresContext
* aPresContext
,
921 PrefFontList
* aGenericFamilies
)
924 void GetFontFamiliesFromGenericFamilies(
925 nsPresContext
* aPresContext
, mozilla::StyleGenericFontFamily aGenericType
,
926 nsTArray
<nsCString
>& aGenericNameFamilies
, nsAtom
* aLangGroup
,
927 PrefFontList
* aFontFamilies
) MOZ_REQUIRES(mLock
);
929 virtual nsresult
InitFontListForPlatform() MOZ_REQUIRES(mLock
) = 0;
930 virtual void InitSharedFontListForPlatform() MOZ_REQUIRES(mLock
) {}
932 virtual gfxFontEntry
* CreateFontEntry(
933 mozilla::fontlist::Face
* aFace
,
934 const mozilla::fontlist::Family
* aFamily
) {
939 * Methods to apply the font.system.whitelist anti-fingerprinting pref,
940 * by filtering the list of installed fonts so that only whitelisted families
942 * There are separate implementations of this for the per-process font list
943 * and for the shared-memory font list.
945 void ApplyWhitelist() MOZ_REQUIRES(mLock
);
946 void ApplyWhitelist(nsTArray
<mozilla::fontlist::Family::InitData
>& aFamilies
);
948 // Create a new gfxFontFamily of the appropriate subclass for the platform,
949 // used when AddWithLegacyFamilyName needs to create a new family.
950 virtual gfxFontFamily
* CreateFontFamily(const nsACString
& aName
,
951 FontVisibility aVisibility
) const = 0;
954 * For the post-startup font info loader task.
955 * Perform platform-specific work to read alternate names (if any) for a
956 * font family, recording them in mAliasTable. Once alternate names have been
957 * loaded for all families, the accumulated records are stored in the shared
958 * font list's mAliases list.
959 * Some platforms (currently Linux/fontconfig) may load alternate names as
960 * part of initially populating the font list with family records, in which
961 * case this method is unused.
963 virtual void ReadFaceNamesForFamily(mozilla::fontlist::Family
* aFamily
,
964 bool aNeedFullnamePostscriptNames
)
965 MOZ_REQUIRES(mLock
) {}
967 typedef nsRefPtrHashtable
<nsCStringHashKey
, gfxFontFamily
> FontFamilyTable
;
968 typedef nsRefPtrHashtable
<nsCStringHashKey
, gfxFontEntry
> FontEntryTable
;
970 // used by memory reporter to accumulate sizes of family names in the table
971 static size_t SizeOfFontFamilyTableExcludingThis(
972 const FontFamilyTable
& aTable
, mozilla::MallocSizeOf aMallocSizeOf
);
973 static size_t SizeOfFontEntryTableExcludingThis(
974 const FontEntryTable
& aTable
, mozilla::MallocSizeOf aMallocSizeOf
);
976 // Platform-specific helper for GetDefaultFont(...).
977 virtual FontFamily
GetDefaultFontForPlatform(nsPresContext
* aPresContext
,
978 const gfxFontStyle
* aStyle
,
979 nsAtom
* aLanguage
= nullptr)
980 MOZ_REQUIRES(mLock
) = 0;
982 // canonical family name ==> family entry (unique, one name per family entry)
983 FontFamilyTable mFontFamilies
MOZ_GUARDED_BY(mLock
);
985 // other family name ==> family entry (not unique, can have multiple names per
986 // family entry, only names *other* than the canonical names are stored here)
987 FontFamilyTable mOtherFamilyNames
MOZ_GUARDED_BY(mLock
);
989 // flag set after InitOtherFamilyNames is called upon first name lookup miss
990 mozilla::Atomic
<bool> mOtherFamilyNamesInitialized
;
992 // The pending InitOtherFamilyNames() task.
993 RefPtr
<mozilla::CancelableRunnable
> mPendingOtherFamilyNameTask
;
995 // flag set after fullname and Postcript name lists are populated
996 mozilla::Atomic
<bool> mFaceNameListsInitialized
;
999 ExtraNames() = default;
1001 // fullname ==> font entry (unique, one name per font entry)
1002 FontEntryTable mFullnames
{64};
1003 // Postscript name ==> font entry (unique, one name per font entry)
1004 FontEntryTable mPostscriptNames
{64};
1006 // The lock is needed to guard access to the actual name tables, but does not
1007 // need to be held to just test whether mExtraNames is non-null as it is set
1008 // during initialization before other threads have a chance to see it.
1009 mozilla::UniquePtr
<ExtraNames
> mExtraNames
MOZ_PT_GUARDED_BY(mLock
);
1011 // face names missed when face name loading takes a long time
1012 mozilla::UniquePtr
<nsTHashSet
<nsCString
>> mFaceNamesMissed
1013 MOZ_GUARDED_BY(mLock
);
1015 // localized family names missed when face name loading takes a long time
1016 mozilla::UniquePtr
<nsTHashSet
<nsCString
>> mOtherNamesMissed
1017 MOZ_GUARDED_BY(mLock
);
1019 typedef mozilla::RangedArray
<mozilla::UniquePtr
<PrefFontList
>,
1020 size_t(mozilla::StyleGenericFontFamily::None
),
1022 mozilla::StyleGenericFontFamily::MozEmoji
)>
1023 PrefFontsForLangGroup
;
1024 mozilla::RangedArray
<PrefFontsForLangGroup
, eFontPrefLang_First
,
1025 eFontPrefLang_Count
>
1026 mLangGroupPrefFonts
MOZ_GUARDED_BY(mLock
);
1027 mozilla::UniquePtr
<PrefFontList
> mEmojiPrefFont
MOZ_GUARDED_BY(mLock
);
1029 // When system-wide font lookup fails for a character, cache it to skip future
1030 // searches. This is an array of bitsets, one for each FontVisibility level.
1031 mozilla::EnumeratedArray
<FontVisibility
, gfxSparseBitSet
,
1032 size_t(FontVisibility::Count
)>
1033 mCodepointsWithNoFonts
MOZ_GUARDED_BY(mLock
);
1035 // the family to use for U+FFFD fallback, to avoid expensive search every time
1036 // on pages with lots of problems
1037 mozilla::EnumeratedArray
<FontVisibility
, FontFamily
,
1038 size_t(FontVisibility::Count
)>
1039 mReplacementCharFallbackFamily
MOZ_GUARDED_BY(mLock
);
1041 // Sorted array of lowercased family names; use ContainsSorted to test
1042 nsTArray
<nsCString
> mBadUnderlineFamilyNames
;
1044 // character map data shared across families
1045 // contains weak ptrs to cmaps shared by font entry objects
1046 nsTHashtable
<CharMapHashKey
> mSharedCmaps
MOZ_GUARDED_BY(mLock
);
1048 nsTHashtable
<ShmemCharMapHashEntry
> mShmemCharMaps
MOZ_GUARDED_BY(mLock
);
1050 // data used as part of the font cmap loading process
1051 nsTArray
<RefPtr
<gfxFontFamily
>> mFontFamiliesToLoad
MOZ_GUARDED_BY(mLock
);
1052 uint32_t mStartIndex
MOZ_GUARDED_BY(mLock
) = 0;
1053 uint32_t mNumFamilies
MOZ_GUARDED_BY(mLock
) = 0;
1055 // xxx - info for diagnosing no default font aborts
1056 // see bugs 636957, 1070983, 1189129
1057 uint32_t mFontlistInitCount
= 0; // num times InitFontList called
1059 nsTHashSet
<gfxUserFontSet
*> mUserFontSetList
MOZ_GUARDED_BY(mLock
);
1061 nsLanguageAtomService
* mLangService
= nullptr;
1063 nsTArray
<uint32_t> mCJKPrefLangs
MOZ_GUARDED_BY(mLock
);
1064 nsTArray
<mozilla::StyleGenericFontFamily
> mDefaultGenericsLangGroup
1065 MOZ_GUARDED_BY(mLock
);
1067 nsTArray
<nsCString
> mEnabledFontsList
;
1068 nsTHashSet
<nsCString
> mIconFontsSet
;
1070 mozilla::UniquePtr
<mozilla::fontlist::FontList
> mSharedFontList
;
1072 nsClassHashtable
<nsCStringHashKey
, mozilla::fontlist::AliasData
> mAliasTable
;
1073 nsTHashMap
<nsCStringHashKey
, mozilla::fontlist::LocalFaceRec::InitData
>
1076 nsRefPtrHashtable
<nsPtrHashKey
<const mozilla::fontlist::Face
>, gfxFontEntry
>
1077 mFontEntries
MOZ_GUARDED_BY(mLock
);
1079 mozilla::UniquePtr
<FontPrefs
> mFontPrefs
;
1081 RefPtr
<gfxFontEntry
> mDefaultFontEntry
MOZ_GUARDED_BY(mLock
);
1083 RefPtr
<mozilla::CancelableRunnable
> mLoadCmapsRunnable
;
1084 uint32_t mStartedLoadingCmapsFrom
MOZ_GUARDED_BY(mLock
) = 0xffffffffu
;
1086 bool mFontFamilyWhitelistActive
= false;
1088 static PRThread
* sInitFontListThread
;
1091 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(gfxPlatformFontList::FindFamiliesFlags
)
1093 #endif /* GFXPLATFORMFONTLIST_H_ */