1 /* 2 * Copyright (C) 2006, 2008 Apple Computer, Inc. All rights reserved. 3 * Copyright (C) 2007-2008 Torch Mobile, Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifndef FontCache_h 31 #define FontCache_h 32 33 #include <limits.h> 34 #include "wtf/Forward.h" 35 #include "wtf/PassRefPtr.h" 36 #include "wtf/RefPtr.h" 37 #include "wtf/Vector.h" 38 #include "wtf/text/WTFString.h" 39 #include "wtf/unicode/Unicode.h" 40 41 #if OS(WINDOWS) 42 #include <windows.h> 43 #include <objidl.h> 44 #include <mlang.h> 45 #endif 46 47 namespace WebCore { 48 49 class Font; 50 class FontPlatformData; 51 class FontData; 52 class FontDescription; 53 class FontSelector; 54 class OpenTypeVerticalData; 55 class SimpleFontData; 56 57 class FontCache { 58 friend class FontCachePurgePreventer; 59 60 WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED; 61 public: 62 friend FontCache* fontCache(); 63 64 enum ShouldRetain { Retain, DoNotRetain }; 65 66 PassRefPtr<FontData> getFontData(const Font&, int& familyIndex, FontSelector*); 67 void releaseFontData(const SimpleFontData*); 68 69 // This method is implemented by the plaform and used by 70 // FontFastPath to lookup the font for a given character. 71 PassRefPtr<SimpleFontData> getFontDataForCharacter(const Font&, UChar32); 72 73 // Also implemented by the platform. 74 void platformInit(); 75 76 void getTraitsInFamily(const AtomicString&, Vector<unsigned>&); 77 78 PassRefPtr<SimpleFontData> getFontResourceData(const FontDescription&, const AtomicString&, bool checkingAlternateName = false, ShouldRetain = Retain); 79 PassRefPtr<SimpleFontData> getLastResortFallbackFont(const FontDescription&, ShouldRetain = Retain); 80 SimpleFontData* getNonRetainedLastResortFallbackFont(const FontDescription&); 81 82 void addClient(FontSelector*); 83 void removeClient(FontSelector*); 84 85 unsigned short generation(); 86 void invalidate(); 87 88 size_t fontDataCount(); 89 size_t inactiveFontDataCount(); 90 void purgeInactiveFontData(int count = INT_MAX); 91 92 #if OS(WINDOWS) 93 PassRefPtr<SimpleFontData> fontDataFromDescriptionAndLogFont(const FontDescription&, ShouldRetain, const LOGFONT&, wchar_t* outFontFamilyName); 94 #endif 95 96 #if ENABLE(OPENTYPE_VERTICAL) 97 typedef uint32_t FontFileKey; 98 PassRefPtr<OpenTypeVerticalData> getVerticalData(const FontFileKey&, const FontPlatformData&); 99 #endif 100 101 #if OS(ANDROID) 102 static AtomicString getGenericFamilyNameForScript(const AtomicString& familyName, UScriptCode); 103 #else 104 struct SimpleFontFamily { 105 String name; 106 bool isBold; 107 bool isItalic; 108 }; 109 static void getFontFamilyForCharacter(UChar32, const char* preferredLocale, SimpleFontFamily*); 110 #endif 111 112 private: 113 FontCache(); 114 ~FontCache(); 115 116 void disablePurging() { m_purgePreventCount++; } 117 void enablePurging() 118 { 119 ASSERT(m_purgePreventCount); 120 if (!--m_purgePreventCount) 121 purgeInactiveFontDataIfNeeded(); 122 } 123 124 void purgeInactiveFontDataIfNeeded(); 125 126 // FIXME: This method should eventually be removed. 127 FontPlatformData* getFontResourcePlatformData(const FontDescription&, const AtomicString& family, bool checkingAlternateName = false); 128 129 // These methods are implemented by each platform. 130 PassRefPtr<SimpleFontData> getSimilarFontPlatformData(const Font&); 131 FontPlatformData* createFontPlatformData(const FontDescription&, const AtomicString& family); 132 133 PassRefPtr<SimpleFontData> getFontResourceData(const FontPlatformData*, ShouldRetain = Retain); 134 const FontPlatformData* getFallbackFontData(const FontDescription&); 135 136 // Don't purge if this count is > 0; 137 int m_purgePreventCount; 138 139 #if OS(DARWIN) || OS(ANDROID) 140 friend class ComplexTextController; 141 #endif 142 friend class SimpleFontData; // For getFontResourceData(const FontPlatformData*) 143 friend class FontFallbackList; 144 }; 145 146 // Get the global fontCache. 147 FontCache* fontCache(); 148 149 class FontCachePurgePreventer { 150 public: 151 FontCachePurgePreventer() { fontCache()->disablePurging(); } 152 ~FontCachePurgePreventer() { fontCache()->enablePurging(); } 153 }; 154 155 } 156 157 #endif 158