Bug 1064737 pt 2 - Support for WOFF2 as a webfont format. r=jdaggett

This commit is contained in:
Jonathan Kew 2014-10-04 11:36:05 +01:00
parent 798e741f99
commit 1b4072d000
12 changed files with 53 additions and 23 deletions

View File

@ -283,9 +283,7 @@ gfxAndroidPlatform::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlag
"strange font format hint set");
// accept supported formats
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMAT_OPENTYPE |
gfxUserFontSet::FLAG_FORMAT_WOFF |
gfxUserFontSet::FLAG_FORMAT_TRUETYPE)) {
if (aFormatFlags & gfxUserFontSet::FLAG_FORMATS_COMMON) {
return true;
}

View File

@ -955,6 +955,10 @@ gfxFontUtils::DetermineFontDataType(const uint8_t *aFontData, uint32_t aFontData
if (uint32_t(*version) == TRUETYPE_TAG('w','O','F','F')) {
return GFX_USERFONT_WOFF;
}
if (Preferences::GetBool(GFX_PREF_WOFF2_ENABLED) &&
uint32_t(*version) == TRUETYPE_TAG('w','O','F','2')) {
return GFX_USERFONT_WOFF2;
}
}
// tests for other formats here

View File

@ -655,8 +655,10 @@ enum gfxUserFontType {
GFX_USERFONT_UNKNOWN = 0,
GFX_USERFONT_OPENTYPE = 1,
GFX_USERFONT_SVG = 2,
GFX_USERFONT_WOFF = 3
GFX_USERFONT_WOFF = 3,
GFX_USERFONT_WOFF2 = 4
};
#define GFX_PREF_WOFF2_ENABLED "gfx.downloadable_fonts.woff2.enabled"
extern const uint8_t sCJKCompatSVSTable[];

View File

@ -198,9 +198,7 @@ gfxPlatformGtk::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags)
// Pango doesn't apply features from AAT TrueType extensions.
// Assume that if this is the only SFNT format specified,
// then AAT extensions are required for complex script support.
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMAT_WOFF |
gfxUserFontSet::FLAG_FORMAT_OPENTYPE |
gfxUserFontSet::FLAG_FORMAT_TRUETYPE)) {
if (aFormatFlags & gfxUserFontSet::FLAG_FORMATS_COMMON) {
return true;
}

View File

@ -168,9 +168,7 @@ gfxPlatformMac::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags)
"strange font format hint set");
// accept supported formats
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMAT_WOFF |
gfxUserFontSet::FLAG_FORMAT_OPENTYPE |
gfxUserFontSet::FLAG_FORMAT_TRUETYPE |
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMATS_COMMON |
gfxUserFontSet::FLAG_FORMAT_TRUETYPE_AAT)) {
return true;
}

View File

@ -165,9 +165,7 @@ gfxQtPlatform::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlags)
// Pango doesn't apply features from AAT TrueType extensions.
// Assume that if this is the only SFNT format specified,
// then AAT extensions are required for complex script support.
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMAT_WOFF |
gfxUserFontSet::FLAG_FORMAT_OPENTYPE |
gfxUserFontSet::FLAG_FORMAT_TRUETYPE)) {
if (aFormatFlags & gfxUserFontSet::FLAG_FORMATS_COMMON) {
return true;
}

View File

@ -221,11 +221,22 @@ const uint8_t*
gfxUserFontEntry::SanitizeOpenTypeData(const uint8_t* aData,
uint32_t aLength,
uint32_t& aSaneLength,
bool aIsCompressed)
gfxUserFontType aFontType)
{
if (aFontType == GFX_USERFONT_UNKNOWN) {
aSaneLength = 0;
return nullptr;
}
uint32_t lengthHint = aLength;
if (aFontType == GFX_USERFONT_WOFF) {
lengthHint *= 2;
} else if (aFontType == GFX_USERFONT_WOFF2) {
lengthHint *= 3;
}
// limit output/expansion to 256MB
ExpandingMemoryStream output(aIsCompressed ? aLength * 2 : aLength,
1024 * 1024 * 256);
ExpandingMemoryStream output(lengthHint, 1024 * 1024 * 256);
gfxOTSContext otsContext(this);
@ -554,8 +565,7 @@ gfxUserFontEntry::LoadPlatformFont(const uint8_t* aFontData, uint32_t& aLength)
// if necessary. The original data in aFontData is left unchanged.
uint32_t saneLen;
const uint8_t* saneData =
SanitizeOpenTypeData(aFontData, aLength, saneLen,
fontType == GFX_USERFONT_WOFF);
SanitizeOpenTypeData(aFontData, aLength, saneLen, fontType);
if (!saneData) {
mFontSet->LogMessage(this, "rejected by sanitizer");
}
@ -690,6 +700,11 @@ gfxUserFontSet::gfxUserFontSet()
if (fp) {
fp->AddUserFontSet(this);
}
// This is a one-time global switch for OTS. However, as long as we use
// a preference to control the availability of WOFF2 support, we will
// not actually pass any WOFF2 data to OTS unless the pref is on.
ots::EnableWOFF2();
}
gfxUserFontSet::~gfxUserFontSet()

