Bug 1885565 - Part 1: Add mozac_ic_avatar_circle_24 to ui-icons r=android-reviewers...
[gecko.git] / layout / style / FontFaceImpl.h
blob70c06609e95588bdcd7e17474b4ca7b8e55c666a
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_FontFaceImpl_h
8 #define mozilla_dom_FontFaceImpl_h
10 #include "mozilla/dom/FontFaceBinding.h"
11 #include "mozilla/FontPropertyTypes.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/Mutex.h"
14 #include "mozilla/ServoStyleConsts.h"
15 #include "gfxUserFontSet.h"
16 #include "nsCSSPropertyID.h"
17 #include "nsCSSValue.h"
18 #include "nsTHashSet.h"
20 class gfxFontFaceBufferSource;
22 namespace mozilla {
23 struct CSSFontFaceDescriptors;
24 class PostTraversalTask;
25 struct StyleLockedFontFaceRule;
26 namespace dom {
27 class CSSFontFaceRule;
28 class FontFace;
29 class FontFaceBufferSource;
30 struct FontFaceDescriptors;
31 class FontFaceSetImpl;
32 class UTF8StringOrArrayBufferOrArrayBufferView;
33 } // namespace dom
34 } // namespace mozilla
36 namespace mozilla::dom {
38 class FontFaceImpl final {
39 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FontFaceImpl)
41 friend class mozilla::PostTraversalTask;
42 friend class FontFaceBufferSource;
43 friend class Entry;
45 public:
46 class Entry final : public gfxUserFontEntry {
47 friend class FontFaceImpl;
49 public:
50 Entry(gfxUserFontSet* aFontSet, nsTArray<gfxFontFaceSrc>&& aFontFaceSrcList,
51 gfxUserFontAttributes&& aAttr)
52 : gfxUserFontEntry(std::move(aFontFaceSrcList), std::move(aAttr)),
53 mMutex("FontFaceImpl::Entry::mMutex"),
54 mFontSet(aFontSet) {}
56 void SetLoadState(UserFontLoadState aLoadState) override;
57 void GetUserFontSets(nsTArray<RefPtr<gfxUserFontSet>>& aResult) override;
58 already_AddRefed<gfxUserFontSet> GetUserFontSet() const override;
60 void CheckUserFontSet() {
61 MutexAutoLock lock(mMutex);
62 CheckUserFontSetLocked();
65 #ifdef DEBUG
66 bool HasUserFontSet(gfxUserFontSet* aFontSet) const {
67 MutexAutoLock lock(mMutex);
68 return mFontSet == aFontSet;
70 #endif
72 void AddFontFace(FontFaceImpl* aOwner);
73 void RemoveFontFace(FontFaceImpl* aOwner);
74 void FindFontFaceOwners(nsTHashSet<FontFace*>& aOwners);
76 protected:
77 void CheckUserFontSetLocked() MOZ_REQUIRES(mMutex);
79 mutable Mutex mMutex;
81 // Font set which owns this entry;
82 gfxUserFontSet* MOZ_NON_OWNING_REF mFontSet MOZ_GUARDED_BY(mMutex);
84 // The FontFace objects that use this user font entry. We need to store
85 // an array of these, not just a single pointer, since the user font
86 // cache can return the same entry for different FontFaces that have
87 // the same descriptor values and come from the same origin.
88 AutoTArray<FontFaceImpl*, 1> mFontFaces MOZ_GUARDED_BY(mMutex);
91 #ifdef DEBUG
92 void AssertIsOnOwningThread() const;
93 #else
94 void AssertIsOnOwningThread() const {}
95 #endif
97 FontFace* GetOwner() const {
98 AssertIsOnOwningThread();
99 return mOwner;
102 static already_AddRefed<FontFaceImpl> CreateForRule(
103 FontFace* aOwner, FontFaceSetImpl* aFontFaceSet,
104 StyleLockedFontFaceRule* aRule);
106 StyleLockedFontFaceRule* GetRule() { return mRule; }
108 bool HasLocalSrc() const;
110 bool GetAttributes(gfxUserFontAttributes& aAttr);
111 gfxUserFontEntry* CreateUserFontEntry();
112 gfxUserFontEntry* GetUserFontEntry() const { return mUserFontEntry; }
113 void SetUserFontEntry(gfxUserFontEntry* aEntry);
116 * Returns whether this object is in the specified FontFaceSet.
118 bool IsInFontFaceSet(FontFaceSetImpl* aFontFaceSet) const;
120 void AddFontFaceSet(FontFaceSetImpl* aFontFaceSet);
121 void RemoveFontFaceSet(FontFaceSetImpl* aFontFaceSet);
123 FontFaceSetImpl* GetPrimaryFontFaceSet() const { return mFontFaceSet; }
126 * Gets the family name of the FontFace as a raw string (such as 'Times', as
127 * opposed to GetFamily, which returns a CSS-escaped string, such as
128 * '"Times"'). Returns null if a valid family name was not available.
130 nsAtom* GetFamilyName() const;
133 * Returns whether this object is CSS-connected, i.e. reflecting an
134 * @font-face rule.
136 bool HasRule() const { return mRule; }
139 * Breaks the connection between this FontFace and its @font-face rule.
141 void DisconnectFromRule();
144 * Returns whether there is an ArrayBuffer or ArrayBufferView of font
145 * data.
147 bool HasFontData() const;
150 * Takes the gfxFontFaceBufferSource to represent the font data
151 * in this object.
153 already_AddRefed<gfxFontFaceBufferSource> TakeBufferSource();
156 * Gets a pointer to and the length of the font data stored in the
157 * ArrayBuffer or ArrayBufferView.
159 bool GetData(uint8_t*& aBuffer, uint32_t& aLength);
162 * Returns the value of the unicode-range descriptor as a gfxCharacterMap.
164 gfxCharacterMap* GetUnicodeRangeAsCharacterMap();
166 // Web IDL
167 void GetFamily(nsACString& aResult);
168 void SetFamily(const nsACString& aValue, ErrorResult& aRv);
169 void GetStyle(nsACString& aResult);
170 void SetStyle(const nsACString& aValue, ErrorResult& aRv);
171 void GetWeight(nsACString& aResult);
172 void SetWeight(const nsACString& aValue, ErrorResult& aRv);
173 void GetStretch(nsACString& aResult);
174 void SetStretch(const nsACString& aValue, ErrorResult& aRv);
175 void GetUnicodeRange(nsACString& aResult);
176 void SetUnicodeRange(const nsACString& aValue, ErrorResult& aRv);
177 void GetVariant(nsACString& aResult);
178 void SetVariant(const nsACString& aValue, ErrorResult& aRv);
179 void GetFeatureSettings(nsACString& aResult);
180 void SetFeatureSettings(const nsACString& aValue, ErrorResult& aRv);
181 void GetVariationSettings(nsACString& aResult);
182 void SetVariationSettings(const nsACString& aValue, ErrorResult& aRv);
183 void GetDisplay(nsACString& aResult);
184 void SetDisplay(const nsACString& aValue, ErrorResult& aRv);
185 void GetAscentOverride(nsACString& aResult);
186 void SetAscentOverride(const nsACString& aValue, ErrorResult& aRv);
187 void GetDescentOverride(nsACString& aResult);
188 void SetDescentOverride(const nsACString& aValue, ErrorResult& aRv);
189 void GetLineGapOverride(nsACString& aResult);
190 void SetLineGapOverride(const nsACString& aValue, ErrorResult& aRv);
191 void GetSizeAdjust(nsACString& aResult);
192 void SetSizeAdjust(const nsACString& aValue, ErrorResult& aRv);
194 FontFaceLoadStatus Status();
195 void Load(ErrorResult& aRv);
197 void Destroy();
199 FontFaceImpl(FontFace* aOwner, FontFaceSetImpl* aFontFaceSet);
201 void InitializeSourceURL(const nsACString& aURL);
202 void InitializeSourceBuffer(uint8_t* aBuffer, uint32_t aLength);
205 * Sets all of the descriptor values in mDescriptors using values passed
206 * to the JS constructor.
207 * Returns true on success, false if parsing any descriptor failed.
209 bool SetDescriptors(const nsACString& aFamily,
210 const FontFaceDescriptors& aDescriptors);
212 private:
213 ~FontFaceImpl();
215 // Helper function for Load.
216 void DoLoad();
217 void UpdateOwnerPromise();
219 // Helper function for the descriptor setter methods.
220 // Returns true if the descriptor was modified, false if descriptor is
221 // unchanged (which may not be an error: check aRv for actual failure).
222 bool SetDescriptor(nsCSSFontDesc aFontDesc, const nsACString& aValue,
223 ErrorResult& aRv);
226 * Called when a descriptor has been modified, so font-face sets can
227 * be told to refresh.
229 void DescriptorUpdated();
232 * Sets the current loading status.
234 void SetStatus(FontFaceLoadStatus aStatus);
236 void GetDesc(nsCSSFontDesc aDescID, nsACString& aResult) const;
238 StyleLockedFontFaceRule* GetData() const {
239 AssertIsOnOwningThread();
240 return HasRule() ? mRule : mDescriptors;
244 * Returns and takes ownership of the buffer storing the font data.
246 void TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength);
248 FontFace* MOZ_NON_OWNING_REF mOwner;
250 // The @font-face rule this FontFace object is reflecting, if it is a
251 // rule backed FontFace.
252 RefPtr<StyleLockedFontFaceRule> mRule;
254 // The FontFace object's user font entry. This is initially null, but is set
255 // during FontFaceSet::UpdateRules and when a FontFace is explicitly loaded.
256 RefPtr<Entry> mUserFontEntry;
258 // The current load status of the font represented by this FontFace.
259 // Note that we can't just reflect the value of the gfxUserFontEntry's
260 // status, since the spec sometimes requires us to go through the event
261 // loop before updating the status, rather than doing it immediately.
262 FontFaceLoadStatus mStatus;
264 // Represents where a FontFace's data is coming from.
265 enum SourceType {
266 eSourceType_FontFaceRule = 1,
267 eSourceType_URLs,
268 eSourceType_Buffer
271 // Where the font data for this FontFace is coming from.
272 SourceType mSourceType;
274 // If the FontFace was constructed with an ArrayBuffer(View), this is a
275 // copy of the data from it.
276 RefPtr<FontFaceBufferSource> mBufferSource;
278 // The values corresponding to the font face descriptors, if we are not
279 // a rule backed FontFace object. For rule backed objects, we use
280 // the descriptors stored in mRule.
281 // FIXME This should hold a unique ptr to just the descriptors inside,
282 // so that we don't need to create a rule for it and don't need to
283 // assign a fake line number and column number. See bug 1450904.
284 RefPtr<StyleLockedFontFaceRule> mDescriptors;
286 // The value of the unicode-range descriptor as a gfxCharacterMap. Valid
287 // only when mUnicodeRangeDirty is false.
288 RefPtr<gfxCharacterMap> mUnicodeRange;
290 // The primary FontFaceSet this FontFace is associated with,
291 // regardless of whether it is currently "in" the set.
292 RefPtr<FontFaceSetImpl> mFontFaceSet;
294 // Other FontFaceSets (apart from mFontFaceSet) that this FontFace
295 // appears in.
296 nsTArray<RefPtr<FontFaceSetImpl>> mOtherFontFaceSets;
298 // Whether mUnicodeRange needs to be rebuilt before being returned from
299 // GetUnicodeRangeAsCharacterMap.
300 bool mUnicodeRangeDirty;
302 // Whether this FontFace appears in mFontFaceSet.
303 bool mInFontFaceSet;
306 } // namespace mozilla::dom
308 #endif // !defined(mozilla_dom_FontFaceImpl_h)