2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "gfxFontconfigUtils.h"
|
2008-09-15 21:40:57 -07:00
|
|
|
#include "gfxFont.h"
|
2012-03-29 14:08:43 -07:00
|
|
|
#include "nsGkAtoms.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-25 21:40:28 -07:00
|
|
|
#include <locale.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <fontconfig/fontconfig.h>
|
|
|
|
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2008-09-25 21:40:28 -07:00
|
|
|
#include "nsILanguageAtomService.h"
|
2009-01-18 12:14:14 -08:00
|
|
|
#include "nsTArray.h"
|
2011-06-11 19:30:16 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2011-06-11 19:30:16 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
/* static */ gfxFontconfigUtils* gfxFontconfigUtils::sUtils = nullptr;
|
|
|
|
static nsILanguageAtomService* gLangService = nullptr;
|
2008-09-25 21:40:28 -07:00
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxFontconfigUtils::Shutdown() {
|
|
|
|
if (sUtils) {
|
|
|
|
delete sUtils;
|
2012-07-30 07:20:58 -07:00
|
|
|
sUtils = nullptr;
|
2008-09-25 21:40:28 -07:00
|
|
|
}
|
|
|
|
NS_IF_RELEASE(gLangService);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/* static */ uint8_t
|
2008-12-05 15:19:27 -08:00
|
|
|
gfxFontconfigUtils::FcSlantToThebesStyle(int aFcSlant)
|
2008-09-15 21:40:57 -07:00
|
|
|
{
|
2008-12-05 15:19:27 -08:00
|
|
|
switch (aFcSlant) {
|
|
|
|
case FC_SLANT_ITALIC:
|
2012-03-15 02:04:46 -07:00
|
|
|
return NS_FONT_STYLE_ITALIC;
|
2008-12-05 15:19:27 -08:00
|
|
|
case FC_SLANT_OBLIQUE:
|
2012-03-15 02:04:46 -07:00
|
|
|
return NS_FONT_STYLE_OBLIQUE;
|
2008-12-05 15:19:27 -08:00
|
|
|
default:
|
2012-03-15 02:04:46 -07:00
|
|
|
return NS_FONT_STYLE_NORMAL;
|
2008-09-15 21:40:57 -07:00
|
|
|
}
|
2008-12-05 15:19:27 -08:00
|
|
|
}
|
2008-09-15 21:40:57 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/* static */ uint8_t
|
2008-12-05 15:19:27 -08:00
|
|
|
gfxFontconfigUtils::GetThebesStyle(FcPattern *aPattern)
|
|
|
|
{
|
|
|
|
int slant;
|
|
|
|
if (FcPatternGetInteger(aPattern, FC_SLANT, 0, &slant) != FcResultMatch) {
|
2012-03-15 02:04:46 -07:00
|
|
|
return NS_FONT_STYLE_NORMAL;
|
2008-12-05 15:19:27 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return FcSlantToThebesStyle(slant);
|
2008-09-15 21:40:57 -07:00
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
/* static */ int
|
|
|
|
gfxFontconfigUtils::GetFcSlant(const gfxFontStyle& aFontStyle)
|
|
|
|
{
|
2012-03-15 02:04:46 -07:00
|
|
|
if (aFontStyle.style == NS_FONT_STYLE_ITALIC)
|
2008-11-06 18:21:34 -08:00
|
|
|
return FC_SLANT_ITALIC;
|
2012-03-15 02:04:46 -07:00
|
|
|
if (aFontStyle.style == NS_FONT_STYLE_OBLIQUE)
|
2008-11-06 18:21:34 -08:00
|
|
|
return FC_SLANT_OBLIQUE;
|
|
|
|
|
|
|
|
return FC_SLANT_ROMAN;
|
|
|
|
}
|
|
|
|
|
2008-09-15 21:40:57 -07:00
|
|
|
// OS/2 weight classes were introduced in fontconfig-2.1.93 (2003).
|
|
|
|
#ifndef FC_WEIGHT_THIN
|
|
|
|
#define FC_WEIGHT_THIN 0 // 2.1.93
|
|
|
|
#define FC_WEIGHT_EXTRALIGHT 40 // 2.1.93
|
|
|
|
#define FC_WEIGHT_REGULAR 80 // 2.1.93
|
|
|
|
#define FC_WEIGHT_EXTRABOLD 205 // 2.1.93
|
|
|
|
#endif
|
|
|
|
// book was introduced in fontconfig-2.2.90 (and so fontconfig-2.3.0 in 2005)
|
|
|
|
#ifndef FC_WEIGHT_BOOK
|
|
|
|
#define FC_WEIGHT_BOOK 75
|
|
|
|
#endif
|
2008-11-06 18:21:34 -08:00
|
|
|
// extra black was introduced in fontconfig-2.4.91 (2007)
|
|
|
|
#ifndef FC_WEIGHT_EXTRABLACK
|
|
|
|
#define FC_WEIGHT_EXTRABLACK 215
|
|
|
|
#endif
|
2008-09-15 21:40:57 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/* static */ uint16_t
|
2008-09-15 21:40:57 -07:00
|
|
|
gfxFontconfigUtils::GetThebesWeight(FcPattern *aPattern)
|
|
|
|
{
|
|
|
|
int weight;
|
|
|
|
if (FcPatternGetInteger(aPattern, FC_WEIGHT, 0, &weight) != FcResultMatch)
|
2012-03-15 02:04:46 -07:00
|
|
|
return NS_FONT_WEIGHT_NORMAL;
|
2008-09-15 21:40:57 -07:00
|
|
|
|
|
|
|
if (weight <= (FC_WEIGHT_THIN + FC_WEIGHT_EXTRALIGHT) / 2)
|
|
|
|
return 100;
|
|
|
|
if (weight <= (FC_WEIGHT_EXTRALIGHT + FC_WEIGHT_LIGHT) / 2)
|
|
|
|
return 200;
|
|
|
|
if (weight <= (FC_WEIGHT_LIGHT + FC_WEIGHT_BOOK) / 2)
|
|
|
|
return 300;
|
|
|
|
if (weight <= (FC_WEIGHT_REGULAR + FC_WEIGHT_MEDIUM) / 2)
|
|
|
|
// This includes FC_WEIGHT_BOOK
|
|
|
|
return 400;
|
|
|
|
if (weight <= (FC_WEIGHT_MEDIUM + FC_WEIGHT_DEMIBOLD) / 2)
|
|
|
|
return 500;
|
|
|
|
if (weight <= (FC_WEIGHT_DEMIBOLD + FC_WEIGHT_BOLD) / 2)
|
|
|
|
return 600;
|
|
|
|
if (weight <= (FC_WEIGHT_BOLD + FC_WEIGHT_EXTRABOLD) / 2)
|
|
|
|
return 700;
|
|
|
|
if (weight <= (FC_WEIGHT_EXTRABOLD + FC_WEIGHT_BLACK) / 2)
|
|
|
|
return 800;
|
|
|
|
if (weight <= FC_WEIGHT_BLACK)
|
|
|
|
return 900;
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
// including FC_WEIGHT_EXTRABLACK
|
2008-09-15 21:40:57 -07:00
|
|
|
return 901;
|
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
/* static */ int
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxFontconfigUtils::FcWeightForBaseWeight(int8_t aBaseWeight)
|
2008-11-06 18:21:34 -08:00
|
|
|
{
|
2009-03-10 19:00:43 -07:00
|
|
|
NS_PRECONDITION(aBaseWeight >= 0 && aBaseWeight <= 10,
|
|
|
|
"base weight out of range");
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
switch (aBaseWeight) {
|
|
|
|
case 2:
|
|
|
|
return FC_WEIGHT_EXTRALIGHT;
|
|
|
|
case 3:
|
|
|
|
return FC_WEIGHT_LIGHT;
|
|
|
|
case 4:
|
|
|
|
return FC_WEIGHT_REGULAR;
|
|
|
|
case 5:
|
|
|
|
return FC_WEIGHT_MEDIUM;
|
|
|
|
case 6:
|
|
|
|
return FC_WEIGHT_DEMIBOLD;
|
|
|
|
case 7:
|
|
|
|
return FC_WEIGHT_BOLD;
|
|
|
|
case 8:
|
|
|
|
return FC_WEIGHT_EXTRABOLD;
|
|
|
|
case 9:
|
|
|
|
return FC_WEIGHT_BLACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// extremes
|
|
|
|
return aBaseWeight < 2 ? FC_WEIGHT_THIN : FC_WEIGHT_EXTRABLACK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
/* static */ int16_t
|
2011-09-05 00:33:43 -07:00
|
|
|
gfxFontconfigUtils::GetThebesStretch(FcPattern *aPattern)
|
|
|
|
{
|
|
|
|
int width;
|
|
|
|
if (FcPatternGetInteger(aPattern, FC_WIDTH, 0, &width) != FcResultMatch) {
|
|
|
|
return NS_FONT_STRETCH_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width <= (FC_WIDTH_ULTRACONDENSED + FC_WIDTH_EXTRACONDENSED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_ULTRA_CONDENSED;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_EXTRACONDENSED + FC_WIDTH_CONDENSED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_EXTRA_CONDENSED;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_CONDENSED + FC_WIDTH_SEMICONDENSED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_CONDENSED;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_SEMICONDENSED + FC_WIDTH_NORMAL) / 2) {
|
|
|
|
return NS_FONT_STRETCH_SEMI_CONDENSED;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_NORMAL + FC_WIDTH_SEMIEXPANDED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_NORMAL;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_SEMIEXPANDED + FC_WIDTH_EXPANDED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_SEMI_EXPANDED;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_EXPANDED + FC_WIDTH_EXTRAEXPANDED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_EXPANDED;
|
|
|
|
}
|
|
|
|
if (width <= (FC_WIDTH_EXTRAEXPANDED + FC_WIDTH_ULTRAEXPANDED) / 2) {
|
|
|
|
return NS_FONT_STRETCH_EXTRA_EXPANDED;
|
|
|
|
}
|
|
|
|
return NS_FONT_STRETCH_ULTRA_EXPANDED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ int
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxFontconfigUtils::FcWidthForThebesStretch(int16_t aStretch)
|
2011-09-05 00:33:43 -07:00
|
|
|
{
|
|
|
|
switch (aStretch) {
|
|
|
|
default: // this will catch "normal" (0) as well as out-of-range values
|
|
|
|
return FC_WIDTH_NORMAL;
|
|
|
|
case NS_FONT_STRETCH_ULTRA_CONDENSED:
|
|
|
|
return FC_WIDTH_ULTRACONDENSED;
|
|
|
|
case NS_FONT_STRETCH_EXTRA_CONDENSED:
|
|
|
|
return FC_WIDTH_EXTRACONDENSED;
|
|
|
|
case NS_FONT_STRETCH_CONDENSED:
|
|
|
|
return FC_WIDTH_CONDENSED;
|
|
|
|
case NS_FONT_STRETCH_SEMI_CONDENSED:
|
|
|
|
return FC_WIDTH_SEMICONDENSED;
|
|
|
|
case NS_FONT_STRETCH_SEMI_EXPANDED:
|
|
|
|
return FC_WIDTH_SEMIEXPANDED;
|
|
|
|
case NS_FONT_STRETCH_EXPANDED:
|
|
|
|
return FC_WIDTH_EXPANDED;
|
|
|
|
case NS_FONT_STRETCH_EXTRA_EXPANDED:
|
|
|
|
return FC_WIDTH_EXTRAEXPANDED;
|
|
|
|
case NS_FONT_STRETCH_ULTRA_EXPANDED:
|
|
|
|
return FC_WIDTH_ULTRAEXPANDED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
// This makes a guess at an FC_WEIGHT corresponding to a base weight and
|
|
|
|
// offset (without any knowledge of which weights are available).
|
|
|
|
|
|
|
|
/* static */ int
|
|
|
|
GuessFcWeight(const gfxFontStyle& aFontStyle)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* weights come in two parts crammed into one
|
|
|
|
* integer -- the "base" weight is weight / 100,
|
|
|
|
* the rest of the value is the "offset" from that
|
|
|
|
* weight -- the number of steps to move to adjust
|
|
|
|
* the weight in the list of supported font weights,
|
|
|
|
* this value can be negative or positive.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
int8_t weight = aFontStyle.ComputeWeight();
|
2008-11-06 18:21:34 -08:00
|
|
|
|
2010-11-10 07:49:52 -08:00
|
|
|
// ComputeWeight trimmed the range of weights for us
|
2008-11-06 18:21:34 -08:00
|
|
|
NS_ASSERTION(weight >= 0 && weight <= 10,
|
|
|
|
"base weight out of range");
|
|
|
|
|
|
|
|
return gfxFontconfigUtils::FcWeightForBaseWeight(weight);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddString(FcPattern *aPattern, const char *object, const char *aString)
|
|
|
|
{
|
2008-12-05 15:19:27 -08:00
|
|
|
FcPatternAddString(aPattern, object,
|
|
|
|
gfxFontconfigUtils::ToFcChar8(aString));
|
2008-11-06 18:21:34 -08:00
|
|
|
}
|
|
|
|
|
2011-03-23 20:14:32 -07:00
|
|
|
static void
|
|
|
|
AddWeakString(FcPattern *aPattern, const char *object, const char *aString)
|
|
|
|
{
|
|
|
|
FcValue value;
|
|
|
|
value.type = FcTypeString;
|
|
|
|
value.u.s = gfxFontconfigUtils::ToFcChar8(aString);
|
|
|
|
|
|
|
|
FcPatternAddWeak(aPattern, object, value, FcTrue);
|
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
static void
|
2010-11-07 14:44:51 -08:00
|
|
|
AddLangGroup(FcPattern *aPattern, nsIAtom *aLangGroup)
|
2008-11-06 18:21:34 -08:00
|
|
|
{
|
|
|
|
// Translate from mozilla's internal mapping into fontconfig's
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString lang;
|
2008-11-06 18:21:34 -08:00
|
|
|
gfxFontconfigUtils::GetSampleLangForGroup(aLangGroup, &lang);
|
|
|
|
|
|
|
|
if (!lang.IsEmpty()) {
|
|
|
|
AddString(aPattern, FC_LANG, lang.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsReturnRef<FcPattern>
|
2008-12-05 15:19:27 -08:00
|
|
|
gfxFontconfigUtils::NewPattern(const nsTArray<nsString>& aFamilies,
|
2008-11-06 18:21:34 -08:00
|
|
|
const gfxFontStyle& aFontStyle,
|
|
|
|
const char *aLang)
|
|
|
|
{
|
2011-03-23 20:14:32 -07:00
|
|
|
static const char* sFontconfigGenerics[] =
|
|
|
|
{ "sans-serif", "serif", "monospace", "fantasy", "cursive" };
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
nsAutoRef<FcPattern> pattern(FcPatternCreate());
|
|
|
|
if (!pattern)
|
|
|
|
return nsReturnRef<FcPattern>();
|
|
|
|
|
|
|
|
FcPatternAddDouble(pattern, FC_PIXEL_SIZE, aFontStyle.size);
|
|
|
|
FcPatternAddInteger(pattern, FC_SLANT, GetFcSlant(aFontStyle));
|
|
|
|
FcPatternAddInteger(pattern, FC_WEIGHT, GuessFcWeight(aFontStyle));
|
2011-09-05 00:33:43 -07:00
|
|
|
FcPatternAddInteger(pattern, FC_WIDTH, FcWidthForThebesStretch(aFontStyle.stretch));
|
2008-11-06 18:21:34 -08:00
|
|
|
|
|
|
|
if (aLang) {
|
|
|
|
AddString(pattern, FC_LANG, aLang);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool useWeakBinding = false;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < aFamilies.Length(); ++i) {
|
2008-12-05 15:19:27 -08:00
|
|
|
NS_ConvertUTF16toUTF8 family(aFamilies[i]);
|
2011-03-23 20:14:32 -07:00
|
|
|
if (!useWeakBinding) {
|
|
|
|
AddString(pattern, FC_FAMILY, family.get());
|
|
|
|
|
|
|
|
// fontconfig generic families are typically implemented with weak
|
|
|
|
// aliases (so that the preferred font depends on language).
|
|
|
|
// However, this would give them lower priority than subsequent
|
|
|
|
// non-generic families in the list. To ensure that subsequent
|
|
|
|
// families do not have a higher priority, they are given weak
|
|
|
|
// bindings.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t g = 0;
|
2011-10-10 22:50:08 -07:00
|
|
|
g < ArrayLength(sFontconfigGenerics);
|
2011-03-23 20:14:32 -07:00
|
|
|
++g) {
|
2011-08-25 15:57:51 -07:00
|
|
|
if (0 == FcStrCmpIgnoreCase(ToFcChar8(sFontconfigGenerics[g]),
|
|
|
|
ToFcChar8(family.get()))) {
|
2011-10-17 07:59:28 -07:00
|
|
|
useWeakBinding = true;
|
2011-03-23 20:14:32 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AddWeakString(pattern, FC_FAMILY, family.get());
|
|
|
|
}
|
2008-11-06 18:21:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return pattern.out();
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxFontconfigUtils::gfxFontconfigUtils()
|
2013-07-31 08:44:31 -07:00
|
|
|
: mLastConfig(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-11-06 18:21:34 -08:00
|
|
|
mFontsByFamily.Init(50);
|
2009-01-02 19:21:49 -08:00
|
|
|
mFontsByFullname.Init(50);
|
2008-11-06 18:21:34 -08:00
|
|
|
mLangSupportTable.Init(20);
|
|
|
|
UpdateFontListInternal();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2010-02-24 09:57:57 -08:00
|
|
|
gfxFontconfigUtils::GetFontList(nsIAtom *aLangGroup,
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsACString& aGenericFamily,
|
2009-01-18 12:14:14 -08:00
|
|
|
nsTArray<nsString>& aListOfFonts)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
aListOfFonts.Clear();
|
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> fonts;
|
2010-11-07 14:44:51 -08:00
|
|
|
nsresult rv = GetFontListInternal(fonts, aLangGroup);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < fonts.Length(); ++i) {
|
2009-01-21 20:15:34 -08:00
|
|
|
aListOfFonts.AppendElement(NS_ConvertUTF8toUTF16(fonts[i]));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-08-26 14:34:03 -07:00
|
|
|
aListOfFonts.Sort();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t serif = 0, sansSerif = 0, monospace = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Fontconfig supports 3 generic fonts, "serif", "sans-serif", and
|
|
|
|
// "monospace", slightly different from CSS's 5.
|
|
|
|
if (aGenericFamily.IsEmpty())
|
|
|
|
serif = sansSerif = monospace = 1;
|
|
|
|
else if (aGenericFamily.LowerCaseEqualsLiteral("serif"))
|
|
|
|
serif = 1;
|
|
|
|
else if (aGenericFamily.LowerCaseEqualsLiteral("sans-serif"))
|
|
|
|
sansSerif = 1;
|
|
|
|
else if (aGenericFamily.LowerCaseEqualsLiteral("monospace"))
|
|
|
|
monospace = 1;
|
|
|
|
else if (aGenericFamily.LowerCaseEqualsLiteral("cursive") ||
|
|
|
|
aGenericFamily.LowerCaseEqualsLiteral("fantasy"))
|
|
|
|
serif = sansSerif = 1;
|
|
|
|
else
|
|
|
|
NS_NOTREACHED("unexpected CSS generic font family");
|
|
|
|
|
2007-08-26 14:34:03 -07:00
|
|
|
// The first in the list becomes the default in
|
|
|
|
// gFontsDialog.readFontSelection() if the preference-selected font is not
|
|
|
|
// available, so put system configured defaults first.
|
2007-03-22 10:30:00 -07:00
|
|
|
if (monospace)
|
2009-01-18 12:14:14 -08:00
|
|
|
aListOfFonts.InsertElementAt(0, NS_LITERAL_STRING("monospace"));
|
2007-08-26 14:34:03 -07:00
|
|
|
if (sansSerif)
|
2009-01-18 12:14:14 -08:00
|
|
|
aListOfFonts.InsertElementAt(0, NS_LITERAL_STRING("sans-serif"));
|
2007-08-26 14:34:03 -07:00
|
|
|
if (serif)
|
2009-01-18 12:14:14 -08:00
|
|
|
aListOfFonts.InsertElementAt(0, NS_LITERAL_STRING("serif"));
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-25 21:40:28 -07:00
|
|
|
struct MozLangGroupData {
|
2010-11-07 14:44:51 -08:00
|
|
|
nsIAtom* const& mozLangGroup;
|
2008-09-25 21:40:28 -07:00
|
|
|
const char *defaultLang;
|
2007-07-17 14:58:45 -07:00
|
|
|
};
|
|
|
|
|
2008-09-25 21:40:28 -07:00
|
|
|
const MozLangGroupData MozLangGroups[] = {
|
2012-03-29 14:08:43 -07:00
|
|
|
{ nsGkAtoms::x_western, "en" },
|
|
|
|
{ nsGkAtoms::x_central_euro, "pl" },
|
|
|
|
{ nsGkAtoms::x_cyrillic, "ru" },
|
|
|
|
{ nsGkAtoms::x_baltic, "lv" },
|
|
|
|
{ nsGkAtoms::x_devanagari, "hi" },
|
|
|
|
{ nsGkAtoms::x_tamil, "ta" },
|
|
|
|
{ nsGkAtoms::x_armn, "hy" },
|
|
|
|
{ nsGkAtoms::x_beng, "bn" },
|
|
|
|
{ nsGkAtoms::x_cans, "iu" },
|
|
|
|
{ nsGkAtoms::x_ethi, "am" },
|
|
|
|
{ nsGkAtoms::x_geor, "ka" },
|
|
|
|
{ nsGkAtoms::x_gujr, "gu" },
|
|
|
|
{ nsGkAtoms::x_guru, "pa" },
|
|
|
|
{ nsGkAtoms::x_khmr, "km" },
|
|
|
|
{ nsGkAtoms::x_knda, "kn" },
|
|
|
|
{ nsGkAtoms::x_mlym, "ml" },
|
|
|
|
{ nsGkAtoms::x_orya, "or" },
|
|
|
|
{ nsGkAtoms::x_sinh, "si" },
|
|
|
|
{ nsGkAtoms::x_telu, "te" },
|
|
|
|
{ nsGkAtoms::x_tibt, "bo" },
|
|
|
|
{ nsGkAtoms::Unicode, 0 },
|
|
|
|
{ nsGkAtoms::x_user_def, 0 }
|
2007-07-17 14:58:45 -07:00
|
|
|
};
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
static bool
|
2008-09-25 21:40:28 -07:00
|
|
|
TryLangForGroup(const nsACString& aOSLang, nsIAtom *aLangGroup,
|
|
|
|
nsACString *aFcLang)
|
2007-07-17 14:58:45 -07:00
|
|
|
{
|
2008-09-25 21:40:28 -07:00
|
|
|
// Truncate at '.' or '@' from aOSLang, and convert '_' to '-'.
|
|
|
|
// aOSLang is in the form "language[_territory][.codeset][@modifier]".
|
|
|
|
// fontconfig takes languages in the form "language-territory".
|
|
|
|
// nsILanguageAtomService takes languages in the form language-subtag,
|
|
|
|
// where subtag may be a territory. fontconfig and nsILanguageAtomService
|
|
|
|
// handle case-conversion for us.
|
|
|
|
const char *pos, *end;
|
|
|
|
aOSLang.BeginReading(pos);
|
|
|
|
aOSLang.EndReading(end);
|
|
|
|
aFcLang->Truncate();
|
|
|
|
while (pos < end) {
|
|
|
|
switch (*pos) {
|
|
|
|
case '.':
|
|
|
|
case '@':
|
|
|
|
end = pos;
|
|
|
|
break;
|
|
|
|
case '_':
|
|
|
|
aFcLang->Append('-');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
aFcLang->Append(*pos);
|
|
|
|
}
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom *atom =
|
2011-05-30 22:09:05 -07:00
|
|
|
gLangService->LookupLanguage(*aFcLang);
|
2008-09-25 21:40:28 -07:00
|
|
|
|
|
|
|
return atom == aLangGroup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2010-11-07 14:44:51 -08:00
|
|
|
gfxFontconfigUtils::GetSampleLangForGroup(nsIAtom *aLangGroup,
|
2008-09-25 21:40:28 -07:00
|
|
|
nsACString *aFcLang)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aFcLang != nullptr, "aFcLang must not be NULL");
|
2008-09-25 21:40:28 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
const MozLangGroupData *langGroup = nullptr;
|
2008-09-25 21:40:28 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
for (unsigned int i = 0; i < ArrayLength(MozLangGroups); ++i) {
|
2010-11-07 14:44:51 -08:00
|
|
|
if (aLangGroup == MozLangGroups[i].mozLangGroup) {
|
2008-09-25 21:40:28 -07:00
|
|
|
langGroup = &MozLangGroups[i];
|
|
|
|
break;
|
2007-07-17 14:58:45 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-25 21:40:28 -07:00
|
|
|
if (!langGroup) {
|
|
|
|
// Not a special mozilla language group.
|
|
|
|
// Use aLangGroup as a language code.
|
2010-11-07 14:44:51 -08:00
|
|
|
aLangGroup->ToUTF8String(*aFcLang);
|
2008-09-25 21:40:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the environment for the users preferred language that corresponds
|
|
|
|
// to this langGroup.
|
|
|
|
if (!gLangService) {
|
|
|
|
CallGetService(NS_LANGUAGEATOMSERVICE_CONTRACTID, &gLangService);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gLangService) {
|
|
|
|
const char *languages = getenv("LANGUAGE");
|
|
|
|
if (languages) {
|
|
|
|
const char separator = ':';
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
for (const char *pos = languages; true; ++pos) {
|
2008-09-25 21:40:28 -07:00
|
|
|
if (*pos == '\0' || *pos == separator) {
|
|
|
|
if (languages < pos &&
|
|
|
|
TryLangForGroup(Substring(languages, pos),
|
2010-11-07 14:44:51 -08:00
|
|
|
aLangGroup, aFcLang))
|
2008-09-25 21:40:28 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (*pos == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
languages = pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-31 08:44:31 -07:00
|
|
|
const char *ctype = setlocale(LC_CTYPE, nullptr);
|
2008-09-25 21:40:28 -07:00
|
|
|
if (ctype &&
|
2010-11-07 14:44:51 -08:00
|
|
|
TryLangForGroup(nsDependentCString(ctype), aLangGroup, aFcLang))
|
2008-09-25 21:40:28 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (langGroup->defaultLang) {
|
|
|
|
aFcLang->Assign(langGroup->defaultLang);
|
|
|
|
} else {
|
|
|
|
aFcLang->Truncate();
|
|
|
|
}
|
2007-07-17 14:58:45 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
2009-01-21 20:15:34 -08:00
|
|
|
gfxFontconfigUtils::GetFontListInternal(nsTArray<nsCString>& aListOfFonts,
|
2010-11-07 14:44:51 -08:00
|
|
|
nsIAtom *aLangGroup)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-07-31 08:44:31 -07:00
|
|
|
FcPattern *pat = nullptr;
|
|
|
|
FcObjectSet *os = nullptr;
|
|
|
|
FcFontSet *fs = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
aListOfFonts.Clear();
|
|
|
|
|
|
|
|
pat = FcPatternCreate();
|
|
|
|
if (!pat)
|
|
|
|
goto end;
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
os = FcObjectSetBuild(FC_FAMILY, nullptr);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!os)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
// take the pattern and add the lang group to it
|
2010-11-07 14:44:51 -08:00
|
|
|
if (aLangGroup) {
|
2008-11-06 18:21:34 -08:00
|
|
|
AddLangGroup(pat, aLangGroup);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
fs = FcFontList(nullptr, pat, os);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!fs)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
for (int i = 0; i < fs->nfont; i++) {
|
|
|
|
char *family;
|
|
|
|
|
|
|
|
if (FcPatternGetString(fs->fonts[i], FC_FAMILY, 0,
|
|
|
|
(FcChar8 **) &family) != FcResultMatch)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove duplicates...
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString strFamily(family);
|
2009-01-21 20:15:34 -08:00
|
|
|
if (aListOfFonts.Contains(strFamily))
|
2007-03-22 10:30:00 -07:00
|
|
|
continue;
|
|
|
|
|
2009-01-21 20:15:34 -08:00
|
|
|
aListOfFonts.AppendElement(strFamily);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
aListOfFonts.Clear();
|
|
|
|
|
|
|
|
if (pat)
|
|
|
|
FcPatternDestroy(pat);
|
|
|
|
if (os)
|
|
|
|
FcObjectSetDestroy(os);
|
|
|
|
if (fs)
|
|
|
|
FcFontSetDestroy(fs);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
gfxFontconfigUtils::UpdateFontList()
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
return UpdateFontListInternal(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
gfxFontconfigUtils::UpdateFontListInternal(bool aForce)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-09-21 13:24:28 -07:00
|
|
|
if (!aForce) {
|
|
|
|
// This checks periodically according to fontconfig's configured
|
|
|
|
// <rescan> interval.
|
|
|
|
FcInitBringUptoDate();
|
2013-07-31 08:44:31 -07:00
|
|
|
} else if (!FcConfigUptoDate(nullptr)) { // check now with aForce
|
|
|
|
mLastConfig = nullptr;
|
2008-09-21 13:24:28 -07:00
|
|
|
FcInitReinitialize();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-09-21 13:24:28 -07:00
|
|
|
// FcInitReinitialize() (used by FcInitBringUptoDate) creates a new config
|
|
|
|
// before destroying the old config, so the only way that we'd miss an
|
|
|
|
// update is if fontconfig did more than one update and the memory for the
|
|
|
|
// most recent config happened to be at the same location as the original
|
|
|
|
// config.
|
|
|
|
FcConfig *currentConfig = FcConfigGetCurrent();
|
|
|
|
if (currentConfig == mLastConfig)
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
// This FcFontSet is owned by fontconfig
|
|
|
|
FcFontSet *fontSet = FcConfigGetFonts(currentConfig, FcSetSystem);
|
2008-11-05 15:26:22 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
mFontsByFamily.Clear();
|
2009-01-02 19:21:49 -08:00
|
|
|
mFontsByFullname.Clear();
|
2008-11-06 18:21:34 -08:00
|
|
|
mLangSupportTable.Clear();
|
|
|
|
mAliasForMultiFonts.Clear();
|
2008-11-06 13:49:14 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
// Record the existing font families
|
|
|
|
for (int f = 0; f < fontSet->nfont; ++f) {
|
|
|
|
FcPattern *font = fontSet->fonts[f];
|
|
|
|
|
|
|
|
FcChar8 *family;
|
|
|
|
for (int v = 0;
|
|
|
|
FcPatternGetString(font, FC_FAMILY, v, &family) == FcResultMatch;
|
|
|
|
++v) {
|
|
|
|
FontsByFcStrEntry *entry = mFontsByFamily.PutEntry(family);
|
|
|
|
if (entry) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool added = entry->AddFont(font);
|
2008-11-06 18:21:34 -08:00
|
|
|
|
|
|
|
if (!entry->mKey) {
|
|
|
|
// The reference to the font pattern keeps the pointer to
|
|
|
|
// string for the key valid. If adding the font failed
|
|
|
|
// then the entry must be removed.
|
|
|
|
if (added) {
|
|
|
|
entry->mKey = family;
|
|
|
|
} else {
|
|
|
|
mFontsByFamily.RawRemoveEntry(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// XXX we don't support all alias names.
|
|
|
|
// Because if we don't check whether the given font name is alias name,
|
|
|
|
// fontconfig converts the non existing font to sans-serif.
|
|
|
|
// This is not good if the web page specifies font-family
|
|
|
|
// that has Windows font name in the first.
|
2011-06-11 19:30:16 -07:00
|
|
|
NS_ENSURE_TRUE(Preferences::GetRootBranch(), NS_ERROR_FAILURE);
|
|
|
|
nsAdoptingCString list = Preferences::GetCString("font.alias-list");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!list.IsEmpty()) {
|
|
|
|
const char kComma = ',';
|
|
|
|
const char *p, *p_end;
|
|
|
|
list.BeginReading(p);
|
|
|
|
list.EndReading(p_end);
|
|
|
|
while (p < p_end) {
|
|
|
|
while (nsCRT::IsAsciiSpace(*p)) {
|
|
|
|
if (++p == p_end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p == p_end)
|
|
|
|
break;
|
|
|
|
const char *start = p;
|
|
|
|
while (++p != p_end && *p != kComma)
|
|
|
|
/* nothing */ ;
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString name(Substring(start, p));
|
2011-10-17 07:59:28 -07:00
|
|
|
name.CompressWhitespace(false, true);
|
2009-01-21 20:15:34 -08:00
|
|
|
mAliasForMultiFonts.AppendElement(name);
|
2007-03-22 10:30:00 -07:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-21 13:24:28 -07:00
|
|
|
mLastConfig = currentConfig;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-03-13 03:32:50 -07:00
|
|
|
nsresult
|
|
|
|
gfxFontconfigUtils::GetStandardFamilyName(const nsAString& aFontName, nsAString& aFamilyName)
|
|
|
|
{
|
|
|
|
aFamilyName.Truncate();
|
|
|
|
|
|
|
|
// The fontconfig has generic family names in the font list.
|
|
|
|
if (aFontName.EqualsLiteral("serif") ||
|
|
|
|
aFontName.EqualsLiteral("sans-serif") ||
|
|
|
|
aFontName.EqualsLiteral("monospace")) {
|
|
|
|
aFamilyName.Assign(aFontName);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-21 13:24:28 -07:00
|
|
|
nsresult rv = UpdateFontListInternal();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2008-03-13 03:32:50 -07:00
|
|
|
NS_ConvertUTF16toUTF8 fontname(aFontName);
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
// return empty string if no such family exists
|
|
|
|
if (!IsExistingFamily(fontname))
|
2008-03-13 03:32:50 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
FcPattern *pat = nullptr;
|
|
|
|
FcObjectSet *os = nullptr;
|
|
|
|
FcFontSet *givenFS = nullptr;
|
2009-01-21 20:15:34 -08:00
|
|
|
nsTArray<nsCString> candidates;
|
2013-07-31 08:44:31 -07:00
|
|
|
FcFontSet *candidateFS = nullptr;
|
2008-09-21 13:24:28 -07:00
|
|
|
rv = NS_ERROR_FAILURE;
|
2008-03-13 03:32:50 -07:00
|
|
|
|
|
|
|
pat = FcPatternCreate();
|
|
|
|
if (!pat)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
FcPatternAddString(pat, FC_FAMILY, (FcChar8 *)fontname.get());
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
os = FcObjectSetBuild(FC_FAMILY, FC_FILE, FC_INDEX, nullptr);
|
2008-03-13 03:32:50 -07:00
|
|
|
if (!os)
|
|
|
|
goto end;
|
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
givenFS = FcFontList(nullptr, pat, os);
|
2008-03-13 03:32:50 -07:00
|
|
|
if (!givenFS)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
// The first value associated with a FC_FAMILY property is the family
|
|
|
|
// returned by GetFontList(), so use this value if appropriate.
|
|
|
|
|
|
|
|
// See if there is a font face with first family equal to the given family.
|
|
|
|
for (int i = 0; i < givenFS->nfont; ++i) {
|
|
|
|
char *firstFamily;
|
|
|
|
if (FcPatternGetString(givenFS->fonts[i], FC_FAMILY, 0,
|
|
|
|
(FcChar8 **) &firstFamily) != FcResultMatch)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nsDependentCString first(firstFamily);
|
2009-01-21 20:15:34 -08:00
|
|
|
if (!candidates.Contains(first)) {
|
|
|
|
candidates.AppendElement(first);
|
2008-03-13 03:32:50 -07:00
|
|
|
|
|
|
|
if (fontname.Equals(first)) {
|
|
|
|
aFamilyName.Assign(aFontName);
|
|
|
|
rv = NS_OK;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if any of the first family names represent the same set of font
|
|
|
|
// faces as the given family.
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t j = 0; j < candidates.Length(); ++j) {
|
2008-03-13 03:32:50 -07:00
|
|
|
FcPatternDel(pat, FC_FAMILY);
|
2009-01-21 20:15:34 -08:00
|
|
|
FcPatternAddString(pat, FC_FAMILY, (FcChar8 *)candidates[j].get());
|
2008-03-13 03:32:50 -07:00
|
|
|
|
2013-07-31 08:44:31 -07:00
|
|
|
candidateFS = FcFontList(nullptr, pat, os);
|
2008-03-13 03:32:50 -07:00
|
|
|
if (!candidateFS)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (candidateFS->nfont != givenFS->nfont)
|
|
|
|
continue;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool equal = true;
|
2008-03-13 03:32:50 -07:00
|
|
|
for (int i = 0; i < givenFS->nfont; ++i) {
|
|
|
|
if (!FcPatternEqual(candidateFS->fonts[i], givenFS->fonts[i])) {
|
2011-10-17 07:59:28 -07:00
|
|
|
equal = false;
|
2008-03-13 03:32:50 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (equal) {
|
2009-01-21 20:15:34 -08:00
|
|
|
AppendUTF8toUTF16(candidates[j], aFamilyName);
|
2008-03-13 03:32:50 -07:00
|
|
|
rv = NS_OK;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No match found; return empty string.
|
|
|
|
rv = NS_OK;
|
|
|
|
|
|
|
|
end:
|
|
|
|
if (pat)
|
|
|
|
FcPatternDestroy(pat);
|
|
|
|
if (os)
|
|
|
|
FcObjectSetDestroy(os);
|
|
|
|
if (givenFS)
|
|
|
|
FcFontSetDestroy(givenFS);
|
|
|
|
if (candidateFS)
|
|
|
|
FcFontSetDestroy(candidateFS);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult
|
|
|
|
gfxFontconfigUtils::ResolveFontName(const nsAString& aFontName,
|
|
|
|
gfxPlatform::FontResolverCallback aCallback,
|
|
|
|
void *aClosure,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool& aAborted)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
aAborted = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = UpdateFontListInternal();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 fontname(aFontName);
|
2008-11-06 18:21:34 -08:00
|
|
|
// Sometimes, the font has two or more names (e.g., "Sazanami Gothic" has
|
|
|
|
// Japanese localized name). We should not resolve to a single name
|
|
|
|
// because different names sometimes have different behavior. e.g., with
|
|
|
|
// the default settings of "Sazanami" on Fedora Core 5, the non-localized
|
|
|
|
// name uses anti-alias, but the localized name uses it. So, we should
|
|
|
|
// check just whether the font is existing, without resolving to regular
|
|
|
|
// name.
|
|
|
|
//
|
|
|
|
// The family names in mAliasForMultiFonts are names understood by
|
|
|
|
// fontconfig. The actual font to which they resolve depends on the
|
|
|
|
// entire match pattern. That info is not available here, but there
|
|
|
|
// will be a font so leave the resolving to the gfxFontGroup.
|
|
|
|
if (IsExistingFamily(fontname) ||
|
2009-01-21 20:15:34 -08:00
|
|
|
mAliasForMultiFonts.Contains(fontname, gfxIgnoreCaseCStringComparator()))
|
2008-11-06 18:21:34 -08:00
|
|
|
aAborted = !(*aCallback)(aFontName, aClosure);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2008-11-06 18:21:34 -08:00
|
|
|
gfxFontconfigUtils::IsExistingFamily(const nsCString& aFamilyName)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return mFontsByFamily.GetEntry(ToFcChar8(aFamilyName)) != nullptr;
|
2008-11-06 18:21:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray< nsCountedRef<FcPattern> >&
|
|
|
|
gfxFontconfigUtils::GetFontsForFamily(const FcChar8 *aFamilyName)
|
|
|
|
{
|
|
|
|
FontsByFcStrEntry *entry = mFontsByFamily.GetEntry(aFamilyName);
|
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
return mEmptyPatternArray;
|
|
|
|
|
|
|
|
return entry->GetFonts();
|
|
|
|
}
|
|
|
|
|
2009-01-02 19:21:49 -08:00
|
|
|
// Fontconfig only provides a fullname property for fonts in formats with SFNT
|
|
|
|
// wrappers. For other font formats (including PCF and PS Type 1), a fullname
|
|
|
|
// must be generated from the family and style properties. Only the first
|
|
|
|
// family and style is checked, but that should be OK, as I don't expect
|
|
|
|
// non-SFNT fonts to have multiple families or styles.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-01-02 19:21:49 -08:00
|
|
|
gfxFontconfigUtils::GetFullnameFromFamilyAndStyle(FcPattern *aFont,
|
|
|
|
nsACString *aFullname)
|
|
|
|
{
|
|
|
|
FcChar8 *family;
|
|
|
|
if (FcPatternGetString(aFont, FC_FAMILY, 0, &family) != FcResultMatch)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2009-01-02 19:21:49 -08:00
|
|
|
|
|
|
|
aFullname->Truncate();
|
|
|
|
aFullname->Append(ToCString(family));
|
|
|
|
|
|
|
|
FcChar8 *style;
|
|
|
|
if (FcPatternGetString(aFont, FC_STYLE, 0, &style) == FcResultMatch &&
|
|
|
|
strcmp(ToCString(style), "Regular") != 0) {
|
|
|
|
aFullname->Append(' ');
|
|
|
|
aFullname->Append(ToCString(style));
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2009-01-02 19:21:49 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2009-01-02 19:21:49 -08:00
|
|
|
gfxFontconfigUtils::FontsByFullnameEntry::KeyEquals(KeyTypePointer aKey) const
|
|
|
|
{
|
|
|
|
const FcChar8 *key = mKey;
|
2013-07-31 08:44:31 -07:00
|
|
|
// If mKey is nullptr, key comes from the style and family of the first
|
|
|
|
// font.
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString fullname;
|
2009-01-02 19:21:49 -08:00
|
|
|
if (!key) {
|
|
|
|
NS_ASSERTION(mFonts.Length(), "No font in FontsByFullnameEntry!");
|
|
|
|
GetFullnameFromFamilyAndStyle(mFonts[0], &fullname);
|
|
|
|
|
|
|
|
key = ToFcChar8(fullname);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FcStrCmpIgnoreCase(aKey, key) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxFontconfigUtils::AddFullnameEntries()
|
|
|
|
{
|
|
|
|
// This FcFontSet is owned by fontconfig
|
2013-07-31 08:44:31 -07:00
|
|
|
FcFontSet *fontSet = FcConfigGetFonts(nullptr, FcSetSystem);
|
2009-01-02 19:21:49 -08:00
|
|
|
|
|
|
|
// Record the existing font families
|
|
|
|
for (int f = 0; f < fontSet->nfont; ++f) {
|
|
|
|
FcPattern *font = fontSet->fonts[f];
|
|
|
|
|
|
|
|
int v = 0;
|
|
|
|
FcChar8 *fullname;
|
|
|
|
while (FcPatternGetString(font,
|
|
|
|
FC_FULLNAME, v, &fullname) == FcResultMatch) {
|
|
|
|
FontsByFullnameEntry *entry = mFontsByFullname.PutEntry(fullname);
|
|
|
|
if (entry) {
|
|
|
|
// entry always has space for one font, so the first AddFont
|
|
|
|
// will always succeed, and so the entry will always have a
|
|
|
|
// font from which to obtain the key.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool added = entry->AddFont(font);
|
2013-07-31 08:44:31 -07:00
|
|
|
// The key may be nullptr either if this is the first font, or
|
|
|
|
// if the first font does not have a fullname property, and so
|
2009-01-02 19:21:49 -08:00
|
|
|
// the key is obtained from the font. Set the key in both
|
|
|
|
// cases. The check that AddFont succeeded is required for
|
|
|
|
// the second case.
|
|
|
|
if (!entry->mKey && added) {
|
|
|
|
entry->mKey = fullname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fontconfig does not provide a fullname property for all fonts.
|
|
|
|
if (v == 0) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString name;
|
2009-01-02 19:21:49 -08:00
|
|
|
if (!GetFullnameFromFamilyAndStyle(font, &name))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
FontsByFullnameEntry *entry =
|
|
|
|
mFontsByFullname.PutEntry(ToFcChar8(name));
|
|
|
|
if (entry) {
|
|
|
|
entry->AddFont(font);
|
|
|
|
// Either entry->mKey has been set for a previous font or it
|
2013-07-31 08:44:31 -07:00
|
|
|
// remains nullptr to indicate that the key is obtained from
|
|
|
|
// the first font.
|
2009-01-02 19:21:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray< nsCountedRef<FcPattern> >&
|
|
|
|
gfxFontconfigUtils::GetFontsForFullname(const FcChar8 *aFullname)
|
|
|
|
{
|
|
|
|
if (mFontsByFullname.Count() == 0) {
|
|
|
|
AddFullnameEntries();
|
|
|
|
}
|
|
|
|
|
|
|
|
FontsByFullnameEntry *entry = mFontsByFullname.GetEntry(aFullname);
|
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
return mEmptyPatternArray;
|
|
|
|
|
|
|
|
return entry->GetFonts();
|
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
static FcLangResult
|
|
|
|
CompareLangString(const FcChar8 *aLangA, const FcChar8 *aLangB) {
|
|
|
|
FcLangResult result = FcLangDifferentLang;
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; ; ++i) {
|
2008-11-06 18:21:34 -08:00
|
|
|
FcChar8 a = FcToLower(aLangA[i]);
|
|
|
|
FcChar8 b = FcToLower(aLangB[i]);
|
|
|
|
|
|
|
|
if (a != b) {
|
|
|
|
if ((a == '\0' && b == '-') || (a == '-' && b == '\0'))
|
|
|
|
return FcLangDifferentCountry;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (a == '\0')
|
|
|
|
return FcLangEqual;
|
|
|
|
|
|
|
|
if (a == '-') {
|
|
|
|
result = FcLangDifferentCountry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
FcLangResult
|
|
|
|
gfxFontconfigUtils::GetLangSupport(FcPattern *aFont, const FcChar8 *aLang)
|
|
|
|
{
|
2008-12-05 15:19:27 -08:00
|
|
|
// When fontconfig builds a pattern for a system font, it will set a
|
|
|
|
// single LangSet property value for the font. That value may be removed
|
|
|
|
// and additional string values may be added through FcConfigSubsitute
|
|
|
|
// with FcMatchScan. Values that are neither LangSet nor string are
|
|
|
|
// considered errors in fontconfig sort and match functions.
|
|
|
|
//
|
|
|
|
// If no string nor LangSet value is found, then either the font is a
|
|
|
|
// system font and the LangSet has been removed through FcConfigSubsitute,
|
|
|
|
// or the font is a web font and its language support is unknown.
|
|
|
|
// Returning FcLangDifferentLang for these fonts ensures that this font
|
|
|
|
// will not be assumed to satisfy the language, and so language will be
|
|
|
|
// prioritized in sorting fallback fonts.
|
2008-11-06 18:21:34 -08:00
|
|
|
FcValue value;
|
|
|
|
FcLangResult best = FcLangDifferentLang;
|
2008-12-05 15:19:27 -08:00
|
|
|
for (int v = 0;
|
|
|
|
FcPatternGet(aFont, FC_LANG, v, &value) == FcResultMatch;
|
|
|
|
++v) {
|
2008-11-06 18:21:34 -08:00
|
|
|
|
|
|
|
FcLangResult support;
|
|
|
|
switch (value.type) {
|
|
|
|
case FcTypeLangSet:
|
|
|
|
support = FcLangSetHasLang(value.u.l, aLang);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
2008-11-06 18:21:34 -08:00
|
|
|
case FcTypeString:
|
|
|
|
support = CompareLangString(value.u.s, aLang);
|
|
|
|
break;
|
|
|
|
default:
|
2008-12-05 15:19:27 -08:00
|
|
|
// error. continue to see if there is a useful value.
|
|
|
|
continue;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
if (support < best) { // lower is better
|
|
|
|
if (support == FcLangEqual)
|
|
|
|
return support;
|
|
|
|
best = support;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
return best;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
gfxFontconfigUtils::LangSupportEntry *
|
2011-09-28 23:19:26 -07:00
|
|
|
gfxFontconfigUtils::GetLangSupportEntry(const FcChar8 *aLang, bool aWithFonts)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-11-06 18:21:34 -08:00
|
|
|
// Currently any unrecognized languages from documents will be converted
|
|
|
|
// to x-unicode by nsILanguageAtomService, so there is a limit on the
|
|
|
|
// langugages that will be added here. Reconsider when/if document
|
|
|
|
// languages are passed to this routine.
|
2008-11-05 14:14:02 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
LangSupportEntry *entry = mLangSupportTable.PutEntry(aLang);
|
|
|
|
if (!entry)
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
FcLangResult best = FcLangDifferentLang;
|
|
|
|
|
|
|
|
if (!entry->IsKeyInitialized()) {
|
|
|
|
entry->InitKey(aLang);
|
|
|
|
} else {
|
|
|
|
// mSupport is already initialized.
|
|
|
|
if (!aWithFonts)
|
|
|
|
return entry;
|
|
|
|
|
|
|
|
best = entry->mSupport;
|
|
|
|
// If there is support for this language, an empty font list indicates
|
|
|
|
// that the list hasn't been initialized yet.
|
|
|
|
if (best == FcLangDifferentLang || entry->mFonts.Length() > 0)
|
|
|
|
return entry;
|
|
|
|
}
|
2008-11-06 12:39:06 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
// This FcFontSet is owned by fontconfig
|
2013-07-31 08:44:31 -07:00
|
|
|
FcFontSet *fontSet = FcConfigGetFonts(nullptr, FcSetSystem);
|
2008-11-06 12:39:06 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
nsAutoTArray<FcPattern*,100> fonts;
|
2008-11-06 12:39:06 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
for (int f = 0; f < fontSet->nfont; ++f) {
|
|
|
|
FcPattern *font = fontSet->fonts[f];
|
2008-11-05 14:14:02 -08:00
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
FcLangResult support = GetLangSupport(font, aLang);
|
|
|
|
|
|
|
|
if (support < best) { // lower is better
|
|
|
|
best = support;
|
|
|
|
if (aWithFonts) {
|
|
|
|
fonts.Clear();
|
|
|
|
} else if (best == FcLangEqual) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The font list in the LangSupportEntry is expected to be used only
|
|
|
|
// when no default fonts support the language. There would be a large
|
|
|
|
// number of fonts in entries for languages using Latin script but
|
|
|
|
// these do not need to be created because default fonts already
|
|
|
|
// support these languages.
|
|
|
|
if (aWithFonts && support != FcLangDifferentLang && support == best) {
|
|
|
|
fonts.AppendElement(font);
|
|
|
|
}
|
2008-11-06 12:39:06 -08:00
|
|
|
}
|
|
|
|
|
2008-11-06 18:21:34 -08:00
|
|
|
entry->mSupport = best;
|
|
|
|
if (aWithFonts) {
|
|
|
|
if (fonts.Length() != 0) {
|
|
|
|
entry->mFonts.AppendElements(fonts.Elements(), fonts.Length());
|
|
|
|
} else if (best != FcLangDifferentLang) {
|
|
|
|
// Previously there was a font that supported this language at the
|
|
|
|
// level of entry->mSupport, but it has now disappeared. At least
|
|
|
|
// entry->mSupport needs to be recalculated, but this is an
|
|
|
|
// indication that the set of installed fonts has changed, so
|
|
|
|
// update all caches.
|
2013-07-31 08:44:31 -07:00
|
|
|
mLastConfig = nullptr; // invalidates caches
|
2011-10-17 07:59:28 -07:00
|
|
|
UpdateFontListInternal(true);
|
2008-11-06 18:21:34 -08:00
|
|
|
return GetLangSupportEntry(aLang, aWithFonts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
FcLangResult
|
|
|
|
gfxFontconfigUtils::GetBestLangSupport(const FcChar8 *aLang)
|
|
|
|
{
|
|
|
|
UpdateFontListInternal();
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
LangSupportEntry *entry = GetLangSupportEntry(aLang, false);
|
2008-11-06 18:21:34 -08:00
|
|
|
if (!entry)
|
|
|
|
return FcLangEqual;
|
|
|
|
|
|
|
|
return entry->mSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsTArray< nsCountedRef<FcPattern> >&
|
|
|
|
gfxFontconfigUtils::GetFontsForLang(const FcChar8 *aLang)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
LangSupportEntry *entry = GetLangSupportEntry(aLang, true);
|
2008-11-06 18:21:34 -08:00
|
|
|
if (!entry)
|
|
|
|
return mEmptyPatternArray;
|
|
|
|
|
|
|
|
return entry->mFonts;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
gfxFontNameList::Exists(nsAString& aName) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < Length(); i++) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aName.Equals(ElementAt(i)))
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|