Bug 1879449 [wpt PR 44489] - [wptrunner] Add `infrastructure/expected-fail/` test...
[gecko.git] / layout / base / StaticPresData.h
blob94763767962cfe52e4086c93fd83b1ed74995777
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/. */
7 #ifndef mozilla_StaticPresData_h
8 #define mozilla_StaticPresData_h
10 #include "mozilla/StaticPtr.h"
11 #include "mozilla/UniquePtr.h"
12 #include "nsCoord.h"
13 #include "nsCOMPtr.h"
14 #include "nsFont.h"
15 #include "nsAtom.h"
16 #include "nsLanguageAtomService.h"
18 namespace mozilla {
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}) {}
33 StyleGenericFontFamily GetDefaultGeneric() const {
34 return mDefaultVariableFont.family.families.list.AsSpan()[0].AsGeneric();
37 void Reset() {
38 // Throw away any other LangGroupFontPrefs objects:
39 mNext = nullptr;
41 // Make GetFontPreferences reinitialize mLangGroupFontPrefs:
42 mLangGroup = nullptr;
45 // Initialize this with the data for a given language
46 void Initialize(nsStaticAtom* aLangGroupAtom);
48 /**
49 * Get the default font for the given language and generic font ID.
50 * aLanguage may not be nullptr.
52 * This object is read-only, you must copy the font to modify it.
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.
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::SystemUi:
76 return &mDefaultSystemUiFont;
77 case StyleGenericFontFamily::MozEmoji:
78 // This shouldn't appear in font family names.
79 break;
81 MOZ_ASSERT_UNREACHABLE("invalid font id");
82 return nullptr;
85 nsStaticAtom* mLangGroup;
86 Length mMinimumFontSize;
87 nsFont mDefaultVariableFont;
88 nsFont mDefaultSerifFont;
89 nsFont mDefaultSansSerifFont;
90 nsFont mDefaultMonospaceFont;
91 nsFont mDefaultCursiveFont;
92 nsFont mDefaultFantasyFont;
93 nsFont mDefaultSystemUiFont;
94 UniquePtr<LangGroupFontPrefs> mNext;
97 /**
98 * Some functionality that has historically lived on nsPresContext does not
99 * actually need to be per-document. This singleton class serves as a host
100 * for that functionality. We delegate to it from nsPresContext where
101 * appropriate, and use it standalone in some cases as well.
103 class StaticPresData {
104 public:
105 // Initialization and shutdown of the singleton. Called exactly once.
106 static void Init();
107 static void Shutdown();
109 // Gets an instance of the singleton. Infallible between the calls to Init
110 // and Shutdown.
111 static StaticPresData* Get();
114 * Given a language, get the language group name, which can
115 * be used as an argument to LangGroupFontPrefs::Initialize()
117 * aNeedsToCache is used for two things. If null, it indicates that
118 * the nsLanguageAtomService is safe to cache the result of the
119 * language group lookup, either because we're on the main thread,
120 * or because we're on a style worker thread but the font lock has
121 * been acquired. If non-null, it indicates that it's not safe to
122 * cache the result of the language group lookup (because we're on
123 * a style worker thread without the lock acquired). In this case,
124 * GetLanguageGroup will store true in *aNeedsToCache true if we
125 * would have cached the result of a new lookup, and false if we
126 * were able to use an existing cached result. Thus, callers that
127 * get a true *aNeedsToCache outparam value should make an effort
128 * to re-call GetLanguageGroup when it is safe to cache, to avoid
129 * recomputing the language group again later.
131 nsStaticAtom* GetLangGroup(nsAtom* aLanguage,
132 bool* aNeedsToCache = nullptr) const;
135 * Same as GetLangGroup, but will not cache the result
137 nsStaticAtom* GetUncachedLangGroup(nsAtom* aLanguage) const;
140 * Fetch the user's font preferences for the given aLanguage's
141 * langugage group.
143 * The original code here is pretty old, and includes an optimization
144 * whereby language-specific prefs are read per-document, and the
145 * results are stored in a linked list, which is assumed to be very short
146 * since most documents only ever use one language.
148 * Storing this per-session rather than per-document would almost certainly
149 * be fine. But just to be on the safe side, we leave the old mechanism as-is,
150 * with an additional per-session cache that new callers can use if they don't
151 * have a PresContext.
153 * See comment on GetLangGroup for the usage of aNeedsToCache.
155 const LangGroupFontPrefs* GetFontPrefsForLang(nsAtom* aLanguage,
156 bool* aNeedsToCache = nullptr);
157 const nsFont* GetDefaultFont(uint8_t aFontID, nsAtom* aLanguage,
158 const LangGroupFontPrefs* aPrefs) const;
160 void InvalidateFontPrefs() { mLangGroupFontPrefs.Reset(); }
162 private:
163 // Private constructor/destructor, to prevent other code from inadvertently
164 // instantiating or deleting us. (Though we need to declare StaticAutoPtr as
165 // a friend to give it permission.)
166 StaticPresData();
167 ~StaticPresData() = default;
168 friend class StaticAutoPtr<StaticPresData>;
170 nsLanguageAtomService* mLangService;
171 LangGroupFontPrefs mLangGroupFontPrefs;
174 } // namespace mozilla
176 #endif // mozilla_StaticPresData_h