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 met: 6 * 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 23 * DAMAGE. 24 */ 25 26 #ifndef FontFaceSet_h 27 #define FontFaceSet_h 28 29 #include "bindings/v8/ScriptPromise.h" 30 #include "core/css/FontFace.h" 31 #include "core/dom/ActiveDOMObject.h" 32 #include "core/events/EventListener.h" 33 #include "core/events/EventTarget.h" 34 #include "core/events/ThreadLocalEventNames.h" 35 #include "platform/AsyncMethodRunner.h" 36 #include "platform/RefCountedSupplement.h" 37 #include "wtf/PassRefPtr.h" 38 #include "wtf/RefCounted.h" 39 #include "wtf/Vector.h" 40 41 // Mac OS X 10.6 SDK defines check() macro that interfares with our check() method 42 #ifdef check 43 #undef check 44 #endif 45 46 namespace WebCore { 47 48 class CSSFontFaceSource; 49 class Dictionary; 50 class Document; 51 class ExceptionState; 52 class Font; 53 class FontResource; 54 class FontsReadyPromiseResolver; 55 class ExecutionContext; 56 57 class FontFaceSet : public RefCountedSupplement<Document, FontFaceSet>, public ActiveDOMObject, public EventTargetWithInlineData { 58 REFCOUNTED_EVENT_TARGET(FontFaceSet); 59 public: 60 virtual ~FontFaceSet(); 61 62 DEFINE_ATTRIBUTE_EVENT_LISTENER(loading); 63 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadingdone); 64 DEFINE_ATTRIBUTE_EVENT_LISTENER(loadingerror); 65 66 Vector<RefPtr<FontFace> > match(const String& font, const String& text, ExceptionState&); 67 bool check(const String& font, const String& text, ExceptionState&); 68 ScriptPromise load(const String& font, const String& text, ExceptionState&); 69 ScriptPromise ready(); 70 71 AtomicString status() const; 72 73 virtual ExecutionContext* executionContext() const OVERRIDE; 74 virtual const AtomicString& interfaceName() const OVERRIDE; 75 76 Document* document() const; 77 78 void didLayout(); 79 void beginFontLoading(FontFace*); 80 void fontLoaded(FontFace*); 81 void loadError(FontFace*); 82 83 // ActiveDOMObject 84 virtual void suspend() OVERRIDE; 85 virtual void resume() OVERRIDE; 86 virtual void stop() OVERRIDE; 87 88 static PassRefPtr<FontFaceSet> from(Document*); 89 static void didLayout(Document*); 90 91 private: 92 typedef RefCountedSupplement<Document, FontFaceSet> SupplementType; 93 94 static PassRefPtr<FontFaceSet> create(Document* document) 95 { 96 return adoptRef<FontFaceSet>(new FontFaceSet(document)); 97 } 98 99 class FontLoadHistogram { 100 public: 101 FontLoadHistogram() : m_count(0), m_recorded(false) { } 102 void incrementCount() { m_count++; } 103 void record(); 104 105 private: 106 int m_count; 107 bool m_recorded; 108 }; 109 110 FontFaceSet(Document*); 111 112 bool hasLoadedFonts() const { return !m_loadedFonts.isEmpty() || !m_failedFonts.isEmpty(); } 113 114 void queueDoneEvent(FontFace*); 115 void fireLoadingEvent(); 116 void fireDoneEventIfPossible(); 117 bool resolveFontStyle(const String&, Font&); 118 void handlePendingEventsAndPromisesSoon(); 119 void handlePendingEventsAndPromises(); 120 121 unsigned m_loadingCount; 122 bool m_shouldFireLoadingEvent; 123 Vector<OwnPtr<FontsReadyPromiseResolver> > m_readyResolvers; 124 FontFaceArray m_loadedFonts; 125 FontFaceArray m_failedFonts; 126 127 AsyncMethodRunner<FontFaceSet> m_asyncRunner; 128 129 FontLoadHistogram m_histogram; 130 }; 131 132 } // namespace WebCore 133 134 #endif // FontFaceSet_h 135