Bug 789788 - Revise the don't-use-document-fonts option so that it will prefer generics (as configured in prefs) but ignore the 'cursive' and 'fantasy' values, but may still use page-specified fonts if necessary for fallback (e.g. icon fonts). r=dbaron

This commit is contained in:
Jonathan Kew 2015-06-27 22:23:05 -07:00
parent 9230f5554e
commit 939f7472f5
3 changed files with 52 additions and 45 deletions

View File

@ -257,6 +257,32 @@ public:
return false;
}
// Find the first generic (but ignoring cursive and fantasy, as they are
// rarely configured in any useful way) in the list.
// If found, move it to the start and return true; else return false.
bool PrioritizeFirstGeneric() {
uint32_t len = mFontlist.Length();
for (uint32_t i = 0; i < len; i++) {
const FontFamilyName name = mFontlist[i];
if (name.IsGeneric()) {
if (name.mType == eFamily_cursive ||
name.mType == eFamily_fantasy) {
continue;
}
if (i > 0) {
mFontlist.RemoveElementAt(i);
mFontlist.InsertElementAt(0, name);
}
return true;
}
}
return false;
}
void PrependGeneric(FontFamilyType aType) {
mFontlist.InsertElementAt(0, FontFamilyName(aType));
}
void ToString(nsAString& aFamilyList,
bool aQuotes = true,
bool aIncludeDefault = false) const {

View File

@ -20,16 +20,16 @@ p {
</head>
<body>
<p>default font</p>
<p>serif</p>
<p>sans-serif</p>
<p style="font-family: serif">serif</p>
<p style="font-family: sans-serif">sans-serif</p>
<p style="font-family: monospace">monospace</p>
<p style="font-family: monospace">-moz-fixed</p>
<p>unknown, serif</p>
<p>unknown, sans-serif</p>
<p style="font-family: serif">unknown, serif</p>
<p style="font-family: sans-serif">unknown, sans-serif</p>
<p style="font-family: monospace">unknown, monospace</p>
<p style="font-family: monospace">unknown, -moz-fixed</p>
<p>unknown, serif, monospace</p>
<p>unknown, sans-serif, -moz-fixed</p>
<p style="font-family: serif">unknown, serif, monospace</p>
<p style="font-family: sans-serif">unknown, sans-serif, -moz-fixed</p>
<p style="font-family: monospace">unknown, monospace, serif</p>
<p style="font-family: monospace">unknown, -moz-fixed, sans-serif</p>
</body>

View File

@ -3394,14 +3394,26 @@ nsRuleNode::SetFont(nsPresContext* aPresContext, nsStyleContext* aContext,
if (eCSSUnit_FontFamilyList == familyValue->GetUnit()) {
// set the correct font if we are using DocumentFonts OR we are overriding for XUL
// MJA: bug 31816
if (aGenericFontID == kGenericFont_NONE) {
uint32_t len = defaultVariableFont->fontlist.Length();
FontFamilyType generic = defaultVariableFont->fontlist.FirstGeneric();
NS_ASSERTION(len == 1 &&
(generic == eFamily_serif || generic == eFamily_sans_serif),
"default variable font must be a single serif or sans-serif");
if (len == 1 && generic != eFamily_none) {
aFont->mFont.fontlist.SetDefaultFontType(generic);
bool useDocumentFonts =
aPresContext->GetCachedBoolPref(kPresContext_UseDocumentFonts);
if (aGenericFontID == kGenericFont_NONE ||
(!useDocumentFonts && (aGenericFontID == kGenericFont_cursive ||
aGenericFontID == kGenericFont_fantasy))) {
FontFamilyType defaultGeneric =
defaultVariableFont->fontlist.FirstGeneric();
MOZ_ASSERT(defaultVariableFont->fontlist.Length() == 1 &&
(defaultGeneric == eFamily_serif ||
defaultGeneric == eFamily_sans_serif));
if (defaultGeneric != eFamily_none) {
if (useDocumentFonts) {
aFont->mFont.fontlist.SetDefaultFontType(defaultGeneric);
} else {
// Either prioritize the first generic in the list,
// or (if there isn't one) prepend the default variable font.
if (!aFont->mFont.fontlist.PrioritizeFirstGeneric()) {
aFont->mFont.fontlist.PrependGeneric(defaultGeneric);
}
}
}
} else {
aFont->mFont.fontlist.SetDefaultFontType(eFamily_none);
@ -3941,18 +3953,6 @@ nsRuleNode::ComputeFontData(void* aStartStruct,
// (although there is a pretty good chance they'll fully specify it
// using the 'font' shorthand).
bool useDocumentFonts =
mPresContext->GetCachedBoolPref(kPresContext_UseDocumentFonts);
// See if we are in the chrome
// We only need to know this to determine if we have to use the
// document fonts (overriding the useDocumentFonts flag).
if (!useDocumentFonts && mPresContext->IsChrome()) {
// if we are not using document fonts, but this is a XUL document,
// then we use the document fonts anyway
useDocumentFonts = true;
}
// Figure out if we are a generic font
uint8_t generic = kGenericFont_NONE;
// XXXldb What if we would have had a string if we hadn't been doing
@ -3991,25 +3991,6 @@ nsRuleNode::ComputeFontData(void* aStartStruct,
break;
}
}
// If we aren't allowed to use document fonts, then we are only entitled
// to use the user's default variable-width font and fixed-width font
if (!useDocumentFonts) {
switch (fontType) {
case eFamily_monospace:
fl = FontFamilyList(eFamily_monospace);
generic = kGenericFont_monospace;
break;
case eFamily_moz_fixed:
fl = FontFamilyList(eFamily_moz_fixed);
generic = kGenericFont_moz_fixed;
break;
default:
fl.Clear();
generic = kGenericFont_NONE;
break;
}
}
}
// Now compute our font struct