2008-09-30 20:04:10 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
#ifndef GFX_USER_FONT_SET_H
|
|
|
|
#define GFX_USER_FONT_SET_H
|
|
|
|
|
|
|
|
#include "gfxTypes.h"
|
|
|
|
#include "gfxFont.h"
|
|
|
|
#include "gfxFontUtils.h"
|
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIFile.h"
|
2010-04-01 11:05:40 -07:00
|
|
|
#include "nsISupportsImpl.h"
|
2011-07-12 04:24:36 -07:00
|
|
|
#include "nsIScriptError.h"
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
class nsIURI;
|
|
|
|
class gfxMixedFontFamily;
|
2012-04-26 10:50:41 -07:00
|
|
|
class nsFontFaceLoader;
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
// parsed CSS @font-face rule information
|
|
|
|
// lifetime: from when @font-face rule processed until font is loaded
|
|
|
|
struct gfxFontFaceSrc {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsLocal; // url or local
|
2008-11-26 19:50:16 -08:00
|
|
|
|
|
|
|
// if url, whether to use the origin principal or not
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mUseOriginPrincipal;
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
// format hint flags, union of all possible formats
|
|
|
|
// (e.g. TrueType, EOT, SVG, etc.)
|
|
|
|
// see FLAG_FORMAT_* enum values below
|
|
|
|
PRUint32 mFormatFlags;
|
2008-11-26 19:50:16 -08:00
|
|
|
|
|
|
|
nsString mLocalName; // full font name if local
|
|
|
|
nsCOMPtr<nsIURI> mURI; // uri if url
|
|
|
|
nsCOMPtr<nsIURI> mReferrer; // referrer url if url
|
|
|
|
nsCOMPtr<nsISupports> mOriginPrincipal; // principal if url
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
};
|
|
|
|
|
2011-06-15 23:31:37 -07:00
|
|
|
// Subclassed to store platform-specific code cleaned out when font entry is
|
|
|
|
// deleted.
|
|
|
|
// Lifetime: from when platform font is created until it is deactivated.
|
|
|
|
// If the platform does not need to add any platform-specific code/data here,
|
|
|
|
// then the gfxUserFontSet will allocate a base gfxUserFontData and attach
|
|
|
|
// to the entry to track the basic user font info fields here.
|
2008-09-30 20:04:10 -07:00
|
|
|
class gfxUserFontData {
|
|
|
|
public:
|
2011-06-15 23:31:37 -07:00
|
|
|
gfxUserFontData()
|
|
|
|
: mSrcIndex(0), mFormat(0), mMetaOrigLen(0)
|
|
|
|
{ }
|
2008-09-30 20:04:10 -07:00
|
|
|
virtual ~gfxUserFontData() { }
|
2011-06-15 23:31:37 -07:00
|
|
|
|
|
|
|
nsTArray<PRUint8> mMetadata; // woff metadata block (compressed), if any
|
|
|
|
nsCOMPtr<nsIURI> mURI; // URI of the source, if it was url()
|
|
|
|
nsString mLocalName; // font name used for the source, if local()
|
2011-06-15 23:31:37 -07:00
|
|
|
nsString mRealName; // original fullname from the font resource
|
2011-06-15 23:31:37 -07:00
|
|
|
PRUint32 mSrcIndex; // index in the rule's source list
|
|
|
|
PRUint32 mFormat; // format hint for the source used, if any
|
|
|
|
PRUint32 mMetaOrigLen; // length needed to decompress metadata
|
2008-09-30 20:04:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// initially contains a set of proxy font entry objects, replaced with
|
|
|
|
// platform/user fonts as downloaded
|
|
|
|
|
|
|
|
class gfxMixedFontFamily : public gfxFontFamily {
|
|
|
|
public:
|
2009-08-16 06:52:12 -07:00
|
|
|
friend class gfxUserFontSet;
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
gfxMixedFontFamily(const nsAString& aName)
|
2009-08-16 06:52:12 -07:00
|
|
|
: gfxFontFamily(aName) { }
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
virtual ~gfxMixedFontFamily() { }
|
|
|
|
|
2011-08-13 05:34:56 -07:00
|
|
|
void AddFontEntry(gfxFontEntry *aFontEntry) {
|
2008-09-30 20:04:10 -07:00
|
|
|
nsRefPtr<gfxFontEntry> fe = aFontEntry;
|
|
|
|
mAvailableFonts.AppendElement(fe);
|
2011-04-12 03:53:20 -07:00
|
|
|
aFontEntry->SetFamily(this);
|
2011-08-09 01:06:01 -07:00
|
|
|
ResetCharacterMap();
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2011-08-09 01:06:01 -07:00
|
|
|
void ReplaceFontEntry(gfxFontEntry *aOldFontEntry,
|
|
|
|
gfxFontEntry *aNewFontEntry) {
|
2008-09-30 20:04:10 -07:00
|
|
|
PRUint32 numFonts = mAvailableFonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < numFonts; i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (fe == aOldFontEntry) {
|
2011-04-12 03:53:20 -07:00
|
|
|
aOldFontEntry->SetFamily(nsnull);
|
2011-05-08 05:09:03 -07:00
|
|
|
// note that this may delete aOldFontEntry, if there's no
|
|
|
|
// other reference to it except from its family
|
|
|
|
mAvailableFonts[i] = aNewFontEntry;
|
2011-04-12 03:53:20 -07:00
|
|
|
aNewFontEntry->SetFamily(this);
|
2011-08-09 01:06:01 -07:00
|
|
|
break;
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 01:06:01 -07:00
|
|
|
ResetCharacterMap();
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2011-08-09 01:06:01 -07:00
|
|
|
void RemoveFontEntry(gfxFontEntry *aFontEntry) {
|
2008-09-30 20:04:10 -07:00
|
|
|
PRUint32 numFonts = mAvailableFonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < numFonts; i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (fe == aFontEntry) {
|
2011-04-12 03:53:20 -07:00
|
|
|
aFontEntry->SetFamily(nsnull);
|
2008-09-30 20:04:10 -07:00
|
|
|
mAvailableFonts.RemoveElementAt(i);
|
2011-08-09 01:06:01 -07:00
|
|
|
break;
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
}
|
2011-08-09 01:06:01 -07:00
|
|
|
ResetCharacterMap();
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
|
2012-02-02 03:24:22 -08:00
|
|
|
// clear family pointer for all entries and remove them from the family;
|
|
|
|
// we need to do this explicitly before inserting the entries into a new
|
|
|
|
// family, in case the old one is not actually deleted until later
|
|
|
|
void DetachFontEntries() {
|
|
|
|
PRUint32 i = mAvailableFonts.Length();
|
|
|
|
while (i--) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (fe) {
|
|
|
|
fe->SetFamily(nsnull);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAvailableFonts.Clear();
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
// temp method to determine if all proxies are loaded
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AllLoaded()
|
2008-09-30 20:04:10 -07:00
|
|
|
{
|
|
|
|
PRUint32 numFonts = mAvailableFonts.Length();
|
|
|
|
for (PRUint32 i = 0; i < numFonts; i++) {
|
|
|
|
gfxFontEntry *fe = mAvailableFonts[i];
|
|
|
|
if (fe->mIsProxy)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-09-30 20:04:10 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class gfxProxyFontEntry;
|
|
|
|
|
|
|
|
class THEBES_API gfxUserFontSet {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2010-04-01 11:05:40 -07:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(gfxUserFontSet)
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2008-12-11 23:31:51 -08:00
|
|
|
gfxUserFontSet();
|
2008-09-30 20:04:10 -07:00
|
|
|
virtual ~gfxUserFontSet();
|
|
|
|
|
|
|
|
enum {
|
2009-01-12 21:16:58 -08:00
|
|
|
// no flags ==> no hint set
|
|
|
|
// unknown ==> unknown format hint set
|
|
|
|
FLAG_FORMAT_UNKNOWN = 1,
|
|
|
|
FLAG_FORMAT_OPENTYPE = 1 << 1,
|
|
|
|
FLAG_FORMAT_TRUETYPE = 1 << 2,
|
|
|
|
FLAG_FORMAT_TRUETYPE_AAT = 1 << 3,
|
|
|
|
FLAG_FORMAT_EOT = 1 << 4,
|
|
|
|
FLAG_FORMAT_SVG = 1 << 5,
|
2009-09-17 04:03:12 -07:00
|
|
|
FLAG_FORMAT_WOFF = 1 << 6,
|
|
|
|
|
2009-01-12 21:16:58 -08:00
|
|
|
// mask of all unused bits, update when adding new formats
|
2009-09-17 04:03:12 -07:00
|
|
|
FLAG_FORMAT_NOT_USED = ~((1 << 7)-1)
|
2008-09-30 20:04:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
enum LoadStatus {
|
|
|
|
STATUS_LOADING = 0,
|
|
|
|
STATUS_LOADED,
|
|
|
|
STATUS_FORMAT_NOT_SUPPORTED,
|
|
|
|
STATUS_ERROR,
|
|
|
|
STATUS_END_OF_LIST
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// add in a font face
|
|
|
|
// weight, stretch - 0 == unknown, [1, 9] otherwise
|
2009-01-29 12:39:18 -08:00
|
|
|
// italic style = constants in gfxFontConstants.h, e.g. NS_FONT_STYLE_NORMAL
|
2008-09-30 20:04:10 -07:00
|
|
|
// TODO: support for unicode ranges not yet implemented
|
2011-04-12 03:53:20 -07:00
|
|
|
gfxFontEntry *AddFontFace(const nsAString& aFamilyName,
|
|
|
|
const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
|
|
|
PRUint32 aWeight,
|
|
|
|
PRUint32 aStretch,
|
|
|
|
PRUint32 aItalicStyle,
|
2012-04-25 23:24:26 -07:00
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
2011-04-12 03:53:20 -07:00
|
|
|
const nsString& aLanguageOverride,
|
|
|
|
gfxSparseBitSet *aUnicodeRanges = nsnull);
|
|
|
|
|
|
|
|
// add in a font face for which we have the gfxFontEntry already
|
|
|
|
void AddFontFace(const nsAString& aFamilyName, gfxFontEntry* aFontEntry);
|
2008-09-30 20:04:10 -07:00
|
|
|
|
2008-12-05 15:19:27 -08:00
|
|
|
// Whether there is a face with this family name
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasFamily(const nsAString& aFamilyName) const
|
2008-12-05 15:19:27 -08:00
|
|
|
{
|
|
|
|
return GetFamily(aFamilyName) != nsnull;
|
|
|
|
}
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
// lookup a font entry for a given style, returns null if not loaded
|
2011-03-23 20:01:50 -07:00
|
|
|
gfxFontEntry *FindFontEntry(const nsAString& aName,
|
|
|
|
const gfxFontStyle& aFontStyle,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool& aFoundFamily,
|
|
|
|
bool& aNeedsBold,
|
|
|
|
bool& aWaitForUserFont);
|
2008-12-11 23:31:51 -08:00
|
|
|
|
|
|
|
// initialize the process that loads external font data, which upon
|
|
|
|
// completion will call OnLoadComplete method
|
2011-04-27 22:04:45 -07:00
|
|
|
virtual nsresult StartLoad(gfxProxyFontEntry *aProxy,
|
2008-12-11 23:31:51 -08:00
|
|
|
const gfxFontFaceSrc *aFontFaceSrc) = 0;
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
// when download has been completed, pass back data here
|
|
|
|
// aDownloadStatus == NS_OK ==> download succeeded, error otherwise
|
|
|
|
// returns true if platform font creation sucessful (or local()
|
|
|
|
// reference was next in line)
|
2009-08-30 06:55:24 -07:00
|
|
|
// Ownership of aFontData is passed in here; the font set must
|
|
|
|
// ensure that it is eventually deleted with NS_Free().
|
2011-09-28 23:19:26 -07:00
|
|
|
bool OnLoadComplete(gfxProxyFontEntry *aProxy,
|
2008-10-29 11:09:50 -07:00
|
|
|
const PRUint8 *aFontData, PRUint32 aLength,
|
2008-09-30 20:04:10 -07:00
|
|
|
nsresult aDownloadStatus);
|
|
|
|
|
2011-04-12 03:53:20 -07:00
|
|
|
// Replace a proxy with a real fontEntry; this is implemented in
|
|
|
|
// nsUserFontSet in order to keep track of the entry corresponding
|
|
|
|
// to each @font-face rule.
|
|
|
|
virtual void ReplaceFontEntry(gfxProxyFontEntry *aProxy,
|
|
|
|
gfxFontEntry *aFontEntry) = 0;
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
// generation - each time a face is loaded, generation is
|
|
|
|
// incremented so that the change can be recognized
|
|
|
|
PRUint64 GetGeneration() { return mGeneration; }
|
|
|
|
|
2011-01-05 13:48:48 -08:00
|
|
|
// increment the generation on font load
|
|
|
|
void IncrementGeneration();
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
protected:
|
|
|
|
// for a given proxy font entry, attempt to load the next resource
|
|
|
|
// in the src list
|
|
|
|
LoadStatus LoadNext(gfxProxyFontEntry *aProxyEntry);
|
|
|
|
|
2012-03-15 20:31:01 -07:00
|
|
|
// helper method for creating a platform font
|
|
|
|
// returns font entry if platform font creation successful
|
|
|
|
// Ownership of aFontData is passed in here; the font set must
|
|
|
|
// ensure that it is eventually deleted with NS_Free().
|
|
|
|
gfxFontEntry* LoadFont(gfxProxyFontEntry *aProxy,
|
2012-03-15 20:31:01 -07:00
|
|
|
const PRUint8 *aFontData, PRUint32 &aLength);
|
2012-03-15 20:31:01 -07:00
|
|
|
|
|
|
|
// parse data for a data URL
|
|
|
|
virtual nsresult SyncLoadFontData(gfxProxyFontEntry *aFontToLoad,
|
|
|
|
const gfxFontFaceSrc *aFontFaceSrc,
|
|
|
|
PRUint8* &aBuffer,
|
|
|
|
PRUint32 &aBufferLength)
|
|
|
|
{
|
|
|
|
// implemented in nsUserFontSet
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2008-12-05 15:19:27 -08:00
|
|
|
gfxMixedFontFamily *GetFamily(const nsAString& aName) const;
|
|
|
|
|
2011-07-12 04:24:36 -07:00
|
|
|
// report a problem of some kind (implemented in nsUserFontSet)
|
|
|
|
virtual nsresult LogMessage(gfxProxyFontEntry *aProxy,
|
|
|
|
const char *aMessage,
|
|
|
|
PRUint32 aFlags = nsIScriptError::errorFlag,
|
|
|
|
nsresult aStatus = 0) = 0;
|
|
|
|
|
2012-05-29 15:44:50 -07:00
|
|
|
const PRUint8* SanitizeOpenTypeData(gfxProxyFontEntry *aProxy,
|
|
|
|
const PRUint8* aData,
|
|
|
|
PRUint32 aLength,
|
|
|
|
PRUint32& aSaneLength,
|
|
|
|
bool aIsCompressed);
|
|
|
|
|
|
|
|
#ifdef MOZ_OTS_REPORT_ERRORS
|
|
|
|
static bool OTSMessage(void *aUserData, const char *format, ...);
|
|
|
|
#endif
|
|
|
|
|
2008-09-30 20:04:10 -07:00
|
|
|
// font families defined by @font-face rules
|
|
|
|
nsRefPtrHashtable<nsStringHashKey, gfxMixedFontFamily> mFontFamilies;
|
|
|
|
|
|
|
|
PRUint64 mGeneration;
|
2011-07-12 04:24:36 -07:00
|
|
|
|
|
|
|
static PRLogModuleInfo *sUserFontsLog;
|
2012-03-06 13:44:23 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void CopyWOFFMetadata(const PRUint8* aFontData,
|
|
|
|
PRUint32 aLength,
|
|
|
|
nsTArray<PRUint8>* aMetadata,
|
|
|
|
PRUint32* aMetaOrigLen);
|
2008-09-30 20:04:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// acts a placeholder until the real font is downloaded
|
|
|
|
|
|
|
|
class gfxProxyFontEntry : public gfxFontEntry {
|
2009-08-16 06:52:12 -07:00
|
|
|
friend class gfxUserFontSet;
|
2008-09-30 20:04:10 -07:00
|
|
|
|
|
|
|
public:
|
2010-07-13 13:31:31 -07:00
|
|
|
gfxProxyFontEntry(const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
2008-12-05 15:19:27 -08:00
|
|
|
gfxMixedFontFamily *aFamily,
|
2010-07-13 13:31:31 -07:00
|
|
|
PRUint32 aWeight,
|
|
|
|
PRUint32 aStretch,
|
|
|
|
PRUint32 aItalicStyle,
|
2010-12-09 03:52:03 -08:00
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
2010-07-13 13:31:31 -07:00
|
|
|
PRUint32 aLanguageOverride,
|
2008-09-30 20:04:10 -07:00
|
|
|
gfxSparseBitSet *aUnicodeRanges);
|
|
|
|
|
|
|
|
virtual ~gfxProxyFontEntry();
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
virtual gfxFont *CreateFontInstance(const gfxFontStyle *aFontStyle, bool aNeedsBold);
|
2009-10-07 08:26:58 -07:00
|
|
|
|
2011-01-05 13:48:48 -08:00
|
|
|
// note that code depends on the ordering of these values!
|
|
|
|
enum LoadingState {
|
|
|
|
NOT_LOADING = 0, // not started to load any font resources yet
|
|
|
|
LOADING_STARTED, // loading has started; hide fallback font
|
|
|
|
LOADING_ALMOST_DONE, // timeout happened but we're nearly done,
|
|
|
|
// so keep hiding fallback font
|
2011-03-23 20:01:50 -07:00
|
|
|
LOADING_SLOWLY, // timeout happened and we're not nearly done,
|
2011-01-05 13:48:48 -08:00
|
|
|
// so use the fallback font
|
2011-03-23 20:01:50 -07:00
|
|
|
LOADING_FAILED // failed to load any source: use fallback
|
2011-01-05 13:48:48 -08:00
|
|
|
};
|
|
|
|
LoadingState mLoadingState;
|
2012-01-05 02:14:11 -08:00
|
|
|
bool mUnsupportedFormat;
|
2011-01-05 13:48:48 -08:00
|
|
|
|
|
|
|
nsTArray<gfxFontFaceSrc> mSrcList;
|
|
|
|
PRUint32 mSrcIndex; // index of loading src item
|
2012-04-26 10:50:41 -07:00
|
|
|
nsFontFaceLoader *mLoader; // current loader for this entry, if any
|
2008-09-30 20:04:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* GFX_USER_FONT_SET_H */
|