Bumping manifests a=b2g-bump
[gecko.git] / gfx / thebes / gfxFontFamilyList.h
blob1906fba828bddda4373861a88c1ef3d40ffd240e
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/. */
6 #ifndef GFX_FONT_FAMILY_LIST_H
7 #define GFX_FONT_FAMILY_LIST_H
9 #include "nsDebug.h"
10 #include "nsISupportsImpl.h"
11 #include "nsString.h"
12 #include "nsTArray.h"
13 #include "mozilla/MemoryReporting.h"
15 namespace mozilla {
17 /**
18 * type of font family name, either a name (e.g. Helvetica) or a
19 * generic (e.g. serif, sans-serif), with the ability to distinguish
20 * between unquoted and quoted names for serializaiton
21 */
23 enum FontFamilyType {
24 eFamily_none = 0, // used when finding generics
26 // explicitly named font family (e.g. Helvetica)
27 eFamily_named,
28 eFamily_named_quoted,
30 // generics
31 eFamily_serif,
32 eFamily_sans_serif,
33 eFamily_monospace,
34 eFamily_cursive,
35 eFamily_fantasy,
37 // special
38 eFamily_moz_variable,
39 eFamily_moz_fixed
42 enum QuotedName { eQuotedName, eUnquotedName };
44 /**
45 * font family name, a string for the name if not a generic and
46 * a font type indicated named family or which generic family
49 struct FontFamilyName MOZ_FINAL {
50 FontFamilyName()
51 : mType(eFamily_named)
54 // named font family - e.g. Helvetica
55 explicit FontFamilyName(const nsAString& aFamilyName,
56 QuotedName aQuoted = eUnquotedName) {
57 mType = (aQuoted == eQuotedName) ? eFamily_named_quoted : eFamily_named;
58 mName = aFamilyName;
61 // generic font family - e.g. sans-serif
62 explicit FontFamilyName(FontFamilyType aType) {
63 NS_ASSERTION(aType != eFamily_named &&
64 aType != eFamily_named_quoted &&
65 aType != eFamily_none,
66 "expected a generic font type");
67 mName.Truncate();
68 mType = aType;
71 FontFamilyName(const FontFamilyName& aCopy) {
72 mType = aCopy.mType;
73 mName = aCopy.mName;
76 bool IsNamed() const {
77 return mType == eFamily_named || mType == eFamily_named_quoted;
80 bool IsGeneric() const {
81 return !IsNamed();
84 void AppendToString(nsAString& aFamilyList, bool aQuotes = true) const {
85 switch (mType) {
86 case eFamily_named:
87 aFamilyList.Append(mName);
88 break;
89 case eFamily_named_quoted:
90 if (aQuotes) {
91 aFamilyList.Append('"');
93 aFamilyList.Append(mName);
94 if (aQuotes) {
95 aFamilyList.Append('"');
97 break;
98 case eFamily_serif:
99 aFamilyList.AppendLiteral("serif");
100 break;
101 case eFamily_sans_serif:
102 aFamilyList.AppendLiteral("sans-serif");
103 break;
104 case eFamily_monospace:
105 aFamilyList.AppendLiteral("monospace");
106 break;
107 case eFamily_cursive:
108 aFamilyList.AppendLiteral("cursive");
109 break;
110 case eFamily_fantasy:
111 aFamilyList.AppendLiteral("fantasy");
112 break;
113 case eFamily_moz_fixed:
114 aFamilyList.AppendLiteral("-moz-fixed");
115 break;
116 default:
117 break;
121 // helper method that converts generic names to the right enum value
122 static FontFamilyName
123 Convert(const nsAString& aFamilyOrGenericName) {
124 // should only be passed a single font - not entirely correct, a family
125 // *could* have a comma in it but in practice never does so
126 // for debug purposes this is fine
127 NS_ASSERTION(aFamilyOrGenericName.FindChar(',') == -1,
128 "Convert method should only be passed a single family name");
130 FontFamilyType genericType = eFamily_none;
131 if (aFamilyOrGenericName.LowerCaseEqualsLiteral("serif")) {
132 genericType = eFamily_serif;
133 } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("sans-serif")) {
134 genericType = eFamily_sans_serif;
135 } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("monospace")) {
136 genericType = eFamily_monospace;
137 } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("cursive")) {
138 genericType = eFamily_cursive;
139 } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("fantasy")) {
140 genericType = eFamily_fantasy;
141 } else if (aFamilyOrGenericName.LowerCaseEqualsLiteral("-moz-fixed")) {
142 genericType = eFamily_moz_fixed;
143 } else {
144 return FontFamilyName(aFamilyOrGenericName, eUnquotedName);
147 return FontFamilyName(genericType);
150 // memory reporting
151 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
152 return mName.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
155 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
156 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
159 FontFamilyType mType;
160 nsString mName; // empty if mType != eFamily_named
163 inline bool
164 operator==(const FontFamilyName& a, const FontFamilyName& b) {
165 return a.mType == b.mType && a.mName == b.mName;
169 * font family list, array of font families and a default font type.
170 * font family names are either named strings or generics. the default
171 * font type is used to preserve the variable font fallback behavior
174 class FontFamilyList {
175 public:
176 FontFamilyList()
177 : mDefaultFontType(eFamily_none)
181 explicit FontFamilyList(FontFamilyType aGenericType)
182 : mDefaultFontType(eFamily_none)
184 Append(FontFamilyName(aGenericType));
187 FontFamilyList(const nsAString& aFamilyName,
188 QuotedName aQuoted)
189 : mDefaultFontType(eFamily_none)
191 Append(FontFamilyName(aFamilyName, aQuoted));
194 FontFamilyList(const FontFamilyList& aOther)
195 : mFontlist(aOther.mFontlist)
196 , mDefaultFontType(aOther.mDefaultFontType)
200 void Append(const FontFamilyName& aFamilyName) {
201 mFontlist.AppendElement(aFamilyName);
204 void Append(const nsTArray<nsString>& aFamilyNameList) {
205 uint32_t len = aFamilyNameList.Length();
206 for (uint32_t i = 0; i < len; i++) {
207 mFontlist.AppendElement(FontFamilyName(aFamilyNameList[i],
208 eUnquotedName));
212 void Clear() {
213 mFontlist.Clear();
216 uint32_t Length() const {
217 return mFontlist.Length();
220 bool IsEmpty() const {
221 return mFontlist.IsEmpty();
224 const nsTArray<FontFamilyName>& GetFontlist() const {
225 return mFontlist;
228 bool Equals(const FontFamilyList& aFontlist) const {
229 return mFontlist == aFontlist.mFontlist &&
230 mDefaultFontType == aFontlist.mDefaultFontType;
233 FontFamilyType FirstGeneric() const {
234 uint32_t len = mFontlist.Length();
235 for (uint32_t i = 0; i < len; i++) {
236 const FontFamilyName& name = mFontlist[i];
237 if (name.IsGeneric()) {
238 return name.mType;
241 return eFamily_none;
244 bool HasGeneric() const {
245 return FirstGeneric() != eFamily_none;
248 bool HasDefaultGeneric() const {
249 uint32_t len = mFontlist.Length();
250 for (uint32_t i = 0; i < len; i++) {
251 const FontFamilyName& name = mFontlist[i];
252 if (name.mType == mDefaultFontType) {
253 return true;
256 return false;
259 void ToString(nsAString& aFamilyList,
260 bool aQuotes = true,
261 bool aIncludeDefault = false) const {
262 aFamilyList.Truncate();
263 uint32_t len = mFontlist.Length();
264 for (uint32_t i = 0; i < len; i++) {
265 if (i != 0) {
266 aFamilyList.Append(',');
268 const FontFamilyName& name = mFontlist[i];
269 name.AppendToString(aFamilyList, aQuotes);
271 if (aIncludeDefault && mDefaultFontType != eFamily_none) {
272 if (!aFamilyList.IsEmpty()) {
273 aFamilyList.Append(',');
275 if (mDefaultFontType == eFamily_serif) {
276 aFamilyList.AppendLiteral("serif");
277 } else {
278 aFamilyList.AppendLiteral("sans-serif");
283 FontFamilyType GetDefaultFontType() const { return mDefaultFontType; }
284 void SetDefaultFontType(FontFamilyType aType) {
285 NS_ASSERTION(aType == eFamily_none || aType == eFamily_serif ||
286 aType == eFamily_sans_serif,
287 "default font type must be either serif or sans-serif");
288 mDefaultFontType = aType;
291 // memory reporting
292 size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
293 return mFontlist.SizeOfExcludingThis(aMallocSizeOf);
296 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const {
297 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
300 private:
301 nsTArray<FontFamilyName> mFontlist;
302 FontFamilyType mDefaultFontType; // none, serif or sans-serif
305 inline bool
306 operator==(const FontFamilyList& a, const FontFamilyList& b) {
307 return a.Equals(b);
310 } // namespace mozilla
312 #endif /* GFX_FONT_FAMILY_LIST_H */