FontFace.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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/. */
  5. #ifndef mozilla_dom_FontFace_h
  6. #define mozilla_dom_FontFace_h
  7. #include "mozilla/dom/FontFaceBinding.h"
  8. #include "gfxUserFontSet.h"
  9. #include "nsAutoPtr.h"
  10. #include "nsCSSPropertyID.h"
  11. #include "nsCSSValue.h"
  12. #include "nsWrapperCache.h"
  13. class gfxFontFaceBufferSource;
  14. class nsCSSFontFaceRule;
  15. namespace mozilla {
  16. struct CSSFontFaceDescriptors;
  17. namespace dom {
  18. class FontFaceBufferSource;
  19. struct FontFaceDescriptors;
  20. class FontFaceSet;
  21. class Promise;
  22. class StringOrArrayBufferOrArrayBufferView;
  23. } // namespace dom
  24. } // namespace mozilla
  25. namespace mozilla {
  26. namespace dom {
  27. class FontFace final : public nsISupports,
  28. public nsWrapperCache
  29. {
  30. friend class mozilla::dom::FontFaceBufferSource;
  31. friend class Entry;
  32. public:
  33. class Entry final : public gfxUserFontEntry {
  34. friend class FontFace;
  35. public:
  36. Entry(gfxUserFontSet* aFontSet,
  37. const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
  38. uint32_t aWeight,
  39. int32_t aStretch,
  40. uint8_t aStyle,
  41. const nsTArray<gfxFontFeature>& aFeatureSettings,
  42. uint32_t aLanguageOverride,
  43. gfxSparseBitSet* aUnicodeRanges,
  44. uint8_t aFontDisplay)
  45. : gfxUserFontEntry(aFontSet, aFontFaceSrcList, aWeight, aStretch,
  46. aStyle, aFeatureSettings, aLanguageOverride,
  47. aUnicodeRanges, aFontDisplay) {}
  48. virtual void SetLoadState(UserFontLoadState aLoadState) override;
  49. virtual void GetUserFontSets(nsTArray<gfxUserFontSet*>& aResult) override;
  50. const AutoTArray<FontFace*,1>& GetFontFaces() { return mFontFaces; }
  51. protected:
  52. // The FontFace objects that use this user font entry. We need to store
  53. // an array of these, not just a single pointer, since the user font
  54. // cache can return the same entry for different FontFaces that have
  55. // the same descriptor values and come from the same origin.
  56. AutoTArray<FontFace*,1> mFontFaces;
  57. };
  58. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  59. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FontFace)
  60. nsISupports* GetParentObject() const { return mParent; }
  61. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  62. static already_AddRefed<FontFace>
  63. CreateForRule(nsISupports* aGlobal, FontFaceSet* aFontFaceSet,
  64. nsCSSFontFaceRule* aRule);
  65. nsCSSFontFaceRule* GetRule() { return mRule; }
  66. void GetDesc(nsCSSFontDesc aDescID, nsCSSValue& aResult) const;
  67. gfxUserFontEntry* CreateUserFontEntry();
  68. gfxUserFontEntry* GetUserFontEntry() const { return mUserFontEntry; }
  69. void SetUserFontEntry(gfxUserFontEntry* aEntry);
  70. /**
  71. * Returns whether this object is in the specified FontFaceSet.
  72. */
  73. bool IsInFontFaceSet(FontFaceSet* aFontFaceSet) const;
  74. void AddFontFaceSet(FontFaceSet* aFontFaceSet);
  75. void RemoveFontFaceSet(FontFaceSet* aFontFaceSet);
  76. FontFaceSet* GetPrimaryFontFaceSet() const { return mFontFaceSet; }
  77. /**
  78. * Gets the family name of the FontFace as a raw string (such as 'Times', as
  79. * opposed to GetFamily, which returns a CSS-escaped string, such as
  80. * '"Times"'). Returns whether a valid family name was available.
  81. */
  82. bool GetFamilyName(nsString& aResult);
  83. /**
  84. * Returns whether this object is CSS-connected, i.e. reflecting an
  85. * @font-face rule.
  86. */
  87. bool HasRule() const { return mRule; }
  88. /**
  89. * Breaks the connection between this FontFace and its @font-face rule.
  90. */
  91. void DisconnectFromRule();
  92. /**
  93. * Returns whether there is an ArrayBuffer or ArrayBufferView of font
  94. * data.
  95. */
  96. bool HasFontData() const;
  97. /**
  98. * Creates a gfxFontFaceBufferSource to represent the font data
  99. * in this object.
  100. */
  101. already_AddRefed<gfxFontFaceBufferSource> CreateBufferSource();
  102. /**
  103. * Gets a pointer to and the length of the font data stored in the
  104. * ArrayBuffer or ArrayBufferView.
  105. */
  106. bool GetData(uint8_t*& aBuffer, uint32_t& aLength);
  107. // Web IDL
  108. static already_AddRefed<FontFace>
  109. Constructor(const GlobalObject& aGlobal,
  110. const nsAString& aFamily,
  111. const mozilla::dom::StringOrArrayBufferOrArrayBufferView& aSource,
  112. const mozilla::dom::FontFaceDescriptors& aDescriptors,
  113. ErrorResult& aRV);
  114. void GetFamily(nsString& aResult);
  115. void SetFamily(const nsAString& aValue, mozilla::ErrorResult& aRv);
  116. void GetStyle(nsString& aResult);
  117. void SetStyle(const nsAString& aValue, mozilla::ErrorResult& aRv);
  118. void GetWeight(nsString& aResult);
  119. void SetWeight(const nsAString& aValue, mozilla::ErrorResult& aRv);
  120. void GetStretch(nsString& aResult);
  121. void SetStretch(const nsAString& aValue, mozilla::ErrorResult& aRv);
  122. void GetUnicodeRange(nsString& aResult);
  123. void SetUnicodeRange(const nsAString& aValue, mozilla::ErrorResult& aRv);
  124. void GetVariant(nsString& aResult);
  125. void SetVariant(const nsAString& aValue, mozilla::ErrorResult& aRv);
  126. void GetFeatureSettings(nsString& aResult);
  127. void SetFeatureSettings(const nsAString& aValue, mozilla::ErrorResult& aRv);
  128. void GetDisplay(nsString& aResult);
  129. void SetDisplay(const nsAString& aValue, mozilla::ErrorResult& aRv);
  130. mozilla::dom::FontFaceLoadStatus Status();
  131. mozilla::dom::Promise* Load(mozilla::ErrorResult& aRv);
  132. mozilla::dom::Promise* GetLoaded(mozilla::ErrorResult& aRv);
  133. private:
  134. FontFace(nsISupports* aParent, FontFaceSet* aFontFaceSet);
  135. ~FontFace();
  136. void InitializeSource(const StringOrArrayBufferOrArrayBufferView& aSource);
  137. // Helper function for Load.
  138. void DoLoad();
  139. /**
  140. * Parses a @font-face descriptor value, storing the result in aResult.
  141. * Returns whether the parsing was successful.
  142. */
  143. bool ParseDescriptor(nsCSSFontDesc aDescID, const nsAString& aString,
  144. nsCSSValue& aResult);
  145. // Helper function for the descriptor setter methods.
  146. void SetDescriptor(nsCSSFontDesc aFontDesc,
  147. const nsAString& aValue,
  148. mozilla::ErrorResult& aRv);
  149. /**
  150. * Sets all of the descriptor values in mDescriptors using values passed
  151. * to the JS constructor.
  152. */
  153. bool SetDescriptors(const nsAString& aFamily,
  154. const FontFaceDescriptors& aDescriptors);
  155. /**
  156. * Sets the current loading status.
  157. */
  158. void SetStatus(mozilla::dom::FontFaceLoadStatus aStatus);
  159. void GetDesc(nsCSSFontDesc aDescID,
  160. nsCSSPropertyID aPropID,
  161. nsString& aResult) const;
  162. /**
  163. * Returns and takes ownership of the buffer storing the font data.
  164. */
  165. void TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength);
  166. // Acts like mLoaded->MaybeReject(aResult), except it doesn't create mLoaded
  167. // if it doesn't already exist.
  168. void Reject(nsresult aResult);
  169. // Creates mLoaded if it doesn't already exist. It may immediately resolve or
  170. // reject mLoaded based on mStatus and mLoadedRejection.
  171. void EnsurePromise();
  172. nsCOMPtr<nsISupports> mParent;
  173. // A Promise that is fulfilled once the font represented by this FontFace is
  174. // loaded, and is rejected if the load fails. This promise is created lazily
  175. // when JS asks for it.
  176. RefPtr<mozilla::dom::Promise> mLoaded;
  177. // Saves the rejection code for mLoaded if mLoaded hasn't been created yet.
  178. nsresult mLoadedRejection;
  179. // The @font-face rule this FontFace object is reflecting, if it is a
  180. // rule backed FontFace.
  181. RefPtr<nsCSSFontFaceRule> mRule;
  182. // The FontFace object's user font entry. This is initially null, but is set
  183. // during FontFaceSet::UpdateRules and when a FontFace is explicitly loaded.
  184. RefPtr<Entry> mUserFontEntry;
  185. // The current load status of the font represented by this FontFace.
  186. // Note that we can't just reflect the value of the gfxUserFontEntry's
  187. // status, since the spec sometimes requires us to go through the event
  188. // loop before updating the status, rather than doing it immediately.
  189. mozilla::dom::FontFaceLoadStatus mStatus;
  190. // Represents where a FontFace's data is coming from.
  191. enum SourceType {
  192. eSourceType_FontFaceRule = 1,
  193. eSourceType_URLs,
  194. eSourceType_Buffer
  195. };
  196. // Where the font data for this FontFace is coming from.
  197. SourceType mSourceType;
  198. // If the FontFace was constructed with an ArrayBuffer(View), this is a
  199. // copy of the data from it.
  200. uint8_t* mSourceBuffer;
  201. uint32_t mSourceBufferLength;
  202. // The values corresponding to the font face descriptors, if we are not
  203. // a rule backed FontFace object. For rule backed objects, we use
  204. // the descriptors stored in mRule.
  205. nsAutoPtr<mozilla::CSSFontFaceDescriptors> mDescriptors;
  206. // The primary FontFaceSet this FontFace is associated with,
  207. // regardless of whether it is currently "in" the set.
  208. RefPtr<FontFaceSet> mFontFaceSet;
  209. // Other FontFaceSets (apart from mFontFaceSet) that this FontFace
  210. // appears in.
  211. nsTArray<RefPtr<FontFaceSet>> mOtherFontFaceSets;
  212. // Whether this FontFace appears in mFontFaceSet.
  213. bool mInFontFaceSet;
  214. };
  215. } // namespace dom
  216. } // namespace mozilla
  217. #endif // !defined(mozilla_dom_FontFace_h)