gecko/gfx/src/nsFont.cpp

219 lines
6.4 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsFont.h"
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "nsCRT.h"
nsFont::nsFont(const char* aName, PRUint8 aStyle, PRUint8 aVariant,
PRUint16 aWeight, PRUint8 aDecoration, nscoord aSize,
float aSizeAdjust)
{
NS_ASSERTION(aName && IsASCII(nsDependentCString(aName)),
"Must only pass ASCII names here");
name.AssignASCII(aName);
style = aStyle;
systemFont = PR_FALSE;
variant = aVariant;
familyNameQuirks = PR_FALSE;
weight = aWeight;
decorations = aDecoration;
size = aSize;
sizeAdjust = aSizeAdjust;
}
nsFont::nsFont(const nsString& aName, PRUint8 aStyle, PRUint8 aVariant,
PRUint16 aWeight, PRUint8 aDecoration, nscoord aSize,
float aSizeAdjust)
: name(aName)
{
style = aStyle;
systemFont = PR_FALSE;
variant = aVariant;
familyNameQuirks = PR_FALSE;
weight = aWeight;
decorations = aDecoration;
size = aSize;
sizeAdjust = aSizeAdjust;
}
nsFont::nsFont(const nsFont& aOther)
: name(aOther.name)
{
style = aOther.style;
systemFont = aOther.systemFont;
variant = aOther.variant;
familyNameQuirks = aOther.familyNameQuirks;
weight = aOther.weight;
decorations = aOther.decorations;
size = aOther.size;
sizeAdjust = aOther.sizeAdjust;
}
nsFont::nsFont()
{
}
nsFont::~nsFont()
{
}
PRBool nsFont::BaseEquals(const nsFont& aOther) const
{
if ((style == aOther.style) &&
(systemFont == aOther.systemFont) &&
(familyNameQuirks == aOther.familyNameQuirks) &&
(weight == aOther.weight) &&
(size == aOther.size) &&
(sizeAdjust == aOther.sizeAdjust) &&
name.Equals(aOther.name, nsCaseInsensitiveStringComparator())) {
return PR_TRUE;
}
return PR_FALSE;
}
PRBool nsFont::Equals(const nsFont& aOther) const
{
if (BaseEquals(aOther) &&
(variant == aOther.variant) &&
(decorations == aOther.decorations)) {
return PR_TRUE;
}
return PR_FALSE;
}
nsFont& nsFont::operator=(const nsFont& aOther)
{
name = aOther.name;
style = aOther.style;
systemFont = aOther.systemFont;
variant = aOther.variant;
familyNameQuirks = aOther.familyNameQuirks;
weight = aOther.weight;
decorations = aOther.decorations;
size = aOther.size;
sizeAdjust = aOther.sizeAdjust;
return *this;
}
static PRBool IsGenericFontFamily(const nsString& aFamily)
{
PRUint8 generic;
nsFont::GetGenericID(aFamily, &generic);
return generic != kGenericFont_NONE;
}
const PRUnichar kNullCh = PRUnichar('\0');
const PRUnichar kSingleQuote = PRUnichar('\'');
const PRUnichar kDoubleQuote = PRUnichar('\"');
const PRUnichar kComma = PRUnichar(',');
PRBool nsFont::EnumerateFamilies(nsFontFamilyEnumFunc aFunc, void* aData) const
{
const PRUnichar *p, *p_end;
name.BeginReading(p);
name.EndReading(p_end);
nsAutoString family;
while (p < p_end) {
while (nsCRT::IsAsciiSpace(*p))
if (++p == p_end)
return PR_TRUE;
PRBool generic;
if (*p == kSingleQuote || *p == kDoubleQuote) {
// quoted font family
PRUnichar quoteMark = *p;
if (++p == p_end)
return PR_TRUE;
const PRUnichar *nameStart = p;
// XXX What about CSS character escapes?
while (*p != quoteMark)
if (++p == p_end)
return PR_TRUE;
family = Substring(nameStart, p);
generic = PR_FALSE;
while (++p != p_end && *p != kComma)
/* nothing */ ;
} else {
// unquoted font family
const PRUnichar *nameStart = p;
while (++p != p_end && *p != kComma)
/* nothing */ ;
family = Substring(nameStart, p);
family.CompressWhitespace(PR_FALSE, PR_TRUE);
generic = IsGenericFontFamily(family);
}
if (!family.IsEmpty() && !(*aFunc)(family, generic, aData))
return PR_FALSE;
++p; // may advance past p_end
}
return PR_TRUE;
}
static PRBool FontEnumCallback(const nsString& aFamily, PRBool aGeneric, void *aData)
{
*((nsString*)aData) = aFamily;
return PR_FALSE;
}
void nsFont::GetFirstFamily(nsString& aFamily) const
{
EnumerateFamilies(FontEnumCallback, &aFamily);
}
/*static*/
void nsFont::GetGenericID(const nsString& aGeneric, PRUint8* aID)
{
*aID = kGenericFont_NONE;
if (aGeneric.LowerCaseEqualsLiteral("-moz-fixed")) *aID = kGenericFont_moz_fixed;
else if (aGeneric.LowerCaseEqualsLiteral("serif")) *aID = kGenericFont_serif;
else if (aGeneric.LowerCaseEqualsLiteral("sans-serif")) *aID = kGenericFont_sans_serif;
else if (aGeneric.LowerCaseEqualsLiteral("cursive")) *aID = kGenericFont_cursive;
else if (aGeneric.LowerCaseEqualsLiteral("fantasy")) *aID = kGenericFont_fantasy;
else if (aGeneric.LowerCaseEqualsLiteral("monospace")) *aID = kGenericFont_monospace;
}