1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_FONT_FACE_H_
32 #define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_FONT_FACE_H_
33 
34 #include "base/macros.h"
35 #include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable.h"
36 #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
37 #include "third_party/blink/renderer/bindings/core/v8/script_promise_property.h"
38 #include "third_party/blink/renderer/core/css/css_value.h"
39 #include "third_party/blink/renderer/core/css/font_display.h"
40 #include "third_party/blink/renderer/core/css/parser/at_rule_descriptors.h"
41 #include "third_party/blink/renderer/core/dom/dom_exception.h"
42 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
43 #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
44 #include "third_party/blink/renderer/platform/fonts/font_selection_types.h"
45 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
46 
47 namespace blink {
48 
49 class CSSFontFace;
50 class CSSValue;
51 class DOMArrayBuffer;
52 class DOMArrayBufferView;
53 class Document;
54 class ExceptionState;
55 class FontFaceDescriptors;
56 class StringOrArrayBufferOrArrayBufferView;
57 class CSSPropertyValueSet;
58 class StyleRuleFontFace;
59 
60 class CORE_EXPORT FontFace : public ScriptWrappable,
61                              public ActiveScriptWrappable<FontFace>,
62                              public ExecutionContextClient {
63   DEFINE_WRAPPERTYPEINFO();
64   USING_GARBAGE_COLLECTED_MIXIN(FontFace);
65 
66  public:
67   enum LoadStatusType { kUnloaded, kLoading, kLoaded, kError };
68 
69   static FontFace* Create(ExecutionContext*,
70                           const AtomicString& family,
71                           StringOrArrayBufferOrArrayBufferView&,
72                           const FontFaceDescriptors*);
73   static FontFace* Create(Document*, const StyleRuleFontFace*);
74 
75   explicit FontFace(ExecutionContext*);
76   FontFace(ExecutionContext*,
77            const AtomicString& family,
78            const FontFaceDescriptors*);
79   ~FontFace() override;
80 
family()81   const AtomicString& family() const { return family_; }
82   String style() const;
83   String weight() const;
84   String stretch() const;
85   String unicodeRange() const;
86   String variant() const;
87   String featureSettings() const;
88   String display() const;
89 
90   // FIXME: Changing these attributes should affect font matching.
setFamily(ExecutionContext *,const AtomicString & s,ExceptionState &)91   void setFamily(ExecutionContext*, const AtomicString& s, ExceptionState&) {
92     family_ = s;
93   }
94   void setStyle(ExecutionContext*, const String&, ExceptionState&);
95   void setWeight(ExecutionContext*, const String&, ExceptionState&);
96   void setStretch(ExecutionContext*, const String&, ExceptionState&);
97   void setUnicodeRange(ExecutionContext*, const String&, ExceptionState&);
98   void setVariant(ExecutionContext*, const String&, ExceptionState&);
99   void setFeatureSettings(ExecutionContext*, const String&, ExceptionState&);
100   void setDisplay(ExecutionContext*, const String&, ExceptionState&);
101 
102   String status() const;
loaded(ScriptState * script_state)103   ScriptPromise loaded(ScriptState* script_state) {
104     return FontStatusPromise(script_state);
105   }
106 
107   ScriptPromise load(ScriptState*);
108 
LoadStatus()109   LoadStatusType LoadStatus() const { return status_; }
110   void SetLoadStatus(LoadStatusType);
111   void SetError(DOMException* = nullptr);
GetError()112   DOMException* GetError() const { return error_; }
113   FontSelectionCapabilities GetFontSelectionCapabilities() const;
CssFontFace()114   CSSFontFace* CssFontFace() { return css_font_face_.Get(); }
115   size_t ApproximateBlankCharacterCount() const;
116   FontDisplay GetFontDisplay() const;
117 
118   void Trace(Visitor*) override;
119 
120   bool HadBlankText() const;
121 
122   class CORE_EXPORT LoadFontCallback : public GarbageCollectedMixin {
123    public:
124     virtual ~LoadFontCallback() = default;
125     virtual void NotifyLoaded(FontFace*) = 0;
126     virtual void NotifyError(FontFace*) = 0;
Trace(Visitor * visitor)127     void Trace(Visitor* visitor) override {}
128   };
129   void LoadWithCallback(LoadFontCallback*);
130   void AddCallback(LoadFontCallback*);
131 
132   void DidBeginImperativeLoad();
133 
134   // ScriptWrappable:
135   bool HasPendingActivity() const final;
136 
137  private:
138   static FontFace* Create(ExecutionContext*,
139                           const AtomicString& family,
140                           DOMArrayBuffer* source,
141                           const FontFaceDescriptors*);
142   static FontFace* Create(ExecutionContext*,
143                           const AtomicString& family,
144                           DOMArrayBufferView*,
145                           const FontFaceDescriptors*);
146   static FontFace* Create(ExecutionContext*,
147                           const AtomicString& family,
148                           const String& source,
149                           const FontFaceDescriptors*);
150 
151   void InitCSSFontFace(ExecutionContext*, const CSSValue& src);
152   void InitCSSFontFace(const unsigned char* data, size_t);
153   void SetPropertyFromString(const ExecutionContext*,
154                              const String&,
155                              AtRuleDescriptorID,
156                              ExceptionState* = nullptr);
157   bool SetPropertyFromStyle(const CSSPropertyValueSet&, AtRuleDescriptorID);
158   bool SetPropertyValue(const CSSValue*, AtRuleDescriptorID);
159   bool SetFamilyValue(const CSSValue&);
160   ScriptPromise FontStatusPromise(ScriptState*);
161   void RunCallbacks();
162 
163   using LoadedProperty = ScriptPromiseProperty<Member<FontFace>,
164                                                Member<DOMException>>;
165 
166   AtomicString family_;
167   String ots_parse_message_;
168   Member<const CSSValue> style_;
169   Member<const CSSValue> weight_;
170   Member<const CSSValue> stretch_;
171   Member<const CSSValue> unicode_range_;
172   Member<const CSSValue> variant_;
173   Member<const CSSValue> feature_settings_;
174   Member<const CSSValue> display_;
175   LoadStatusType status_;
176   Member<DOMException> error_;
177 
178   Member<LoadedProperty> loaded_property_;
179   Member<CSSFontFace> css_font_face_;
180   HeapVector<Member<LoadFontCallback>> callbacks_;
181   DISALLOW_COPY_AND_ASSIGN(FontFace);
182 };
183 
184 using FontFaceArray = HeapVector<Member<FontFace>>;
185 
186 }  // namespace blink
187 
188 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_FONT_FACE_H_
189