2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "bindings/core/v8/ScriptPromise.h"
35 #include "bindings/core/v8/ScriptPromiseProperty.h"
36 #include "bindings/core/v8/ScriptWrappable.h"
37 #include "core/CSSPropertyNames.h"
38 #include "core/css/CSSValue.h"
39 #include "core/dom/ActiveDOMObject.h"
40 #include "core/dom/DOMException.h"
41 #include "platform/fonts/FontTraits.h"
42 #include "wtf/PassRefPtr.h"
43 #include "wtf/RefCounted.h"
44 #include "wtf/text/WTFString.h"
51 class DOMArrayBufferView;
55 class FontFaceDescriptors;
56 class StringOrArrayBufferOrArrayBufferView;
57 class StylePropertySet;
58 class StyleRuleFontFace;
60 class FontFace : public RefCountedWillBeGarbageCollectedFinalized<FontFace>, public ScriptWrappable, public ActiveDOMObject {
61 DEFINE_WRAPPERTYPEINFO();
63 enum LoadStatus { Unloaded, Loading, Loaded, Error };
65 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, StringOrArrayBufferOrArrayBufferView&, const FontFaceDescriptors&);
66 static PassRefPtrWillBeRawPtr<FontFace> create(Document*, const StyleRuleFontFace*);
70 const AtomicString& family() const { return m_family; }
72 String weight() const;
73 String stretch() const;
74 String unicodeRange() const;
75 String variant() const;
76 String featureSettings() const;
78 // FIXME: Changing these attributes should affect font matching.
79 void setFamily(ExecutionContext*, const AtomicString& s, ExceptionState&) { m_family = s; }
80 void setStyle(ExecutionContext*, const String&, ExceptionState&);
81 void setWeight(ExecutionContext*, const String&, ExceptionState&);
82 void setStretch(ExecutionContext*, const String&, ExceptionState&);
83 void setUnicodeRange(ExecutionContext*, const String&, ExceptionState&);
84 void setVariant(ExecutionContext*, const String&, ExceptionState&);
85 void setFeatureSettings(ExecutionContext*, const String&, ExceptionState&);
87 String status() const;
88 ScriptPromise loaded(ScriptState* scriptState) { return fontStatusPromise(scriptState); }
90 ScriptPromise load(ScriptState*);
92 LoadStatus loadStatus() const { return m_status; }
93 void setLoadStatus(LoadStatus);
94 void setError(PassRefPtrWillBeRawPtr<DOMException> = nullptr);
95 DOMException* error() const { return m_error.get(); }
96 FontTraits traits() const;
97 CSSFontFace* cssFontFace() { return m_cssFontFace.get(); }
101 bool hadBlankText() const;
103 class LoadFontCallback : public RefCountedWillBeGarbageCollectedFinalized<LoadFontCallback> {
105 virtual ~LoadFontCallback() { }
106 virtual void notifyLoaded(FontFace*) = 0;
107 virtual void notifyError(FontFace*) = 0;
108 virtual void trace(Visitor*) { }
110 void loadWithCallback(PassRefPtrWillBeRawPtr<LoadFontCallback>, ExecutionContext*);
113 virtual bool hasPendingActivity() const override;
116 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, PassRefPtr<DOMArrayBuffer> source, const FontFaceDescriptors&);
117 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, PassRefPtr<DOMArrayBufferView>, const FontFaceDescriptors&);
118 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, const String& source, const FontFaceDescriptors&);
120 explicit FontFace(ExecutionContext*);
121 FontFace(ExecutionContext*, const AtomicString& family, const FontFaceDescriptors&);
123 void initCSSFontFace(Document*, PassRefPtrWillBeRawPtr<CSSValue> src);
124 void initCSSFontFace(const unsigned char* data, unsigned size);
125 void setPropertyFromString(const Document*, const String&, CSSPropertyID, ExceptionState* = 0);
126 bool setPropertyFromStyle(const StylePropertySet&, CSSPropertyID);
127 bool setPropertyValue(PassRefPtrWillBeRawPtr<CSSValue>, CSSPropertyID);
128 bool setFamilyValue(CSSValueList*);
129 void loadInternal(ExecutionContext*);
130 ScriptPromise fontStatusPromise(ScriptState*);
132 typedef ScriptPromiseProperty<RawPtrWillBeMember<FontFace>, RawPtrWillBeMember<FontFace>, RefPtrWillBeMember<DOMException> > LoadedProperty;
134 AtomicString m_family;
135 RefPtrWillBeMember<CSSValue> m_src;
136 RefPtrWillBeMember<CSSValue> m_style;
137 RefPtrWillBeMember<CSSValue> m_weight;
138 RefPtrWillBeMember<CSSValue> m_stretch;
139 RefPtrWillBeMember<CSSValue> m_unicodeRange;
140 RefPtrWillBeMember<CSSValue> m_variant;
141 RefPtrWillBeMember<CSSValue> m_featureSettings;
143 RefPtrWillBeMember<DOMException> m_error;
145 PersistentWillBeMember<LoadedProperty> m_loadedProperty;
146 OwnPtrWillBeMember<CSSFontFace> m_cssFontFace;
147 WillBeHeapVector<RefPtrWillBeMember<LoadFontCallback> > m_callbacks;
150 typedef WillBeHeapVector<RefPtrWillBeMember<FontFace> > FontFaceArray;