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 FontFace_h 32 #define FontFace_h 33 34 #include "bindings/v8/ScriptPromise.h" 35 #include "core/CSSPropertyNames.h" 36 #include "core/css/CSSValue.h" 37 #include "core/dom/DOMError.h" 38 #include "platform/fonts/FontTraits.h" 39 #include "wtf/PassRefPtr.h" 40 #include "wtf/RefCounted.h" 41 #include "wtf/text/WTFString.h" 42 43 namespace WebCore { 44 45 class CSSFontFace; 46 class CSSValueList; 47 class Dictionary; 48 class Document; 49 class ExceptionState; 50 class FontFaceReadyPromiseResolver; 51 class StylePropertySet; 52 class StyleRuleFontFace; 53 54 class FontFace : public RefCountedWillBeGarbageCollectedFinalized<FontFace> { 55 public: 56 enum LoadStatus { Unloaded, Loading, Loaded, Error }; 57 58 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, PassRefPtr<ArrayBuffer> source, const Dictionary&, ExceptionState&); 59 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, PassRefPtr<ArrayBufferView>, const Dictionary&, ExceptionState&); 60 static PassRefPtrWillBeRawPtr<FontFace> create(ExecutionContext*, const AtomicString& family, const String& source, const Dictionary&, ExceptionState&); 61 static PassRefPtrWillBeRawPtr<FontFace> create(Document*, const StyleRuleFontFace*); 62 63 ~FontFace(); 64 65 const AtomicString& family() const { return m_family; } 66 String style() const; 67 String weight() const; 68 String stretch() const; 69 String unicodeRange() const; 70 String variant() const; 71 String featureSettings() const; 72 73 // FIXME: Changing these attributes should affect font matching. 74 void setFamily(ExecutionContext*, const AtomicString& s, ExceptionState&) { m_family = s; } 75 void setStyle(ExecutionContext*, const String&, ExceptionState&); 76 void setWeight(ExecutionContext*, const String&, ExceptionState&); 77 void setStretch(ExecutionContext*, const String&, ExceptionState&); 78 void setUnicodeRange(ExecutionContext*, const String&, ExceptionState&); 79 void setVariant(ExecutionContext*, const String&, ExceptionState&); 80 void setFeatureSettings(ExecutionContext*, const String&, ExceptionState&); 81 82 String status() const; 83 ScriptPromise loaded(ScriptState* scriptState) { return fontStatusPromise(scriptState); } 84 85 ScriptPromise load(ScriptState*); 86 87 LoadStatus loadStatus() const { return m_status; } 88 void setLoadStatus(LoadStatus); 89 DOMError* error() const { return m_error.get(); } 90 FontTraits traits() const; 91 CSSFontFace* cssFontFace() { return m_cssFontFace.get(); } 92 93 void trace(Visitor*); 94 95 bool hadBlankText() const; 96 97 class LoadFontCallback : public RefCountedWillBeGarbageCollectedFinalized<LoadFontCallback> { 98 public: 99 virtual ~LoadFontCallback() { } 100 virtual void notifyLoaded(FontFace*) = 0; 101 virtual void notifyError(FontFace*) = 0; 102 virtual void trace(Visitor*) { } 103 }; 104 void loadWithCallback(PassRefPtrWillBeRawPtr<LoadFontCallback>, ExecutionContext*); 105 106 private: 107 FontFace(); 108 109 void initCSSFontFace(Document*, PassRefPtrWillBeRawPtr<CSSValue> src); 110 void initCSSFontFace(const unsigned char* data, unsigned size); 111 void setPropertyFromString(const Document*, const String&, CSSPropertyID, ExceptionState&); 112 bool setPropertyFromStyle(const StylePropertySet&, CSSPropertyID); 113 bool setPropertyValue(PassRefPtrWillBeRawPtr<CSSValue>, CSSPropertyID); 114 bool setFamilyValue(CSSValueList*); 115 void resolveReadyPromises(); 116 void loadInternal(ExecutionContext*); 117 ScriptPromise fontStatusPromise(ScriptState*); 118 119 AtomicString m_family; 120 RefPtrWillBeMember<CSSValue> m_src; 121 RefPtrWillBeMember<CSSValue> m_style; 122 RefPtrWillBeMember<CSSValue> m_weight; 123 RefPtrWillBeMember<CSSValue> m_stretch; 124 RefPtrWillBeMember<CSSValue> m_unicodeRange; 125 RefPtrWillBeMember<CSSValue> m_variant; 126 RefPtrWillBeMember<CSSValue> m_featureSettings; 127 LoadStatus m_status; 128 RefPtrWillBeMember<DOMError> m_error; 129 130 Vector<OwnPtr<FontFaceReadyPromiseResolver> > m_readyResolvers; 131 OwnPtrWillBeMember<CSSFontFace> m_cssFontFace; 132 WillBeHeapVector<RefPtrWillBeMember<LoadFontCallback> > m_callbacks; 133 }; 134 135 typedef WillBeHeapVector<RefPtrWillBeMember<FontFace> > FontFaceArray; 136 137 } // namespace WebCore 138 139 #endif // FontFace_h 140