2008-10-01 07:04:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is thebes gfx code.
|
|
|
|
*
|
2009-12-22 00:50:30 +03:00
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
2009-08-30 17:55:24 +04:00
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008-2009
|
2008-10-01 07:04:10 +04:00
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* John Daggett <jdaggett@mozilla.com>
|
2009-08-30 17:55:24 +04:00
|
|
|
* Jonathan Kew <jfkthame@gmail.com>
|
2008-10-01 07:04:10 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#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 "nsReadableUtils.h"
|
|
|
|
#include "nsUnicharUtils.h"
|
|
|
|
#include "prlong.h"
|
2012-03-16 07:31:01 +04:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIProtocolHandler.h"
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2009-08-30 17:55:24 +04:00
|
|
|
#include "woff.h"
|
|
|
|
|
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
|
2011-07-12 15:24:36 +04:00
|
|
|
PRLogModuleInfo *gfxUserFontSet::sUserFontsLog = PR_NewLogModule("userfonts");
|
2008-10-01 07:04:10 +04:00
|
|
|
#endif /* PR_LOGGING */
|
|
|
|
|
2011-07-12 15:24:36 +04:00
|
|
|
#define LOG(args) PR_LOG(sUserFontsLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(sUserFontsLog, PR_LOG_DEBUG)
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
static PRUint64 sFontSetGeneration = LL_INIT(0, 0);
|
|
|
|
|
|
|
|
// TODO: support for unicode ranges not yet implemented
|
|
|
|
|
2010-07-14 00:31:31 +04:00
|
|
|
gfxProxyFontEntry::gfxProxyFontEntry(const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
2008-12-06 02:19:27 +03:00
|
|
|
gfxMixedFontFamily *aFamily,
|
2010-07-14 00:31:31 +04:00
|
|
|
PRUint32 aWeight,
|
|
|
|
PRUint32 aStretch,
|
|
|
|
PRUint32 aItalicStyle,
|
2010-12-09 14:52:03 +03:00
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
2010-07-14 00:31:31 +04:00
|
|
|
PRUint32 aLanguageOverride,
|
2008-10-01 07:04:10 +04:00
|
|
|
gfxSparseBitSet *aUnicodeRanges)
|
2011-01-06 00:48:48 +03:00
|
|
|
: gfxFontEntry(NS_LITERAL_STRING("Proxy"), aFamily),
|
2012-01-05 14:14:11 +04:00
|
|
|
mLoadingState(NOT_LOADING),
|
2012-04-26 22:09:20 +04:00
|
|
|
mUnsupportedFormat(false)
|
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;
|
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
|
|
|
|
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
|
|
|
|
return nsnull;
|
2008-10-01 07:04:10 +04:00
|
|
|
}
|
|
|
|
|
2008-12-12 10:31:51 +03:00
|
|
|
gfxUserFontSet::gfxUserFontSet()
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
|
|
|
mFontFamilies.Init(5);
|
|
|
|
IncrementGeneration();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxUserFontSet::~gfxUserFontSet()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
PRUint32 aWeight,
|
|
|
|
PRUint32 aStretch,
|
|
|
|
PRUint32 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)
|
|
|
|
{
|
2011-04-12 14:53:20 +04:00
|
|
|
gfxProxyFontEntry *proxyEntry = nsnull;
|
|
|
|
|
2008-10-01 07:04:10 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a new face and add it into the family
|
2011-04-12 14:53:20 +04:00
|
|
|
PRUint32 languageOverride =
|
|
|
|
gfxFontStyle::ParseFontLanguageOverride(aLanguageOverride);
|
|
|
|
proxyEntry =
|
|
|
|
new gfxProxyFontEntry(aFontFaceSrcList, family, aWeight, aStretch,
|
|
|
|
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
|
2011-04-12 14:53:20 +04:00
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
LOG(("userfonts (%p) added (%s) with style: %s weight: %d stretch: %d",
|
|
|
|
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*
|
|
|
|
gfxUserFontSet::FindFontEntry(const nsAString& aName,
|
|
|
|
const gfxFontStyle& aFontStyle,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool& aFoundFamily,
|
|
|
|
bool& aNeedsBold,
|
|
|
|
bool& aWaitForUserFont)
|
2008-10-01 07:04:10 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
aWaitForUserFont = false;
|
2008-12-06 02:19:27 +03:00
|
|
|
gfxMixedFontFamily *family = GetFamily(aName);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
|
|
|
// no user font defined for this name
|
2011-01-06 00:48:48 +03:00
|
|
|
if (!family) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aFoundFamily = false;
|
2008-10-01 07:04:10 +04:00
|
|
|
return nsnull;
|
2011-01-06 00:48:48 +03:00
|
|
|
}
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
aFoundFamily = true;
|
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);
|
2008-10-01 07:04:10 +04:00
|
|
|
return nsnull;
|
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
|
2008-10-01 07:04:10 +04:00
|
|
|
status = LoadNext(proxyEntry);
|
|
|
|
|
|
|
|
// 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
|
2008-10-01 07:04:10 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2009-08-30 17:55:24 +04:00
|
|
|
// Given a buffer of downloaded font data, do any necessary preparation
|
|
|
|
// to make it into usable OpenType.
|
|
|
|
// May return the original pointer unchanged, or a newly-allocated
|
|
|
|
// block (in which case the passed-in block is NS_Free'd).
|
|
|
|
// aLength is updated if necessary to the new length of the data.
|
|
|
|
// Returns NULL and NS_Free's the incoming data in case of errors.
|
2010-10-07 11:59:19 +04:00
|
|
|
static const PRUint8*
|
2009-08-30 17:55:24 +04:00
|
|
|
PrepareOpenTypeData(const PRUint8* aData, PRUint32* aLength)
|
|
|
|
{
|
|
|
|
switch(gfxFontUtils::DetermineFontDataType(aData, *aLength)) {
|
|
|
|
|
|
|
|
case GFX_USERFONT_OPENTYPE:
|
|
|
|
// nothing to do
|
|
|
|
return aData;
|
|
|
|
|
|
|
|
case GFX_USERFONT_WOFF: {
|
|
|
|
PRUint32 status = eWOFF_ok;
|
|
|
|
PRUint32 bufferSize = woffGetDecodedSize(aData, *aLength, &status);
|
|
|
|
if (WOFF_FAILURE(status)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PRUint8* decodedData = static_cast<PRUint8*>(NS_Alloc(bufferSize));
|
|
|
|
if (!decodedData) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
woffDecodeToBuffer(aData, *aLength,
|
|
|
|
decodedData, bufferSize,
|
|
|
|
aLength, &status);
|
|
|
|
// replace original data with the decoded version
|
|
|
|
NS_Free((void*)aData);
|
|
|
|
aData = decodedData;
|
|
|
|
if (WOFF_FAILURE(status)) {
|
|
|
|
// something went wrong, discard the data and return NULL
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// success, return the decoded data
|
|
|
|
return aData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// xxx - add support for other wrappers here
|
|
|
|
|
|
|
|
default:
|
|
|
|
NS_WARNING("unknown font format");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// discard downloaded data that couldn't be used
|
|
|
|
NS_Free((void*)aData);
|
2008-10-01 07:04:10 +04:00
|
|
|
|
2009-08-30 17:55:24 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
mPtr = nsnull;
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Call the OTS library to sanitize an sfnt before attempting to use it.
|
|
|
|
// Returns a newly-allocated block, or NULL in case of fatal errors.
|
|
|
|
static const PRUint8*
|
|
|
|
SanitizeOpenTypeData(const PRUint8* aData, PRUint32 aLength,
|
|
|
|
PRUint32& aSaneLength, bool aIsCompressed)
|
|
|
|
{
|
|
|
|
// limit output/expansion to 256MB
|
|
|
|
ExpandingMemoryStream output(aIsCompressed ? aLength * 2 : aLength,
|
|
|
|
1024 * 1024 * 256);
|
2011-12-10 02:32:29 +04:00
|
|
|
#ifdef MOZ_GRAPHITE
|
|
|
|
#define PRESERVE_GRAPHITE true
|
|
|
|
#else
|
|
|
|
#define PRESERVE_GRAPHITE false
|
|
|
|
#endif
|
|
|
|
if (ots::Process(&output, aData, aLength, PRESERVE_GRAPHITE)) {
|
2010-10-07 11:59:19 +04:00
|
|
|
aSaneLength = output.Tell();
|
|
|
|
return static_cast<PRUint8*>(output.forget());
|
|
|
|
} else {
|
|
|
|
aSaneLength = 0;
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-16 10:31:37 +04:00
|
|
|
static void
|
2011-06-16 10:31:37 +04:00
|
|
|
StoreUserFontData(gfxFontEntry* aFontEntry, gfxProxyFontEntry* aProxy,
|
2011-06-16 10:31:37 +04:00
|
|
|
const nsAString& aOriginalName,
|
2011-06-16 10:31:37 +04:00
|
|
|
nsTArray<PRUint8>* aMetadata, PRUint32 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;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
gfxUserFontSet::CopyWOFFMetadata(const PRUint8* aFontData,
|
|
|
|
PRUint32 aLength,
|
|
|
|
nsTArray<PRUint8>* aMetadata,
|
|
|
|
PRUint32* 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);
|
|
|
|
PRUint32 metaOffset = woff->metaOffset;
|
|
|
|
PRUint32 metaCompLen = woff->metaCompLen;
|
|
|
|
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
|
2011-04-28 09:04:45 +04:00
|
|
|
gfxUserFontSet::OnLoadComplete(gfxProxyFontEntry *aProxy,
|
2011-07-12 15:24:36 +04:00
|
|
|
const PRUint8 *aFontData, PRUint32 aLength,
|
2008-10-01 07:04:10 +04:00
|
|
|
nsresult aDownloadStatus)
|
|
|
|
{
|
|
|
|
// download successful, make platform font using font data
|
|
|
|
if (NS_SUCCEEDED(aDownloadStatus)) {
|
2012-03-16 07:31:01 +04:00
|
|
|
gfxFontEntry *fe = LoadFont(aProxy, aFontData, aLength);
|
2012-03-16 07:31:01 +04:00
|
|
|
aFontData = nsnull;
|
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
|
2011-07-12 15:24:36 +04:00
|
|
|
LogMessage(aProxy, "download failed", nsIScriptError::errorFlag,
|
|
|
|
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
|
2011-11-21 12:19:57 +04:00
|
|
|
(void)LoadNext(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
|
|
|
|
gfxUserFontSet::LoadNext(gfxProxyFontEntry *aProxyEntry)
|
|
|
|
{
|
|
|
|
PRUint32 numSrc = aProxyEntry->mSrcList.Length();
|
|
|
|
|
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);
|
2008-10-01 07:04:10 +04:00
|
|
|
if (fe) {
|
2011-07-12 15:24:36 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] loaded local: (%s) for (%s) gen: %8.8x\n",
|
|
|
|
this, aProxyEntry->mSrcIndex,
|
|
|
|
NS_ConvertUTF16toUTF8(currSrc.mLocalName).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get(),
|
2008-10-01 07:04:10 +04:00
|
|
|
PRUint32(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;
|
2011-06-16 10:31:37 +04:00
|
|
|
StoreUserFontData(fe, aProxyEntry, nsString(), nsnull, 0);
|
2011-04-12 14:53:20 +04:00
|
|
|
ReplaceFontEntry(aProxyEntry, fe);
|
2008-10-01 07:04:10 +04:00
|
|
|
return STATUS_LOADED;
|
|
|
|
} else {
|
2011-07-12 15:24:36 +04:00
|
|
|
LOG(("userfonts (%p) [src %d] failed local: (%s) for (%s)\n",
|
|
|
|
this, aProxyEntry->mSrcIndex,
|
|
|
|
NS_ConvertUTF16toUTF8(currSrc.mLocalName).get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->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
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
bool loadDoesntSpin = false;
|
|
|
|
rv = NS_URIChainHasFlags(currSrc.mURI,
|
2012-03-16 07:31:01 +04:00
|
|
|
nsIProtocolHandler::URI_SYNC_LOAD_IS_OK,
|
2012-03-16 07:31:01 +04:00
|
|
|
&loadDoesntSpin);
|
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && loadDoesntSpin) {
|
2012-03-16 07:31:01 +04:00
|
|
|
PRUint8 *buffer = nsnull;
|
|
|
|
PRUint32 bufferLength = 0;
|
|
|
|
|
|
|
|
// sync load font immediately
|
|
|
|
rv = SyncLoadFontData(aProxyEntry, &currSrc, buffer,
|
|
|
|
bufferLength);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
2012-03-16 07:31:01 +04:00
|
|
|
LoadFont(aProxyEntry, buffer, bufferLength)) {
|
2012-03-16 07:31:01 +04:00
|
|
|
return STATUS_LOADED;
|
|
|
|
} else {
|
|
|
|
LogMessage(aProxyEntry, "font load failed",
|
|
|
|
nsIScriptError::errorFlag, rv);
|
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 {
|
2012-03-16 07:31:01 +04:00
|
|
|
// otherwise load font async
|
|
|
|
rv = StartLoad(aProxyEntry, &currSrc);
|
2012-04-04 13:15:10 +04:00
|
|
|
bool loadOK = NS_SUCCEEDED(rv);
|
2012-03-16 07:31:01 +04:00
|
|
|
|
|
|
|
if (loadOK) {
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCAutoString fontURI;
|
|
|
|
currSrc.mURI->GetSpec(fontURI);
|
|
|
|
LOG(("userfonts (%p) [src %d] loading uri: (%s) for (%s)\n",
|
|
|
|
this, aProxyEntry->mSrcIndex, fontURI.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->Name()).get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return STATUS_LOADING;
|
|
|
|
} else {
|
|
|
|
LogMessage(aProxyEntry, "download failed",
|
|
|
|
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) {
|
|
|
|
LogMessage(aProxyEntry, "no supported format found",
|
|
|
|
nsIScriptError::warningFlag);
|
|
|
|
}
|
|
|
|
|
2011-03-24 06:01:50 +03:00
|
|
|
// all src's failed; mark this entry as unusable (so fallback will occur)
|
2011-07-12 15:24:36 +04:00
|
|
|
LOG(("userfonts (%p) failed all src for (%s)\n",
|
|
|
|
this, NS_ConvertUTF16toUTF8(aProxyEntry->mFamily->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
|
|
|
|
LL_ADD(sFontSetGeneration, sFontSetGeneration, 1);
|
|
|
|
if (LL_IS_ZERO(sFontSetGeneration))
|
|
|
|
LL_ADD(sFontSetGeneration, sFontSetGeneration, 1);
|
|
|
|
mGeneration = sFontSetGeneration;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-16 07:31:01 +04:00
|
|
|
gfxFontEntry*
|
|
|
|
gfxUserFontSet::LoadFont(gfxProxyFontEntry *aProxy,
|
2012-03-16 07:31:01 +04:00
|
|
|
const PRUint8 *aFontData, PRUint32 &aLength)
|
2012-03-16 07:31:01 +04:00
|
|
|
{
|
|
|
|
gfxFontEntry *fe = nsnull;
|
|
|
|
|
|
|
|
gfxUserFontType fontType =
|
|
|
|
gfxFontUtils::DetermineFontDataType(aFontData, aLength);
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
// NOTE: after the non-OTS codepath using PrepareOpenTypeData is
|
|
|
|
// removed, we should defer this until after we've created the new
|
|
|
|
// fontEntry.
|
|
|
|
nsTArray<PRUint8> metadata;
|
|
|
|
PRUint32 metaOrigLen = 0;
|
|
|
|
if (fontType == GFX_USERFONT_WOFF) {
|
|
|
|
CopyWOFFMetadata(aFontData, aLength, &metadata, &metaOrigLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
if (gfxPlatform::GetPlatform()->SanitizeDownloadedFonts()) {
|
|
|
|
// Call the OTS sanitizer; this will also decode WOFF to sfnt
|
|
|
|
// if necessary. The original data in aFontData is left unchanged.
|
|
|
|
PRUint32 saneLen;
|
|
|
|
const PRUint8* saneData =
|
|
|
|
SanitizeOpenTypeData(aFontData, aLength, saneLen,
|
|
|
|
fontType == GFX_USERFONT_WOFF);
|
|
|
|
if (!saneData) {
|
|
|
|
LogMessage(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(aProxy, "not usable by platform");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FIXME: this code can be removed once we remove the pref to
|
|
|
|
// disable the sanitizer; the PrepareOpenTypeData and
|
|
|
|
// ValidateSFNTHeaders functions will then be obsolete.
|
|
|
|
aFontData = PrepareOpenTypeData(aFontData, &aLength);
|
|
|
|
|
|
|
|
if (aFontData) {
|
|
|
|
if (gfxFontUtils::ValidateSFNTHeaders(aFontData, aLength)) {
|
|
|
|
// ValidateSFNTHeaders has checked that we have a valid
|
|
|
|
// sfnt structure and a usable 'name' table
|
|
|
|
gfxFontUtils::GetFullNameFromSFNT(aFontData, aLength,
|
|
|
|
originalFullName);
|
|
|
|
// Here ownership of aFontData is passed to the platform,
|
|
|
|
// which will delete it when no longer required
|
|
|
|
fe = gfxPlatform::GetPlatform()->MakePlatformFont(aProxy,
|
|
|
|
aFontData,
|
|
|
|
aLength);
|
|
|
|
if (!fe) {
|
|
|
|
LogMessage(aProxy, "not usable by platform");
|
|
|
|
}
|
|
|
|
aFontData = nsnull; // we must NOT free this!
|
|
|
|
} else {
|
|
|
|
// the data was unusable, so just discard it
|
|
|
|
// (error will be reported below, if logging is enabled)
|
|
|
|
LogMessage(aProxy, "SFNT header or tables invalid");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aFontData) {
|
|
|
|
NS_Free((void*)aFontData);
|
|
|
|
aFontData = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fe) {
|
|
|
|
// copy OpenType feature/language settings from the proxy to the
|
|
|
|
// newly-created font entry
|
|
|
|
fe->mFeatureSettings.AppendElements(aProxy->mFeatureSettings);
|
|
|
|
fe->mLanguageOverride = aProxy->mLanguageOverride;
|
|
|
|
StoreUserFontData(fe, aProxy, originalFullName,
|
|
|
|
&metadata, metaOrigLen);
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
// must do this before ReplaceFontEntry() because that will
|
|
|
|
// clear the proxy's mFamily pointer!
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCAutoString fontURI;
|
|
|
|
aProxy->mSrcList[aProxy->mSrcIndex].mURI->GetSpec(fontURI);
|
|
|
|
LOG(("userfonts (%p) [src %d] loaded uri: (%s) for (%s) gen: %8.8x\n",
|
|
|
|
this, aProxy->mSrcIndex, fontURI.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aProxy->mFamily->Name()).get(),
|
|
|
|
PRUint32(mGeneration)));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ReplaceFontEntry(aProxy, fe);
|
|
|
|
} else {
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
|
|
|
nsCAutoString fontURI;
|
|
|
|
aProxy->mSrcList[aProxy->mSrcIndex].mURI->GetSpec(fontURI);
|
|
|
|
LOG(("userfonts (%p) [src %d] failed uri: (%s) for (%s)"
|
|
|
|
" error making platform font\n",
|
|
|
|
this, aProxy->mSrcIndex, fontURI.get(),
|
|
|
|
NS_ConvertUTF16toUTF8(aProxy->mFamily->Name()).get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return fe;
|
|
|
|
}
|
|
|
|
|
2008-12-06 02:19:27 +03:00
|
|
|
gfxMixedFontFamily*
|
|
|
|
gfxUserFontSet::GetFamily(const nsAString& aFamilyName) const
|
|
|
|
{
|
|
|
|
nsAutoString key(aFamilyName);
|
|
|
|
ToLowerCase(key);
|
|
|
|
|
|
|
|
return mFontFamilies.GetWeak(key);
|
|
|
|
}
|
2012-03-16 07:31:01 +04:00
|
|
|
|