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
10 #include "nsISupportsImpl.h"
13 #include "mozilla/MemoryReporting.h"
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
24 eFamily_none
= 0, // used when finding generics
26 // explicitly named font family (e.g. Helvetica)
42 enum QuotedName
{ eQuotedName
, eUnquotedName
};
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
{
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
;
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");
71 FontFamilyName(const FontFamilyName
& aCopy
) {
76 bool IsNamed() const {
77 return mType
== eFamily_named
|| mType
== eFamily_named_quoted
;
80 bool IsGeneric() const {
84 void AppendToString(nsAString
& aFamilyList
, bool aQuotes
= true) const {
87 aFamilyList
.Append(mName
);
89 case eFamily_named_quoted
:
91 aFamilyList
.Append('"');
93 aFamilyList
.Append(mName
);
95 aFamilyList
.Append('"');
99 aFamilyList
.AppendLiteral("serif");
101 case eFamily_sans_serif
:
102 aFamilyList
.AppendLiteral("sans-serif");
104 case eFamily_monospace
:
105 aFamilyList
.AppendLiteral("monospace");
107 case eFamily_cursive
:
108 aFamilyList
.AppendLiteral("cursive");
110 case eFamily_fantasy
:
111 aFamilyList
.AppendLiteral("fantasy");
113 case eFamily_moz_fixed
:
114 aFamilyList
.AppendLiteral("-moz-fixed");
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
;
144 return FontFamilyName(aFamilyOrGenericName
, eUnquotedName
);
147 return FontFamilyName(genericType
);
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
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
{
177 : mDefaultFontType(eFamily_none
)
181 explicit FontFamilyList(FontFamilyType aGenericType
)
182 : mDefaultFontType(eFamily_none
)
184 Append(FontFamilyName(aGenericType
));
187 FontFamilyList(const nsAString
& aFamilyName
,
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
],
216 uint32_t Length() const {
217 return mFontlist
.Length();
220 bool IsEmpty() const {
221 return mFontlist
.IsEmpty();
224 const nsTArray
<FontFamilyName
>& GetFontlist() const {
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()) {
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
) {
259 void ToString(nsAString
& aFamilyList
,
261 bool aIncludeDefault
= false) const {
262 aFamilyList
.Truncate();
263 uint32_t len
= mFontlist
.Length();
264 for (uint32_t i
= 0; i
< len
; i
++) {
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");
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
;
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
);
301 nsTArray
<FontFamilyName
> mFontlist
;
302 FontFamilyType mDefaultFontType
; // none, serif or sans-serif
306 operator==(const FontFamilyList
& a
, const FontFamilyList
& b
) {
310 } // namespace mozilla
312 #endif /* GFX_FONT_FAMILY_LIST_H */