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
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.
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 ***** */
40 #include "nsUnicharUtils.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
);
53 systemFont
= PR_FALSE
;
55 familyNameQuirks
= PR_FALSE
;
58 decorations
= aDecoration
;
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
)
77 systemFont
= PR_FALSE
;
79 familyNameQuirks
= PR_FALSE
;
82 decorations
= aDecoration
;
84 sizeAdjust
= aSizeAdjust
;
85 if (aFeatureSettings
) {
86 featureSettings
= *aFeatureSettings
;
88 if (aLanguageOverride
) {
89 languageOverride
= *aLanguageOverride
;
93 nsFont::nsFont(const nsFont
& aOther
)
97 systemFont
= aOther
.systemFont
;
98 variant
= aOther
.variant
;
99 familyNameQuirks
= aOther
.familyNameQuirks
;
100 weight
= aOther
.weight
;
101 stretch
= aOther
.stretch
;
102 decorations
= aOther
.decorations
;
104 sizeAdjust
= aOther
.sizeAdjust
;
105 featureSettings
= aOther
.featureSettings
;
106 languageOverride
= aOther
.languageOverride
;
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
)) {
134 PRBool
nsFont::Equals(const nsFont
& aOther
) const
136 if (BaseEquals(aOther
) &&
137 (variant
== aOther
.variant
) &&
138 (decorations
== aOther
.decorations
)) {
144 nsFont
& nsFont::operator=(const nsFont
& aOther
)
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
;
155 sizeAdjust
= aOther
.sizeAdjust
;
156 featureSettings
= aOther
.featureSettings
;
157 languageOverride
= aOther
.languageOverride
;
161 static PRBool
IsGenericFontFamily(const nsString
& aFamily
)
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
);
181 while (nsCRT::IsAsciiSpace(*p
))
186 if (*p
== kSingleQuote
|| *p
== kDoubleQuote
) {
187 // quoted font family
188 PRUnichar quoteMark
= *p
;
191 const PRUnichar
*nameStart
= p
;
193 // XXX What about CSS character escapes?
194 while (*p
!= quoteMark
)
198 family
= Substring(nameStart
, p
);
201 while (++p
!= p_end
&& *p
!= kComma
)
205 // unquoted font family
206 const PRUnichar
*nameStart
= p
;
207 while (++p
!= p_end
&& *p
!= kComma
)
210 family
= Substring(nameStart
, p
);
211 family
.CompressWhitespace(PR_FALSE
, PR_TRUE
);
212 generic
= IsGenericFontFamily(family
);
215 if (!family
.IsEmpty() && !(*aFunc
)(family
, generic
, aData
))
218 ++p
; // may advance past p_end
224 static PRBool
FontEnumCallback(const nsString
& aFamily
, PRBool aGeneric
, void *aData
)
226 *((nsString
*)aData
) = aFamily
;
230 void nsFont::GetFirstFamily(nsString
& aFamily
) const
232 EnumerateFamilies(FontEnumCallback
, &aFamily
);
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
;