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
, PRUint8 aDecoration
, nscoord aSize
,
47 NS_ASSERTION(aName
&& IsASCII(nsDependentCString(aName
)),
48 "Must only pass ASCII names here");
49 name
.AssignASCII(aName
);
51 systemFont
= PR_FALSE
;
53 familyNameQuirks
= PR_FALSE
;
55 decorations
= aDecoration
;
57 sizeAdjust
= aSizeAdjust
;
60 nsFont::nsFont(const nsString
& aName
, PRUint8 aStyle
, PRUint8 aVariant
,
61 PRUint16 aWeight
, PRUint8 aDecoration
, nscoord aSize
,
66 systemFont
= PR_FALSE
;
68 familyNameQuirks
= PR_FALSE
;
70 decorations
= aDecoration
;
72 sizeAdjust
= aSizeAdjust
;
75 nsFont::nsFont(const nsFont
& aOther
)
79 systemFont
= aOther
.systemFont
;
80 variant
= aOther
.variant
;
81 familyNameQuirks
= aOther
.familyNameQuirks
;
82 weight
= aOther
.weight
;
83 decorations
= aOther
.decorations
;
85 sizeAdjust
= aOther
.sizeAdjust
;
96 PRBool
nsFont::BaseEquals(const nsFont
& aOther
) const
98 if ((style
== aOther
.style
) &&
99 (systemFont
== aOther
.systemFont
) &&
100 (familyNameQuirks
== aOther
.familyNameQuirks
) &&
101 (weight
== aOther
.weight
) &&
102 (size
== aOther
.size
) &&
103 (sizeAdjust
== aOther
.sizeAdjust
) &&
104 name
.Equals(aOther
.name
, nsCaseInsensitiveStringComparator())) {
110 PRBool
nsFont::Equals(const nsFont
& aOther
) const
112 if (BaseEquals(aOther
) &&
113 (variant
== aOther
.variant
) &&
114 (decorations
== aOther
.decorations
)) {
120 nsFont
& nsFont::operator=(const nsFont
& aOther
)
123 style
= aOther
.style
;
124 systemFont
= aOther
.systemFont
;
125 variant
= aOther
.variant
;
126 familyNameQuirks
= aOther
.familyNameQuirks
;
127 weight
= aOther
.weight
;
128 decorations
= aOther
.decorations
;
130 sizeAdjust
= aOther
.sizeAdjust
;
134 static PRBool
IsGenericFontFamily(const nsString
& aFamily
)
137 nsFont::GetGenericID(aFamily
, &generic
);
138 return generic
!= kGenericFont_NONE
;
141 const PRUnichar kNullCh
= PRUnichar('\0');
142 const PRUnichar kSingleQuote
= PRUnichar('\'');
143 const PRUnichar kDoubleQuote
= PRUnichar('\"');
144 const PRUnichar kComma
= PRUnichar(',');
146 PRBool
nsFont::EnumerateFamilies(nsFontFamilyEnumFunc aFunc
, void* aData
) const
148 const PRUnichar
*p
, *p_end
;
149 name
.BeginReading(p
);
150 name
.EndReading(p_end
);
154 while (nsCRT::IsAsciiSpace(*p
))
159 if (*p
== kSingleQuote
|| *p
== kDoubleQuote
) {
160 // quoted font family
161 PRUnichar quoteMark
= *p
;
164 const PRUnichar
*nameStart
= p
;
166 // XXX What about CSS character escapes?
167 while (*p
!= quoteMark
)
171 family
= Substring(nameStart
, p
);
174 while (++p
!= p_end
&& *p
!= kComma
)
178 // unquoted font family
179 const PRUnichar
*nameStart
= p
;
180 while (++p
!= p_end
&& *p
!= kComma
)
183 family
= Substring(nameStart
, p
);
184 family
.CompressWhitespace(PR_FALSE
, PR_TRUE
);
185 generic
= IsGenericFontFamily(family
);
188 if (!family
.IsEmpty() && !(*aFunc
)(family
, generic
, aData
))
191 ++p
; // may advance past p_end
197 static PRBool
FontEnumCallback(const nsString
& aFamily
, PRBool aGeneric
, void *aData
)
199 *((nsString
*)aData
) = aFamily
;
203 void nsFont::GetFirstFamily(nsString
& aFamily
) const
205 EnumerateFamilies(FontEnumCallback
, &aFamily
);
209 void nsFont::GetGenericID(const nsString
& aGeneric
, PRUint8
* aID
)
211 *aID
= kGenericFont_NONE
;
212 if (aGeneric
.LowerCaseEqualsLiteral("-moz-fixed")) *aID
= kGenericFont_moz_fixed
;
213 else if (aGeneric
.LowerCaseEqualsLiteral("serif")) *aID
= kGenericFont_serif
;
214 else if (aGeneric
.LowerCaseEqualsLiteral("sans-serif")) *aID
= kGenericFont_sans_serif
;
215 else if (aGeneric
.LowerCaseEqualsLiteral("cursive")) *aID
= kGenericFont_cursive
;
216 else if (aGeneric
.LowerCaseEqualsLiteral("fantasy")) *aID
= kGenericFont_fantasy
;
217 else if (aGeneric
.LowerCaseEqualsLiteral("monospace")) *aID
= kGenericFont_monospace
;