Bug 1730256 [wpt PR 30555] - Move getWindowSegments to visualViewport.segments, a...
[gecko.git] / layout / style / FontFace.h
blobe3f790ecd23ef9d9bbd79d8481a2e55feb541df5
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_FontFace_h
8 #define mozilla_dom_FontFace_h
10 #include "mozilla/dom/FontFaceBinding.h"
11 #include "mozilla/FontPropertyTypes.h"
12 #include "mozilla/Maybe.h"
13 #include "mozilla/ServoStyleConsts.h"
14 #include "gfxUserFontSet.h"
15 #include "nsCSSPropertyID.h"
16 #include "nsCSSValue.h"
17 #include "nsWrapperCache.h"
19 class gfxFontFaceBufferSource;
20 struct RawServoFontFaceRule;
22 namespace mozilla {
23 struct CSSFontFaceDescriptors;
24 class PostTraversalTask;
25 namespace dom {
26 class CSSFontFaceRule;
27 class FontFaceBufferSource;
28 struct FontFaceDescriptors;
29 class FontFaceSet;
30 class Promise;
31 class UTF8StringOrArrayBufferOrArrayBufferView;
32 } // namespace dom
33 } // namespace mozilla
35 namespace mozilla {
36 namespace dom {
38 class FontFace final : public nsISupports, public nsWrapperCache {
39 friend class mozilla::PostTraversalTask;
40 friend class FontFaceBufferSource;
41 friend class Entry;
43 public:
44 class Entry final : public gfxUserFontEntry {
45 friend class FontFace;
47 public:
48 Entry(gfxUserFontSet* aFontSet,
49 const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList, WeightRange aWeight,
50 StretchRange aStretch, SlantStyleRange aStyle,
51 const nsTArray<gfxFontFeature>& aFeatureSettings,
52 const nsTArray<gfxFontVariation>& aVariationSettings,
53 uint32_t aLanguageOverride, gfxCharacterMap* aUnicodeRanges,
54 StyleFontDisplay aFontDisplay, RangeFlags aRangeFlags,
55 float aAscentOverride, float aDescentOverride, float aLineGapOverride,
56 float aSizeAdjust)
57 : gfxUserFontEntry(aFontSet, aFontFaceSrcList, aWeight, aStretch,
58 aStyle, aFeatureSettings, aVariationSettings,
59 aLanguageOverride, aUnicodeRanges, aFontDisplay,
60 aRangeFlags, aAscentOverride, aDescentOverride,
61 aLineGapOverride, aSizeAdjust) {}
63 virtual void SetLoadState(UserFontLoadState aLoadState) override;
64 virtual void GetUserFontSets(nsTArray<gfxUserFontSet*>& aResult) override;
65 const AutoTArray<FontFace*, 1>& GetFontFaces() { return mFontFaces; }
67 protected:
68 // The FontFace objects that use this user font entry. We need to store
69 // an array of these, not just a single pointer, since the user font
70 // cache can return the same entry for different FontFaces that have
71 // the same descriptor values and come from the same origin.
72 AutoTArray<FontFace*, 1> mFontFaces;
75 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
76 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FontFace)
78 nsISupports* GetParentObject() const { return mParent; }
79 virtual JSObject* WrapObject(JSContext* aCx,
80 JS::Handle<JSObject*> aGivenProto) override;
82 static already_AddRefed<FontFace> CreateForRule(nsISupports* aGlobal,
83 FontFaceSet* aFontFaceSet,
84 RawServoFontFaceRule* aRule);
86 RawServoFontFaceRule* GetRule() { return mRule; }
88 bool HasLocalSrc() const;
89 Maybe<StyleComputedFontWeightRange> GetFontWeight() const;
90 Maybe<StyleComputedFontStretchRange> GetFontStretch() const;
91 Maybe<StyleComputedFontStyleDescriptor> GetFontStyle() const;
92 Maybe<StyleFontDisplay> GetFontDisplay() const;
93 void GetFontFeatureSettings(nsTArray<gfxFontFeature>&) const;
94 void GetFontVariationSettings(nsTArray<gfxFontVariation>&) const;
95 void GetSources(nsTArray<StyleFontFaceSourceListComponent>&) const;
96 Maybe<StyleFontLanguageOverride> GetFontLanguageOverride() const;
97 Maybe<StylePercentage> GetAscentOverride() const;
98 Maybe<StylePercentage> GetDescentOverride() const;
99 Maybe<StylePercentage> GetLineGapOverride() const;
100 Maybe<StylePercentage> GetSizeAdjust() const;
102 gfxUserFontEntry* CreateUserFontEntry();
103 gfxUserFontEntry* GetUserFontEntry() const { return mUserFontEntry; }
104 void SetUserFontEntry(gfxUserFontEntry* aEntry);
107 * Returns whether this object is in the specified FontFaceSet.
109 bool IsInFontFaceSet(FontFaceSet* aFontFaceSet) const;
111 void AddFontFaceSet(FontFaceSet* aFontFaceSet);
112 void RemoveFontFaceSet(FontFaceSet* aFontFaceSet);
114 FontFaceSet* GetPrimaryFontFaceSet() const { return mFontFaceSet; }
117 * Gets the family name of the FontFace as a raw string (such as 'Times', as
118 * opposed to GetFamily, which returns a CSS-escaped string, such as
119 * '"Times"'). Returns null if a valid family name was not available.
121 nsAtom* GetFamilyName() const;
124 * Returns whether this object is CSS-connected, i.e. reflecting an
125 * @font-face rule.
127 bool HasRule() const { return mRule; }
130 * Breaks the connection between this FontFace and its @font-face rule.
132 void DisconnectFromRule();
135 * Returns whether there is an ArrayBuffer or ArrayBufferView of font
136 * data.
138 bool HasFontData() const;
141 * Creates a gfxFontFaceBufferSource to represent the font data
142 * in this object.
144 already_AddRefed<gfxFontFaceBufferSource> CreateBufferSource();
147 * Gets a pointer to and the length of the font data stored in the
148 * ArrayBuffer or ArrayBufferView.
150 bool GetData(uint8_t*& aBuffer, uint32_t& aLength);
153 * Returns the value of the unicode-range descriptor as a gfxCharacterMap.
155 gfxCharacterMap* GetUnicodeRangeAsCharacterMap();
157 // Web IDL
158 static already_AddRefed<FontFace> Constructor(
159 const GlobalObject& aGlobal, const nsACString& aFamily,
160 const UTF8StringOrArrayBufferOrArrayBufferView& aSource,
161 const FontFaceDescriptors& aDescriptors, ErrorResult& aRV);
163 void GetFamily(nsACString& aResult);
164 void SetFamily(const nsACString& aValue, ErrorResult& aRv);
165 void GetStyle(nsACString& aResult);
166 void SetStyle(const nsACString& aValue, ErrorResult& aRv);
167 void GetWeight(nsACString& aResult);
168 void SetWeight(const nsACString& aValue, ErrorResult& aRv);
169 void GetStretch(nsACString& aResult);
170 void SetStretch(const nsACString& aValue, ErrorResult& aRv);
171 void GetUnicodeRange(nsACString& aResult);
172 void SetUnicodeRange(const nsACString& aValue, ErrorResult& aRv);
173 void GetVariant(nsACString& aResult);
174 void SetVariant(const nsACString& aValue, ErrorResult& aRv);
175 void GetFeatureSettings(nsACString& aResult);
176 void SetFeatureSettings(const nsACString& aValue, ErrorResult& aRv);
177 void GetVariationSettings(nsACString& aResult);
178 void SetVariationSettings(const nsACString& aValue, ErrorResult& aRv);
179 void GetDisplay(nsACString& aResult);
180 void SetDisplay(const nsACString& aValue, ErrorResult& aRv);
181 void GetAscentOverride(nsACString& aResult);
182 void SetAscentOverride(const nsACString& aValue, ErrorResult& aRv);
183 void GetDescentOverride(nsACString& aResult);
184 void SetDescentOverride(const nsACString& aValue, ErrorResult& aRv);
185 void GetLineGapOverride(nsACString& aResult);
186 void SetLineGapOverride(const nsACString& aValue, ErrorResult& aRv);
187 void GetSizeAdjust(nsACString& aResult);
188 void SetSizeAdjust(const nsACString& aValue, ErrorResult& aRv);
190 FontFaceLoadStatus Status();
191 Promise* Load(ErrorResult& aRv);
192 Promise* GetLoaded(ErrorResult& aRv);
194 private:
195 FontFace(nsISupports* aParent, FontFaceSet* aFontFaceSet);
196 ~FontFace();
198 void InitializeSource(const UTF8StringOrArrayBufferOrArrayBufferView&);
200 // Helper function for Load.
201 void DoLoad();
203 // Helper function for the descriptor setter methods.
204 // Returns true if the descriptor was modified, false if descriptor is
205 // unchanged (which may not be an error: check aRv for actual failure).
206 bool SetDescriptor(nsCSSFontDesc aFontDesc, const nsACString& aValue,
207 ErrorResult& aRv);
210 * Sets all of the descriptor values in mDescriptors using values passed
211 * to the JS constructor.
212 * Returns true on success, false if parsing any descriptor failed.
214 bool SetDescriptors(const nsACString& aFamily,
215 const FontFaceDescriptors& aDescriptors);
218 * Called when a descriptor has been modified, so font-face sets can
219 * be told to refresh.
221 void DescriptorUpdated();
224 * Sets the current loading status.
226 void SetStatus(FontFaceLoadStatus aStatus);
228 void GetDesc(nsCSSFontDesc aDescID, nsACString& aResult) const;
230 already_AddRefed<URLExtraData> GetURLExtraData() const;
232 RawServoFontFaceRule* GetData() const {
233 return HasRule() ? mRule : mDescriptors;
237 * Returns and takes ownership of the buffer storing the font data.
239 void TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength);
241 // Acts like mLoaded->MaybeReject(aResult), except it doesn't create mLoaded
242 // if it doesn't already exist.
243 void Reject(nsresult aResult);
245 // Creates mLoaded if it doesn't already exist. It may immediately resolve or
246 // reject mLoaded based on mStatus and mLoadedRejection.
247 void EnsurePromise();
249 void DoResolve();
250 void DoReject(nsresult aResult);
252 nsCOMPtr<nsISupports> mParent;
254 // A Promise that is fulfilled once the font represented by this FontFace is
255 // loaded, and is rejected if the load fails. This promise is created lazily
256 // when JS asks for it.
257 RefPtr<Promise> mLoaded;
259 // Saves the rejection code for mLoaded if mLoaded hasn't been created yet.
260 nsresult mLoadedRejection;
262 // The @font-face rule this FontFace object is reflecting, if it is a
263 // rule backed FontFace.
264 RefPtr<RawServoFontFaceRule> mRule;
266 // The FontFace object's user font entry. This is initially null, but is set
267 // during FontFaceSet::UpdateRules and when a FontFace is explicitly loaded.
268 RefPtr<Entry> mUserFontEntry;
270 // The current load status of the font represented by this FontFace.
271 // Note that we can't just reflect the value of the gfxUserFontEntry's
272 // status, since the spec sometimes requires us to go through the event
273 // loop before updating the status, rather than doing it immediately.
274 FontFaceLoadStatus mStatus;
276 // Represents where a FontFace's data is coming from.
277 enum SourceType {
278 eSourceType_FontFaceRule = 1,
279 eSourceType_URLs,
280 eSourceType_Buffer
283 // Where the font data for this FontFace is coming from.
284 SourceType mSourceType;
286 // If the FontFace was constructed with an ArrayBuffer(View), this is a
287 // copy of the data from it.
288 uint8_t* mSourceBuffer;
289 uint32_t mSourceBufferLength;
291 // The values corresponding to the font face descriptors, if we are not
292 // a rule backed FontFace object. For rule backed objects, we use
293 // the descriptors stored in mRule.
294 // FIXME This should hold a unique ptr to just the descriptors inside,
295 // so that we don't need to create a rule for it and don't need to
296 // assign a fake line number and column number. See bug 1450904.
297 RefPtr<RawServoFontFaceRule> mDescriptors;
299 // The value of the unicode-range descriptor as a gfxCharacterMap. Valid
300 // only when mUnicodeRangeDirty is false.
301 RefPtr<gfxCharacterMap> mUnicodeRange;
303 // The primary FontFaceSet this FontFace is associated with,
304 // regardless of whether it is currently "in" the set.
305 RefPtr<FontFaceSet> mFontFaceSet;
307 // Other FontFaceSets (apart from mFontFaceSet) that this FontFace
308 // appears in.
309 nsTArray<RefPtr<FontFaceSet>> mOtherFontFaceSets;
311 // Whether mUnicodeRange needs to be rebuilt before being returned from
312 // GetUnicodeRangeAsCharacterMap.
313 bool mUnicodeRangeDirty;
315 // Whether this FontFace appears in mFontFaceSet.
316 bool mInFontFaceSet;
319 } // namespace dom
320 } // namespace mozilla
322 #endif // !defined(mozilla_dom_FontFace_h)