2005-10-08 05:09:42 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** 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 Foundation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Stuart Parmenter <stuart@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either 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 ***** */
|
|
|
|
|
2006-01-09 17:58:17 +00:00
|
|
|
#include "nsIPref.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2006-02-27 15:04:31 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2006-01-09 17:58:17 +00:00
|
|
|
|
2005-10-08 05:09:42 +00:00
|
|
|
#include "gfxFont.h"
|
|
|
|
|
|
|
|
#include "prtypes.h"
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2006-06-10 00:21:05 +00:00
|
|
|
gfxFont::gfxFont(const nsAString &aName, const gfxFontStyle *aFontStyle) :
|
|
|
|
mName(aName), mStyle(aFontStyle)
|
2006-03-14 23:17:55 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-01-09 17:58:17 +00:00
|
|
|
gfxFontGroup::gfxFontGroup(const nsAString& aFamilies, const gfxFontStyle *aStyle)
|
2006-02-15 02:45:37 +00:00
|
|
|
: mFamilies(aFamilies), mStyle(*aStyle)
|
2005-10-08 05:09:42 +00:00
|
|
|
{
|
2006-06-10 00:21:05 +00:00
|
|
|
|
2005-10-08 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
2006-01-09 17:58:17 +00:00
|
|
|
PRBool
|
|
|
|
gfxFontGroup::ForEachFont(FontCreationCallback fc,
|
|
|
|
void *closure)
|
2006-06-10 00:21:05 +00:00
|
|
|
{
|
|
|
|
return ForEachFont(mFamilies, mStyle.langGroup, fc, closure);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::ForEachFont(const nsAString& aFamilies,
|
|
|
|
const nsACString& aLangGroup,
|
|
|
|
FontCreationCallback fc,
|
|
|
|
void *closure)
|
2006-08-17 13:40:59 +00:00
|
|
|
{
|
|
|
|
return ForEachFontInternal(aFamilies, aLangGroup,
|
|
|
|
fc, closure, PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool
|
|
|
|
gfxFontGroup::ForEachFontInternal(const nsAString& aFamilies,
|
|
|
|
const nsACString& aLangGroup,
|
|
|
|
FontCreationCallback fc,
|
|
|
|
void *closure,
|
|
|
|
PRBool aAllowRecursive)
|
2005-10-08 05:09:42 +00:00
|
|
|
{
|
|
|
|
const PRUnichar kSingleQuote = PRUnichar('\'');
|
|
|
|
const PRUnichar kDoubleQuote = PRUnichar('\"');
|
|
|
|
const PRUnichar kComma = PRUnichar(',');
|
|
|
|
|
2006-01-09 17:58:17 +00:00
|
|
|
nsCOMPtr<nsIPref> prefs;
|
|
|
|
prefs = do_GetService(NS_PREF_CONTRACTID);
|
|
|
|
|
2006-06-10 00:21:05 +00:00
|
|
|
nsPromiseFlatString families(aFamilies);
|
2005-10-08 05:09:42 +00:00
|
|
|
const PRUnichar *p, *p_end;
|
2006-06-10 00:21:05 +00:00
|
|
|
families.BeginReading(p);
|
|
|
|
families.EndReading(p_end);
|
2005-10-08 05:09:42 +00:00
|
|
|
nsAutoString family;
|
2006-08-17 13:40:59 +00:00
|
|
|
nsCAutoString lcFamily;
|
2006-01-09 17:58:17 +00:00
|
|
|
nsAutoString genericFamily;
|
2005-10-08 05:09:42 +00:00
|
|
|
|
|
|
|
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);
|
2006-01-09 17:58:17 +00:00
|
|
|
genericFamily.SetIsVoid(PR_TRUE);
|
2005-10-08 05:09:42 +00:00
|
|
|
|
|
|
|
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);
|
2006-01-09 17:58:17 +00:00
|
|
|
|
2006-02-28 07:36:01 +00:00
|
|
|
if (family.LowerCaseEqualsLiteral("serif") ||
|
|
|
|
family.LowerCaseEqualsLiteral("sans-serif") ||
|
|
|
|
family.LowerCaseEqualsLiteral("monospace") ||
|
|
|
|
family.LowerCaseEqualsLiteral("cursive") ||
|
|
|
|
family.LowerCaseEqualsLiteral("fantasy"))
|
2006-01-09 17:58:17 +00:00
|
|
|
{
|
|
|
|
generic = PR_TRUE;
|
|
|
|
|
2006-02-28 07:36:01 +00:00
|
|
|
ToLowerCase(NS_LossyConvertUTF16toASCII(family), lcFamily);
|
|
|
|
|
2006-01-09 17:58:17 +00:00
|
|
|
nsCAutoString prefName("font.name.");
|
2006-02-28 07:36:01 +00:00
|
|
|
prefName.Append(lcFamily);
|
2006-01-09 17:58:17 +00:00
|
|
|
prefName.AppendLiteral(".");
|
2006-06-10 00:21:05 +00:00
|
|
|
prefName.Append(aLangGroup);
|
2006-01-09 17:58:17 +00:00
|
|
|
|
2006-02-27 17:42:16 +00:00
|
|
|
// prefs file always uses (must use) UTF-8 so that we can use
|
|
|
|
// |GetCharPref| and treat the result as a UTF-8 string.
|
2006-06-10 00:21:05 +00:00
|
|
|
nsXPIDLString value;
|
|
|
|
nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(value));
|
2006-01-09 17:58:17 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2006-02-28 07:36:01 +00:00
|
|
|
CopyASCIItoUTF16(lcFamily, genericFamily);
|
2006-06-10 00:21:05 +00:00
|
|
|
family = value;
|
2006-01-09 17:58:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
generic = PR_FALSE;
|
|
|
|
genericFamily.SetIsVoid(PR_TRUE);
|
|
|
|
}
|
2005-10-08 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!family.IsEmpty()) {
|
2006-06-10 00:21:05 +00:00
|
|
|
if (!((*fc) (family, NS_LossyConvertUTF16toASCII(genericFamily), closure)))
|
2006-01-09 17:58:17 +00:00
|
|
|
return PR_FALSE;
|
2005-10-08 05:09:42 +00:00
|
|
|
}
|
|
|
|
|
2006-08-17 13:40:59 +00:00
|
|
|
if (generic && aAllowRecursive) {
|
|
|
|
nsCAutoString prefName("font.name-list.");
|
|
|
|
prefName.Append(lcFamily);
|
|
|
|
prefName.AppendLiteral(".");
|
|
|
|
prefName.Append(aLangGroup);
|
|
|
|
nsXPIDLString value;
|
|
|
|
nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(value));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ForEachFontInternal(value, aLangGroup, fc, closure, PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-10-08 05:09:42 +00:00
|
|
|
++p; // may advance past p_end
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2006-06-10 00:21:05 +00:00
|
|
|
void
|
|
|
|
gfxFontGroup::FindGenericFontFromStyle(FontCreationCallback fc,
|
|
|
|
void *closure)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPref> prefs(do_GetService(NS_PREF_CONTRACTID));
|
|
|
|
if (!prefs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCAutoString prefName;
|
|
|
|
nsXPIDLString genericName;
|
|
|
|
nsXPIDLString familyName;
|
|
|
|
|
|
|
|
// add the default font to the end of the list
|
|
|
|
prefName.AssignLiteral("font.default.");
|
|
|
|
prefName.Append(mStyle.langGroup);
|
|
|
|
nsresult rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(genericName));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
prefName.AssignLiteral("font.name.");
|
|
|
|
prefName.Append(NS_LossyConvertUTF16toASCII(genericName));
|
|
|
|
prefName.AppendLiteral(".");
|
|
|
|
prefName.Append(mStyle.langGroup);
|
|
|
|
|
|
|
|
rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(familyName));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
(*fc)(familyName, NS_LossyConvertUTF16toASCII(genericName), closure);
|
|
|
|
}
|
2006-08-17 13:40:59 +00:00
|
|
|
|
|
|
|
prefName.AssignLiteral("font.name-list.");
|
|
|
|
prefName.Append(NS_LossyConvertUTF16toASCII(genericName));
|
|
|
|
prefName.AppendLiteral(".");
|
|
|
|
prefName.Append(mStyle.langGroup);
|
|
|
|
|
|
|
|
rv = prefs->CopyUnicharPref(prefName.get(), getter_Copies(familyName));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
ForEachFontInternal(familyName, mStyle.langGroup,
|
|
|
|
fc, closure, PR_FALSE);
|
|
|
|
}
|
2006-06-10 00:21:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gfxFontStyle::gfxFontStyle(PRUint8 aStyle, PRUint8 aVariant,
|
|
|
|
PRUint16 aWeight, PRUint8 aDecoration, gfxFloat aSize,
|
|
|
|
const nsACString& aLangGroup,
|
|
|
|
float aSizeAdjust, PRPackedBool aSystemFont,
|
|
|
|
PRPackedBool aFamilyNameQuirks) :
|
|
|
|
style(aStyle), systemFont(aSystemFont), variant(aVariant),
|
|
|
|
familyNameQuirks(aFamilyNameQuirks), weight(aWeight),
|
|
|
|
decorations(aDecoration), size(PR_MIN(aSize, 5000)), langGroup(aLangGroup), sizeAdjust(aSizeAdjust)
|
|
|
|
{
|
|
|
|
if (langGroup.IsEmpty()) {
|
|
|
|
NS_WARNING("empty langgroup");
|
|
|
|
langGroup.Assign("x-western");
|
|
|
|
}
|
|
|
|
}
|
2006-03-14 23:17:55 +00:00
|
|
|
|
2006-02-22 01:44:31 +00:00
|
|
|
void
|
2006-06-19 23:41:15 +00:00
|
|
|
gfxFontStyle::ComputeWeightAndOffset(PRInt8 *outBaseWeight, PRInt8 *outOffset) const
|
2006-02-22 01:44:31 +00:00
|
|
|
{
|
2006-06-19 23:41:15 +00:00
|
|
|
PRInt8 baseWeight = (weight + 50) / 100;
|
|
|
|
PRInt8 offset = weight - baseWeight * 100;
|
2006-02-22 01:44:31 +00:00
|
|
|
|
|
|
|
if (baseWeight < 0)
|
|
|
|
baseWeight = 0;
|
|
|
|
if (baseWeight > 9)
|
|
|
|
baseWeight = 9;
|
|
|
|
|
|
|
|
if (outBaseWeight)
|
|
|
|
*outBaseWeight = baseWeight;
|
|
|
|
if (outOffset)
|
|
|
|
*outOffset = offset;
|
|
|
|
}
|