Line data Source code
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 "nsAutoPtr.h"
11 : #include "nsCoord.h"
12 : #include "nsCOMPtr.h"
13 : #include "nsFont.h"
14 : #include "nsIAtom.h"
15 : #include "nsLanguageAtomService.h"
16 :
17 : namespace mozilla {
18 :
19 0 : struct LangGroupFontPrefs {
20 : // Font sizes default to zero; they will be set in GetFontPreferences
21 31 : LangGroupFontPrefs()
22 31 : : mLangGroup(nullptr)
23 : , mMinimumFontSize(0)
24 : , mDefaultVariableFont(mozilla::eFamily_serif, 0)
25 : , mDefaultFixedFont(mozilla::eFamily_monospace, 0)
26 : , mDefaultSerifFont(mozilla::eFamily_serif, 0)
27 : , mDefaultSansSerifFont(mozilla::eFamily_sans_serif, 0)
28 : , mDefaultMonospaceFont(mozilla::eFamily_monospace, 0)
29 : , mDefaultCursiveFont(mozilla::eFamily_cursive, 0)
30 31 : , mDefaultFantasyFont(mozilla::eFamily_fantasy, 0)
31 31 : {}
32 :
33 86 : void Reset()
34 : {
35 : // Throw away any other LangGroupFontPrefs objects:
36 86 : mNext = nullptr;
37 :
38 : // Make GetFontPreferences reinitialize mLangGroupFontPrefs:
39 86 : mLangGroup = nullptr;
40 86 : }
41 :
42 21 : size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
43 21 : size_t n = 0;
44 21 : LangGroupFontPrefs* curr = mNext;
45 21 : while (curr) {
46 0 : n += aMallocSizeOf(curr);
47 :
48 : // Measurement of the following members may be added later if DMD finds
49 : // it is worthwhile:
50 : // - mLangGroup
51 : // - mDefault*Font
52 :
53 0 : curr = curr->mNext;
54 : }
55 21 : return n;
56 : }
57 :
58 : // Initialize this with the data for a given language
59 : void Initialize(nsIAtom* aLangGroupAtom);
60 :
61 : nsCOMPtr<nsIAtom> mLangGroup;
62 : nscoord mMinimumFontSize;
63 : nsFont mDefaultVariableFont;
64 : nsFont mDefaultFixedFont;
65 : nsFont mDefaultSerifFont;
66 : nsFont mDefaultSansSerifFont;
67 : nsFont mDefaultMonospaceFont;
68 : nsFont mDefaultCursiveFont;
69 : nsFont mDefaultFantasyFont;
70 : nsAutoPtr<LangGroupFontPrefs> mNext;
71 : };
72 :
73 : /**
74 : * Some functionality that has historically lived on nsPresContext does not
75 : * actually need to be per-document. This singleton class serves as a host
76 : * for that functionality. We delegate to it from nsPresContext where
77 : * appropriate, and use it standalone in some cases as well.
78 : */
79 : class StaticPresData
80 : {
81 : public:
82 : // Initialization and shutdown of the singleton. Called exactly once.
83 : static void Init();
84 : static void Shutdown();
85 :
86 : // Gets an instance of the singleton. Infallible between the calls to Init
87 : // and Shutdown.
88 : static StaticPresData* Get();
89 :
90 : /**
91 : * This table maps border-width enums 'thin', 'medium', 'thick'
92 : * to actual nscoord values.
93 : */
94 59 : const nscoord* GetBorderWidthTable() { return mBorderWidthTable; }
95 :
96 : /**
97 : * Given a language, get the language group name, which can
98 : * be used as an argument to LangGroupFontPrefs::Initialize()
99 : *
100 : * aNeedsToCache is used for two things. If null, it indicates that
101 : * the nsLanguageAtomService is safe to cache the result of the
102 : * language group lookup, either because we're on the main thread,
103 : * or because we're on a style worker thread but the font lock has
104 : * been acquired. If non-null, it indicates that it's not safe to
105 : * cache the result of the language group lookup (because we're on
106 : * a style worker thread without the lock acquired). In this case,
107 : * GetLanguageGroup will store true in *aNeedsToCache true if we
108 : * would have cached the result of a new lookup, and false if we
109 : * were able to use an existing cached result. Thus, callers that
110 : * get a true *aNeedsToCache outparam value should make an effort
111 : * to re-call GetLanguageGroup when it is safe to cache, to avoid
112 : * recomputing the language group again later.
113 : */
114 : nsIAtom* GetLangGroup(nsIAtom* aLanguage, bool* aNeedsToCache = nullptr) const;
115 :
116 : /**
117 : * Same as GetLangGroup, but will not cache the result
118 : *
119 : */
120 : already_AddRefed<nsIAtom> GetUncachedLangGroup(nsIAtom* aLanguage) const;
121 :
122 : /**
123 : * Fetch the user's font preferences for the given aLanguage's
124 : * langugage group.
125 : *
126 : * The original code here is pretty old, and includes an optimization
127 : * whereby language-specific prefs are read per-document, and the
128 : * results are stored in a linked list, which is assumed to be very short
129 : * since most documents only ever use one language.
130 : *
131 : * Storing this per-session rather than per-document would almost certainly
132 : * be fine. But just to be on the safe side, we leave the old mechanism as-is,
133 : * with an additional per-session cache that new callers can use if they don't
134 : * have a PresContext.
135 : *
136 : * See comment on GetLangGroup for the usage of aNeedsToCache.
137 : */
138 : const LangGroupFontPrefs* GetFontPrefsForLangHelper(nsIAtom* aLanguage,
139 : const LangGroupFontPrefs* aPrefs,
140 : bool* aNeedsToCache = nullptr) const;
141 : /**
142 : * Get the default font for the given language and generic font ID.
143 : * aLanguage may not be nullptr.
144 : *
145 : * This object is read-only, you must copy the font to modify it.
146 : *
147 : * When aFontID is kPresContext_DefaultVariableFontID or
148 : * kPresContext_DefaultFixedFontID (which equals
149 : * kGenericFont_moz_fixed, which is used for the -moz-fixed generic),
150 : * the nsFont returned has its name as a CSS generic family (serif or
151 : * sans-serif for the former, monospace for the latter), and its size
152 : * as the default font size for variable or fixed fonts for the
153 : * language group.
154 : *
155 : * For aFontID corresponding to a CSS Generic, the nsFont returned has
156 : * its name set to that generic font's name, and its size set to
157 : * the user's preference for font size for that generic and the
158 : * given language.
159 : */
160 : const nsFont* GetDefaultFontHelper(uint8_t aFontID,
161 : nsIAtom* aLanguage,
162 : const LangGroupFontPrefs* aPrefs) const;
163 :
164 : /*
165 : * These versions operate on the font pref cache on StaticPresData.
166 : */
167 :
168 : const nsFont* GetDefaultFont(uint8_t aFontID, nsIAtom* aLanguage) const
169 : {
170 : MOZ_ASSERT(aLanguage);
171 : return GetDefaultFontHelper(aFontID, aLanguage, GetFontPrefsForLang(aLanguage));
172 : }
173 : const LangGroupFontPrefs* GetFontPrefsForLang(nsIAtom* aLanguage, bool* aNeedsToCache = nullptr) const
174 : {
175 : MOZ_ASSERT(aLanguage);
176 : return GetFontPrefsForLangHelper(aLanguage, &mStaticLangGroupFontPrefs, aNeedsToCache);
177 : }
178 :
179 28 : void ResetCachedFontPrefs() { mStaticLangGroupFontPrefs.Reset(); }
180 :
181 : private:
182 : StaticPresData();
183 0 : ~StaticPresData() {}
184 :
185 : nsLanguageAtomService* mLangService;
186 : nscoord mBorderWidthTable[3];
187 : LangGroupFontPrefs mStaticLangGroupFontPrefs;
188 : };
189 :
190 : } // namespace mozilla
191 :
192 : #endif // mozilla_StaticPresData_h
|