Bug 458861. Validate TrueType headers before activating downloaded font. r=roc, sr...
[wine-gecko.git] / gfx / src / nsFont.cpp
blobc17118e225451159d310f8c7d1d9e38159277476
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, PRUint8 aDecoration, nscoord aSize,
45 float aSizeAdjust)
47 NS_ASSERTION(aName && IsASCII(nsDependentCString(aName)),
48 "Must only pass ASCII names here");
49 name.AssignASCII(aName);
50 style = aStyle;
51 systemFont = PR_FALSE;
52 variant = aVariant;
53 familyNameQuirks = PR_FALSE;
54 weight = aWeight;
55 decorations = aDecoration;
56 size = aSize;
57 sizeAdjust = aSizeAdjust;
60 nsFont::nsFont(const nsString& aName, PRUint8 aStyle, PRUint8 aVariant,
61 PRUint16 aWeight, PRUint8 aDecoration, nscoord aSize,
62 float aSizeAdjust)
63 : name(aName)
65 style = aStyle;
66 systemFont = PR_FALSE;
67 variant = aVariant;
68 familyNameQuirks = PR_FALSE;
69 weight = aWeight;
70 decorations = aDecoration;
71 size = aSize;
72 sizeAdjust = aSizeAdjust;
75 nsFont::nsFont(const nsFont& aOther)
76 : name(aOther.name)
78 style = aOther.style;
79 systemFont = aOther.systemFont;
80 variant = aOther.variant;
81 familyNameQuirks = aOther.familyNameQuirks;
82 weight = aOther.weight;
83 decorations = aOther.decorations;
84 size = aOther.size;
85 sizeAdjust = aOther.sizeAdjust;
88 nsFont::nsFont()
92 nsFont::~nsFont()
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())) {
105 return PR_TRUE;
107 return PR_FALSE;
110 PRBool nsFont::Equals(const nsFont& aOther) const
112 if (BaseEquals(aOther) &&
113 (variant == aOther.variant) &&
114 (decorations == aOther.decorations)) {
115 return PR_TRUE;
117 return PR_FALSE;
120 nsFont& nsFont::operator=(const nsFont& aOther)
122 name = aOther.name;
123 style = aOther.style;
124 systemFont = aOther.systemFont;
125 variant = aOther.variant;
126 familyNameQuirks = aOther.familyNameQuirks;
127 weight = aOther.weight;
128 decorations = aOther.decorations;
129 size = aOther.size;
130 sizeAdjust = aOther.sizeAdjust;
131 return *this;
134 static PRBool IsGenericFontFamily(const nsString& aFamily)
136 PRUint8 generic;
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);
151 nsAutoString family;
153 while (p < p_end) {
154 while (nsCRT::IsAsciiSpace(*p))
155 if (++p == p_end)
156 return PR_TRUE;
158 PRBool generic;
159 if (*p == kSingleQuote || *p == kDoubleQuote) {
160 // quoted font family
161 PRUnichar quoteMark = *p;
162 if (++p == p_end)
163 return PR_TRUE;
164 const PRUnichar *nameStart = p;
166 // XXX What about CSS character escapes?
167 while (*p != quoteMark)
168 if (++p == p_end)
169 return PR_TRUE;
171 family = Substring(nameStart, p);
172 generic = PR_FALSE;
174 while (++p != p_end && *p != kComma)
175 /* nothing */ ;
177 } else {
178 // unquoted font family
179 const PRUnichar *nameStart = p;
180 while (++p != p_end && *p != kComma)
181 /* nothing */ ;
183 family = Substring(nameStart, p);
184 family.CompressWhitespace(PR_FALSE, PR_TRUE);
185 generic = IsGenericFontFamily(family);
188 if (!family.IsEmpty() && !(*aFunc)(family, generic, aData))
189 return PR_FALSE;
191 ++p; // may advance past p_end
194 return PR_TRUE;
197 static PRBool FontEnumCallback(const nsString& aFamily, PRBool aGeneric, void *aData)
199 *((nsString*)aData) = aFamily;
200 return PR_FALSE;
203 void nsFont::GetFirstFamily(nsString& aFamily) const
205 EnumerateFamilies(FontEnumCallback, &aFamily);
208 /*static*/
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;