no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / layout / style / FontFaceSet.h
blob04de363df3452ab2d3d957138f1fea93cf2e836d
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;
18 class nsIPrincipal;
19 class nsIGlobalObject;
21 namespace mozilla {
22 class PostTraversalTask;
23 class SharedFontList;
24 namespace dom {
25 class Promise;
26 class WorkerPrivate;
27 } // namespace dom
28 } // namespace mozilla
30 namespace mozilla::dom {
32 class FontFaceSet final : public DOMEventTargetHelper {
33 friend class mozilla::PostTraversalTask;
35 public:
36 NS_DECL_ISUPPORTS_INHERITED
37 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(FontFaceSet, DOMEventTargetHelper)
39 static bool IsEnabled();
41 static bool IsEnabled(JSContext* aCx, JSObject* aObj) { return IsEnabled(); }
43 static already_AddRefed<FontFaceSet> CreateForDocument(
44 dom::Document* aDocument);
46 static already_AddRefed<FontFaceSet> CreateForWorker(
47 nsIGlobalObject* aParent, WorkerPrivate* aWorkerPrivate);
49 virtual JSObject* WrapObject(JSContext* aCx,
50 JS::Handle<JSObject*> aGivenProto) override;
52 bool UpdateRules(const nsTArray<nsFontFaceRuleContainer>& aRules);
54 /**
55 * Notification method called by the nsPresContext to indicate that the
56 * refresh driver ticked and flushed style and layout.
57 * were just flushed.
59 void DidRefresh();
61 void FlushUserFontSet();
63 void RefreshStandardFontLoadPrincipal();
65 void CopyNonRuleFacesTo(FontFaceSet* aFontFaceSet) const;
67 void CacheFontLoadability() { mImpl->CacheFontLoadability(); }
69 FontFaceSetImpl* GetImpl() const { return mImpl; }
71 // -- Web IDL --------------------------------------------------------------
73 IMPL_EVENT_HANDLER(loading)
74 IMPL_EVENT_HANDLER(loadingdone)
75 IMPL_EVENT_HANDLER(loadingerror)
76 already_AddRefed<dom::Promise> Load(JSContext* aCx, const nsACString& aFont,
77 const nsAString& aText, ErrorResult& aRv);
78 bool Check(const nsACString& aFont, const nsAString& aText, ErrorResult& aRv);
79 dom::Promise* GetReady(ErrorResult& aRv);
80 dom::FontFaceSetLoadStatus Status();
82 void Add(FontFace& aFontFace, ErrorResult& aRv);
83 void Clear();
84 bool Delete(FontFace& aFontFace);
85 bool Has(FontFace& aFontFace);
86 /**
87 * This returns the number of Author origin fonts only.
88 * (see also SizeIncludingNonAuthorOrigins() below)
90 uint32_t Size();
91 already_AddRefed<dom::FontFaceSetIterator> Entries();
92 already_AddRefed<dom::FontFaceSetIterator> Values();
93 MOZ_CAN_RUN_SCRIPT
94 void ForEach(JSContext* aCx, FontFaceSetForEachCallback& aCallback,
95 JS::Handle<JS::Value> aThisArg, ErrorResult& aRv);
97 /**
98 * Unlike Size(), this returns the size including non-Author origin fonts.
100 uint32_t SizeIncludingNonAuthorOrigins();
102 void MaybeResolve();
104 void DispatchLoadingFinishedEvent(
105 const nsAString& aType, nsTArray<OwningNonNull<FontFace>>&& aFontFaces);
107 void DispatchLoadingEventAndReplaceReadyPromise();
108 void DispatchCheckLoadingFinishedAfterDelay();
110 // Whether mReady is pending, or would be when created.
111 bool ReadyPromiseIsPending() const;
113 void InsertRuleFontFace(FontFace* aFontFace, StyleOrigin aOrigin);
115 private:
116 friend mozilla::dom::FontFaceSetIterator; // needs GetFontFaceAt()
118 explicit FontFaceSet(nsIGlobalObject* aParent);
119 ~FontFaceSet();
122 * Returns whether the given FontFace is currently "in" the FontFaceSet.
124 bool HasAvailableFontFace(FontFace* aFontFace);
127 * Removes any listeners and observers.
129 void Destroy();
132 * Returns the font at aIndex if it's an Author origin font, or nullptr
133 * otherwise.
135 FontFace* GetFontFaceAt(uint32_t aIndex);
137 // Note: if you add new cycle collected objects to FontFaceRecord,
138 // make sure to update FontFaceSet's cycle collection macros
139 // accordingly.
140 struct FontFaceRecord {
141 RefPtr<FontFace> mFontFace;
142 Maybe<StyleOrigin> mOrigin; // only relevant for mRuleFaces entries
144 // When true, indicates that when finished loading, the FontFace should be
145 // included in the subsequent loadingdone/loadingerror event fired at the
146 // FontFaceSet.
147 bool mLoadEventShouldFire;
150 #ifdef DEBUG
151 bool HasRuleFontFace(FontFace* aFontFace);
152 #endif
154 // The underlying implementation for FontFaceSet.
155 RefPtr<FontFaceSetImpl> mImpl;
157 // A Promise that is fulfilled once all of the FontFace objects
158 // in mRuleFaces and mNonRuleFaces that started or were loading at the
159 // time the Promise was created have finished loading. It is rejected if
160 // any of those fonts failed to load. mReady is replaced with
161 // a new Promise object whenever mReady is settled and another
162 // FontFace in mRuleFaces or mNonRuleFaces starts to load.
163 // Note that mReady is created lazily when GetReady() is called.
164 RefPtr<dom::Promise> mReady;
165 // Whether the ready promise must be resolved when it's created.
166 bool mResolveLazilyCreatedReadyPromise = false;
168 // The @font-face rule backed FontFace objects in the FontFaceSet.
169 nsTArray<FontFaceRecord> mRuleFaces;
171 // The non rule backed FontFace objects that have been added to this
172 // FontFaceSet.
173 nsTArray<FontFaceRecord> mNonRuleFaces;
176 } // namespace mozilla::dom
178 #endif // !defined(mozilla_dom_FontFaceSet_h)