2008-10-01 07:04:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04: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/. */
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
#ifdef MOZ_LOGGING
|
|
|
|
#define FORCE_PR_LOG /* Allow logging in the release build */
|
|
|
|
#endif /* MOZ_LOGGING */
|
|
|
|
#include "prlog.h"
|
|
|
|
|
|
|
|
#include "gfxUserFontSet.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
2012-03-16 07:31:01 +04:00
|
|
|
#include "nsNetUtil.h"
|
2014-06-05 21:32:04 +04:00
|
|
|
#include "nsICacheService.h"
|
2012-03-16 07:31:01 +04:00
|
|
|
#include "nsIProtocolHandler.h"
|
2012-12-10 13:31:07 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxFontConstants.h"
|
2013-05-22 08:39:47 +04:00
|
|
|
#include "mozilla/Services.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-04-17 18:17:22 +04:00
|
|
|
#include "gfxPlatformFontList.h"
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2010-10-07 11:59:19 +04:00
|
|
|
#include "opentype-sanitiser.h"
|
|
|
|
#include "ots-memory-stream.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
#ifdef PR_LOGGING
|
2012-10-30 03:32:10 +04:00
|
|
|
PRLogModuleInfo *
|
|
|
|
gfxUserFontSet::GetUserFontsLog()
|
|
|
|
{
|
|
|
|
static PRLogModuleInfo *sLog;
|
|
|
|
if (!sLog)
|
|
|
|
sLog = PR_NewLogModule("userfonts");
|
|
|
|
return sLog;
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
#endif /* PR_LOGGING */
|
|
|
|
|
2013-11-25 06:02:14 +04:00
|
|
|
#define LOG(args) PR_LOG(GetUserFontsLog(), PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(GetUserFontsLog(), PR_LOG_DEBUG)
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2012-11-02 04:57:25 +04:00
|
|
|
static uint64_t sFontSetGeneration = 0;
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// TODO: support for unicode ranges not yet implemented
|
|
|
|
|
2010-07-14 00:31:31 +04:00
|
|
|
gfxProxyFontEntry::gfxProxyFontEntry(const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aWeight,
|
2013-08-12 13:07:55 +04:00
|
|
|
int32_t aStretch,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aItalicStyle,
|
2010-12-09 14:52:03 +03:00
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLanguageOverride,
|
2008-10-01 07:04:10 +04:00
|
|
|
gfxSparseBitSet *aUnicodeRanges)
|
2013-01-04 18:01:44 +04:00
|
|
|
: gfxFontEntry(NS_LITERAL_STRING("Proxy")),
|
2012-01-05 14:14:11 +04:00
|
|
|
mLoadingState(NOT_LOADING),
|
2012-04-26 21:50:41 +04:00
|
|
|
mUnsupportedFormat(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mLoader(nullptr)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsProxy = true;
|
2008-10-01 07:04:10 +04:00
|
|
|
mSrcList = aFontFaceSrcList;
|
|
|
|
mSrcIndex = 0;
|
|
|
|
mWeight = aWeight;
|
|
|
|
mStretch = aStretch;
|
2013-08-12 13:07:55 +04:00
|
|
|
// XXX Currently, we don't distinguish 'italic' and 'oblique' styles;
|
|
|
|
// we need to fix this. (Bug 543715)
|
2012-03-15 13:04:46 +04:00
|
|
|
mItalic = (aItalicStyle & (NS_FONT_STYLE_ITALIC | NS_FONT_STYLE_OBLIQUE)) != 0;
|
2010-12-09 14:52:03 +03:00
|
|
|
mFeatureSettings.AppendElements(aFeatureSettings);
|
2010-07-14 00:31:31 +04:00
|
|
|
mLanguageOverride = aLanguageOverride;
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsUserFont = true;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxProxyFontEntry::~gfxProxyFontEntry()
|
|
|
|
{
|
2009-10-07 19:26:58 +04:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2013-08-12 13:07:55 +04:00
|
|
|
bool
|
|
|
|
gfxProxyFontEntry::Matches(const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
|
|
|
uint32_t aWeight,
|
|
|
|
int32_t aStretch,
|
|
|
|
uint32_t aItalicStyle,
|
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
|
|
|
uint32_t aLanguageOverride,
|
|
|
|
gfxSparseBitSet *aUnicodeRanges)
|
|
|
|
{
|
|
|
|
// XXX font entries don't distinguish italic from oblique (bug 543715)
|
|
|
|
bool isItalic =
|
|
|
|
(aItalicStyle & (NS_FONT_STYLE_ITALIC | NS_FONT_STYLE_OBLIQUE)) != 0;
|
|
|
|
|
|
|
|
return mWeight == aWeight &&
|
|
|
|
mStretch == aStretch &&
|
|
|
|
mItalic == isItalic &&
|
|
|
|
mFeatureSettings == aFeatureSettings &&
|
|
|
|
mLanguageOverride == aLanguageOverride &&
|
|
|
|
mSrcList == aFontFaceSrcList;
|
|
|
|
// XXX once we support unicode-range (bug 475891),
|
|
|
|
// we'll need to compare that here as well
|
|
|
|
}
|
|
|
|
|
2009-10-07 19:26:58 +04:00
|
|
|
gfxFont*
|
2011-09-29 10:19:26 +04:00
|
|
|
gfxProxyFontEntry::CreateFontInstance(const gfxFontStyle *aFontStyle, bool aNeedsBold)
|
2009-10-07 19:26:58 +04:00
|
|
|
{
|
|
|
|
// cannot create an actual font for a proxy entry
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2008-12-12 10:31:51 +03:00
|
|
|
gfxUserFontSet::gfxUserFontSet()
|
2014-04-17 18:17:22 +04:00
|
|
|
: mFontFamilies(5), mLocalRulesUsed(false)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
|
|
|
IncrementGeneration();
|
2014-04-17 18:17:22 +04:00
|
|
|
gfxPlatformFontList *fp = gfxPlatformFontList::PlatformFontList();
|
|
|
|
if (fp) {
|
|
|
|
fp->AddUserFontSet(this);
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxUserFontSet::~gfxUserFontSet()
|
|
|
|
{
|
2014-04-17 18:17:22 +04:00
|
|
|
gfxPlatformFontList *fp = gfxPlatformFontList::PlatformFontList();
|
|
|
|
if (fp) {
|
|
|
|
fp->RemoveUserFontSet(this);
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2011-04-12 14:53:20 +04:00
|
|
|
gfxFontEntry*
|
2010-07-14 00:31:31 +04:00
|
|
|
gfxUserFontSet::AddFontFace(const nsAString& aFamilyName,
|
|
|
|
const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aWeight,
|
2013-08-12 13:07:55 +04:00
|
|
|
int32_t aStretch,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aItalicStyle,
|
2012-04-26 10:24:26 +04:00
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
2010-07-14 00:31:31 +04:00
|
|
|
const nsString& aLanguageOverride,
|
2008-10-01 07:04:10 +04:00
|
|
|
gfxSparseBitSet *aUnicodeRanges)
|
|
|
|
{
|
|
|
|
nsAutoString key(aFamilyName);
|
|
|
|
ToLowerCase(key);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found;
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
if (aWeight == 0)
|
2012-03-15 13:04:46 +04:00
|
|
|
aWeight = NS_FONT_WEIGHT_NORMAL;
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// stretch, italic/oblique ==> zero implies normal
|
|
|
|
|
|
|
|
gfxMixedFontFamily *family = mFontFamilies.GetWeak(key, &found);
|
|
|
|
if (!family) {
|
|
|
|
family = new gfxMixedFontFamily(aFamilyName);
|
|
|
|
mFontFamilies.Put(key, family);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t languageOverride =
|
2011-04-12 14:53:20 +04:00
|
|
|
gfxFontStyle::ParseFontLanguageOverride(aLanguageOverride);
|
2013-08-12 13:07:55 +04:00
|
|
|
|
|
|
|
// If there's already a proxy in the family whose descriptors all match,
|
|
|
|
// we can just move it to the end of the list instead of adding a new
|
|
|
|
// face that will always "shadow" the old one.
|
|
|
|
// Note that we can't do this for "real" (non-proxy) entries, even if the
|
|
|
|
// style descriptors match, as they might have had a different source list,
|
|
|
|
// but we no longer have the old source list available to check.
|
|
|
|
nsTArray<nsRefPtr<gfxFontEntry> >& fontList = family->GetFontList();
|
|
|
|
for (uint32_t i = 0, count = fontList.Length(); i < count; i++) {
|
|
|
|
if (!fontList[i]->mIsProxy) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxProxyFontEntry *existingProxyEntry =
|
|
|
|
static_cast<gfxProxyFontEntry*>(fontList[i].get());
|
|
|
|
if (!existingProxyEntry->Matches(aFontFaceSrcList,
|
|
|
|
aWeight, aStretch, aItalicStyle,
|
|
|
|
aFeatureSettings, languageOverride,
|
|
|
|
aUnicodeRanges)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We've found an entry that matches the new face exactly, so just add
|
|
|
|
// it to the end of the list. gfxMixedFontFamily::AddFontEntry() will
|
|
|
|
// automatically remove any earlier occurrence of the same proxy.
|
|
|
|
family->AddFontEntry(existingProxyEntry);
|
|
|
|
return existingProxyEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a new face and add it into the family
|
|
|
|
gfxProxyFontEntry *proxyEntry =
|
2013-01-04 18:01:44 +04:00
|
|
|
new gfxProxyFontEntry(aFontFaceSrcList, aWeight, aStretch,
|
2011-04-12 14:53:20 +04:00
|
|
|
aItalicStyle,
|
2012-04-26 10:24:26 +04:00
|
|
|
aFeatureSettings,
|
2011-04-12 14:53:20 +04:00
|
|
|
languageOverride,
|
|
|
|
aUnicodeRanges);
|
|
|
|
family->AddFontEntry(proxyEntry);
|
2008-10-01 07:04:10 +04:00
|
|
|
#ifdef PR_LOGGING
|
2013-11-25 06:02:14 +04:00
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
LOG(("userfonts (%p) added (%s) with style: %s weight: %d stretch: %d",
|
2011-04-12 14:53:20 +04:00
|
|
|
this, NS_ConvertUTF16toUTF8(aFamilyName).get(),
|
2012-03-15 13:04:46 +04:00
|
|
|
(aItalicStyle & NS_FONT_STYLE_ITALIC ? "italic" :
|
|
|
|
(aItalicStyle & NS_FONT_STYLE_OBLIQUE ? "oblique" : "normal")),
|
2011-04-12 14:53:20 +04:00
|
|
|
aWeight, aStretch));
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
#endif
|
2011-04-12 14:53:20 +04:00
|
|
|
|
|
|
|
return proxyEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxUserFontSet::AddFontFace(const nsAString& aFamilyName,
|
|
|
|
gfxFontEntry *aFontEntry)
|
|
|
|
{
|
|
|
|
nsAutoString key(aFamilyName);
|
|
|
|
ToLowerCase(key);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found;
|
2011-04-12 14:53:20 +04:00
|
|
|
|
|
|
|
gfxMixedFontFamily *family = mFontFamilies.GetWeak(key, &found);
|
|
|
|
if (!family) {
|
|
|
|
family = new gfxMixedFontFamily(aFamilyName);
|
|
|
|
mFontFamilies.Put(key, family);
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
2011-04-12 14:53:20 +04:00
|
|
|
|
|
|
|
family->AddFontEntry(aFontEntry);
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry*
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxUserFontSet::FindFontEntry(gfxFontFamily *aFamily,
|
|
|
|
const gfxFontStyle& aFontStyle,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool& aNeedsBold,
|
|
|
|
bool& aWaitForUserFont)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
aWaitForUserFont = false;
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxMixedFontFamily *family = static_cast<gfxMixedFontFamily*>(aFamily);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
gfxFontEntry* fe = family->FindFontForStyle(aFontStyle, aNeedsBold);
|
|
|
|
|
|
|
|
// if not a proxy, font has already been loaded
|
2011-01-06 00:48:48 +03:00
|
|
|
if (!fe->mIsProxy) {
|
2008-10-01 07:04:10 +04:00
|
|
|
return fe;
|
2011-01-06 00:48:48 +03:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
gfxProxyFontEntry *proxyEntry = static_cast<gfxProxyFontEntry*> (fe);
|
|
|
|
|
|
|
|
// if currently loading, return null for now
|
2011-01-06 00:48:48 +03:00
|
|
|
if (proxyEntry->mLoadingState > gfxProxyFontEntry::NOT_LOADING) {
|
|
|
|
aWaitForUserFont =
|
|
|
|
(proxyEntry->mLoadingState < gfxProxyFontEntry::LOADING_SLOWLY);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-01-06 00:48:48 +03:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// hasn't been loaded yet, start the load process
|
|
|
|
LoadStatus status;
|
|
|
|
|
2011-02-12 00:19:23 +03:00
|
|
|
// NOTE that if all sources in the entry fail, this will delete proxyEntry,
|
|
|
|
// so we cannot use it again if status==STATUS_END_OF_LIST
|
2012-12-19 13:42:25 +04:00
|
|
|
status = LoadNext(family, proxyEntry);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// if the load succeeded immediately, the font entry was replaced so
|
|
|
|
// search again
|
2011-01-06 00:48:48 +03:00
|
|
|
if (status == STATUS_LOADED) {
|
2008-10-01 07:04:10 +04:00
|
|
|
return family->FindFontForStyle(aFontStyle, aNeedsBold);
|
2011-01-06 00:48:48 +03:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2011-02-12 00:19:23 +03:00
|
|
|
// check whether we should wait for load to complete before painting
|
|
|
|
// a fallback font -- but not if all sources failed (bug 633500)
|
|
|
|
aWaitForUserFont = (status != STATUS_END_OF_LIST) &&
|
2011-01-06 00:48:48 +03:00
|
|
|
(proxyEntry->mLoadingState < gfxProxyFontEntry::LOADING_SLOWLY);
|
2011-02-12 00:19:23 +03:00
|
|
|
|
|
|
|
// if either loading or an error occurred, return null
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2010-10-07 11:59:19 +04:00
|
|
|
// Based on ots::ExpandingMemoryStream from ots-memory-stream.h,
|
|
|
|
// adapted to use Mozilla allocators and to allow the final
|
|
|
|
// memory buffer to be adopted by the client.
|
|
|
|
class ExpandingMemoryStream : public ots::OTSStream {
|
|
|
|
public:
|
|
|
|
ExpandingMemoryStream(size_t initial, size_t limit)
|
|
|
|
: mLength(initial), mLimit(limit), mOff(0) {
|
|
|
|
mPtr = NS_Alloc(mLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExpandingMemoryStream() {
|
|
|
|
NS_Free(mPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the buffer, and give up ownership of it
|
|
|
|
// so the caller becomes responsible to call NS_Free
|
|
|
|
// when finished with it
|
|
|
|
void* forget() {
|
|
|
|
void* p = mPtr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mPtr = nullptr;
|
2010-10-07 11:59:19 +04:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WriteRaw(const void *data, size_t length) {
|
|
|
|
if ((mOff + length > mLength) ||
|
|
|
|
(mLength > std::numeric_limits<size_t>::max() - mOff)) {
|
|
|
|
if (mLength == mLimit) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t newLength = (mLength + 1) * 2;
|
|
|
|
if (newLength < mLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (newLength > mLimit) {
|
|
|
|
newLength = mLimit;
|
|
|
|
}
|
|
|
|
mPtr = NS_Realloc(mPtr, newLength);
|
|
|
|
mLength = newLength;
|
|
|
|
return WriteRaw(data, length);
|
|
|
|
}
|
|
|
|
std::memcpy(static_cast<char*>(mPtr) + mOff, data, length);
|
|
|
|
mOff += length;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Seek(off_t position) {
|
|
|
|
if (position < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (static_cast<size_t>(position) > mLength) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mOff = position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
off_t Tell() const {
|
|
|
|
return mOff;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void* mPtr;
|
|
|
|
size_t mLength;
|
|
|
|
const size_t mLimit;
|
|
|
|
off_t mOff;
|
|
|
|
};
|
|
|
|
|
2014-01-31 19:09:20 +04:00
|
|
|
static ots::TableAction
|
|
|
|
OTSTableAction(uint32_t aTag, void *aUserData)
|
|
|
|
{
|
2014-05-26 14:07:24 +04:00
|
|
|
// preserve Graphite, color glyph and SVG tables
|
2014-01-31 19:09:20 +04:00
|
|
|
if (aTag == TRUETYPE_TAG('S', 'i', 'l', 'f') ||
|
|
|
|
aTag == TRUETYPE_TAG('S', 'i', 'l', 'l') ||
|
|
|
|
aTag == TRUETYPE_TAG('G', 'l', 'o', 'c') ||
|
|
|
|
aTag == TRUETYPE_TAG('G', 'l', 'a', 't') ||
|
|
|
|
aTag == TRUETYPE_TAG('F', 'e', 'a', 't') ||
|
2014-05-26 14:07:24 +04:00
|
|
|
aTag == TRUETYPE_TAG('S', 'V', 'G', ' ') ||
|
|
|
|
aTag == TRUETYPE_TAG('C', 'O', 'L', 'R') ||
|
|
|
|
aTag == TRUETYPE_TAG('C', 'P', 'A', 'L')) {
|
2014-01-31 19:09:20 +04:00
|
|
|
return ots::TABLE_ACTION_PASSTHRU;
|
|
|
|
}
|
|
|
|
return ots::TABLE_ACTION_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2012-05-30 02:44:50 +04:00
|
|
|
struct OTSCallbackUserData {
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxUserFontSet *mFontSet;
|
|
|
|
gfxMixedFontFamily *mFamily;
|
|
|
|
gfxProxyFontEntry *mProxy;
|
2012-05-30 02:44:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
gfxUserFontSet::OTSMessage(void *aUserData, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
|
|
|
|
// buf should be more than adequate for any message OTS generates,
|
|
|
|
// so we don't worry about checking the result of vsnprintf()
|
|
|
|
char buf[512];
|
|
|
|
(void)vsnprintf(buf, sizeof(buf), format, va);
|
|
|
|
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
OTSCallbackUserData *d = static_cast<OTSCallbackUserData*>(aUserData);
|
2012-12-19 13:42:25 +04:00
|
|
|
d->mFontSet->LogMessage(d->mFamily, d->mProxy, buf);
|
2012-05-30 02:44:50 +04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-07 11:59:19 +04:00
|
|
|
// Call the OTS library to sanitize an sfnt before attempting to use it.
|
2013-07-31 19:44:31 +04:00
|
|
|
// Returns a newly-allocated block, or nullptr in case of fatal errors.
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t*
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxUserFontSet::SanitizeOpenTypeData(gfxMixedFontFamily *aFamily,
|
|
|
|
gfxProxyFontEntry *aProxy,
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t* aData, uint32_t aLength,
|
|
|
|
uint32_t& aSaneLength, bool aIsCompressed)
|
2010-10-07 11:59:19 +04:00
|
|
|
{
|
|
|
|
// limit output/expansion to 256MB
|
|
|
|
ExpandingMemoryStream output(aIsCompressed ? aLength * 2 : aLength,
|
|
|
|
1024 * 1024 * 256);
|
2012-05-30 02:44:50 +04:00
|
|
|
|
|
|
|
OTSCallbackUserData userData;
|
|
|
|
userData.mFontSet = this;
|
2012-12-19 13:42:25 +04:00
|
|
|
userData.mFamily = aFamily;
|
2012-05-30 02:44:50 +04:00
|
|
|
userData.mProxy = aProxy;
|
|
|
|
|
2014-01-31 19:09:20 +04:00
|
|
|
ots::SetTableActionCallback(&OTSTableAction, nullptr);
|
|
|
|
ots::SetMessageCallback(&gfxUserFontSet::OTSMessage, &userData);
|
|
|
|
|
|
|
|
if (ots::Process(&output, aData, aLength)) {
|
2010-10-07 11:59:19 +04:00
|
|
|
aSaneLength = output.Tell();
|
2012-08-22 19:56:38 +04:00
|
|
|
return static_cast<uint8_t*>(output.forget());
|
2010-10-07 11:59:19 +04:00
|
|
|
} else {
|
|
|
|
aSaneLength = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-10-07 11:59:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 10:31:37 +04:00
|
|
|
static void
|
2011-06-16 10:31:37 +04:00
|
|
|
StoreUserFontData(gfxFontEntry* aFontEntry, gfxProxyFontEntry* aProxy,
|
2013-05-22 10:42:30 +04:00
|
|
|
bool aPrivate, const nsAString& aOriginalName,
|
2014-02-08 22:10:07 +04:00
|
|
|
FallibleTArray<uint8_t>* aMetadata, uint32_t aMetaOrigLen)
|
2011-06-16 10:31:37 +04:00
|
|
|
{
|
|
|
|
if (!aFontEntry->mUserFontData) {
|
|
|
|
aFontEntry->mUserFontData = new gfxUserFontData;
|
|
|
|
}
|
|
|
|
gfxUserFontData* userFontData = aFontEntry->mUserFontData;
|
|
|
|
userFontData->mSrcIndex = aProxy->mSrcIndex;
|
|
|
|
const gfxFontFaceSrc& src = aProxy->mSrcList[aProxy->mSrcIndex];
|
|
|
|
if (src.mIsLocal) {
|
|
|
|
userFontData->mLocalName = src.mLocalName;
|
|
|
|
} else {
|
|
|
|
userFontData->mURI = src.mURI;
|
2012-12-10 13:31:07 +04:00
|
|
|
userFontData->mPrincipal = aProxy->mPrincipal;
|
2011-06-16 10:31:37 +04:00
|
|
|
}
|
2013-05-22 10:42:30 +04:00
|
|
|
userFontData->mPrivate = aPrivate;
|
2011-06-16 10:31:37 +04:00
|
|
|
userFontData->mFormat = src.mFormatFlags;
|
2011-06-16 10:31:37 +04:00
|
|
|
userFontData->mRealName = aOriginalName;
|
2011-06-16 10:31:37 +04:00
|
|
|
if (aMetadata) {
|
|
|
|
userFontData->mMetadata.SwapElements(*aMetadata);
|
|
|
|
userFontData->mMetaOrigLen = aMetaOrigLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-09 06:05:33 +04:00
|
|
|
struct WOFFHeader {
|
|
|
|
AutoSwap_PRUint32 signature;
|
|
|
|
AutoSwap_PRUint32 flavor;
|
|
|
|
AutoSwap_PRUint32 length;
|
|
|
|
AutoSwap_PRUint16 numTables;
|
|
|
|
AutoSwap_PRUint16 reserved;
|
|
|
|
AutoSwap_PRUint32 totalSfntSize;
|
|
|
|
AutoSwap_PRUint16 majorVersion;
|
|
|
|
AutoSwap_PRUint16 minorVersion;
|
|
|
|
AutoSwap_PRUint32 metaOffset;
|
|
|
|
AutoSwap_PRUint32 metaCompLen;
|
|
|
|
AutoSwap_PRUint32 metaOrigLen;
|
|
|
|
AutoSwap_PRUint32 privOffset;
|
|
|
|
AutoSwap_PRUint32 privLen;
|
|
|
|
};
|
|
|
|
|
2012-03-07 01:44:23 +04:00
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
gfxUserFontSet::CopyWOFFMetadata(const uint8_t* aFontData,
|
|
|
|
uint32_t aLength,
|
2014-02-08 22:10:07 +04:00
|
|
|
FallibleTArray<uint8_t>* aMetadata,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t* aMetaOrigLen)
|
2011-06-16 10:31:37 +04:00
|
|
|
{
|
|
|
|
// This function may be called with arbitrary, unvalidated "font" data
|
|
|
|
// from @font-face, so it needs to be careful to bounds-check, etc.,
|
|
|
|
// before trying to read anything.
|
|
|
|
// This just saves a copy of the compressed data block; it does NOT check
|
|
|
|
// that the block can be successfully decompressed, or that it contains
|
|
|
|
// well-formed/valid XML metadata.
|
|
|
|
if (aLength < sizeof(WOFFHeader)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const WOFFHeader* woff = reinterpret_cast<const WOFFHeader*>(aFontData);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t metaOffset = woff->metaOffset;
|
|
|
|
uint32_t metaCompLen = woff->metaCompLen;
|
2011-06-16 10:31:37 +04:00
|
|
|
if (!metaOffset || !metaCompLen || !woff->metaOrigLen) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (metaOffset >= aLength || metaCompLen > aLength - metaOffset) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!aMetadata->SetLength(woff->metaCompLen)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(aMetadata->Elements(), aFontData + metaOffset, metaCompLen);
|
|
|
|
*aMetaOrigLen = woff->metaOrigLen;
|
2011-06-16 10:31:37 +04:00
|
|
|
}
|
|
|
|
|
2009-08-30 17:55:24 +04:00
|
|
|
// This is called when a font download finishes.
|
|
|
|
// Ownership of aFontData passes in here, and the font set must
|
|
|
|
// ensure that it is eventually deleted via NS_Free().
|
2012-03-16 07:31:01 +04:00
|
|
|
bool
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxUserFontSet::OnLoadComplete(gfxMixedFontFamily *aFamily,
|
|
|
|
gfxProxyFontEntry *aProxy,
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t *aFontData, uint32_t aLength,
|
2008-10-01 07:04:10 +04:00
|
|
|
nsresult aDownloadStatus)
|
|
|
|
{
|
2012-04-26 21:50:41 +04:00
|
|
|
// forget about the loader, as we no longer potentially need to cancel it
|
|
|
|
// if the entry is obsoleted
|
2012-07-30 18:20:58 +04:00
|
|
|
aProxy->mLoader = nullptr;
|
2012-04-26 21:50:41 +04:00
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
// download successful, make platform font using font data
|
|
|
|
if (NS_SUCCEEDED(aDownloadStatus)) {
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxFontEntry *fe = LoadFont(aFamily, aProxy, aFontData, aLength);
|
2012-07-30 18:20:58 +04:00
|
|
|
aFontData = nullptr;
|
2010-10-07 11:59:19 +04:00
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
if (fe) {
|
2011-04-28 09:04:45 +04:00
|
|
|
IncrementGeneration();
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
} else {
|
|
|
|
// download failed
|
2012-12-19 13:42:25 +04:00
|
|
|
LogMessage(aFamily, aProxy,
|
|
|
|
"download failed", nsIScriptError::errorFlag,
|
2011-07-12 15:24:36 +04:00
|
|
|
aDownloadStatus);
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2010-10-07 11:59:19 +04:00
|
|
|
if (aFontData) {
|
|
|
|
NS_Free((void*)aFontData);
|
|
|
|
}
|
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
// error occurred, load next src
|
2012-12-19 13:42:25 +04:00
|
|
|
(void)LoadNext(aFamily, aProxy);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2011-11-21 12:19:57 +04:00
|
|
|
// We ignore the status returned by LoadNext();
|
|
|
|
// even if loading failed, we need to bump the font-set generation
|
2011-01-06 00:48:48 +03:00
|
|
|
// and return true in order to trigger reflow, so that fallback
|
|
|
|
// will be used where the text was "masked" by the pending download
|
|
|
|
IncrementGeneration();
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gfxUserFontSet::LoadStatus
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxUserFontSet::LoadNext(gfxMixedFontFamily *aFamily,
|
|
|
|
gfxProxyFontEntry *aProxyEntry)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t numSrc = aProxyEntry->mSrcList.Length();
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2011-07-12 15:24:36 +04:00
|
|
|
NS_ASSERTION(aProxyEntry->mSrcIndex < numSrc,
|
|
|
|
"already at the end of the src list for user font");
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2011-01-06 00:48:48 +03:00
|
|
|
if (aProxyEntry->mLoadingState == gfxProxyFontEntry::NOT_LOADING) {
|
|
|
|
aProxyEntry->mLoadingState = gfxProxyFontEntry::LOADING_STARTED;
|
2012-01-05 14:14:11 +04:00
|
|
|
aProxyEntry->mUnsupportedFormat = false;
|
2008-10-01 07:04:10 +04:00
|
|
|
} else {
|
2011-01-06 00:48:48 +03:00
|
|
|
// we were already loading; move to the next source,
|
|
|
|
// but don't reset state - if we've already timed out,
|
|
|
|
// that counts against the new download
|
|
|
|
aProxyEntry->mSrcIndex++;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2011-07-12 15:24:36 +04:00
|
|
|
// load each src entry in turn, until a local face is found
|
|
|
|
// or a download begins successfully
|
2008-10-01 07:04:10 +04:00
|
|
|
while (aProxyEntry->mSrcIndex < numSrc) {
|
|
|
|
const gfxFontFaceSrc& currSrc = aProxyEntry->mSrcList[aProxyEntry->mSrcIndex];
|
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
// src local ==> lookup and load immediately
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
if (currSrc.mIsLocal) {
|
2009-01-03 06:21:49 +03:00
|
|
|
gfxFontEntry *fe =
|
|
|
|
gfxPlatform::GetPlatform()->LookupLocalFont(aProxyEntry,
|
|
|
|
currSrc.mLocalName);
|
2014-04-17 18:17:22 +04:00
|
|
|
mLocalRulesUsed = true;
|
2008-10-01 07:04:10 +04:00
|
|
|
if (fe) {
|
2013-11-25 06:02:14 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] loaded local: (%s) for (%s) gen: %8.8x\n",
|
2011-07-12 15:24:36 +04:00
|
|
|
this, aProxyEntry->mSrcIndex,
|
|
|
|
NS_ConvertUTF16toUTF8(currSrc.mLocalName).get(),
|
2012-12-19 13:42:25 +04:00
|
|
|
NS_ConvertUTF16toUTF8(aFamily->Name()).get(),
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t(mGeneration)));
|
2010-12-09 14:52:03 +03:00
|
|
|
fe->mFeatureSettings.AppendElements(aProxyEntry->mFeatureSettings);
|
2010-07-14 00:31:31 +04:00
|
|
|
fe->mLanguageOverride = aProxyEntry->mLanguageOverride;
|
2013-05-22 10:42:30 +04:00
|
|
|
// For src:local(), we don't care whether the request is from
|
|
|
|
// a private window as there's no issue of caching resources;
|
|
|
|
// local fonts are just available all the time.
|
|
|
|
StoreUserFontData(fe, aProxyEntry, false, nsString(), nullptr, 0);
|
2012-12-19 13:42:25 +04:00
|
|
|
ReplaceFontEntry(aFamily, aProxyEntry, fe);
|
2008-10-01 07:04:10 +04:00
|
|
|
return STATUS_LOADED;
|
|
|
|
} else {
|
2013-11-25 06:02:14 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] failed local: (%s) for (%s)\n",
|
2011-07-12 15:24:36 +04:00
|
|
|
this, aProxyEntry->mSrcIndex,
|
|
|
|
NS_ConvertUTF16toUTF8(currSrc.mLocalName).get(),
|
2012-12-19 13:42:25 +04:00
|
|
|
NS_ConvertUTF16toUTF8(aFamily->Name()).get()));
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
2011-07-12 15:24:36 +04:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// src url ==> start the load process
|
|
|
|
else {
|
2011-07-12 15:24:36 +04:00
|
|
|
if (gfxPlatform::GetPlatform()->IsFontFormatSupported(currSrc.mURI,
|
2008-10-01 07:04:10 +04:00
|
|
|
currSrc.mFormatFlags)) {
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2012-12-10 13:31:07 +04:00
|
|
|
nsIPrincipal *principal = nullptr;
|
2013-05-22 08:39:39 +04:00
|
|
|
bool bypassCache;
|
|
|
|
nsresult rv = CheckFontLoad(&currSrc, &principal, &bypassCache);
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2012-12-10 13:31:07 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && principal != nullptr) {
|
2013-05-22 08:39:39 +04:00
|
|
|
if (!bypassCache) {
|
|
|
|
// see if we have an existing entry for this source
|
|
|
|
gfxFontEntry *fe =
|
|
|
|
UserFontCache::GetFont(currSrc.mURI, principal,
|
2013-05-22 10:42:30 +04:00
|
|
|
aProxyEntry,
|
|
|
|
GetPrivateBrowsing());
|
2013-05-22 08:39:39 +04:00
|
|
|
if (fe) {
|
|
|
|
ReplaceFontEntry(aFamily, aProxyEntry, fe);
|
|
|
|
return STATUS_LOADED;
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2012-12-10 13:31:07 +04:00
|
|
|
// record the principal returned by CheckFontLoad,
|
|
|
|
// for use when creating a channel
|
|
|
|
// and when caching the loaded entry
|
|
|
|
aProxyEntry->mPrincipal = principal;
|
|
|
|
|
|
|
|
bool loadDoesntSpin = false;
|
|
|
|
rv = NS_URIChainHasFlags(currSrc.mURI,
|
|
|
|
nsIProtocolHandler::URI_SYNC_LOAD_IS_OK,
|
|
|
|
&loadDoesntSpin);
|
|
|
|
if (NS_SUCCEEDED(rv) && loadDoesntSpin) {
|
|
|
|
uint8_t *buffer = nullptr;
|
|
|
|
uint32_t bufferLength = 0;
|
|
|
|
|
|
|
|
// sync load font immediately
|
|
|
|
rv = SyncLoadFontData(aProxyEntry, &currSrc,
|
|
|
|
buffer, bufferLength);
|
2013-05-22 08:39:39 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && LoadFont(aFamily, aProxyEntry,
|
|
|
|
buffer, bufferLength)) {
|
2012-12-10 13:31:07 +04:00
|
|
|
return STATUS_LOADED;
|
|
|
|
} else {
|
2012-12-19 13:42:25 +04:00
|
|
|
LogMessage(aFamily, aProxyEntry,
|
|
|
|
"font load failed",
|
2012-12-10 13:31:07 +04:00
|
|
|
nsIScriptError::errorFlag, rv);
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
|
|
|
} else {
|
2012-12-10 13:31:07 +04:00
|
|
|
// otherwise load font async
|
2012-12-19 13:42:25 +04:00
|
|
|
rv = StartLoad(aFamily, aProxyEntry, &currSrc);
|
2012-12-10 13:31:07 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-25 06:02:14 +04:00
|
|
|
if (LOG_ENABLED()) {
|
2012-12-10 13:31:07 +04:00
|
|
|
nsAutoCString fontURI;
|
|
|
|
currSrc.mURI->GetSpec(fontURI);
|
2013-11-25 06:02:14 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] loading uri: (%s) for (%s)\n",
|
2012-12-10 13:31:07 +04:00
|
|
|
this, aProxyEntry->mSrcIndex, fontURI.get(),
|
2012-12-19 13:42:25 +04:00
|
|
|
NS_ConvertUTF16toUTF8(aFamily->Name()).get()));
|
2012-12-10 13:31:07 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return STATUS_LOADING;
|
|
|
|
} else {
|
2012-12-19 13:42:25 +04:00
|
|
|
LogMessage(aFamily, aProxyEntry,
|
|
|
|
"download failed",
|
2012-12-10 13:31:07 +04:00
|
|
|
nsIScriptError::errorFlag, rv);
|
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
2012-12-10 13:31:07 +04:00
|
|
|
} else {
|
2012-12-19 13:42:25 +04:00
|
|
|
LogMessage(aFamily, aProxyEntry, "download not allowed",
|
2012-12-10 13:31:07 +04:00
|
|
|
nsIScriptError::errorFlag, rv);
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-05 14:14:11 +04:00
|
|
|
// We don't log a warning to the web console yet,
|
|
|
|
// as another source may load successfully
|
|
|
|
aProxyEntry->mUnsupportedFormat = true;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aProxyEntry->mSrcIndex++;
|
|
|
|
}
|
|
|
|
|
2012-01-05 14:14:11 +04:00
|
|
|
if (aProxyEntry->mUnsupportedFormat) {
|
2012-12-19 13:42:25 +04:00
|
|
|
LogMessage(aFamily, aProxyEntry, "no supported format found",
|
2012-01-05 14:14:11 +04:00
|
|
|
nsIScriptError::warningFlag);
|
|
|
|
}
|
|
|
|
|
2011-03-24 06:01:50 +03:00
|
|
|
// all src's failed; mark this entry as unusable (so fallback will occur)
|
2013-11-25 06:02:14 +04:00
|
|
|
LOG(("userfonts (%p) failed all src for (%s)\n",
|
2012-12-19 13:42:25 +04:00
|
|
|
this, NS_ConvertUTF16toUTF8(aFamily->Name()).get()));
|
2011-03-24 06:01:50 +03:00
|
|
|
aProxyEntry->mLoadingState = gfxProxyFontEntry::LOADING_FAILED;
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
return STATUS_END_OF_LIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxUserFontSet::IncrementGeneration()
|
|
|
|
{
|
|
|
|
// add one, increment again if zero
|
2012-10-05 09:05:27 +04:00
|
|
|
++sFontSetGeneration;
|
2012-10-03 18:13:18 +04:00
|
|
|
if (sFontSetGeneration == 0)
|
2012-10-05 09:05:27 +04:00
|
|
|
++sFontSetGeneration;
|
2008-10-01 07:04:10 +04:00
|
|
|
mGeneration = sFontSetGeneration;
|
|
|
|
}
|
|
|
|
|
2014-04-17 18:17:22 +04:00
|
|
|
void
|
|
|
|
gfxUserFontSet::RebuildLocalRules()
|
|
|
|
{
|
|
|
|
if (mLocalRulesUsed) {
|
|
|
|
DoRebuildUserFontSet();
|
|
|
|
}
|
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
gfxFontEntry*
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxUserFontSet::LoadFont(gfxMixedFontFamily *aFamily,
|
|
|
|
gfxProxyFontEntry *aProxy,
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint8_t *aFontData, uint32_t &aLength)
|
2012-03-16 07:31:01 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
gfxFontEntry *fe = nullptr;
|
2012-03-16 07:31:01 +04:00
|
|
|
|
|
|
|
gfxUserFontType fontType =
|
|
|
|
gfxFontUtils::DetermineFontDataType(aFontData, aLength);
|
|
|
|
|
|
|
|
// Unwrap/decompress/sanitize or otherwise munge the downloaded data
|
|
|
|
// to make a usable sfnt structure.
|
|
|
|
|
|
|
|
// Because platform font activation code may replace the name table
|
|
|
|
// in the font with a synthetic one, we save the original name so that
|
|
|
|
// it can be reported via the nsIDOMFontFace API.
|
|
|
|
nsAutoString originalFullName;
|
|
|
|
|
2013-01-23 20:41:18 +04:00
|
|
|
// Call the OTS sanitizer; this will also decode WOFF to sfnt
|
|
|
|
// if necessary. The original data in aFontData is left unchanged.
|
|
|
|
uint32_t saneLen;
|
|
|
|
const uint8_t* saneData =
|
|
|
|
SanitizeOpenTypeData(aFamily, aProxy, aFontData, aLength, saneLen,
|
|
|
|
fontType == GFX_USERFONT_WOFF);
|
|
|
|
if (!saneData) {
|
|
|
|
LogMessage(aFamily, aProxy, "rejected by sanitizer");
|
|
|
|
}
|
|
|
|
if (saneData) {
|
|
|
|
// The sanitizer ensures that we have a valid sfnt and a usable
|
|
|
|
// name table, so this should never fail unless we're out of
|
|
|
|
// memory, and GetFullNameFromSFNT is not directly exposed to
|
|
|
|
// arbitrary/malicious data from the web.
|
|
|
|
gfxFontUtils::GetFullNameFromSFNT(saneData, saneLen,
|
|
|
|
originalFullName);
|
|
|
|
// Here ownership of saneData is passed to the platform,
|
|
|
|
// which will delete it when no longer required
|
|
|
|
fe = gfxPlatform::GetPlatform()->MakePlatformFont(aProxy,
|
|
|
|
saneData,
|
|
|
|
saneLen);
|
|
|
|
if (!fe) {
|
|
|
|
LogMessage(aFamily, aProxy, "not usable by platform");
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fe) {
|
2013-01-23 20:41:18 +04:00
|
|
|
// Save a copy of the metadata block (if present) for nsIDOMFontFace
|
|
|
|
// to use if required. Ownership of the metadata block will be passed
|
|
|
|
// to the gfxUserFontData record below.
|
2014-02-08 22:10:07 +04:00
|
|
|
FallibleTArray<uint8_t> metadata;
|
2013-01-23 20:41:18 +04:00
|
|
|
uint32_t metaOrigLen = 0;
|
|
|
|
if (fontType == GFX_USERFONT_WOFF) {
|
|
|
|
CopyWOFFMetadata(aFontData, aLength, &metadata, &metaOrigLen);
|
|
|
|
}
|
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
// copy OpenType feature/language settings from the proxy to the
|
|
|
|
// newly-created font entry
|
|
|
|
fe->mFeatureSettings.AppendElements(aProxy->mFeatureSettings);
|
|
|
|
fe->mLanguageOverride = aProxy->mLanguageOverride;
|
2013-05-22 10:42:30 +04:00
|
|
|
StoreUserFontData(fe, aProxy, GetPrivateBrowsing(),
|
|
|
|
originalFullName, &metadata, metaOrigLen);
|
2012-03-16 07:31:01 +04:00
|
|
|
#ifdef PR_LOGGING
|
2013-11-25 06:02:14 +04:00
|
|
|
if (LOG_ENABLED()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString fontURI;
|
2012-03-16 07:31:01 +04:00
|
|
|
aProxy->mSrcList[aProxy->mSrcIndex].mURI->GetSpec(fontURI);
|
2013-11-25 06:02:14 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] loaded uri: (%s) for (%s) gen: %8.8x\n",
|
2012-03-16 07:31:01 +04:00
|
|
|
this, aProxy->mSrcIndex, fontURI.get(),
|
2012-12-19 13:42:25 +04:00
|
|
|
NS_ConvertUTF16toUTF8(aFamily->Name()).get(),
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t(mGeneration)));
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
|
|
|
#endif
|
2012-12-19 13:42:25 +04:00
|
|
|
ReplaceFontEntry(aFamily, aProxy, fe);
|
2013-02-18 06:22:55 +04:00
|
|
|
UserFontCache::CacheFont(fe);
|
2012-03-16 07:31:01 +04:00
|
|
|
} else {
|
|
|
|
#ifdef PR_LOGGING
|
2013-11-25 06:02:14 +04:00
|
|
|
if (LOG_ENABLED()) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString fontURI;
|
2012-03-16 07:31:01 +04:00
|
|
|
aProxy->mSrcList[aProxy->mSrcIndex].mURI->GetSpec(fontURI);
|
2013-11-25 06:02:14 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] failed uri: (%s) for (%s)"
|
2012-03-16 07:31:01 +04:00
|
|
|
" error making platform font\n",
|
|
|
|
this, aProxy->mSrcIndex, fontURI.get(),
|
2012-12-19 13:42:25 +04:00
|
|
|
NS_ConvertUTF16toUTF8(aFamily->Name()).get()));
|
2012-03-16 07:31:01 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-01-23 20:41:18 +04:00
|
|
|
// The downloaded data can now be discarded; the font entry is using the
|
|
|
|
// sanitized copy
|
|
|
|
NS_Free((void*)aFontData);
|
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2012-12-19 13:42:25 +04:00
|
|
|
gfxFontFamily*
|
2008-12-06 02:19:27 +03:00
|
|
|
gfxUserFontSet::GetFamily(const nsAString& aFamilyName) const
|
|
|
|
{
|
|
|
|
nsAutoString key(aFamilyName);
|
|
|
|
ToLowerCase(key);
|
|
|
|
|
|
|
|
return mFontFamilies.GetWeak(key);
|
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
|
2013-01-03 02:27:49 +04:00
|
|
|
struct FindFamilyCallbackData {
|
|
|
|
gfxFontEntry *mFontEntry;
|
|
|
|
gfxFontFamily *mFamily;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
FindFamilyCallback(const nsAString& aName,
|
|
|
|
gfxMixedFontFamily* aFamily,
|
|
|
|
void* aUserArg)
|
|
|
|
{
|
|
|
|
FindFamilyCallbackData *d = static_cast<FindFamilyCallbackData*>(aUserArg);
|
|
|
|
if (aFamily->ContainsFace(d->mFontEntry)) {
|
|
|
|
d->mFamily = aFamily;
|
|
|
|
return PL_DHASH_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontFamily*
|
|
|
|
gfxUserFontSet::FindFamilyFor(gfxFontEntry* aFontEntry) const
|
|
|
|
{
|
|
|
|
FindFamilyCallbackData d = { aFontEntry, nullptr };
|
|
|
|
mFontFamilies.EnumerateRead(FindFamilyCallback, &d);
|
|
|
|
return d.mFamily;
|
|
|
|
}
|
|
|
|
|
2012-12-10 13:31:07 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// gfxUserFontSet::UserFontCache - re-use platform font entries for user fonts
|
|
|
|
// across pages/fontsets rather than instantiating new platform fonts.
|
|
|
|
//
|
|
|
|
// Entries are added to this cache when a platform font is instantiated from
|
|
|
|
// downloaded data, and removed when the platform font entry is destroyed.
|
|
|
|
// We don't need to use a timed expiration scheme here because the gfxFontEntry
|
|
|
|
// for a downloaded font will be kept alive by its corresponding gfxFont
|
|
|
|
// instance(s) until they are deleted, and *that* happens using an expiration
|
|
|
|
// tracker (gfxFontCache). The result is that the downloaded font instances
|
|
|
|
// recorded here will persist between pages and can get reused (provided the
|
|
|
|
// source URI and principal match, of course).
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
nsTHashtable<gfxUserFontSet::UserFontCache::Entry>*
|
|
|
|
gfxUserFontSet::UserFontCache::sUserFonts = nullptr;
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(gfxUserFontSet::UserFontCache::Flusher, nsIObserver)
|
2013-05-22 08:39:47 +04:00
|
|
|
|
2013-05-22 10:42:30 +04:00
|
|
|
PLDHashOperator
|
|
|
|
gfxUserFontSet::UserFontCache::Entry::RemoveIfPrivate(Entry* aEntry,
|
|
|
|
void* aUserData)
|
|
|
|
{
|
|
|
|
return aEntry->mPrivate ? PL_DHASH_REMOVE : PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-09-18 14:48:38 +04:00
|
|
|
PLDHashOperator
|
|
|
|
gfxUserFontSet::UserFontCache::Entry::RemoveIfMatches(Entry* aEntry,
|
|
|
|
void* aUserData)
|
|
|
|
{
|
|
|
|
return aEntry->GetFontEntry() == static_cast<gfxFontEntry*>(aUserData) ?
|
|
|
|
PL_DHASH_REMOVE : PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-05-22 19:48:55 +04:00
|
|
|
PLDHashOperator
|
|
|
|
gfxUserFontSet::UserFontCache::Entry::DisconnectSVG(Entry* aEntry,
|
|
|
|
void* aUserData)
|
|
|
|
{
|
|
|
|
aEntry->GetFontEntry()->DisconnectSVG();
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-05-22 08:39:47 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
gfxUserFontSet::UserFontCache::Flusher::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* aData)
|
2013-05-22 08:39:47 +04:00
|
|
|
{
|
2013-05-22 10:42:30 +04:00
|
|
|
if (!sUserFonts) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-05 21:32:04 +04:00
|
|
|
if (!strcmp(aTopic, NS_CACHESERVICE_EMPTYCACHE_TOPIC_ID)) {
|
2013-05-22 08:39:47 +04:00
|
|
|
sUserFonts->Clear();
|
2013-05-22 10:42:30 +04:00
|
|
|
} else if (!strcmp(aTopic, "last-pb-context-exited")) {
|
|
|
|
sUserFonts->EnumerateEntries(Entry::RemoveIfPrivate, nullptr);
|
2013-05-22 19:48:55 +04:00
|
|
|
} else if (!strcmp(aTopic, "xpcom-shutdown")) {
|
|
|
|
sUserFonts->EnumerateEntries(Entry::DisconnectSVG, nullptr);
|
2013-05-22 10:42:30 +04:00
|
|
|
} else {
|
|
|
|
NS_NOTREACHED("unexpected topic");
|
2013-05-22 08:39:47 +04:00
|
|
|
}
|
2013-05-22 10:42:30 +04:00
|
|
|
|
2013-05-22 08:39:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-12-10 13:31:07 +04:00
|
|
|
bool
|
|
|
|
gfxUserFontSet::UserFontCache::Entry::KeyEquals(const KeyTypePointer aKey) const
|
|
|
|
{
|
|
|
|
bool equal;
|
|
|
|
if (NS_FAILED(mURI->Equals(aKey->mURI, &equal)) || !equal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(mPrincipal->Equals(aKey->mPrincipal, &equal)) || !equal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-22 10:42:30 +04:00
|
|
|
if (mPrivate != aKey->mPrivate) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-12-10 13:31:07 +04:00
|
|
|
const gfxFontEntry *fe = aKey->mFontEntry;
|
|
|
|
if (mFontEntry->mItalic != fe->mItalic ||
|
|
|
|
mFontEntry->mWeight != fe->mWeight ||
|
|
|
|
mFontEntry->mStretch != fe->mStretch ||
|
|
|
|
mFontEntry->mFeatureSettings != fe->mFeatureSettings ||
|
2013-02-18 06:22:55 +04:00
|
|
|
mFontEntry->mLanguageOverride != fe->mLanguageOverride ||
|
|
|
|
mFontEntry->mFamilyName != fe->mFamilyName) {
|
2012-12-10 13:31:07 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxUserFontSet::UserFontCache::CacheFont(gfxFontEntry *aFontEntry)
|
|
|
|
{
|
2013-02-18 06:22:55 +04:00
|
|
|
NS_ASSERTION(aFontEntry->mFamilyName.Length() != 0,
|
|
|
|
"caching a font associated with no family yet");
|
2012-12-10 13:31:07 +04:00
|
|
|
if (!sUserFonts) {
|
|
|
|
sUserFonts = new nsTHashtable<Entry>;
|
2013-05-22 08:39:47 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
2013-05-22 10:42:30 +04:00
|
|
|
Flusher *flusher = new Flusher;
|
2014-06-05 21:32:04 +04:00
|
|
|
obs->AddObserver(flusher, NS_CACHESERVICE_EMPTYCACHE_TOPIC_ID,
|
2013-05-22 08:39:47 +04:00
|
|
|
false);
|
2013-05-22 10:42:30 +04:00
|
|
|
obs->AddObserver(flusher, "last-pb-context-exited", false);
|
2013-05-22 19:48:55 +04:00
|
|
|
obs->AddObserver(flusher, "xpcom-shutdown", false);
|
2013-05-22 08:39:47 +04:00
|
|
|
}
|
2012-12-10 13:31:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxUserFontData *data = aFontEntry->mUserFontData;
|
2013-05-22 10:42:30 +04:00
|
|
|
sUserFonts->PutEntry(Key(data->mURI, data->mPrincipal, aFontEntry,
|
|
|
|
data->mPrivate));
|
2013-09-19 05:56:49 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_USERFONT_CACHE
|
|
|
|
printf("userfontcache added fontentry: %p\n", aFontEntry);
|
|
|
|
Dump();
|
|
|
|
#endif
|
2012-12-10 13:31:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxUserFontSet::UserFontCache::ForgetFont(gfxFontEntry *aFontEntry)
|
|
|
|
{
|
|
|
|
if (!sUserFonts) {
|
|
|
|
// if we've already deleted the cache (i.e. during shutdown),
|
|
|
|
// just ignore this
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-18 14:48:38 +04:00
|
|
|
// We can't simply use RemoveEntry here because it's possible the principal
|
|
|
|
// may have changed since the font was cached, in which case the lookup
|
|
|
|
// would no longer find the entry (bug 838105).
|
|
|
|
sUserFonts->EnumerateEntries(
|
|
|
|
gfxUserFontSet::UserFontCache::Entry::RemoveIfMatches, aFontEntry);
|
2013-09-19 05:56:49 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_USERFONT_CACHE
|
|
|
|
printf("userfontcache removed fontentry: %p\n", aFontEntry);
|
|
|
|
Dump();
|
|
|
|
#endif
|
2012-12-10 13:31:07 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxFontEntry*
|
|
|
|
gfxUserFontSet::UserFontCache::GetFont(nsIURI *aSrcURI,
|
|
|
|
nsIPrincipal *aPrincipal,
|
2013-05-22 10:42:30 +04:00
|
|
|
gfxProxyFontEntry *aProxy,
|
|
|
|
bool aPrivate)
|
2012-12-10 13:31:07 +04:00
|
|
|
{
|
|
|
|
if (!sUserFonts) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-05-22 10:42:30 +04:00
|
|
|
Entry* entry = sUserFonts->GetEntry(Key(aSrcURI, aPrincipal, aProxy,
|
|
|
|
aPrivate));
|
2012-12-10 13:31:07 +04:00
|
|
|
if (entry) {
|
|
|
|
return entry->GetFontEntry();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxUserFontSet::UserFontCache::Shutdown()
|
|
|
|
{
|
|
|
|
if (sUserFonts) {
|
|
|
|
delete sUserFonts;
|
|
|
|
sUserFonts = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2013-09-19 05:56:49 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG_USERFONT_CACHE
|
|
|
|
|
|
|
|
PLDHashOperator
|
|
|
|
gfxUserFontSet::UserFontCache::Entry::DumpEntry(Entry* aEntry, void* aUserData)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsAutoCString principalURISpec;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> principalURI;
|
|
|
|
rv = aEntry->mPrincipal->GetURI(getter_AddRefs(principalURI));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
principalURI->GetSpec(principalURISpec);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool setDomain = false;
|
|
|
|
nsCOMPtr<nsIURI> domainURI;
|
|
|
|
|
|
|
|
aEntry->mPrincipal->GetDomain(getter_AddRefs(domainURI));
|
|
|
|
if (domainURI) {
|
|
|
|
setDomain = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aEntry->mURI, "null URI in userfont cache entry");
|
|
|
|
|
|
|
|
printf("userfontcache fontEntry: %p fonturihash: %8.8x family: %s domainset: %s principal: [%s]\n",
|
|
|
|
aEntry->mFontEntry,
|
|
|
|
nsURIHashKey::HashKey(aEntry->mURI),
|
|
|
|
NS_ConvertUTF16toUTF8(aEntry->mFontEntry->FamilyName()).get(),
|
|
|
|
(setDomain ? "true" : "false"),
|
|
|
|
principalURISpec.get()
|
|
|
|
);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxUserFontSet::UserFontCache::Dump()
|
|
|
|
{
|
|
|
|
if (!sUserFonts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("userfontcache dump count: %d ========\n", sUserFonts->Count());
|
|
|
|
sUserFonts->EnumerateEntries(Entry::DumpEntry, nullptr);
|
|
|
|
printf("userfontcache dump ==================\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|