StaticPresData.h (6693B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_StaticPresData_h 8 #define mozilla_StaticPresData_h 9 10 #include "mozilla/StaticPtr.h" 11 #include "mozilla/UniquePtr.h" 12 #include "nsAtom.h" 13 #include "nsCOMPtr.h" 14 #include "nsCoord.h" 15 #include "nsFont.h" 16 #include "nsLanguageAtomService.h" 17 18 namespace mozilla { 19 20 struct LangGroupFontPrefs { 21 // Font sizes default to zero; they will be set in GetFontPreferences 22 LangGroupFontPrefs() 23 : mLangGroup(nullptr), 24 mMinimumFontSize({0}), 25 mDefaultVariableFont(StyleGenericFontFamily::Serif, {0}), 26 mDefaultSerifFont(StyleGenericFontFamily::Serif, {0}), 27 mDefaultSansSerifFont(StyleGenericFontFamily::SansSerif, {0}), 28 mDefaultMonospaceFont(StyleGenericFontFamily::Monospace, {0}), 29 mDefaultCursiveFont(StyleGenericFontFamily::Cursive, {0}), 30 mDefaultFantasyFont(StyleGenericFontFamily::Fantasy, {0}), 31 mDefaultSystemUiFont(StyleGenericFontFamily::SystemUi, {0}) {} 32 33 StyleGenericFontFamily GetDefaultGeneric() const { 34 return mDefaultVariableFont.family.families.list.AsSpan()[0].AsGeneric(); 35 } 36 37 void Reset() { 38 // Throw away any other LangGroupFontPrefs objects: 39 mNext = nullptr; 40 41 // Make GetFontPreferences reinitialize mLangGroupFontPrefs: 42 mLangGroup = nullptr; 43 } 44 45 // Initialize this with the data for a given language 46 void Initialize(nsStaticAtom* aLangGroupAtom); 47 48 /** 49 * Get the default font for the given language and generic font ID. 50 * aLanguage may not be nullptr. 51 * 52 * This object is read-only, you must copy the font to modify it. 53 * 54 * For aFontID corresponding to a CSS Generic, the nsFont returned has 55 * its name set to that generic font's name, and its size set to 56 * the user's preference for font size for that generic and the 57 * given language. 58 */ 59 const nsFont* GetDefaultFont(StyleGenericFontFamily aFamily) const { 60 switch (aFamily) { 61 // Special (our default variable width font and fixed width font) 62 case StyleGenericFontFamily::None: 63 return &mDefaultVariableFont; 64 // CSS 65 case StyleGenericFontFamily::Serif: 66 return &mDefaultSerifFont; 67 case StyleGenericFontFamily::SansSerif: 68 return &mDefaultSansSerifFont; 69 case StyleGenericFontFamily::Monospace: 70 return &mDefaultMonospaceFont; 71 case StyleGenericFontFamily::Cursive: 72 return &mDefaultCursiveFont; 73 case StyleGenericFontFamily::Fantasy: 74 return &mDefaultFantasyFont; 75 case StyleGenericFontFamily::Math: 76 // TODO(eri): Change to a specific math font once the generic 77 // math family is no longer mapped to "serif.x-math". See 78 // `gfxPlatformFontList::AddGenericFonts`. 79 return &mDefaultSerifFont; 80 case StyleGenericFontFamily::SystemUi: 81 return &mDefaultSystemUiFont; 82 case StyleGenericFontFamily::MozEmoji: 83 // This shouldn't appear in font family names. 84 break; 85 } 86 MOZ_ASSERT_UNREACHABLE("invalid font id"); 87 return nullptr; 88 } 89 90 nsStaticAtom* mLangGroup; 91 Length mMinimumFontSize; 92 nsFont mDefaultVariableFont; 93 nsFont mDefaultSerifFont; 94 nsFont mDefaultSansSerifFont; 95 nsFont mDefaultMonospaceFont; 96 nsFont mDefaultCursiveFont; 97 nsFont mDefaultFantasyFont; 98 nsFont mDefaultSystemUiFont; 99 UniquePtr<LangGroupFontPrefs> mNext; 100 }; 101 102 /** 103 * Some functionality that has historically lived on nsPresContext does not 104 * actually need to be per-document. This singleton class serves as a host 105 * for that functionality. We delegate to it from nsPresContext where 106 * appropriate, and use it standalone in some cases as well. 107 */ 108 class StaticPresData { 109 public: 110 // Initialization and shutdown of the singleton. Called exactly once. 111 static void Init(); 112 static void Shutdown(); 113 114 // Gets an instance of the singleton. Infallible between the calls to Init 115 // and Shutdown. 116 static StaticPresData* Get(); 117 118 /** 119 * Given a language, get the language group name, which can 120 * be used as an argument to LangGroupFontPrefs::Initialize() 121 */ 122 nsStaticAtom* GetLangGroup(nsAtom* aLanguage) const; 123 124 /** 125 * Fetch the user's font preferences for the given aLanguage's 126 * langugage group. 127 * 128 * The original code here is pretty old, and includes an optimization 129 * whereby language-specific prefs are read per-document, and the 130 * results are stored in a linked list, which is assumed to be very short 131 * since most documents only ever use one language. 132 * 133 * Storing this per-session rather than per-document would almost certainly 134 * be fine. But just to be on the safe side, we leave the old mechanism as-is, 135 * with an additional per-session cache that new callers can use if they don't 136 * have a PresContext. 137 * 138 * aNeedsToCache is used for two things. If null, it indicates that 139 * it is safe for the StaticPresData to cache the result of the 140 * prefs lookup, either because we're on the main thread, 141 * or because we're on a style worker thread but the font lock has 142 * been acquired. If non-null, it indicates that it's not safe to 143 * cache the result of the prefs lookup (because we're on 144 * a style worker thread without the lock acquired). In this case, 145 * GetFontPrefsForLang will store true in *aNeedsToCache true if we 146 * would have cached the result of a new lookup, and false if we 147 * were able to use an existing cached result. Thus, callers that 148 * get a true *aNeedsToCache outparam value should make an effort 149 * to re-call GetFontPrefsForLang when it is safe to cache, to avoid 150 * recomputing the prefs again later. 151 */ 152 const LangGroupFontPrefs* GetFontPrefsForLang(nsAtom* aLanguage, 153 bool* aNeedsToCache = nullptr); 154 const nsFont* GetDefaultFont(uint8_t aFontID, nsAtom* aLanguage, 155 const LangGroupFontPrefs* aPrefs) const; 156 157 void InvalidateFontPrefs() { mLangGroupFontPrefs.Reset(); } 158 159 private: 160 // Private constructor/destructor, to prevent other code from inadvertently 161 // instantiating or deleting us. (Though we need to declare StaticAutoPtr as 162 // a friend to give it permission.) 163 StaticPresData(); 164 ~StaticPresData() = default; 165 friend class StaticAutoPtr<StaticPresData>; 166 167 nsLanguageAtomService* mLangService; 168 LangGroupFontPrefs mLangGroupFontPrefs; 169 }; 170 171 } // namespace mozilla 172 173 #endif // mozilla_StaticPresData_h