Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/FontFace.h
Line
Count
Source (jump to first uncovered line)
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/. */
6
7
#ifndef mozilla_dom_FontFace_h
8
#define mozilla_dom_FontFace_h
9
10
#include "mozilla/dom/FontFaceBinding.h"
11
#include "mozilla/FontPropertyTypes.h"
12
#include "gfxUserFontSet.h"
13
#include "nsAutoPtr.h"
14
#include "nsCSSPropertyID.h"
15
#include "nsCSSValue.h"
16
#include "nsWrapperCache.h"
17
18
class gfxFontFaceBufferSource;
19
struct RawServoFontFaceRule;
20
21
namespace mozilla {
22
struct CSSFontFaceDescriptors;
23
class PostTraversalTask;
24
namespace dom {
25
class CSSFontFaceRule;
26
class FontFaceBufferSource;
27
struct FontFaceDescriptors;
28
class FontFaceSet;
29
class Promise;
30
class StringOrArrayBufferOrArrayBufferView;
31
} // namespace dom
32
} // namespace mozilla
33
34
namespace mozilla {
35
namespace dom {
36
37
class FontFace final : public nsISupports,
38
                       public nsWrapperCache
39
{
40
  friend class mozilla::PostTraversalTask;
41
  friend class mozilla::dom::FontFaceBufferSource;
42
  friend class Entry;
43
44
public:
45
  class Entry final : public gfxUserFontEntry {
46
    friend class FontFace;
47
48
  public:
49
    Entry(gfxUserFontSet* aFontSet,
50
          const nsTArray<gfxFontFaceSrc>& aFontFaceSrcList,
51
          WeightRange aWeight,
52
          StretchRange aStretch,
53
          SlantStyleRange aStyle,
54
          const nsTArray<gfxFontFeature>& aFeatureSettings,
55
          const nsTArray<gfxFontVariation>& aVariationSettings,
56
          uint32_t aLanguageOverride,
57
          gfxCharacterMap* aUnicodeRanges,
58
          uint8_t aFontDisplay,
59
          RangeFlags aRangeFlags)
60
      : gfxUserFontEntry(aFontSet, aFontFaceSrcList, aWeight, aStretch,
61
                         aStyle, aFeatureSettings, aVariationSettings,
62
                         aLanguageOverride,
63
                         aUnicodeRanges, aFontDisplay,
64
0
                         aRangeFlags) {}
65
66
    virtual void SetLoadState(UserFontLoadState aLoadState) override;
67
    virtual void GetUserFontSets(nsTArray<gfxUserFontSet*>& aResult) override;
68
0
    const AutoTArray<FontFace*,1>& GetFontFaces() { return mFontFaces; }
69
70
  protected:
71
    // The FontFace objects that use this user font entry.  We need to store
72
    // an array of these, not just a single pointer, since the user font
73
    // cache can return the same entry for different FontFaces that have
74
    // the same descriptor values and come from the same origin.
75
    AutoTArray<FontFace*,1> mFontFaces;
76
  };
77
78
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
79
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(FontFace)
80
81
0
  nsISupports* GetParentObject() const { return mParent; }
82
  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
83
84
  static already_AddRefed<FontFace>
85
  CreateForRule(nsISupports* aGlobal, FontFaceSet* aFontFaceSet,
86
                RawServoFontFaceRule* aRule);
87
88
0
  RawServoFontFaceRule* GetRule() { return mRule; }
89
90
  void GetDesc(nsCSSFontDesc aDescID, nsCSSValue& aResult) const;
91
92
  gfxUserFontEntry* CreateUserFontEntry();
93
0
  gfxUserFontEntry* GetUserFontEntry() const { return mUserFontEntry; }
94
  void SetUserFontEntry(gfxUserFontEntry* aEntry);
95
96
  /**
97
   * Returns whether this object is in the specified FontFaceSet.
98
   */
99
  bool IsInFontFaceSet(FontFaceSet* aFontFaceSet) const;
100
101
  void AddFontFaceSet(FontFaceSet* aFontFaceSet);
102
  void RemoveFontFaceSet(FontFaceSet* aFontFaceSet);
103
104
0
  FontFaceSet* GetPrimaryFontFaceSet() const { return mFontFaceSet; }
105
106
  /**
107
   * Gets the family name of the FontFace as a raw string (such as 'Times', as
108
   * opposed to GetFamily, which returns a CSS-escaped string, such as
109
   * '"Times"').  Returns whether a valid family name was available.
110
   */
111
  bool GetFamilyName(nsCString& aResult);
112
113
  /**
114
   * Returns whether this object is CSS-connected, i.e. reflecting an
115
   * @font-face rule.
116
   */
117
0
  bool HasRule() const { return mRule; }
118
119
  /**
120
   * Breaks the connection between this FontFace and its @font-face rule.
121
   */
122
  void DisconnectFromRule();
123
124
  /**
125
   * Returns whether there is an ArrayBuffer or ArrayBufferView of font
126
   * data.
127
   */
128
  bool HasFontData() const;
129
130
  /**
131
   * Creates a gfxFontFaceBufferSource to represent the font data
132
   * in this object.
133
   */
134
  already_AddRefed<gfxFontFaceBufferSource> CreateBufferSource();
135
136
  /**
137
   * Gets a pointer to and the length of the font data stored in the
138
   * ArrayBuffer or ArrayBufferView.
139
   */
140
  bool GetData(uint8_t*& aBuffer, uint32_t& aLength);
141
142
  /**
143
   * Returns the value of the unicode-range descriptor as a gfxCharacterMap.
144
   */
145
  gfxCharacterMap* GetUnicodeRangeAsCharacterMap();
146
147
  // Web IDL
148
  static already_AddRefed<FontFace>
149
  Constructor(const GlobalObject& aGlobal,
150
              const nsAString& aFamily,
151
              const mozilla::dom::StringOrArrayBufferOrArrayBufferView& aSource,
152
              const mozilla::dom::FontFaceDescriptors& aDescriptors,
153
              ErrorResult& aRV);
154
155
  void GetFamily(nsString& aResult);
156
  void SetFamily(const nsAString& aValue, mozilla::ErrorResult& aRv);
157
  void GetStyle(nsString& aResult);
158
  void SetStyle(const nsAString& aValue, mozilla::ErrorResult& aRv);
159
  void GetWeight(nsString& aResult);
160
  void SetWeight(const nsAString& aValue, mozilla::ErrorResult& aRv);
161
  void GetStretch(nsString& aResult);
162
  void SetStretch(const nsAString& aValue, mozilla::ErrorResult& aRv);
163
  void GetUnicodeRange(nsString& aResult);
164
  void SetUnicodeRange(const nsAString& aValue, mozilla::ErrorResult& aRv);
165
  void GetVariant(nsString& aResult);
166
  void SetVariant(const nsAString& aValue, mozilla::ErrorResult& aRv);
167
  void GetFeatureSettings(nsString& aResult);
168
  void SetFeatureSettings(const nsAString& aValue, mozilla::ErrorResult& aRv);
169
  void GetVariationSettings(nsString& aResult);
170
  void SetVariationSettings(const nsAString& aValue, mozilla::ErrorResult& aRv);
171
  void GetDisplay(nsString& aResult);
172
  void SetDisplay(const nsAString& aValue, mozilla::ErrorResult& aRv);
173
174
  mozilla::dom::FontFaceLoadStatus Status();
175
  mozilla::dom::Promise* Load(mozilla::ErrorResult& aRv);
176
  mozilla::dom::Promise* GetLoaded(mozilla::ErrorResult& aRv);
177
178
private:
179
  FontFace(nsISupports* aParent, FontFaceSet* aFontFaceSet);
180
  ~FontFace();
181
182
  void InitializeSource(const StringOrArrayBufferOrArrayBufferView& aSource);
183
184
  // Helper function for Load.
185
  void DoLoad();
186
187
  // Helper function for the descriptor setter methods.
188
  // Returns whether it successfully sets the descriptor.
189
  bool SetDescriptor(nsCSSFontDesc aFontDesc,
190
                     const nsAString& aValue,
191
                     mozilla::ErrorResult& aRv);
192
193
  /**
194
   * Sets all of the descriptor values in mDescriptors using values passed
195
   * to the JS constructor.
196
   */
197
  bool SetDescriptors(const nsAString& aFamily,
198
                      const FontFaceDescriptors& aDescriptors);
199
200
  /**
201
   * Sets the current loading status.
202
   */
203
  void SetStatus(mozilla::dom::FontFaceLoadStatus aStatus);
204
205
  void GetDesc(nsCSSFontDesc aDescID, nsString& aResult) const;
206
207
  already_AddRefed<URLExtraData> GetURLExtraData() const;
208
209
  RawServoFontFaceRule* GetData() const
210
0
    { return HasRule() ? mRule : mDescriptors; }
211
212
  /**
213
   * Returns and takes ownership of the buffer storing the font data.
214
   */
215
  void TakeBuffer(uint8_t*& aBuffer, uint32_t& aLength);
216
217
  // Acts like mLoaded->MaybeReject(aResult), except it doesn't create mLoaded
218
  // if it doesn't already exist.
219
  void Reject(nsresult aResult);
220
221
  // Creates mLoaded if it doesn't already exist. It may immediately resolve or
222
  // reject mLoaded based on mStatus and mLoadedRejection.
223
  void EnsurePromise();
224
225
  void DoResolve();
226
  void DoReject(nsresult aResult);
227
228
  nsCOMPtr<nsISupports> mParent;
229
230
  // A Promise that is fulfilled once the font represented by this FontFace is
231
  // loaded, and is rejected if the load fails. This promise is created lazily
232
  // when JS asks for it.
233
  RefPtr<mozilla::dom::Promise> mLoaded;
234
235
  // Saves the rejection code for mLoaded if mLoaded hasn't been created yet.
236
  nsresult mLoadedRejection;
237
238
  // The @font-face rule this FontFace object is reflecting, if it is a
239
  // rule backed FontFace.
240
  RefPtr<RawServoFontFaceRule> mRule;
241
242
  // The FontFace object's user font entry.  This is initially null, but is set
243
  // during FontFaceSet::UpdateRules and when a FontFace is explicitly loaded.
244
  RefPtr<Entry> mUserFontEntry;
245
246
  // The current load status of the font represented by this FontFace.
247
  // Note that we can't just reflect the value of the gfxUserFontEntry's
248
  // status, since the spec sometimes requires us to go through the event
249
  // loop before updating the status, rather than doing it immediately.
250
  mozilla::dom::FontFaceLoadStatus mStatus;
251
252
  // Represents where a FontFace's data is coming from.
253
  enum SourceType {
254
    eSourceType_FontFaceRule = 1,
255
    eSourceType_URLs,
256
    eSourceType_Buffer
257
  };
258
259
  // Where the font data for this FontFace is coming from.
260
  SourceType mSourceType;
261
262
  // If the FontFace was constructed with an ArrayBuffer(View), this is a
263
  // copy of the data from it.
264
  uint8_t* mSourceBuffer;
265
  uint32_t mSourceBufferLength;
266
267
  // The values corresponding to the font face descriptors, if we are not
268
  // a rule backed FontFace object.  For rule backed objects, we use
269
  // the descriptors stored in mRule.
270
  // FIXME This should hold a unique ptr to just the descriptors inside,
271
  // so that we don't need to create a rule for it and don't need to
272
  // assign a fake line number and column number. See bug 1450904.
273
  RefPtr<RawServoFontFaceRule> mDescriptors;
274
275
  // The value of the unicode-range descriptor as a gfxCharacterMap.  Valid
276
  // only when mUnicodeRangeDirty is false.
277
  RefPtr<gfxCharacterMap> mUnicodeRange;
278
279
  // The primary FontFaceSet this FontFace is associated with,
280
  // regardless of whether it is currently "in" the set.
281
  RefPtr<FontFaceSet> mFontFaceSet;
282
283
  // Other FontFaceSets (apart from mFontFaceSet) that this FontFace
284
  // appears in.
285
  nsTArray<RefPtr<FontFaceSet>> mOtherFontFaceSets;
286
287
  // Whether mUnicodeRange needs to be rebuilt before being returned from
288
  // GetUnicodeRangeAsCharacterMap.
289
  bool mUnicodeRangeDirty;
290
291
  // Whether this FontFace appears in mFontFaceSet.
292
  bool mInFontFaceSet;
293
};
294
295
} // namespace dom
296
} // namespace mozilla
297
298
#endif // !defined(mozilla_dom_FontFace_h)