2014-10-02 06:32:05 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_FontFaceSet_h
|
|
|
|
#define mozilla_dom_FontFaceSet_h
|
|
|
|
|
2014-10-02 06:32:06 +04:00
|
|
|
#include "mozilla/dom/FontFace.h"
|
2014-10-02 06:32:05 +04:00
|
|
|
#include "mozilla/dom/FontFaceSetBinding.h"
|
2014-10-02 06:32:09 +04:00
|
|
|
#include "mozilla/DOMEventTargetHelper.h"
|
2014-10-02 06:32:05 +04:00
|
|
|
#include "gfxUserFontSet.h"
|
|
|
|
#include "nsCSSRules.h"
|
2014-10-02 06:32:09 +04:00
|
|
|
#include "nsICSSLoaderObserver.h"
|
2014-10-02 06:32:05 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
2014-10-02 06:32:05 +04:00
|
|
|
struct gfxFontFaceSrc;
|
|
|
|
class gfxUserFontEntry;
|
|
|
|
class nsFontFaceLoader;
|
|
|
|
class nsIPrincipal;
|
2014-10-02 06:32:05 +04:00
|
|
|
class nsPIDOMWindow;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2015-06-27 04:41:10 +03:00
|
|
|
namespace css {
|
|
|
|
class FontFamilyListRefCnt;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace css
|
2014-10-02 06:32:05 +04:00
|
|
|
namespace dom {
|
|
|
|
class FontFace;
|
|
|
|
class Promise;
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2014-10-02 06:32:05 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class FontFaceSet final : public DOMEventTargetHelper
|
2015-03-27 21:52:19 +03:00
|
|
|
, public nsIDOMEventListener
|
|
|
|
, public nsICSSLoaderObserver
|
2014-10-02 06:32:05 +04:00
|
|
|
{
|
2014-10-02 06:32:05 +04:00
|
|
|
friend class UserFontSet;
|
|
|
|
|
2014-10-02 06:32:05 +04:00
|
|
|
public:
|
2014-10-02 06:32:05 +04:00
|
|
|
/**
|
|
|
|
* A gfxUserFontSet that integrates with the layout and style systems to
|
|
|
|
* manage @font-face rules and handle network requests for font loading.
|
|
|
|
*
|
|
|
|
* We would combine this class and FontFaceSet into the one class if it were
|
|
|
|
* possible; it's not because FontFaceSet is cycle collected and
|
|
|
|
* gfxUserFontSet isn't (and can't be, as gfx classes don't use the cycle
|
|
|
|
* collector). So UserFontSet exists just to override the needed virtual
|
|
|
|
* methods from gfxUserFontSet and to forward them on FontFaceSet.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class UserFontSet final : public gfxUserFontSet
|
2014-10-02 06:32:05 +04:00
|
|
|
{
|
|
|
|
friend class FontFaceSet;
|
|
|
|
|
|
|
|
public:
|
2014-09-24 17:16:53 +04:00
|
|
|
explicit UserFontSet(FontFaceSet* aFontFaceSet)
|
2014-10-02 06:32:05 +04:00
|
|
|
: mFontFaceSet(aFontFaceSet)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FontFaceSet* GetFontFaceSet() { return mFontFaceSet; }
|
|
|
|
|
|
|
|
virtual nsresult CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc,
|
|
|
|
nsIPrincipal** aPrincipal,
|
2015-03-21 19:28:04 +03:00
|
|
|
bool* aBypassCache) override;
|
2014-10-02 06:32:05 +04:00
|
|
|
virtual nsresult StartLoad(gfxUserFontEntry* aUserFontEntry,
|
2015-03-21 19:28:04 +03:00
|
|
|
const gfxFontFaceSrc* aFontFaceSrc) override;
|
2014-10-02 06:32:05 +04:00
|
|
|
|
|
|
|
protected:
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual bool GetPrivateBrowsing() override;
|
2014-10-02 06:32:05 +04:00
|
|
|
virtual nsresult SyncLoadFontData(gfxUserFontEntry* aFontToLoad,
|
|
|
|
const gfxFontFaceSrc* aFontFaceSrc,
|
|
|
|
uint8_t*& aBuffer,
|
2015-03-21 19:28:04 +03:00
|
|
|
uint32_t& aBufferLength) override;
|
2014-10-02 06:32:05 +04:00
|
|
|
virtual nsresult LogMessage(gfxUserFontEntry* aUserFontEntry,
|
|
|
|
const char* aMessage,
|
|
|
|
uint32_t aFlags = nsIScriptError::errorFlag,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult aStatus = NS_OK) override;
|
|
|
|
virtual void DoRebuildUserFontSet() override;
|
2014-10-02 06:32:07 +04:00
|
|
|
virtual already_AddRefed<gfxUserFontEntry> CreateUserFontEntry(
|
2014-10-02 06:32:06 +04:00
|
|
|
const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
|
|
|
|
uint32_t aWeight,
|
|
|
|
int32_t aStretch,
|
2015-10-18 21:22:22 +03:00
|
|
|
uint32_t aItalicStyle,
|
2014-10-02 06:32:06 +04:00
|
|
|
const nsTArray<gfxFontFeature>& aFeatureSettings,
|
|
|
|
uint32_t aLanguageOverride,
|
2015-03-21 19:28:04 +03:00
|
|
|
gfxSparseBitSet* aUnicodeRanges) override;
|
2014-10-02 06:32:05 +04:00
|
|
|
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FontFaceSet> mFontFaceSet;
|
2014-10-02 06:32:05 +04:00
|
|
|
};
|
|
|
|
|
2014-10-02 06:32:05 +04:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FontFaceSet, DOMEventTargetHelper)
|
2014-10-02 06:32:09 +04:00
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2014-10-02 06:32:05 +04:00
|
|
|
|
2015-06-27 04:39:54 +03:00
|
|
|
FontFaceSet(nsPIDOMWindow* aWindow, nsIDocument* aDocument);
|
2014-10-02 06:32:05 +04:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2014-10-02 06:32:05 +04:00
|
|
|
|
2014-10-02 06:32:05 +04:00
|
|
|
UserFontSet* GetUserFontSet() { return mUserFontSet; }
|
|
|
|
|
|
|
|
// Called by nsFontFaceLoader when the loader has completed normally.
|
|
|
|
// It's removed from the mLoaders set.
|
|
|
|
void RemoveLoader(nsFontFaceLoader* aLoader);
|
|
|
|
|
|
|
|
bool UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules);
|
|
|
|
|
2015-06-27 04:39:54 +03:00
|
|
|
nsPresContext* GetPresContext();
|
2014-10-02 06:32:05 +04:00
|
|
|
|
|
|
|
// search for @font-face rule that matches a platform font entry
|
|
|
|
nsCSSFontFaceRule* FindRuleForEntry(gfxFontEntry* aFontEntry);
|
|
|
|
|
|
|
|
void IncrementGeneration(bool aIsRebuild = false);
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
|
|
|
* Finds an existing entry in the user font cache or creates a new user
|
|
|
|
* font entry for the given FontFace object.
|
|
|
|
*/
|
|
|
|
already_AddRefed<gfxUserFontEntry>
|
|
|
|
FindOrCreateUserFontEntryFromFontFace(FontFace* aFontFace);
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
|
|
|
* Notification method called by a FontFace to indicate that its loading
|
|
|
|
* status has changed.
|
|
|
|
*/
|
|
|
|
void OnFontFaceStatusChanged(FontFace* aFontFace);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification method called by the nsPresContext to indicate that the
|
|
|
|
* refresh driver ticked and flushed style and layout.
|
|
|
|
* were just flushed.
|
|
|
|
*/
|
|
|
|
void DidRefresh();
|
|
|
|
|
2014-11-11 06:53:55 +03:00
|
|
|
/**
|
|
|
|
* Returns whether the "layout.css.font-loading-api.enabled" pref is true.
|
|
|
|
*/
|
|
|
|
static bool PrefEnabled();
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
// nsICSSLoaderObserver
|
|
|
|
NS_IMETHOD StyleSheetLoaded(mozilla::CSSStyleSheet* aSheet,
|
|
|
|
bool aWasAlternate,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsresult aStatus) override;
|
2014-10-02 06:32:09 +04:00
|
|
|
|
2015-03-31 06:05:33 +03:00
|
|
|
FontFace* GetFontFaceAt(uint32_t aIndex);
|
|
|
|
|
2015-06-27 04:39:54 +03:00
|
|
|
void FlushUserFontSet();
|
|
|
|
|
2015-10-16 09:10:14 +03:00
|
|
|
static nsPresContext* GetPresContextFor(gfxUserFontSet* aUserFontSet)
|
|
|
|
{
|
|
|
|
FontFaceSet* set = static_cast<UserFontSet*>(aUserFontSet)->mFontFaceSet;
|
|
|
|
return set ? set->GetPresContext() : nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-02 06:32:05 +04:00
|
|
|
// -- Web IDL --------------------------------------------------------------
|
|
|
|
|
2014-10-02 06:32:05 +04:00
|
|
|
IMPL_EVENT_HANDLER(loading)
|
|
|
|
IMPL_EVENT_HANDLER(loadingdone)
|
|
|
|
IMPL_EVENT_HANDLER(loadingerror)
|
2015-06-27 04:41:10 +03:00
|
|
|
already_AddRefed<mozilla::dom::Promise> Load(JSContext* aCx,
|
|
|
|
const nsAString& aFont,
|
2014-10-02 06:32:05 +04:00
|
|
|
const nsAString& aText,
|
|
|
|
mozilla::ErrorResult& aRv);
|
|
|
|
bool Check(const nsAString& aFont,
|
|
|
|
const nsAString& aText,
|
|
|
|
mozilla::ErrorResult& aRv);
|
|
|
|
mozilla::dom::Promise* GetReady(mozilla::ErrorResult& aRv);
|
|
|
|
mozilla::dom::FontFaceSetLoadStatus Status();
|
|
|
|
|
|
|
|
FontFaceSet* Add(FontFace& aFontFace, mozilla::ErrorResult& aRv);
|
|
|
|
void Clear();
|
2015-05-13 05:24:34 +03:00
|
|
|
bool Delete(FontFace& aFontFace);
|
2014-10-02 06:32:05 +04:00
|
|
|
bool Has(FontFace& aFontFace);
|
2015-03-31 06:05:33 +03:00
|
|
|
uint32_t Size();
|
2015-07-16 10:35:17 +03:00
|
|
|
already_AddRefed<mozilla::dom::FontFaceSetIterator> Entries();
|
|
|
|
already_AddRefed<mozilla::dom::FontFaceSetIterator> Values();
|
2015-03-31 06:05:33 +03:00
|
|
|
void ForEach(JSContext* aCx, FontFaceSetForEachCallback& aCallback,
|
|
|
|
JS::Handle<JS::Value> aThisArg,
|
|
|
|
mozilla::ErrorResult& aRv);
|
2014-10-02 06:32:05 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
~FontFaceSet();
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
|
|
|
* Returns whether the given FontFace is currently "in" the FontFaceSet.
|
|
|
|
*/
|
|
|
|
bool HasAvailableFontFace(FontFace* aFontFace);
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
|
|
|
* Removes any listeners and observers.
|
|
|
|
*/
|
|
|
|
void Disconnect();
|
|
|
|
|
2015-05-13 08:06:52 +03:00
|
|
|
void RemoveDOMContentLoadedListener();
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
|
|
|
* Returns whether there might be any pending font loads, which should cause
|
|
|
|
* the mReady Promise not to be resolved yet.
|
|
|
|
*/
|
|
|
|
bool MightHavePendingFontLoads();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see whether it is time to replace mReady and dispatch a
|
|
|
|
* "loading" event.
|
|
|
|
*/
|
|
|
|
void CheckLoadingStarted();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks to see whether it is time to resolve mReady and dispatch any
|
|
|
|
* "loadingdone" and "loadingerror" events.
|
|
|
|
*/
|
|
|
|
void CheckLoadingFinished();
|
|
|
|
|
2015-03-27 13:05:22 +03:00
|
|
|
/**
|
|
|
|
* Callback for invoking CheckLoadingFinished after going through the
|
|
|
|
* event loop. See OnFontFaceStatusChanged.
|
|
|
|
*/
|
|
|
|
void CheckLoadingFinishedAfterDelay();
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
2015-08-11 05:19:52 +03:00
|
|
|
* Dispatches a FontFaceSetLoadEvent to this object.
|
2014-10-02 06:32:09 +04:00
|
|
|
*/
|
|
|
|
void DispatchLoadingFinishedEvent(
|
|
|
|
const nsAString& aType,
|
|
|
|
const nsTArray<FontFace*>& aFontFaces);
|
|
|
|
|
2014-10-02 06:32:08 +04:00
|
|
|
// Note: if you add new cycle collected objects to FontFaceRecord,
|
|
|
|
// make sure to update FontFaceSet's cycle collection macros
|
|
|
|
// accordingly.
|
2014-10-02 06:32:07 +04:00
|
|
|
struct FontFaceRecord {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<FontFace> mFontFace;
|
2015-03-27 13:05:22 +03:00
|
|
|
uint8_t mSheetType; // only relevant for mRuleFaces entries
|
|
|
|
|
|
|
|
// When true, indicates that when finished loading, the FontFace should be
|
|
|
|
// included in the subsequent loadingdone/loadingerror event fired at the
|
|
|
|
// FontFaceSet.
|
|
|
|
bool mLoadEventShouldFire;
|
2014-10-02 06:32:05 +04:00
|
|
|
};
|
|
|
|
|
2014-10-02 06:32:08 +04:00
|
|
|
already_AddRefed<gfxUserFontEntry> FindOrCreateUserFontEntryFromFontFace(
|
2014-10-02 06:32:05 +04:00
|
|
|
const nsAString& aFamilyName,
|
2014-10-02 06:32:08 +04:00
|
|
|
FontFace* aFontFace,
|
2014-10-02 06:32:05 +04:00
|
|
|
uint8_t aSheetType);
|
|
|
|
|
|
|
|
// search for @font-face rule that matches a userfont font entry
|
|
|
|
nsCSSFontFaceRule* FindRuleForUserFontEntry(gfxUserFontEntry* aUserFontEntry);
|
|
|
|
|
|
|
|
nsresult StartLoad(gfxUserFontEntry* aUserFontEntry,
|
|
|
|
const gfxFontFaceSrc* aFontFaceSrc);
|
|
|
|
nsresult CheckFontLoad(const gfxFontFaceSrc* aFontFaceSrc,
|
|
|
|
nsIPrincipal** aPrincipal,
|
|
|
|
bool* aBypassCache);
|
|
|
|
bool GetPrivateBrowsing();
|
|
|
|
nsresult SyncLoadFontData(gfxUserFontEntry* aFontToLoad,
|
|
|
|
const gfxFontFaceSrc* aFontFaceSrc,
|
|
|
|
uint8_t*& aBuffer,
|
|
|
|
uint32_t& aBufferLength);
|
|
|
|
nsresult LogMessage(gfxUserFontEntry* aUserFontEntry,
|
|
|
|
const char* aMessage,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsresult aStatus);
|
2015-06-27 04:39:54 +03:00
|
|
|
void RebuildUserFontSet();
|
2014-10-02 06:32:05 +04:00
|
|
|
|
2014-10-02 06:32:10 +04:00
|
|
|
void InsertRuleFontFace(FontFace* aFontFace, uint8_t aSheetType,
|
|
|
|
nsTArray<FontFaceRecord>& aOldRecords,
|
|
|
|
bool& aFontSetModified);
|
|
|
|
void InsertNonRuleFontFace(FontFace* aFontFace, bool& aFontSetModified);
|
2014-10-02 06:32:08 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-10-02 06:32:10 +04:00
|
|
|
bool HasRuleFontFace(FontFace* aFontFace);
|
2014-10-02 06:32:08 +04:00
|
|
|
#endif
|
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
/**
|
|
|
|
* Returns whether we have any loading FontFace objects in the FontFaceSet.
|
|
|
|
*/
|
|
|
|
bool HasLoadingFontFaces();
|
|
|
|
|
|
|
|
// Helper function for HasLoadingFontFaces.
|
|
|
|
void UpdateHasLoadingFontFaces();
|
|
|
|
|
2015-06-27 04:41:10 +03:00
|
|
|
void ParseFontShorthandForMatching(
|
|
|
|
const nsAString& aFont,
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::css::FontFamilyListRefCnt>& aFamilyList,
|
2015-06-27 04:41:10 +03:00
|
|
|
uint32_t& aWeight,
|
|
|
|
int32_t& aStretch,
|
2015-10-18 21:22:22 +03:00
|
|
|
uint32_t& aItalicStyle,
|
2015-06-27 04:41:10 +03:00
|
|
|
ErrorResult& aRv);
|
|
|
|
void FindMatchingFontFaces(const nsAString& aFont,
|
|
|
|
const nsAString& aText,
|
|
|
|
nsTArray<FontFace*>& aFontFaces,
|
|
|
|
mozilla::ErrorResult& aRv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<UserFontSet> mUserFontSet;
|
2014-10-02 06:32:05 +04:00
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
// The document this is a FontFaceSet for.
|
|
|
|
nsCOMPtr<nsIDocument> mDocument;
|
|
|
|
|
|
|
|
// A Promise that is fulfilled once all of the FontFace objects
|
2014-10-02 06:32:10 +04:00
|
|
|
// in mRuleFaces and mNonRuleFaces that started or were loading at the
|
2014-10-02 06:32:09 +04:00
|
|
|
// time the Promise was created have finished loading. It is rejected if
|
|
|
|
// any of those fonts failed to load. mReady is replaced with
|
|
|
|
// a new Promise object whenever mReady is settled and another
|
2014-10-02 06:32:10 +04:00
|
|
|
// FontFace in mRuleFaces or mNonRuleFaces starts to load.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozilla::dom::Promise> mReady;
|
2014-10-02 06:32:05 +04:00
|
|
|
|
|
|
|
// Set of all loaders pointing to us. These are not strong pointers,
|
|
|
|
// but that's OK because nsFontFaceLoader always calls RemoveLoader on
|
|
|
|
// us before it dies (unless we die first).
|
|
|
|
nsTHashtable< nsPtrHashKey<nsFontFaceLoader> > mLoaders;
|
|
|
|
|
2014-10-02 06:32:10 +04:00
|
|
|
// The @font-face rule backed FontFace objects in the FontFaceSet.
|
|
|
|
nsTArray<FontFaceRecord> mRuleFaces;
|
2014-10-02 06:32:08 +04:00
|
|
|
|
2014-10-02 06:32:10 +04:00
|
|
|
// The non rule backed FontFace objects that have been added to this
|
2015-03-27 13:05:22 +03:00
|
|
|
// FontFaceSet.
|
|
|
|
nsTArray<FontFaceRecord> mNonRuleFaces;
|
2014-10-02 06:32:08 +04:00
|
|
|
|
2014-10-02 06:32:09 +04:00
|
|
|
// The overall status of the loading or loaded fonts in the FontFaceSet.
|
|
|
|
mozilla::dom::FontFaceSetLoadStatus mStatus;
|
|
|
|
|
2014-10-02 06:32:10 +04:00
|
|
|
// Whether mNonRuleFaces has changed since last time UpdateRules ran.
|
|
|
|
bool mNonRuleFacesDirty;
|
2014-10-02 06:32:09 +04:00
|
|
|
|
2014-10-02 06:32:10 +04:00
|
|
|
// Whether any FontFace objects in mRuleFaces or mNonRuleFaces are
|
2014-10-02 06:32:09 +04:00
|
|
|
// loading. Only valid when mHasLoadingFontFacesIsDirty is false. Don't use
|
|
|
|
// this variable directly; call the HasLoadingFontFaces method instead.
|
|
|
|
bool mHasLoadingFontFaces;
|
|
|
|
|
|
|
|
// This variable is only valid when mLoadingDirty is false.
|
|
|
|
bool mHasLoadingFontFacesIsDirty;
|
2015-03-27 13:05:22 +03:00
|
|
|
|
|
|
|
// Whether CheckLoadingFinished calls should be ignored. See comment in
|
|
|
|
// OnFontFaceStatusChanged.
|
|
|
|
bool mDelayedLoadCheck;
|
2014-10-02 06:32:05 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // !defined(mozilla_dom_FontFaceSet_h)
|