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"
53 class FontFaceDescriptors;
54 class StylePropertySet;
55 class StyleRuleFontFace;
57 class FontFace : public RefCountedWillBeGarbageCollectedFinalized<FontFace>, public ScriptWrappable, public ActiveDOMObject {
58 DEFINE_WRAPPERTYPEINFO();
60 enum LoadStatus { Unloaded, Loading, Loaded, Error };
62 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, PassRefPtr<ArrayBuffer> source, const FontFaceDescriptors&);
63 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, PassRefPtr<ArrayBufferView>, const FontFaceDescriptors&);
64 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, const String& source, const FontFaceDescriptors&);
65 static PassRefPtrWillBeRawPtr<FontFace> create(Document*, const StyleRuleFontFace*);
69 const AtomicString& family() const { return m_family; }
71 String weight() const;
72 String stretch() const;
73 String unicodeRange() const;
74 String variant() const;
75 String featureSettings() const;
77 // FIXME: Changing these attributes should affect font matching.
78 void setFamily(ExecutionContext*, const AtomicString& s, ExceptionState&) { m_family = s; }
79 void setStyle(ExecutionContext*, const String&, ExceptionState&);
80 void setWeight(ExecutionContext*, const String&, ExceptionState&);
81 void setStretch(ExecutionContext*, const String&, ExceptionState&);
82 void setUnicodeRange(ExecutionContext*, const String&, ExceptionState&);
83 void setVariant(ExecutionContext*, const String&, ExceptionState&);
84 void setFeatureSettings(ExecutionContext*, const String&, ExceptionState&);
86 String status() const;
87 ScriptPromise loaded(ScriptState* scriptState) { return fontStatusPromise(scriptState); }
89 ScriptPromise load(ScriptState*);
91 LoadStatus loadStatus() const { return m_status; }
92 void setLoadStatus(LoadStatus);
93 void setError(PassRefPtrWillBeRawPtr<DOMException> = nullptr);
94 DOMException* error() const { return m_error.get(); }
95 FontTraits traits() const;
96 CSSFontFace* cssFontFace() { return m_cssFontFace.get(); }
100 bool hadBlankText() const;
102 class LoadFontCallback : public RefCountedWillBeGarbageCollectedFinalized<LoadFontCallback> {
104 virtual ~LoadFontCallback() { }
105 virtual void notifyLoaded(FontFace*) = 0;
106 virtual void notifyError(FontFace*) = 0;
107 virtual void trace(Visitor*) { }
109 void loadWithCallback(PassRefPtrWillBeRawPtr<LoadFontCallback>, ExecutionContext*);
112 virtual bool hasPendingActivity() const OVERRIDE;
115 explicit FontFace(ExecutionContext*);
116 FontFace(ExecutionContext*, const AtomicString& family, const FontFaceDescriptors&);
118 void initCSSFontFace(Document*, PassRefPtrWillBeRawPtr<CSSValue> src);
119 void initCSSFontFace(const unsigned char* data, unsigned size);
120 void setPropertyFromString(const Document*, const String&, CSSPropertyID, ExceptionState* = 0);
121 bool setPropertyFromStyle(const StylePropertySet&, CSSPropertyID);
122 bool setPropertyValue(PassRefPtrWillBeRawPtr<CSSValue>, CSSPropertyID);
123 bool setFamilyValue(CSSValueList*);
124 void loadInternal(ExecutionContext*);
125 ScriptPromise fontStatusPromise(ScriptState*);
127 typedef ScriptPromiseProperty<RawPtrWillBeMember<FontFace>, RawPtrWillBeMember<FontFace>, RefPtrWillBeMember<DOMException> > LoadedProperty;
129 AtomicString m_family;
130 RefPtrWillBeMember<CSSValue> m_src;
131 RefPtrWillBeMember<CSSValue> m_style;
132 RefPtrWillBeMember<CSSValue> m_weight;
133 RefPtrWillBeMember<CSSValue> m_stretch;
134 RefPtrWillBeMember<CSSValue> m_unicodeRange;
135 RefPtrWillBeMember<CSSValue> m_variant;
136 RefPtrWillBeMember<CSSValue> m_featureSettings;
138 RefPtrWillBeMember<DOMException> m_error;
140 PersistentWillBeMember<LoadedProperty> m_loadedProperty;
141 OwnPtrWillBeMember<CSSFontFace> m_cssFontFace;
142 WillBeHeapVector<RefPtrWillBeMember<LoadFontCallback> > m_callbacks;
145 typedef WillBeHeapVector<RefPtrWillBeMember<FontFace> > FontFaceArray;