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/UniquePtr.h"
15 #include "nsLanguageAtomService.h"
19 struct LangGroupFontPrefs
{
20 // Font sizes default to zero; they will be set in GetFontPreferences
22 : mLangGroup(nullptr),
23 mMinimumFontSize({0}),
24 mDefaultVariableFont(),
25 mDefaultSerifFont(StyleGenericFontFamily::Serif
, {0}),
26 mDefaultSansSerifFont(StyleGenericFontFamily::SansSerif
, {0}),
27 mDefaultMonospaceFont(StyleGenericFontFamily::Monospace
, {0}),
28 mDefaultCursiveFont(StyleGenericFontFamily::Cursive
, {0}),
29 mDefaultFantasyFont(StyleGenericFontFamily::Fantasy
, {0}) {
30 mDefaultVariableFont
.fontlist
.SetDefaultFontType(
31 StyleGenericFontFamily::Serif
);
32 // We create mDefaultVariableFont.fontlist with defaultType as the
33 // fallback font, and not as part of the font list proper. This way,
34 // it can be overwritten should there be a language change.
38 // Throw away any other LangGroupFontPrefs objects:
41 // Make GetFontPreferences reinitialize mLangGroupFontPrefs:
45 // Initialize this with the data for a given language
46 void Initialize(nsStaticAtom
* aLangGroupAtom
);
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
59 const nsFont
* GetDefaultFont(StyleGenericFontFamily aFamily
) const {
61 // Special (our default variable width font and fixed width font)
62 case StyleGenericFontFamily::None
:
63 return &mDefaultVariableFont
;
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::MozEmoji
:
76 // This shouldn't appear in font family names.
79 MOZ_ASSERT_UNREACHABLE("invalid font id");
83 nsStaticAtom
* mLangGroup
;
84 Length mMinimumFontSize
;
85 nsFont mDefaultVariableFont
;
86 nsFont mDefaultSerifFont
;
87 nsFont mDefaultSansSerifFont
;
88 nsFont mDefaultMonospaceFont
;
89 nsFont mDefaultCursiveFont
;
90 nsFont mDefaultFantasyFont
;
91 UniquePtr
<LangGroupFontPrefs
> mNext
;
95 * Some functionality that has historically lived on nsPresContext does not
96 * actually need to be per-document. This singleton class serves as a host
97 * for that functionality. We delegate to it from nsPresContext where
98 * appropriate, and use it standalone in some cases as well.
100 class StaticPresData
{
102 // Initialization and shutdown of the singleton. Called exactly once.
104 static void Shutdown();
106 // Gets an instance of the singleton. Infallible between the calls to Init
108 static StaticPresData
* Get();
111 * Given a language, get the language group name, which can
112 * be used as an argument to LangGroupFontPrefs::Initialize()
114 * aNeedsToCache is used for two things. If null, it indicates that
115 * the nsLanguageAtomService is safe to cache the result of the
116 * language group lookup, either because we're on the main thread,
117 * or because we're on a style worker thread but the font lock has
118 * been acquired. If non-null, it indicates that it's not safe to
119 * cache the result of the language group lookup (because we're on
120 * a style worker thread without the lock acquired). In this case,
121 * GetLanguageGroup will store true in *aNeedsToCache true if we
122 * would have cached the result of a new lookup, and false if we
123 * were able to use an existing cached result. Thus, callers that
124 * get a true *aNeedsToCache outparam value should make an effort
125 * to re-call GetLanguageGroup when it is safe to cache, to avoid
126 * recomputing the language group again later.
128 nsStaticAtom
* GetLangGroup(nsAtom
* aLanguage
,
129 bool* aNeedsToCache
= nullptr) const;
132 * Same as GetLangGroup, but will not cache the result
134 nsStaticAtom
* GetUncachedLangGroup(nsAtom
* aLanguage
) const;
137 * Fetch the user's font preferences for the given aLanguage's
140 * The original code here is pretty old, and includes an optimization
141 * whereby language-specific prefs are read per-document, and the
142 * results are stored in a linked list, which is assumed to be very short
143 * since most documents only ever use one language.
145 * Storing this per-session rather than per-document would almost certainly
146 * be fine. But just to be on the safe side, we leave the old mechanism as-is,
147 * with an additional per-session cache that new callers can use if they don't
148 * have a PresContext.
150 * See comment on GetLangGroup for the usage of aNeedsToCache.
152 const LangGroupFontPrefs
* GetFontPrefsForLang(nsAtom
* aLanguage
,
153 bool* aNeedsToCache
= nullptr);
154 const nsFont
* GetDefaultFont(uint8_t aFontID
, nsAtom
* aLanguage
,
155 const LangGroupFontPrefs
* aPrefs
) const;
157 void InvalidateFontPrefs() { mLangGroupFontPrefs
.Reset(); }
161 ~StaticPresData() = default;
163 nsLanguageAtomService
* mLangService
;
164 LangGroupFontPrefs mLangGroupFontPrefs
;
167 } // namespace mozilla
169 #endif // mozilla_StaticPresData_h