Bug 574454 - Add window frame css styles. r=dbaron.
[mozilla-central.git] / gfx / src / nsFont.cpp
blob405b33accf3b7a0a383a86b35b9dc10b788b3ea8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsFont.h"
39 #include "nsString.h"
40 #include "nsUnicharUtils.h"
41 #include "nsCRT.h"
43 nsFont::nsFont(const char* aName, PRUint8 aStyle, PRUint8 aVariant,
44 PRUint16 aWeight, PRInt16 aStretch, PRUint8 aDecoration,
45 nscoord aSize, float aSizeAdjust,
46 const nsString* aFeatureSettings,
47 const nsString* aLanguageOverride)
49 NS_ASSERTION(aName && IsASCII(nsDependentCString(aName)),
50 "Must only pass ASCII names here");
51 name.AssignASCII(aName);
52 style = aStyle;
53 systemFont = PR_FALSE;
54 variant = aVariant;
55 familyNameQuirks = PR_FALSE;
56 weight = aWeight;
57 stretch = aStretch;
58 decorations = aDecoration;
59 size = aSize;
60 sizeAdjust = aSizeAdjust;
61 if (aFeatureSettings) {
62 featureSettings = *aFeatureSettings;
64 if (aLanguageOverride) {
65 languageOverride = *aLanguageOverride;
69 nsFont::nsFont(const nsString& aName, PRUint8 aStyle, PRUint8 aVariant,
70 PRUint16 aWeight, PRInt16 aStretch, PRUint8 aDecoration,
71 nscoord aSize, float aSizeAdjust,
72 const nsString* aFeatureSettings,
73 const nsString* aLanguageOverride)
74 : name(aName)
76 style = aStyle;
77 systemFont = PR_FALSE;
78 variant = aVariant;
79 familyNameQuirks = PR_FALSE;
80 weight = aWeight;
81 stretch = aStretch;
82 decorations = aDecoration;
83 size = aSize;
84 sizeAdjust = aSizeAdjust;
85 if (aFeatureSettings) {
86 featureSettings = *aFeatureSettings;
88 if (aLanguageOverride) {
89 languageOverride = *aLanguageOverride;
93 nsFont::nsFont(const nsFont& aOther)
94 : name(aOther.name)
96 style = aOther.style;
97 systemFont = aOther.systemFont;
98 variant = aOther.variant;
99 familyNameQuirks = aOther.familyNameQuirks;
100 weight = aOther.weight;
101 stretch = aOther.stretch;
102 decorations = aOther.decorations;
103 size = aOther.size;
104 sizeAdjust = aOther.sizeAdjust;
105 featureSettings = aOther.featureSettings;
106 languageOverride = aOther.languageOverride;
109 nsFont::nsFont()
113 nsFont::~nsFont()
117 PRBool nsFont::BaseEquals(const nsFont& aOther) const
119 if ((style == aOther.style) &&
120 (systemFont == aOther.systemFont) &&
121 (familyNameQuirks == aOther.familyNameQuirks) &&
122 (weight == aOther.weight) &&
123 (stretch == aOther.stretch) &&
124 (size == aOther.size) &&
125 (sizeAdjust == aOther.sizeAdjust) &&
126 name.Equals(aOther.name, nsCaseInsensitiveStringComparator()) &&
127 (featureSettings == aOther.featureSettings) &&
128 (languageOverride == aOther.languageOverride)) {
129 return PR_TRUE;
131 return PR_FALSE;
134 PRBool nsFont::Equals(const nsFont& aOther) const
136 if (BaseEquals(aOther) &&
137 (variant == aOther.variant) &&
138 (decorations == aOther.decorations)) {
139 return PR_TRUE;
141 return PR_FALSE;
144 nsFont& nsFont::operator=(const nsFont& aOther)
146 name = aOther.name;
147 style = aOther.style;
148 systemFont = aOther.systemFont;
149 variant = aOther.variant;
150 familyNameQuirks = aOther.familyNameQuirks;
151 weight = aOther.weight;
152 stretch = aOther.stretch;
153 decorations = aOther.decorations;
154 size = aOther.size;
155 sizeAdjust = aOther.sizeAdjust;
156 featureSettings = aOther.featureSettings;
157 languageOverride = aOther.languageOverride;
158 return *this;
161 static PRBool IsGenericFontFamily(const nsString& aFamily)
163 PRUint8 generic;
164 nsFont::GetGenericID(aFamily, &generic);
165 return generic != kGenericFont_NONE;
168 const PRUnichar kNullCh = PRUnichar('\0');
169 const PRUnichar kSingleQuote = PRUnichar('\'');
170 const PRUnichar kDoubleQuote = PRUnichar('\"');
171 const PRUnichar kComma = PRUnichar(',');
173 PRBool nsFont::EnumerateFamilies(nsFontFamilyEnumFunc aFunc, void* aData) const
175 const PRUnichar *p, *p_end;
176 name.BeginReading(p);
177 name.EndReading(p_end);
178 nsAutoString family;
180 while (p < p_end) {
181 while (nsCRT::IsAsciiSpace(*p))
182 if (++p == p_end)
183 return PR_TRUE;
185 PRBool generic;
186 if (*p == kSingleQuote || *p == kDoubleQuote) {
187 // quoted font family
188 PRUnichar quoteMark = *p;
189 if (++p == p_end)
190 return PR_TRUE;
191 const PRUnichar *nameStart = p;
193 // XXX What about CSS character escapes?
194 while (*p != quoteMark)
195 if (++p == p_end)
196 return PR_TRUE;
198 family = Substring(nameStart, p);
199 generic = PR_FALSE;
201 while (++p != p_end && *p != kComma)
202 /* nothing */ ;
204 } else {
205 // unquoted font family
206 const PRUnichar *nameStart = p;
207 while (++p != p_end && *p != kComma)
208 /* nothing */ ;
210 family = Substring(nameStart, p);
211 family.CompressWhitespace(PR_FALSE, PR_TRUE);
212 generic = IsGenericFontFamily(family);
215 if (!family.IsEmpty() && !(*aFunc)(family, generic, aData))
216 return PR_FALSE;
218 ++p; // may advance past p_end
221 return PR_TRUE;
224 static PRBool FontEnumCallback(const nsString& aFamily, PRBool aGeneric, void *aData)
226 *((nsString*)aData) = aFamily;
227 return PR_FALSE;
230 void nsFont::GetFirstFamily(nsString& aFamily) const
232 EnumerateFamilies(FontEnumCallback, &aFamily);
235 /*static*/
236 void nsFont::GetGenericID(const nsString& aGeneric, PRUint8* aID)
238 *aID = kGenericFont_NONE;
239 if (aGeneric.LowerCaseEqualsLiteral("-moz-fixed")) *aID = kGenericFont_moz_fixed;
240 else if (aGeneric.LowerCaseEqualsLiteral("serif")) *aID = kGenericFont_serif;
241 else if (aGeneric.LowerCaseEqualsLiteral("sans-serif")) *aID = kGenericFont_sans_serif;
242 else if (aGeneric.LowerCaseEqualsLiteral("cursive")) *aID = kGenericFont_cursive;
243 else if (aGeneric.LowerCaseEqualsLiteral("fantasy")) *aID = kGenericFont_fantasy;
244 else if (aGeneric.LowerCaseEqualsLiteral("monospace")) *aID = kGenericFont_monospace;