View File

@ -173,9 +173,16 @@ public:
FLAG_FORMAT_EOT = 1 << 4,
FLAG_FORMAT_SVG = 1 << 5,
FLAG_FORMAT_WOFF = 1 << 6,
FLAG_FORMAT_WOFF2 = 1 << 7,
// the common formats that we support everywhere
FLAG_FORMATS_COMMON = FLAG_FORMAT_OPENTYPE |
FLAG_FORMAT_TRUETYPE |
FLAG_FORMAT_WOFF |
FLAG_FORMAT_WOFF2,
// mask of all unused bits, update when adding new formats
FLAG_FORMAT_NOT_USED = ~((1 << 7)-1)
FLAG_FORMAT_NOT_USED = ~((1 << 8)-1)
};
@ -568,7 +575,7 @@ protected:
const uint8_t* SanitizeOpenTypeData(const uint8_t* aData,
uint32_t aLength,
uint32_t& aSaneLength,
bool aIsCompressed);
gfxUserFontType aFontType);
// attempt to load the next resource in the src list.
void LoadNextSrc();

View File

@ -969,9 +969,7 @@ gfxWindowsPlatform::IsFontFormatSupported(nsIURI *aFontURI, uint32_t aFormatFlag
"strange font format hint set");
// accept supported formats
if (aFormatFlags & (gfxUserFontSet::FLAG_FORMAT_WOFF |
gfxUserFontSet::FLAG_FORMAT_OPENTYPE |
gfxUserFontSet::FLAG_FORMAT_TRUETYPE)) {
if (aFormatFlags & gfxUserFontSet::FLAG_FORMATS_COMMON) {
return true;
}

View File

@ -183,6 +183,9 @@ nsFontFace::GetFormat(nsAString & aFormat)
if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF) {
AppendToFormat(aFormat, "woff");
}
if (formatFlags & gfxUserFontSet::FLAG_FORMAT_WOFF2) {
AppendToFormat(aFormat, "woff2");
}
}
return NS_OK;
}

View File

@ -17,6 +17,7 @@
#include "mozilla/dom/FontFaceSetBinding.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/Preferences.h"
#include "nsCrossSiteListenerProxy.h"
#include "nsFontFaceLoader.h"
#include "nsIChannelPolicy.h"
@ -902,6 +903,9 @@ FontFaceSet::FindOrCreateUserFontEntryFromFontFace(const nsAString& aFamilyName,
nsDependentString valueString(val.GetStringBufferValue());
if (valueString.LowerCaseEqualsASCII("woff")) {
face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_WOFF;
} else if (Preferences::GetBool(GFX_PREF_WOFF2_ENABLED) &&
valueString.LowerCaseEqualsASCII("woff2")) {
face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_WOFF2;
} else if (valueString.LowerCaseEqualsASCII("opentype")) {
face->mFormatFlags |= gfxUserFontSet::FLAG_FORMAT_OPENTYPE;
} else if (valueString.LowerCaseEqualsASCII("truetype")) {

View File

@ -534,6 +534,11 @@ pref("gfx.color_management.enablev4", false);
pref("gfx.downloadable_fonts.enabled", true);
pref("gfx.downloadable_fonts.fallback_delay", 3000);
#ifdef RELEASE_BUILD
pref("gfx.downloadable_fonts.woff2.enabled", false);
#else
pref("gfx.downloadable_fonts.woff2.enabled", true);
#endif
#ifdef ANDROID
pref("gfx.bundled_fonts.enabled", true);