Back to index

lightning-sunbird  0.9+nobinonly
nsFont.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsFont.h"
00039 #include "nsString.h"
00040 #include "nsUnicharUtils.h"
00041 #include "nsCRT.h"
00042 
00043 nsFont::nsFont(const char* aName, PRUint8 aStyle, PRUint8 aVariant,
00044                PRUint16 aWeight, PRUint8 aDecoration, nscoord aSize,
00045                float aSizeAdjust)
00046 {
00047   name.AssignWithConversion(aName);
00048   style = aStyle;
00049   systemFont = PR_FALSE;
00050   variant = aVariant;
00051   familyNameQuirks = PR_FALSE;
00052   weight = aWeight;
00053   decorations = aDecoration;
00054   size = aSize;
00055   sizeAdjust = aSizeAdjust;
00056 }
00057 
00058 nsFont::nsFont(const nsString& aName, PRUint8 aStyle, PRUint8 aVariant,
00059                PRUint16 aWeight, PRUint8 aDecoration, nscoord aSize,
00060                float aSizeAdjust)
00061   : name(aName)
00062 {
00063   style = aStyle;
00064   systemFont = PR_FALSE;
00065   variant = aVariant;
00066   familyNameQuirks = PR_FALSE;
00067   weight = aWeight;
00068   decorations = aDecoration;
00069   size = aSize;
00070   sizeAdjust = aSizeAdjust;
00071 }
00072 
00073 nsFont::nsFont(const nsFont& aOther)
00074   : name(aOther.name)
00075 {
00076   style = aOther.style;
00077   systemFont = aOther.systemFont;
00078   variant = aOther.variant;
00079   familyNameQuirks = aOther.familyNameQuirks;
00080   weight = aOther.weight;
00081   decorations = aOther.decorations;
00082   size = aOther.size;
00083   sizeAdjust = aOther.sizeAdjust;
00084 }
00085 
00086 nsFont::nsFont()
00087 {
00088 }
00089 
00090 nsFont::~nsFont()
00091 {
00092 }
00093 
00094 PRBool nsFont::Equals(const nsFont& aOther) const
00095 {
00096   if ((style == aOther.style) &&
00097       (systemFont == aOther.systemFont) &&
00098       (variant == aOther.variant) &&
00099       (familyNameQuirks == aOther.familyNameQuirks) &&
00100       (weight == aOther.weight) &&
00101       (decorations == aOther.decorations) &&
00102       (size == aOther.size) &&
00103       (sizeAdjust == aOther.sizeAdjust) &&
00104       name.Equals(aOther.name, nsCaseInsensitiveStringComparator())) {
00105     return PR_TRUE;
00106   }
00107   return PR_FALSE;
00108 }
00109 
00110 nsFont& nsFont::operator=(const nsFont& aOther)
00111 {
00112   name = aOther.name;
00113   style = aOther.style;
00114   systemFont = aOther.systemFont;
00115   variant = aOther.variant;
00116   familyNameQuirks = aOther.familyNameQuirks;
00117   weight = aOther.weight;
00118   decorations = aOther.decorations;
00119   size = aOther.size;
00120   sizeAdjust = aOther.sizeAdjust;
00121   return *this;
00122 }
00123 
00124 static PRBool IsGenericFontFamily(const nsString& aFamily)
00125 {
00126   PRUint8 generic;
00127   nsFont::GetGenericID(aFamily, &generic);
00128   return generic != kGenericFont_NONE;
00129 }
00130 
00131 const PRUnichar kNullCh       = PRUnichar('\0');
00132 const PRUnichar kSingleQuote  = PRUnichar('\'');
00133 const PRUnichar kDoubleQuote  = PRUnichar('\"');
00134 const PRUnichar kComma        = PRUnichar(',');
00135 
00136 PRBool nsFont::EnumerateFamilies(nsFontFamilyEnumFunc aFunc, void* aData) const
00137 {
00138   const PRUnichar *p, *p_end;
00139   name.BeginReading(p);
00140   name.EndReading(p_end);
00141   nsAutoString family;
00142 
00143   while (p < p_end) {
00144     while (nsCRT::IsAsciiSpace(*p))
00145       if (++p == p_end)
00146         return PR_TRUE;
00147 
00148     PRBool generic;
00149     if (*p == kSingleQuote || *p == kDoubleQuote) {
00150       // quoted font family
00151       PRUnichar quoteMark = *p;
00152       if (++p == p_end)
00153         return PR_TRUE;
00154       const PRUnichar *nameStart = p;
00155 
00156       // XXX What about CSS character escapes?
00157       while (*p != quoteMark)
00158         if (++p == p_end)
00159           return PR_TRUE;
00160 
00161       family = Substring(nameStart, p);
00162       generic = PR_FALSE;
00163 
00164       while (++p != p_end && *p != kComma)
00165         /* nothing */ ;
00166 
00167     } else {
00168       // unquoted font family
00169       const PRUnichar *nameStart = p;
00170       while (++p != p_end && *p != kComma)
00171         /* nothing */ ;
00172 
00173       family = Substring(nameStart, p);
00174       family.CompressWhitespace(PR_FALSE, PR_TRUE);
00175       generic = IsGenericFontFamily(family);
00176     }
00177 
00178     if (!family.IsEmpty() && !(*aFunc)(family, generic, aData))
00179       return PR_FALSE;
00180 
00181     ++p; // may advance past p_end
00182   }
00183 
00184   return PR_TRUE;
00185 }
00186 
00187 static PRBool FontEnumCallback(const nsString& aFamily, PRBool aGeneric, void *aData)
00188 {
00189   *((nsString*)aData) = aFamily;
00190   return PR_FALSE;
00191 }
00192 
00193 void nsFont::GetFirstFamily(nsString& aFamily) const
00194 {
00195   EnumerateFamilies(FontEnumCallback, &aFamily);
00196 }
00197 
00198 /*static*/
00199 void nsFont::GetGenericID(const nsString& aGeneric, PRUint8* aID)
00200 {
00201   *aID = kGenericFont_NONE;
00202   if (aGeneric.LowerCaseEqualsLiteral("-moz-fixed"))      *aID = kGenericFont_moz_fixed;
00203   else if (aGeneric.LowerCaseEqualsLiteral("serif"))      *aID = kGenericFont_serif;
00204   else if (aGeneric.LowerCaseEqualsLiteral("sans-serif")) *aID = kGenericFont_sans_serif;
00205   else if (aGeneric.LowerCaseEqualsLiteral("cursive"))    *aID = kGenericFont_cursive;
00206   else if (aGeneric.LowerCaseEqualsLiteral("fantasy"))    *aID = kGenericFont_fantasy;
00207   else if (aGeneric.LowerCaseEqualsLiteral("monospace"))  *aID = kGenericFont_monospace;
00208 }