1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_dom_FontFace_h
7 #define mozilla_dom_FontFace_h
9 #include "mozilla/dom/FontFaceBinding.h"
10 #include "gfxUserFontSet.h"
11 #include "nsCSSProperty.h"
12 #include "nsCSSValue.h"
13 #include "nsWrapperCache.h"
15 class gfxFontFaceBufferSource
;
16 class nsCSSFontFaceRule
;
20 struct CSSFontFaceDescriptors
;
22 class FontFaceBufferSource
;
23 struct FontFaceDescriptors
;
25 class FontFaceInitializer
;
26 class FontFaceStatusSetter
;
28 class StringOrArrayBufferOrArrayBufferView
;
35 class FontFace MOZ_FINAL
: public nsISupports
,
38 friend class mozilla::dom::FontFaceBufferSource
;
39 friend class mozilla::dom::FontFaceInitializer
;
40 friend class mozilla::dom::FontFaceStatusSetter
;
44 class Entry MOZ_FINAL
: public gfxUserFontEntry
{
45 friend class FontFace
;
48 Entry(gfxUserFontSet
* aFontSet
,
49 const nsTArray
<gfxFontFaceSrc
>& aFontFaceSrcList
,
52 uint32_t aItalicStyle
,
53 const nsTArray
<gfxFontFeature
>& aFeatureSettings
,
54 uint32_t aLanguageOverride
,
55 gfxSparseBitSet
* aUnicodeRanges
)
56 : gfxUserFontEntry(aFontSet
, aFontFaceSrcList
, aWeight
, aStretch
,
57 aItalicStyle
, aFeatureSettings
, aLanguageOverride
,
60 virtual void SetLoadState(UserFontLoadState aLoadState
) MOZ_OVERRIDE
;
63 // The FontFace objects that use this user font entry. We need to store
64 // an array of these, not just a single pointer, since the user font
65 // cache can return the same entry for different FontFaces that have
66 // the same descriptor values and come from the same origin.
67 nsAutoTArray
<FontFace
*,1> mFontFaces
;
70 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
71 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FontFace
)
73 nsISupports
* GetParentObject() const { return mParent
; }
74 virtual JSObject
* WrapObject(JSContext
* aCx
) MOZ_OVERRIDE
;
76 static already_AddRefed
<FontFace
>
77 CreateForRule(nsISupports
* aGlobal
, nsPresContext
* aPresContext
,
78 nsCSSFontFaceRule
* aRule
);
80 nsCSSFontFaceRule
* GetRule() { return mRule
; }
82 void GetDesc(nsCSSFontDesc aDescID
, nsCSSValue
& aResult
) const;
84 gfxUserFontEntry
* GetUserFontEntry() const { return mUserFontEntry
; }
85 void SetUserFontEntry(gfxUserFontEntry
* aEntry
);
88 * Returns whether this object is in a FontFaceSet.
90 bool IsInFontFaceSet() { return mInFontFaceSet
; }
93 * Sets whether this object is in a FontFaceSet. This is called by the
94 * FontFaceSet when Add, Remove, etc. are called.
96 void SetIsInFontFaceSet(bool aInFontFaceSet
) {
97 MOZ_ASSERT(!(!aInFontFaceSet
&& HasRule()),
98 "use DisconnectFromRule instead");
99 mInFontFaceSet
= aInFontFaceSet
;
103 * Returns whether this FontFace is initialized. A rule backed
104 * FontFace is considered initialized at construction time. For
105 * FontFace objects created using the FontFace JS constructor, it
106 * is once all the descriptors have been parsed.
108 bool IsInitialized() const { return mInitialized
; }
110 FontFaceSet
* GetFontFaceSet() const { return mFontFaceSet
; }
113 * Gets the family name of the FontFace as a raw string (such as 'Times', as
114 * opposed to GetFamily, which returns a CSS-escaped string, such as
115 * '"Times"'). Returns whether a valid family name was available.
117 bool GetFamilyName(nsString
& aResult
);
120 * Returns whether this object is CSS-connected, i.e. reflecting an
123 bool HasRule() const { return mRule
; }
126 * Breaks the connection between this FontFace and its @font-face rule.
128 void DisconnectFromRule();
131 * Returns whether there is an ArrayBuffer or ArrayBufferView of font
134 bool HasFontData() const;
137 * Creates a gfxFontFaceBufferSource to represent the font data
140 already_AddRefed
<gfxFontFaceBufferSource
> CreateBufferSource();
143 * Gets a pointer to and the length of the font data stored in the
144 * ArrayBuffer or ArrayBufferView.
146 bool GetData(uint8_t*& aBuffer
, uint32_t& aLength
);
149 static already_AddRefed
<FontFace
>
150 Constructor(const GlobalObject
& aGlobal
,
151 const nsAString
& aFamily
,
152 const mozilla::dom::StringOrArrayBufferOrArrayBufferView
& aSource
,
153 const mozilla::dom::FontFaceDescriptors
& aDescriptors
,
156 void GetFamily(nsString
& aResult
);
157 void SetFamily(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
158 void GetStyle(nsString
& aResult
);
159 void SetStyle(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
160 void GetWeight(nsString
& aResult
);
161 void SetWeight(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
162 void GetStretch(nsString
& aResult
);
163 void SetStretch(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
164 void GetUnicodeRange(nsString
& aResult
);
165 void SetUnicodeRange(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
166 void GetVariant(nsString
& aResult
);
167 void SetVariant(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
168 void GetFeatureSettings(nsString
& aResult
);
169 void SetFeatureSettings(const nsAString
& aValue
, mozilla::ErrorResult
& aRv
);
171 mozilla::dom::FontFaceLoadStatus
Status();
172 mozilla::dom::Promise
* Load(mozilla::ErrorResult
& aRv
);
173 mozilla::dom::Promise
* GetLoaded(mozilla::ErrorResult
& aRv
);
176 FontFace(nsISupports
* aParent
, nsPresContext
* aPresContext
);
180 * Initializes the source and descriptors on this object based on values that
181 * were passed in to the JS constructor. If the source was specified as
182 * an ArrayBuffer or ArrayBufferView, parsing of the font data in there
185 void Initialize(FontFaceInitializer
* aInitializer
);
187 // Helper function for Load.
191 * Parses a @font-face descriptor value, storing the result in aResult.
192 * Returns whether the parsing was successful.
194 bool ParseDescriptor(nsCSSFontDesc aDescID
, const nsAString
& aString
,
195 nsCSSValue
& aResult
);
197 // Helper function for the descriptor setter methods.
198 void SetDescriptor(nsCSSFontDesc aFontDesc
,
199 const nsAString
& aValue
,
200 mozilla::ErrorResult
& aRv
);
203 * Sets all of the descriptor values in mDescriptors using values passed
204 * to the JS constructor.
206 bool SetDescriptors(const nsAString
& aFamily
,
207 const FontFaceDescriptors
& aDescriptors
);
210 * Marks the FontFace as initialized and informs the FontFaceSet it is in,
213 void OnInitialized();
216 * Sets the current loading status.
218 void SetStatus(mozilla::dom::FontFaceLoadStatus aStatus
);
220 void GetDesc(nsCSSFontDesc aDescID
,
221 nsCSSProperty aPropID
,
222 nsString
& aResult
) const;
225 * Returns and takes ownership of the buffer storing the font data.
227 void TakeBuffer(uint8_t*& aBuffer
, uint32_t& aLength
);
229 nsCOMPtr
<nsISupports
> mParent
;
230 nsPresContext
* mPresContext
;
232 // A Promise that is fulfilled once the font represented by this FontFace
233 // is loaded, and is rejected if the load fails.
234 nsRefPtr
<mozilla::dom::Promise
> mLoaded
;
236 // The @font-face rule this FontFace object is reflecting, if it is a
237 // rule backed FontFace.
238 nsRefPtr
<nsCSSFontFaceRule
> mRule
;
240 // The FontFace object's user font entry. This is initially null, but is set
241 // during FontFaceSet::UpdateRules and when a FontFace is explicitly loaded.
242 nsRefPtr
<Entry
> mUserFontEntry
;
244 // The current load status of the font represented by this FontFace.
245 // Note that we can't just reflect the value of the gfxUserFontEntry's
246 // status, since the spec sometimes requires us to go through the event
247 // loop before updating the status, rather than doing it immediately.
248 mozilla::dom::FontFaceLoadStatus mStatus
;
250 // Represents where a FontFace's data is coming from.
252 eSourceType_FontFaceRule
= 1,
257 // Where the font data for this FontFace is coming from.
258 SourceType mSourceType
;
260 // If the FontFace was constructed with an ArrayBuffer(View), this is a
261 // copy of the data from it.
262 uint8_t* mSourceBuffer
;
263 uint32_t mSourceBufferLength
;
265 // The values corresponding to the font face descriptors, if we are not
266 // a rule backed FontFace object. For rule backed objects, we use
267 // the descriptors stored in mRule.
268 nsAutoPtr
<mozilla::CSSFontFaceDescriptors
> mDescriptors
;
270 // The FontFaceSet this FontFace is associated with, regardless of whether
271 // it is currently "in" the set.
272 nsRefPtr
<FontFaceSet
> mFontFaceSet
;
274 // Whether this FontFace appears in the FontFaceSet.
277 // Whether the FontFace has been fully initialized. This takes at least one
278 // run around the event loop, as the parsing of the src descriptor is done
279 // off an event queue task.
282 // Records whether Load() was called on this FontFace before it was
283 // initialized. When the FontFace eventually does become initialized,
284 // mLoadPending is checked and Load() is called if needed.
285 bool mLoadWhenInitialized
;
289 } // namespace mozilla
291 #endif // !defined(mozilla_dom_FontFace_h)