/work/obj-fuzz/dist/include/gfxFontFamilyList.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
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/. */ |
5 | | |
6 | | #ifndef GFX_FONT_FAMILY_LIST_H |
7 | | #define GFX_FONT_FAMILY_LIST_H |
8 | | |
9 | | #include "nsAtom.h" |
10 | | #include "nsDebug.h" |
11 | | #include "nsISupportsImpl.h" |
12 | | #include "nsString.h" |
13 | | #include "nsUnicharUtils.h" |
14 | | #include "nsTArray.h" |
15 | | #include "mozilla/MemoryReporting.h" |
16 | | #include "mozilla/NotNull.h" |
17 | | #include "mozilla/StaticPtr.h" |
18 | | |
19 | | namespace mozilla { |
20 | | |
21 | | /** |
22 | | * type of font family name, either a name (e.g. Helvetica) or a |
23 | | * generic (e.g. serif, sans-serif), with the ability to distinguish |
24 | | * between unquoted and quoted names for serializaiton |
25 | | */ |
26 | | |
27 | | enum FontFamilyType : uint8_t { |
28 | | eFamily_none = 0, // used when finding generics |
29 | | |
30 | | // explicitly named font family (e.g. Helvetica) |
31 | | eFamily_named, |
32 | | eFamily_named_quoted, |
33 | | |
34 | | // generics |
35 | | eFamily_serif, // pref font code relies on this ordering!!! |
36 | | eFamily_sans_serif, |
37 | | eFamily_monospace, |
38 | | eFamily_cursive, |
39 | | eFamily_fantasy, |
40 | | |
41 | | // special |
42 | | eFamily_moz_variable, |
43 | | eFamily_moz_fixed, |
44 | | eFamily_moz_emoji, |
45 | | |
46 | | eFamily_generic_first = eFamily_serif, |
47 | | eFamily_generic_last = eFamily_fantasy, |
48 | | eFamily_generic_count = (eFamily_fantasy - eFamily_serif + 1) |
49 | | }; |
50 | | |
51 | | enum QuotedName { eQuotedName, eUnquotedName }; |
52 | | |
53 | | /** |
54 | | * font family name, an Atom for the name if not a generic and |
55 | | * a font type indicated named family or which generic family |
56 | | */ |
57 | | |
58 | | struct FontFamilyName final { |
59 | | FontFamilyName() |
60 | | : mType(eFamily_named) |
61 | 0 | {} |
62 | | |
63 | | // named font family - e.g. Helvetica |
64 | | explicit FontFamilyName(nsAtom* aFamilyName, |
65 | 0 | QuotedName aQuoted = eUnquotedName) { |
66 | 0 | mType = (aQuoted == eQuotedName) ? eFamily_named_quoted : eFamily_named; |
67 | 0 | mName = aFamilyName; |
68 | 0 | } |
69 | | |
70 | | explicit FontFamilyName(const nsACString& aFamilyName, |
71 | | QuotedName aQuoted = eUnquotedName) { |
72 | | mType = (aQuoted == eQuotedName) ? eFamily_named_quoted : eFamily_named; |
73 | | mName = NS_Atomize(aFamilyName); |
74 | | } |
75 | | |
76 | | // generic font family - e.g. sans-serif |
77 | 18 | explicit FontFamilyName(FontFamilyType aType) { |
78 | 18 | NS_ASSERTION(aType != eFamily_named && |
79 | 18 | aType != eFamily_named_quoted && |
80 | 18 | aType != eFamily_none, |
81 | 18 | "expected a generic font type"); |
82 | 18 | mName = nullptr; |
83 | 18 | mType = aType; |
84 | 18 | } |
85 | | |
86 | 18 | FontFamilyName(const FontFamilyName& aCopy) { |
87 | 18 | mType = aCopy.mType; |
88 | 18 | mName = aCopy.mName; |
89 | 18 | } |
90 | | |
91 | | bool IsNamed() const { |
92 | | return mType == eFamily_named || mType == eFamily_named_quoted; |
93 | | } |
94 | | |
95 | | bool IsGeneric() const { |
96 | | return !IsNamed(); |
97 | | } |
98 | | |
99 | 0 | void AppendToString(nsACString& aFamilyList, bool aQuotes = true) const { |
100 | 0 | switch (mType) { |
101 | 0 | case eFamily_named: |
102 | 0 | aFamilyList.Append(nsAtomCString(mName)); |
103 | 0 | break; |
104 | 0 | case eFamily_named_quoted: |
105 | 0 | if (aQuotes) { |
106 | 0 | aFamilyList.Append('"'); |
107 | 0 | } |
108 | 0 | aFamilyList.Append(nsAtomCString(mName)); |
109 | 0 | if (aQuotes) { |
110 | 0 | aFamilyList.Append('"'); |
111 | 0 | } |
112 | 0 | break; |
113 | 0 | case eFamily_serif: |
114 | 0 | aFamilyList.AppendLiteral("serif"); |
115 | 0 | break; |
116 | 0 | case eFamily_sans_serif: |
117 | 0 | aFamilyList.AppendLiteral("sans-serif"); |
118 | 0 | break; |
119 | 0 | case eFamily_monospace: |
120 | 0 | aFamilyList.AppendLiteral("monospace"); |
121 | 0 | break; |
122 | 0 | case eFamily_cursive: |
123 | 0 | aFamilyList.AppendLiteral("cursive"); |
124 | 0 | break; |
125 | 0 | case eFamily_fantasy: |
126 | 0 | aFamilyList.AppendLiteral("fantasy"); |
127 | 0 | break; |
128 | 0 | case eFamily_moz_fixed: |
129 | 0 | aFamilyList.AppendLiteral("-moz-fixed"); |
130 | 0 | break; |
131 | 0 | default: |
132 | 0 | break; |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | // helper method that converts generic names to the right enum value |
137 | | static FontFamilyName |
138 | | Convert(const nsACString& aFamilyOrGenericName) { |
139 | | // should only be passed a single font - not entirely correct, a family |
140 | | // *could* have a comma in it but in practice never does so |
141 | | // for debug purposes this is fine |
142 | | NS_ASSERTION(aFamilyOrGenericName.FindChar(',') == -1, |
143 | | "Convert method should only be passed a single family name"); |
144 | | |
145 | | FontFamilyType genericType = eFamily_none; |
146 | | if (aFamilyOrGenericName.LowerCaseEqualsLiteral("serif")) { |
147 | | genericType = eFamily_serif; |
148 | | } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("sans-serif")) { |
149 | | genericType = eFamily_sans_serif; |
150 | | } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("monospace")) { |
151 | | genericType = eFamily_monospace; |
152 | | } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("cursive")) { |
153 | | genericType = eFamily_cursive; |
154 | | } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("fantasy")) { |
155 | | genericType = eFamily_fantasy; |
156 | | } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("-moz-fixed")) { |
157 | | genericType = eFamily_moz_fixed; |
158 | | } else { |
159 | | return FontFamilyName(aFamilyOrGenericName, eUnquotedName); |
160 | | } |
161 | | |
162 | | return FontFamilyName(genericType); |
163 | | } |
164 | | |
165 | | FontFamilyType mType; |
166 | | RefPtr<nsAtom> mName; // null if mType != eFamily_named |
167 | | }; |
168 | | |
169 | | inline bool |
170 | 0 | operator==(const FontFamilyName& a, const FontFamilyName& b) { |
171 | 0 | return a.mType == b.mType && a.mName == b.mName; |
172 | 0 | } |
173 | | |
174 | | /** |
175 | | * A refcounted array of FontFamilyNames. We use this to store the specified |
176 | | * value (in Servo) and the computed value (in both Gecko and Servo) of the |
177 | | * font-family property. |
178 | | */ |
179 | | class SharedFontList |
180 | | { |
181 | | public: |
182 | | NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedFontList); |
183 | | |
184 | | SharedFontList() |
185 | 3 | { |
186 | 3 | } |
187 | | |
188 | | explicit SharedFontList(FontFamilyType aGenericType) |
189 | | : mNames { FontFamilyName(aGenericType) } |
190 | 18 | { |
191 | 18 | } |
192 | | |
193 | | SharedFontList(nsAtom* aFamilyName, QuotedName aQuoted) |
194 | | : mNames { FontFamilyName(aFamilyName, aQuoted) } |
195 | 0 | { |
196 | 0 | } |
197 | | |
198 | | SharedFontList(const nsACString& aFamilyName, QuotedName aQuoted) |
199 | | : mNames { FontFamilyName(aFamilyName, aQuoted) } |
200 | 0 | { |
201 | 0 | } |
202 | | |
203 | | explicit SharedFontList(const FontFamilyName& aName) |
204 | | : mNames { aName } |
205 | 0 | { |
206 | 0 | } |
207 | | |
208 | | explicit SharedFontList(nsTArray<FontFamilyName>&& aNames) |
209 | | : mNames(std::move(aNames)) |
210 | 0 | { |
211 | 0 | } |
212 | | |
213 | | FontFamilyType FirstGeneric() const |
214 | 0 | { |
215 | 0 | for (const FontFamilyName& name : mNames) { |
216 | 0 | if (name.IsGeneric()) { |
217 | 0 | return name.mType; |
218 | 0 | } |
219 | 0 | } |
220 | 0 | return eFamily_none; |
221 | 0 | } |
222 | | |
223 | | bool HasGeneric() const |
224 | 0 | { |
225 | 0 | return FirstGeneric() != eFamily_none; |
226 | 0 | } |
227 | | |
228 | | size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const |
229 | 0 | { |
230 | 0 | size_t n = 0; |
231 | 0 | n += aMallocSizeOf(this); |
232 | 0 | n += mNames.ShallowSizeOfExcludingThis(aMallocSizeOf); |
233 | 0 | return n; |
234 | 0 | } |
235 | | |
236 | | size_t SizeOfIncludingThisIfUnshared(MallocSizeOf aMallocSizeOf) const |
237 | 0 | { |
238 | 0 | size_t n = 0; |
239 | 0 | if (mRefCnt.get() == 1) { |
240 | 0 | n += SizeOfIncludingThis(aMallocSizeOf); |
241 | 0 | } |
242 | 0 | return n; |
243 | 0 | } |
244 | | |
245 | | const nsTArray<FontFamilyName> mNames; |
246 | | |
247 | | static void Initialize(); |
248 | | static void Shutdown(); |
249 | | static StaticRefPtr<SharedFontList> sEmpty; |
250 | | |
251 | | private: |
252 | 0 | ~SharedFontList() = default; |
253 | | }; |
254 | | |
255 | | /** |
256 | | * font family list, array of font families and a default font type. |
257 | | * font family names are either named strings or generics. the default |
258 | | * font type is used to preserve the variable font fallback behavior |
259 | | */ |
260 | | class FontFamilyList { |
261 | | public: |
262 | | FontFamilyList() |
263 | | : mFontlist(WrapNotNull(SharedFontList::sEmpty.get())) |
264 | | , mDefaultFontType(eFamily_none) |
265 | 3 | { |
266 | 3 | } |
267 | | |
268 | | explicit FontFamilyList(FontFamilyType aGenericType) |
269 | | : mFontlist(MakeNotNull<SharedFontList*>(aGenericType)) |
270 | | , mDefaultFontType(eFamily_none) |
271 | 18 | { |
272 | 18 | } |
273 | | |
274 | | FontFamilyList(nsAtom* aFamilyName, |
275 | | QuotedName aQuoted) |
276 | | : mFontlist(MakeNotNull<SharedFontList*>(aFamilyName, aQuoted)) |
277 | | , mDefaultFontType(eFamily_none) |
278 | 0 | { |
279 | 0 | } |
280 | | |
281 | | FontFamilyList(const nsACString& aFamilyName, |
282 | | QuotedName aQuoted) |
283 | | : mFontlist(MakeNotNull<SharedFontList*>(aFamilyName, aQuoted)) |
284 | | , mDefaultFontType(eFamily_none) |
285 | 0 | { |
286 | 0 | } |
287 | | |
288 | | explicit FontFamilyList(const FontFamilyName& aName) |
289 | | : mFontlist(MakeNotNull<SharedFontList*>(aName)) |
290 | | , mDefaultFontType(eFamily_none) |
291 | 0 | { |
292 | 0 | } |
293 | | |
294 | | explicit FontFamilyList(nsTArray<FontFamilyName>&& aNames) |
295 | | : mFontlist(MakeNotNull<SharedFontList*>(std::move(aNames))) |
296 | | , mDefaultFontType(eFamily_none) |
297 | 0 | { |
298 | 0 | } |
299 | | |
300 | | FontFamilyList(const FontFamilyList& aOther) |
301 | | : mFontlist(aOther.mFontlist) |
302 | | , mDefaultFontType(aOther.mDefaultFontType) |
303 | 0 | { |
304 | 0 | } |
305 | | |
306 | | explicit FontFamilyList(NotNull<SharedFontList*> aFontList) |
307 | | : mFontlist(aFontList) |
308 | | , mDefaultFontType(eFamily_none) |
309 | 0 | { |
310 | 0 | } |
311 | | |
312 | | void SetFontlist(nsTArray<FontFamilyName>&& aNames) |
313 | 0 | { |
314 | 0 | mFontlist = MakeNotNull<SharedFontList*>(std::move(aNames)); |
315 | 0 | } |
316 | | |
317 | | void SetFontlist(NotNull<SharedFontList*> aFontlist) |
318 | 0 | { |
319 | 0 | mFontlist = aFontlist; |
320 | 0 | } |
321 | | |
322 | 0 | uint32_t Length() const { |
323 | 0 | return mFontlist->mNames.Length(); |
324 | 0 | } |
325 | | |
326 | 0 | bool IsEmpty() const { |
327 | 0 | return mFontlist->mNames.IsEmpty(); |
328 | 0 | } |
329 | | |
330 | 0 | NotNull<SharedFontList*> GetFontlist() const { |
331 | 0 | return mFontlist; |
332 | 0 | } |
333 | | |
334 | 0 | bool Equals(const FontFamilyList& aFontlist) const { |
335 | 0 | return (mFontlist == aFontlist.mFontlist || |
336 | 0 | mFontlist->mNames == aFontlist.mFontlist->mNames) && |
337 | 0 | mDefaultFontType == aFontlist.mDefaultFontType; |
338 | 0 | } |
339 | | |
340 | 0 | FontFamilyType FirstGeneric() const { |
341 | 0 | return mFontlist->FirstGeneric(); |
342 | 0 | } |
343 | | |
344 | | bool HasGeneric() const |
345 | 0 | { |
346 | 0 | return mFontlist->HasGeneric(); |
347 | 0 | } |
348 | | |
349 | 0 | bool HasDefaultGeneric() const { |
350 | 0 | for (const FontFamilyName& name : mFontlist->mNames) { |
351 | 0 | if (name.mType == mDefaultFontType) { |
352 | 0 | return true; |
353 | 0 | } |
354 | 0 | } |
355 | 0 | return false; |
356 | 0 | } |
357 | | |
358 | | // Find the first generic (but ignoring cursive and fantasy, as they are |
359 | | // rarely configured in any useful way) in the list. |
360 | | // If found, move it to the start and return true; else return false. |
361 | 0 | bool PrioritizeFirstGeneric() { |
362 | 0 | uint32_t len = mFontlist->mNames.Length(); |
363 | 0 | for (uint32_t i = 0; i < len; i++) { |
364 | 0 | const FontFamilyName name = mFontlist->mNames[i]; |
365 | 0 | if (name.IsGeneric()) { |
366 | 0 | if (name.mType == eFamily_cursive || |
367 | 0 | name.mType == eFamily_fantasy) { |
368 | 0 | continue; |
369 | 0 | } |
370 | 0 | if (i > 0) { |
371 | 0 | nsTArray<FontFamilyName> names; |
372 | 0 | names.AppendElements(mFontlist->mNames); |
373 | 0 | names.RemoveElementAt(i); |
374 | 0 | names.InsertElementAt(0, name); |
375 | 0 | SetFontlist(std::move(names)); |
376 | 0 | } |
377 | 0 | return true; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | return false; |
381 | 0 | } |
382 | | |
383 | 0 | void PrependGeneric(FontFamilyType aType) { |
384 | 0 | nsTArray<FontFamilyName> names; |
385 | 0 | names.AppendElements(mFontlist->mNames); |
386 | 0 | names.InsertElementAt(0, FontFamilyName(aType)); |
387 | 0 | SetFontlist(std::move(names)); |
388 | 0 | } |
389 | | |
390 | | void ToString(nsACString& aFamilyList, |
391 | | bool aQuotes = true, |
392 | 0 | bool aIncludeDefault = false) const { |
393 | 0 | const nsTArray<FontFamilyName>& names = mFontlist->mNames; |
394 | 0 | aFamilyList.Truncate(); |
395 | 0 | uint32_t len = names.Length(); |
396 | 0 | for (uint32_t i = 0; i < len; i++) { |
397 | 0 | if (i != 0) { |
398 | 0 | aFamilyList.Append(','); |
399 | 0 | } |
400 | 0 | const FontFamilyName& name = names[i]; |
401 | 0 | name.AppendToString(aFamilyList, aQuotes); |
402 | 0 | } |
403 | 0 | if (aIncludeDefault && mDefaultFontType != eFamily_none) { |
404 | 0 | if (!aFamilyList.IsEmpty()) { |
405 | 0 | aFamilyList.Append(','); |
406 | 0 | } |
407 | 0 | if (mDefaultFontType == eFamily_serif) { |
408 | 0 | aFamilyList.AppendLiteral("serif"); |
409 | 0 | } else { |
410 | 0 | aFamilyList.AppendLiteral("sans-serif"); |
411 | 0 | } |
412 | 0 | } |
413 | 0 | } |
414 | | |
415 | | // searches for a specific non-generic name, lowercase comparison |
416 | 0 | bool Contains(const nsACString& aFamilyName) const { |
417 | 0 | NS_ConvertUTF8toUTF16 fam(aFamilyName); |
418 | 0 | ToLowerCase(fam); |
419 | 0 | for (const FontFamilyName& name : mFontlist->mNames) { |
420 | 0 | if (!name.IsNamed()) { |
421 | 0 | continue; |
422 | 0 | } |
423 | 0 | nsAtomString listname(name.mName); |
424 | 0 | ToLowerCase(listname); |
425 | 0 | if (listname.Equals(fam)) { |
426 | 0 | return true; |
427 | 0 | } |
428 | 0 | } |
429 | 0 | return false; |
430 | 0 | } |
431 | | |
432 | 0 | FontFamilyType GetDefaultFontType() const { return mDefaultFontType; } |
433 | 3 | void SetDefaultFontType(FontFamilyType aType) { |
434 | 3 | NS_ASSERTION(aType == eFamily_none || aType == eFamily_serif || |
435 | 3 | aType == eFamily_sans_serif, |
436 | 3 | "default font type must be either serif or sans-serif"); |
437 | 3 | mDefaultFontType = aType; |
438 | 3 | } |
439 | | |
440 | | // memory reporting |
441 | 0 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { |
442 | 0 | size_t n = 0; |
443 | 0 | n += mFontlist->SizeOfIncludingThisIfUnshared(aMallocSizeOf); |
444 | 0 | return n; |
445 | 0 | } |
446 | | |
447 | 0 | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { |
448 | 0 | return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf); |
449 | 0 | } |
450 | | |
451 | | protected: |
452 | | NotNull<RefPtr<SharedFontList>> mFontlist; |
453 | | FontFamilyType mDefaultFontType; // none, serif or sans-serif |
454 | | }; |
455 | | |
456 | | inline bool |
457 | 0 | operator==(const FontFamilyList& a, const FontFamilyList& b) { |
458 | 0 | return a.Equals(b); |
459 | 0 | } |
460 | | |
461 | | inline bool |
462 | 0 | operator!=(const FontFamilyList& a, const FontFamilyList& b) { |
463 | 0 | return !a.Equals(b); |
464 | 0 | } |
465 | | |
466 | | } // namespace mozilla |
467 | | |
468 | | #endif /* GFX_FONT_FAMILY_LIST_H */ |