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
;
23 struct CSSFontFaceDescriptors
;
24 class PostTraversalTask
;
26 class CSSFontFaceRule
;
27 class FontFaceBufferSource
;
28 struct FontFaceDescriptors
;
31 class UTF8StringOrArrayBufferOrArrayBufferView
;
33 } // namespace mozilla
38 class FontFace final
: public nsISupports
, public nsWrapperCache
{
39 friend class mozilla::PostTraversalTask
;
40 friend class FontFaceBufferSource
;
44 class Entry final
: public gfxUserFontEntry
{
45 friend class FontFace
;
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 : gfxUserFontEntry(aFontSet
, aFontFaceSrcList
, aWeight
, aStretch
,
56 aStyle
, aFeatureSettings
, aVariationSettings
,
57 aLanguageOverride
, aUnicodeRanges
, aFontDisplay
,
60 virtual void SetLoadState(UserFontLoadState aLoadState
) override
;
61 virtual void GetUserFontSets(nsTArray
<gfxUserFontSet
*>& aResult
) override
;
62 const AutoTArray
<FontFace
*, 1>& GetFontFaces() { return mFontFaces
; }
65 // The FontFace objects that use this user font entry. We need to store
66 // an array of these, not just a single pointer, since the user font
67 // cache can return the same entry for different FontFaces that have
68 // the same descriptor values and come from the same origin.
69 AutoTArray
<FontFace
*, 1> mFontFaces
;
72 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
73 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FontFace
)
75 nsISupports
* GetParentObject() const { return mParent
; }
76 virtual JSObject
* WrapObject(JSContext
* aCx
,
77 JS::Handle
<JSObject
*> aGivenProto
) override
;
79 static already_AddRefed
<FontFace
> CreateForRule(nsISupports
* aGlobal
,
80 FontFaceSet
* aFontFaceSet
,
81 RawServoFontFaceRule
* aRule
);
83 RawServoFontFaceRule
* GetRule() { return mRule
; }
85 bool HasLocalSrc() const;
86 Maybe
<StyleComputedFontWeightRange
> GetFontWeight() const;
87 Maybe
<StyleComputedFontStretchRange
> GetFontStretch() const;
88 Maybe
<StyleComputedFontStyleDescriptor
> GetFontStyle() const;
89 Maybe
<StyleFontDisplay
> GetFontDisplay() const;
90 void GetFontFeatureSettings(nsTArray
<gfxFontFeature
>&) const;
91 void GetFontVariationSettings(nsTArray
<gfxFontVariation
>&) const;
92 void GetSources(nsTArray
<StyleFontFaceSourceListComponent
>&) const;
93 Maybe
<StyleFontLanguageOverride
> GetFontLanguageOverride() const;
95 gfxUserFontEntry
* CreateUserFontEntry();
96 gfxUserFontEntry
* GetUserFontEntry() const { return mUserFontEntry
; }
97 void SetUserFontEntry(gfxUserFontEntry
* aEntry
);
100 * Returns whether this object is in the specified FontFaceSet.
102 bool IsInFontFaceSet(FontFaceSet
* aFontFaceSet
) const;
104 void AddFontFaceSet(FontFaceSet
* aFontFaceSet
);
105 void RemoveFontFaceSet(FontFaceSet
* aFontFaceSet
);
107 FontFaceSet
* GetPrimaryFontFaceSet() const { return mFontFaceSet
; }
110 * Gets the family name of the FontFace as a raw string (such as 'Times', as
111 * opposed to GetFamily, which returns a CSS-escaped string, such as
112 * '"Times"'). Returns null if a valid family name was not available.
114 nsAtom
* GetFamilyName() const;
117 * Returns whether this object is CSS-connected, i.e. reflecting an
120 bool HasRule() const { return mRule
; }
123 * Breaks the connection between this FontFace and its @font-face rule.
125 void DisconnectFromRule();
128 * Returns whether there is an ArrayBuffer or ArrayBufferView of font
131 bool HasFontData() const;
134 * Creates a gfxFontFaceBufferSource to represent the font data
137 already_AddRefed
<gfxFontFaceBufferSource
> CreateBufferSource();
140 * Gets a pointer to and the length of the font data stored in the
141 * ArrayBuffer or ArrayBufferView.
143 bool GetData(uint8_t*& aBuffer
, uint32_t& aLength
);
146 * Returns the value of the unicode-range descriptor as a gfxCharacterMap.
148 gfxCharacterMap
* GetUnicodeRangeAsCharacterMap();
151 static already_AddRefed
<FontFace
> Constructor(
152 const GlobalObject
& aGlobal
, const nsACString
& aFamily
,
153 const UTF8StringOrArrayBufferOrArrayBufferView
& aSource
,
154 const FontFaceDescriptors
& aDescriptors
, ErrorResult
& aRV
);
156 void GetFamily(nsACString
& aResult
);
157 void SetFamily(const nsACString
& aValue
, ErrorResult
& aRv
);
158 void GetStyle(nsACString
& aResult
);
159 void SetStyle(const nsACString
& aValue
, ErrorResult
& aRv
);
160 void GetWeight(nsACString
& aResult
);
161 void SetWeight(const nsACString
& aValue
, ErrorResult
& aRv
);
162 void GetStretch(nsACString
& aResult
);
163 void SetStretch(const nsACString
& aValue
, ErrorResult
& aRv
);
164 void GetUnicodeRange(nsACString
& aResult
);
165 void SetUnicodeRange(const nsACString
& aValue
, ErrorResult
& aRv
);
166 void GetVariant(nsACString
& aResult
);
167 void SetVariant(const nsACString
& aValue
, ErrorResult
& aRv
);
168 void GetFeatureSettings(nsACString
& aResult
);
169 void SetFeatureSettings(const nsACString
& aValue
, ErrorResult
& aRv
);
170 void GetVariationSettings(nsACString
& aResult
);
171 void SetVariationSettings(const nsACString
& aValue
, ErrorResult
& aRv
);
172 void GetDisplay(nsACString
& aResult
);
173 void SetDisplay(const nsACString
& aValue
, ErrorResult
& aRv
);
175 FontFaceLoadStatus
Status();
176 Promise
* Load(ErrorResult
& aRv
);
177 Promise
* GetLoaded(ErrorResult
& aRv
);
180 FontFace(nsISupports
* aParent
, FontFaceSet
* aFontFaceSet
);
183 void InitializeSource(const UTF8StringOrArrayBufferOrArrayBufferView
&);
185 // Helper function for Load.
188 // Helper function for the descriptor setter methods.
189 // Returns true if the descriptor was modified, false if descriptor is
190 // unchanged (which may not be an error: check aRv for actual failure).
191 bool SetDescriptor(nsCSSFontDesc aFontDesc
, const nsACString
& aValue
,
195 * Sets all of the descriptor values in mDescriptors using values passed
196 * to the JS constructor.
197 * Returns true on success, false if parsing any descriptor failed.
199 bool SetDescriptors(const nsACString
& aFamily
,
200 const FontFaceDescriptors
& aDescriptors
);
203 * Called when a descriptor has been modified, so font-face sets can
204 * be told to refresh.
206 void DescriptorUpdated();
209 * Sets the current loading status.
211 void SetStatus(FontFaceLoadStatus aStatus
);
213 void GetDesc(nsCSSFontDesc aDescID
, nsACString
& aResult
) const;
215 already_AddRefed
<URLExtraData
> GetURLExtraData() const;
217 RawServoFontFaceRule
* GetData() const {
218 return HasRule() ? mRule
: mDescriptors
;
222 * Returns and takes ownership of the buffer storing the font data.
224 void TakeBuffer(uint8_t*& aBuffer
, uint32_t& aLength
);
226 // Acts like mLoaded->MaybeReject(aResult), except it doesn't create mLoaded
227 // if it doesn't already exist.
228 void Reject(nsresult aResult
);
230 // Creates mLoaded if it doesn't already exist. It may immediately resolve or
231 // reject mLoaded based on mStatus and mLoadedRejection.
232 void EnsurePromise();
235 void DoReject(nsresult aResult
);
237 nsCOMPtr
<nsISupports
> mParent
;
239 // A Promise that is fulfilled once the font represented by this FontFace is
240 // loaded, and is rejected if the load fails. This promise is created lazily
241 // when JS asks for it.
242 RefPtr
<Promise
> mLoaded
;
244 // Saves the rejection code for mLoaded if mLoaded hasn't been created yet.
245 nsresult mLoadedRejection
;
247 // The @font-face rule this FontFace object is reflecting, if it is a
248 // rule backed FontFace.
249 RefPtr
<RawServoFontFaceRule
> mRule
;
251 // The FontFace object's user font entry. This is initially null, but is set
252 // during FontFaceSet::UpdateRules and when a FontFace is explicitly loaded.
253 RefPtr
<Entry
> mUserFontEntry
;
255 // The current load status of the font represented by this FontFace.
256 // Note that we can't just reflect the value of the gfxUserFontEntry's
257 // status, since the spec sometimes requires us to go through the event
258 // loop before updating the status, rather than doing it immediately.
259 FontFaceLoadStatus mStatus
;
261 // Represents where a FontFace's data is coming from.
263 eSourceType_FontFaceRule
= 1,
268 // Where the font data for this FontFace is coming from.
269 SourceType mSourceType
;
271 // If the FontFace was constructed with an ArrayBuffer(View), this is a
272 // copy of the data from it.
273 uint8_t* mSourceBuffer
;
274 uint32_t mSourceBufferLength
;
276 // The values corresponding to the font face descriptors, if we are not
277 // a rule backed FontFace object. For rule backed objects, we use
278 // the descriptors stored in mRule.
279 // FIXME This should hold a unique ptr to just the descriptors inside,
280 // so that we don't need to create a rule for it and don't need to
281 // assign a fake line number and column number. See bug 1450904.
282 RefPtr
<RawServoFontFaceRule
> mDescriptors
;
284 // The value of the unicode-range descriptor as a gfxCharacterMap. Valid
285 // only when mUnicodeRangeDirty is false.
286 RefPtr
<gfxCharacterMap
> mUnicodeRange
;
288 // The primary FontFaceSet this FontFace is associated with,
289 // regardless of whether it is currently "in" the set.
290 RefPtr
<FontFaceSet
> mFontFaceSet
;
292 // Other FontFaceSets (apart from mFontFaceSet) that this FontFace
294 nsTArray
<RefPtr
<FontFaceSet
>> mOtherFontFaceSets
;
296 // Whether mUnicodeRange needs to be rebuilt before being returned from
297 // GetUnicodeRangeAsCharacterMap.
298 bool mUnicodeRangeDirty
;
300 // Whether this FontFace appears in mFontFaceSet.
305 } // namespace mozilla
307 #endif // !defined(mozilla_dom_FontFace_h)