1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_FontFaceSet_h
8 #define mozilla_dom_FontFaceSet_h
10 #include "mozilla/dom/FontFace.h"
11 #include "mozilla/dom/FontFaceSetBinding.h"
12 #include "mozilla/dom/FontFaceSetImpl.h"
13 #include "mozilla/DOMEventTargetHelper.h"
14 #include "nsICSSLoaderObserver.h"
15 #include "nsIDOMEventListener.h"
17 class nsFontFaceLoader
;
19 class nsIGlobalObject
;
22 class PostTraversalTask
;
28 } // namespace mozilla
30 namespace mozilla::dom
{
32 class FontFaceSet final
: public DOMEventTargetHelper
{
33 friend class mozilla::PostTraversalTask
;
36 NS_DECL_ISUPPORTS_INHERITED
37 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FontFaceSet
, DOMEventTargetHelper
)
39 static already_AddRefed
<FontFaceSet
> CreateForDocument(
40 dom::Document
* aDocument
);
42 static already_AddRefed
<FontFaceSet
> CreateForWorker(
43 nsIGlobalObject
* aParent
, WorkerPrivate
* aWorkerPrivate
);
45 virtual JSObject
* WrapObject(JSContext
* aCx
,
46 JS::Handle
<JSObject
*> aGivenProto
) override
;
48 bool UpdateRules(const nsTArray
<nsFontFaceRuleContainer
>& aRules
);
51 * Notification method called by the nsPresContext to indicate that the
52 * refresh driver ticked and flushed style and layout.
57 void FlushUserFontSet();
59 void RefreshStandardFontLoadPrincipal();
61 void CopyNonRuleFacesTo(FontFaceSet
* aFontFaceSet
) const;
63 void CacheFontLoadability() { mImpl
->CacheFontLoadability(); }
65 FontFaceSetImpl
* GetImpl() const { return mImpl
; }
67 // -- Web IDL --------------------------------------------------------------
69 IMPL_EVENT_HANDLER(loading
)
70 IMPL_EVENT_HANDLER(loadingdone
)
71 IMPL_EVENT_HANDLER(loadingerror
)
72 already_AddRefed
<dom::Promise
> Load(JSContext
* aCx
, const nsACString
& aFont
,
73 const nsAString
& aText
, ErrorResult
& aRv
);
74 bool Check(const nsACString
& aFont
, const nsAString
& aText
, ErrorResult
& aRv
);
75 dom::Promise
* GetReady(ErrorResult
& aRv
);
76 dom::FontFaceSetLoadStatus
Status();
78 void Add(FontFace
& aFontFace
, ErrorResult
& aRv
);
80 bool Delete(FontFace
& aFontFace
);
81 bool Has(FontFace
& aFontFace
);
83 * This returns the number of Author origin fonts only.
84 * (see also SizeIncludingNonAuthorOrigins() below)
87 already_AddRefed
<dom::FontFaceSetIterator
> Entries();
88 already_AddRefed
<dom::FontFaceSetIterator
> Values();
90 void ForEach(JSContext
* aCx
, FontFaceSetForEachCallback
& aCallback
,
91 JS::Handle
<JS::Value
> aThisArg
, ErrorResult
& aRv
);
94 * Unlike Size(), this returns the size including non-Author origin fonts.
96 uint32_t SizeIncludingNonAuthorOrigins();
100 void DispatchLoadingFinishedEvent(
101 const nsAString
& aType
, nsTArray
<OwningNonNull
<FontFace
>>&& aFontFaces
);
103 void DispatchLoadingEventAndReplaceReadyPromise();
104 void DispatchCheckLoadingFinishedAfterDelay();
106 // Whether mReady is pending, or would be when created.
107 bool ReadyPromiseIsPending() const;
109 void InsertRuleFontFace(FontFace
* aFontFace
, StyleOrigin aOrigin
);
112 friend mozilla::dom::FontFaceSetIterator
; // needs GetFontFaceAt()
114 explicit FontFaceSet(nsIGlobalObject
* aParent
);
118 * Returns whether the given FontFace is currently "in" the FontFaceSet.
120 bool HasAvailableFontFace(FontFace
* aFontFace
);
123 * Removes any listeners and observers.
128 * Returns the font at aIndex if it's an Author origin font, or nullptr
131 FontFace
* GetFontFaceAt(uint32_t aIndex
);
133 // Note: if you add new cycle collected objects to FontFaceRecord,
134 // make sure to update FontFaceSet's cycle collection macros
136 struct FontFaceRecord
{
137 RefPtr
<FontFace
> mFontFace
;
138 Maybe
<StyleOrigin
> mOrigin
; // only relevant for mRuleFaces entries
140 // When true, indicates that when finished loading, the FontFace should be
141 // included in the subsequent loadingdone/loadingerror event fired at the
143 bool mLoadEventShouldFire
;
147 bool HasRuleFontFace(FontFace
* aFontFace
);
150 // The underlying implementation for FontFaceSet.
151 RefPtr
<FontFaceSetImpl
> mImpl
;
153 // A Promise that is fulfilled once all of the FontFace objects
154 // in mRuleFaces and mNonRuleFaces that started or were loading at the
155 // time the Promise was created have finished loading. It is rejected if
156 // any of those fonts failed to load. mReady is replaced with
157 // a new Promise object whenever mReady is settled and another
158 // FontFace in mRuleFaces or mNonRuleFaces starts to load.
159 // Note that mReady is created lazily when GetReady() is called.
160 RefPtr
<dom::Promise
> mReady
;
161 // Whether the ready promise must be resolved when it's created.
162 bool mResolveLazilyCreatedReadyPromise
= false;
164 // The @font-face rule backed FontFace objects in the FontFaceSet.
165 nsTArray
<FontFaceRecord
> mRuleFaces
;
167 // The non rule backed FontFace objects that have been added to this
169 nsTArray
<FontFaceRecord
> mNonRuleFaces
;
172 } // namespace mozilla::dom
174 #endif // !defined(mozilla_dom_FontFaceSet_h)