2016-03-31 22:47:20 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/StaticPresData.h"
|
|
|
|
|
|
|
|
#include "mozilla/Preferences.h"
|
2017-05-26 01:04:13 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2016-03-31 22:47:20 +03:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
static StaticPresData* sSingleton = nullptr;
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticPresData::Init()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!sSingleton);
|
|
|
|
sSingleton = new StaticPresData();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
StaticPresData::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sSingleton);
|
|
|
|
delete sSingleton;
|
|
|
|
sSingleton = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticPresData*
|
|
|
|
StaticPresData::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(sSingleton);
|
|
|
|
return sSingleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
StaticPresData::StaticPresData()
|
|
|
|
{
|
2015-04-03 22:39:23 +03:00
|
|
|
mLangService = nsLanguageAtomService::GetService();
|
2016-03-31 22:47:20 +03:00
|
|
|
|
|
|
|
mBorderWidthTable[NS_STYLE_BORDER_WIDTH_THIN] = nsPresContext::CSSPixelsToAppUnits(1);
|
|
|
|
mBorderWidthTable[NS_STYLE_BORDER_WIDTH_MEDIUM] = nsPresContext::CSSPixelsToAppUnits(3);
|
|
|
|
mBorderWidthTable[NS_STYLE_BORDER_WIDTH_THICK] = nsPresContext::CSSPixelsToAppUnits(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAKE_FONT_PREF_KEY(_pref, _s0, _s1) \
|
|
|
|
_pref.Assign(_s0); \
|
|
|
|
_pref.Append(_s1);
|
|
|
|
|
|
|
|
static const char* const kGenericFont[] = {
|
|
|
|
".variable.",
|
|
|
|
".fixed.",
|
|
|
|
".serif.",
|
|
|
|
".sans-serif.",
|
|
|
|
".monospace.",
|
|
|
|
".cursive.",
|
|
|
|
".fantasy."
|
|
|
|
};
|
|
|
|
|
|
|
|
// These are private, use the list in nsFont.h if you want a public list.
|
|
|
|
enum {
|
|
|
|
eDefaultFont_Variable,
|
|
|
|
eDefaultFont_Fixed,
|
|
|
|
eDefaultFont_Serif,
|
|
|
|
eDefaultFont_SansSerif,
|
|
|
|
eDefaultFont_Monospace,
|
|
|
|
eDefaultFont_Cursive,
|
|
|
|
eDefaultFont_Fantasy,
|
|
|
|
eDefaultFont_COUNT
|
|
|
|
};
|
|
|
|
|
2017-04-04 21:11:27 +03:00
|
|
|
void
|
|
|
|
LangGroupFontPrefs::Initialize(nsIAtom* aLangGroupAtom)
|
2016-03-31 22:47:20 +03:00
|
|
|
{
|
2017-04-04 21:11:27 +03:00
|
|
|
mLangGroup = aLangGroupAtom;
|
2016-03-31 22:47:20 +03:00
|
|
|
|
|
|
|
/* Fetch the font prefs to be used -- see bug 61883 for details.
|
|
|
|
Not all prefs are needed upfront. Some are fallback prefs intended
|
|
|
|
for the GFX font sub-system...
|
|
|
|
|
|
|
|
1) unit : assumed to be the same for all language groups -------------
|
|
|
|
font.size.unit = px | pt XXX could be folded in the size... bug 90440
|
|
|
|
|
|
|
|
2) attributes for generic fonts --------------------------------------
|
|
|
|
font.default.[langGroup] = serif | sans-serif - fallback generic font
|
|
|
|
font.name.[generic].[langGroup] = current user' selected font on the pref dialog
|
|
|
|
font.name-list.[generic].[langGroup] = fontname1, fontname2, ... [factory pre-built list]
|
|
|
|
font.size.[generic].[langGroup] = integer - settable by the user
|
|
|
|
font.size-adjust.[generic].[langGroup] = "float" - settable by the user
|
|
|
|
font.minimum-size.[langGroup] = integer - settable by the user
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsAutoCString langGroup;
|
2017-04-04 21:11:27 +03:00
|
|
|
aLangGroupAtom->ToUTF8String(langGroup);
|
2016-03-31 22:47:20 +03:00
|
|
|
|
2017-04-04 21:11:27 +03:00
|
|
|
mDefaultVariableFont.size = nsPresContext::CSSPixelsToAppUnits(16);
|
|
|
|
mDefaultFixedFont.size = nsPresContext::CSSPixelsToAppUnits(13);
|
2016-03-31 22:47:20 +03:00
|
|
|
|
|
|
|
nsAutoCString pref;
|
|
|
|
|
|
|
|
// get the current applicable font-size unit
|
|
|
|
enum {eUnit_unknown = -1, eUnit_px, eUnit_pt};
|
|
|
|
int32_t unit = eUnit_px;
|
|
|
|
|
2017-07-31 07:28:48 +03:00
|
|
|
nsAutoCString cvalue;
|
|
|
|
Preferences::GetCString("font.size.unit", cvalue);
|
2016-03-31 22:47:20 +03:00
|
|
|
|
|
|
|
if (!cvalue.IsEmpty()) {
|
|
|
|
if (cvalue.EqualsLiteral("px")) {
|
|
|
|
unit = eUnit_px;
|
|
|
|
}
|
|
|
|
else if (cvalue.EqualsLiteral("pt")) {
|
|
|
|
unit = eUnit_pt;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// XXX should really send this warning to the user (Error Console?).
|
|
|
|
// And just default to unit = eUnit_px?
|
|
|
|
NS_WARNING("unexpected font-size unit -- expected: 'px' or 'pt'");
|
|
|
|
unit = eUnit_unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get font.minimum-size.[langGroup]
|
|
|
|
|
|
|
|
MAKE_FONT_PREF_KEY(pref, "font.minimum-size.", langGroup);
|
|
|
|
|
|
|
|
int32_t size = Preferences::GetInt(pref.get());
|
|
|
|
if (unit == eUnit_px) {
|
2017-04-04 21:11:27 +03:00
|
|
|
mMinimumFontSize = nsPresContext::CSSPixelsToAppUnits(size);
|
2016-03-31 22:47:20 +03:00
|
|
|
}
|
|
|
|
else if (unit == eUnit_pt) {
|
2017-04-04 21:11:27 +03:00
|
|
|
mMinimumFontSize = nsPresContext::CSSPointsToAppUnits(size);
|
2016-03-31 22:47:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsFont* fontTypes[] = {
|
2017-04-04 21:11:27 +03:00
|
|
|
&mDefaultVariableFont,
|
|
|
|
&mDefaultFixedFont,
|
|
|
|
&mDefaultSerifFont,
|
|
|
|
&mDefaultSansSerifFont,
|
|
|
|
&mDefaultMonospaceFont,
|
|
|
|
&mDefaultCursiveFont,
|
|
|
|
&mDefaultFantasyFont
|
2016-03-31 22:47:20 +03:00
|
|
|
};
|
|
|
|
static_assert(MOZ_ARRAY_LENGTH(fontTypes) == eDefaultFont_COUNT,
|
|
|
|
"FontTypes array count is not correct");
|
|
|
|
|
|
|
|
// Get attributes specific to each generic font. We do not get the user's
|
|
|
|
// generic-font-name-to-specific-family-name preferences because its the
|
|
|
|
// generic name that should be fed into the cascade. It is up to the GFX
|
|
|
|
// code to look up the font prefs to convert generic names to specific
|
|
|
|
// family names as necessary.
|
|
|
|
nsAutoCString generic_dot_langGroup;
|
|
|
|
for (uint32_t eType = 0; eType < ArrayLength(fontTypes); ++eType) {
|
|
|
|
generic_dot_langGroup.Assign(kGenericFont[eType]);
|
|
|
|
generic_dot_langGroup.Append(langGroup);
|
|
|
|
|
|
|
|
nsFont* font = fontTypes[eType];
|
|
|
|
|
|
|
|
// set the default variable font (the other fonts are seen as 'generic' fonts
|
|
|
|
// in GFX and will be queried there when hunting for alternative fonts)
|
|
|
|
if (eType == eDefaultFont_Variable) {
|
2017-03-29 17:21:47 +03:00
|
|
|
// XXX "font.name.variable."? There is no such pref...
|
2016-03-31 22:47:20 +03:00
|
|
|
MAKE_FONT_PREF_KEY(pref, "font.name.variable.", langGroup);
|
|
|
|
|
2017-07-31 07:23:50 +03:00
|
|
|
nsAutoString value;
|
|
|
|
Preferences::GetString(pref.get(), value);
|
2016-03-31 22:47:20 +03:00
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
FontFamilyName defaultVariableName = FontFamilyName::Convert(value);
|
|
|
|
FontFamilyType defaultType = defaultVariableName.mType;
|
|
|
|
NS_ASSERTION(defaultType == eFamily_serif ||
|
|
|
|
defaultType == eFamily_sans_serif,
|
|
|
|
"default type must be serif or sans-serif");
|
2017-08-18 22:56:02 +03:00
|
|
|
mDefaultVariableFont.fontlist = FontFamilyList();
|
|
|
|
mDefaultVariableFont.fontlist.SetDefaultFontType(defaultType);
|
|
|
|
// We create mDefaultVariableFont.fontlist with defaultType as the
|
|
|
|
// fallback font, and not as part of the font list proper. This way,
|
|
|
|
// it can be overwritten should there be a language change.
|
2016-03-31 22:47:20 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
MAKE_FONT_PREF_KEY(pref, "font.default.", langGroup);
|
2017-07-31 07:23:50 +03:00
|
|
|
Preferences::GetString(pref.get(), value);
|
2016-03-31 22:47:20 +03:00
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
FontFamilyName defaultVariableName = FontFamilyName::Convert(value);
|
|
|
|
FontFamilyType defaultType = defaultVariableName.mType;
|
|
|
|
NS_ASSERTION(defaultType == eFamily_serif ||
|
|
|
|
defaultType == eFamily_sans_serif,
|
|
|
|
"default type must be serif or sans-serif");
|
2017-08-18 22:56:02 +03:00
|
|
|
mDefaultVariableFont.fontlist = FontFamilyList();
|
|
|
|
mDefaultVariableFont.fontlist.SetDefaultFontType(defaultType);
|
|
|
|
// We create mDefaultVariableFont.fontlist with defaultType as the
|
|
|
|
// (fallback) font, and not as part of the font list proper. This way,
|
|
|
|
// it can be overwritten should there be a language change.
|
2016-03-31 22:47:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (eType == eDefaultFont_Monospace) {
|
|
|
|
// This takes care of the confusion whereby people often expect "monospace"
|
|
|
|
// to have the same default font-size as "-moz-fixed" (this tentative
|
|
|
|
// size may be overwritten with the specific value for "monospace" when
|
|
|
|
// "font.size.monospace.[langGroup]" is read -- see below)
|
2017-04-04 21:11:27 +03:00
|
|
|
mDefaultMonospaceFont.size = mDefaultFixedFont.size;
|
2016-03-31 22:47:20 +03:00
|
|
|
}
|
|
|
|
else if (eType != eDefaultFont_Fixed) {
|
|
|
|
// all the other generic fonts are initialized with the size of the
|
|
|
|
// variable font, but their specific size can supersede later -- see below
|
2017-04-04 21:11:27 +03:00
|
|
|
font->size = mDefaultVariableFont.size;
|
2016-03-31 22:47:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bug 84398: for spec purists, a different font-size only applies to the
|
|
|
|
// .variable. and .fixed. fonts and the other fonts should get |font-size-adjust|.
|
|
|
|
// The problem is that only GfxWin has the support for |font-size-adjust|. So for
|
|
|
|
// parity, we enable the ability to set a different font-size on all platforms.
|
|
|
|
|
|
|
|
// get font.size.[generic].[langGroup]
|
|
|
|
// size=0 means 'Auto', i.e., generic fonts retain the size of the variable font
|
|
|
|
MAKE_FONT_PREF_KEY(pref, "font.size", generic_dot_langGroup);
|
|
|
|
size = Preferences::GetInt(pref.get());
|
|
|
|
if (size > 0) {
|
|
|
|
if (unit == eUnit_px) {
|
|
|
|
font->size = nsPresContext::CSSPixelsToAppUnits(size);
|
|
|
|
}
|
|
|
|
else if (unit == eUnit_pt) {
|
|
|
|
font->size = nsPresContext::CSSPointsToAppUnits(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get font.size-adjust.[generic].[langGroup]
|
|
|
|
// XXX only applicable on GFX ports that handle |font-size-adjust|
|
|
|
|
MAKE_FONT_PREF_KEY(pref, "font.size-adjust", generic_dot_langGroup);
|
2017-07-31 07:28:48 +03:00
|
|
|
cvalue.Truncate();
|
|
|
|
Preferences::GetCString(pref.get(), cvalue);
|
2016-03-31 22:47:20 +03:00
|
|
|
if (!cvalue.IsEmpty()) {
|
|
|
|
font->sizeAdjust = (float)atof(cvalue.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_rbs
|
|
|
|
printf("%s Family-list:%s size:%d sizeAdjust:%.2f\n",
|
|
|
|
generic_dot_langGroup.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(font->name).get(), font->size,
|
|
|
|
font->sizeAdjust);
|
|
|
|
#endif
|
|
|
|
}
|
2017-04-04 21:11:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIAtom*
|
2017-05-26 01:04:13 +03:00
|
|
|
StaticPresData::GetLangGroup(nsIAtom* aLanguage,
|
|
|
|
bool* aNeedsToCache) const
|
2017-04-04 21:11:27 +03:00
|
|
|
{
|
|
|
|
nsIAtom* langGroupAtom = nullptr;
|
2017-05-26 01:04:13 +03:00
|
|
|
langGroupAtom = mLangService->GetLanguageGroup(aLanguage, aNeedsToCache);
|
2015-04-03 22:39:23 +03:00
|
|
|
if (!langGroupAtom) {
|
2017-04-04 21:11:27 +03:00
|
|
|
langGroupAtom = nsGkAtoms::x_western; // Assume x-western is safe...
|
|
|
|
}
|
|
|
|
return langGroupAtom;
|
|
|
|
}
|
|
|
|
|
2017-04-04 21:11:27 +03:00
|
|
|
already_AddRefed<nsIAtom>
|
|
|
|
StaticPresData::GetUncachedLangGroup(nsIAtom* aLanguage) const
|
|
|
|
{
|
2015-04-03 22:39:23 +03:00
|
|
|
nsCOMPtr<nsIAtom> langGroupAtom = mLangService->GetUncachedLanguageGroup(aLanguage);
|
|
|
|
if (!langGroupAtom) {
|
2017-04-04 21:11:27 +03:00
|
|
|
langGroupAtom = nsGkAtoms::x_western; // Assume x-western is safe...
|
|
|
|
}
|
|
|
|
return langGroupAtom.forget();
|
|
|
|
}
|
|
|
|
|
2017-04-04 21:11:27 +03:00
|
|
|
const LangGroupFontPrefs*
|
|
|
|
StaticPresData::GetFontPrefsForLangHelper(nsIAtom* aLanguage,
|
2017-05-26 01:04:13 +03:00
|
|
|
const LangGroupFontPrefs* aPrefs,
|
|
|
|
bool* aNeedsToCache) const
|
2017-04-04 21:11:27 +03:00
|
|
|
{
|
|
|
|
// Get language group for aLanguage:
|
|
|
|
MOZ_ASSERT(aLanguage);
|
|
|
|
MOZ_ASSERT(mLangService);
|
|
|
|
MOZ_ASSERT(aPrefs);
|
|
|
|
|
2017-05-26 01:04:13 +03:00
|
|
|
nsIAtom* langGroupAtom = GetLangGroup(aLanguage, aNeedsToCache);
|
|
|
|
|
|
|
|
if (aNeedsToCache && *aNeedsToCache) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-04-04 21:11:27 +03:00
|
|
|
|
|
|
|
LangGroupFontPrefs* prefs = const_cast<LangGroupFontPrefs*>(aPrefs);
|
|
|
|
if (prefs->mLangGroup) { // if initialized
|
|
|
|
DebugOnly<uint32_t> count = 0;
|
|
|
|
for (;;) {
|
|
|
|
NS_ASSERTION(++count < 35, "Lang group count exceeded!!!");
|
|
|
|
if (prefs->mLangGroup == langGroupAtom) {
|
|
|
|
return prefs;
|
|
|
|
}
|
|
|
|
if (!prefs->mNext) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prefs = prefs->mNext;
|
|
|
|
}
|
2017-05-26 01:04:13 +03:00
|
|
|
if (aNeedsToCache) {
|
|
|
|
*aNeedsToCache = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
AssertIsMainThreadOrServoLangFontPrefsCacheLocked();
|
2017-04-04 21:11:27 +03:00
|
|
|
// nothing cached, so go on and fetch the prefs for this lang group:
|
|
|
|
prefs = prefs->mNext = new LangGroupFontPrefs;
|
|
|
|
}
|
|
|
|
|
2017-05-26 01:04:13 +03:00
|
|
|
if (aNeedsToCache) {
|
|
|
|
*aNeedsToCache = true;
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-05-20 22:44:37 +03:00
|
|
|
|
2017-05-26 01:04:13 +03:00
|
|
|
AssertIsMainThreadOrServoLangFontPrefsCacheLocked();
|
2017-04-04 21:11:27 +03:00
|
|
|
prefs->Initialize(langGroupAtom);
|
2016-03-31 22:47:20 +03:00
|
|
|
|
|
|
|
return prefs;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsFont*
|
|
|
|
StaticPresData::GetDefaultFontHelper(uint8_t aFontID, nsIAtom *aLanguage,
|
|
|
|
const LangGroupFontPrefs* aPrefs) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aLanguage);
|
|
|
|
MOZ_ASSERT(aPrefs);
|
|
|
|
|
|
|
|
const nsFont *font;
|
|
|
|
switch (aFontID) {
|
|
|
|
// Special (our default variable width font and fixed width font)
|
|
|
|
case kPresContext_DefaultVariableFont_ID:
|
|
|
|
font = &aPrefs->mDefaultVariableFont;
|
|
|
|
break;
|
|
|
|
case kPresContext_DefaultFixedFont_ID:
|
|
|
|
font = &aPrefs->mDefaultFixedFont;
|
|
|
|
break;
|
|
|
|
// CSS
|
|
|
|
case kGenericFont_serif:
|
|
|
|
font = &aPrefs->mDefaultSerifFont;
|
|
|
|
break;
|
|
|
|
case kGenericFont_sans_serif:
|
|
|
|
font = &aPrefs->mDefaultSansSerifFont;
|
|
|
|
break;
|
|
|
|
case kGenericFont_monospace:
|
|
|
|
font = &aPrefs->mDefaultMonospaceFont;
|
|
|
|
break;
|
|
|
|
case kGenericFont_cursive:
|
|
|
|
font = &aPrefs->mDefaultCursiveFont;
|
|
|
|
break;
|
|
|
|
case kGenericFont_fantasy:
|
|
|
|
font = &aPrefs->mDefaultFantasyFont;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
font = nullptr;
|
|
|
|
NS_ERROR("invalid arg");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|