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 "platform/PlatformExport.h" 35 #include "wtf/Forward.h" 36 #include "wtf/HashMap.h" 37 #include "wtf/PassRefPtr.h" 38 #include "wtf/RefPtr.h" 39 #include "wtf/Vector.h" 40 #include "wtf/text/CString.h" 41 #include "wtf/text/WTFString.h" 42 #include "wtf/unicode/Unicode.h" 43 44 #if OS(WIN) 45 #include "SkFontMgr.h" 46 #include <windows.h> 47 #include <objidl.h> 48 #include <mlang.h> 49 struct IDWriteFactory; 50 #endif 51 52 #if OS(ANDROID) 53 #include <unicode/uscript.h> 54 #endif 55 56 class SkTypeface; 57 58 namespace WebCore { 59 60 class FontCacheClient; 61 class FontPlatformData; 62 class FontData; 63 class FontDescription; 64 class OpenTypeVerticalData; 65 class SimpleFontData; 66 67 enum ShouldRetain { Retain, DoNotRetain }; 68 enum PurgeSeverity { PurgeIfNeeded, ForcePurge }; 69 70 class PLATFORM_EXPORT FontCache { 71 friend class FontCachePurgePreventer; 72 73 WTF_MAKE_NONCOPYABLE(FontCache); WTF_MAKE_FAST_ALLOCATED; 74 public: 75 static FontCache* fontCache(); 76 77 void releaseFontData(const SimpleFontData*); 78 79 // This method is implemented by the plaform and used by 80 // FontFastPath to lookup the font for a given character. 81 PassRefPtr<SimpleFontData> fallbackFontForCharacter(const FontDescription&, UChar32, const SimpleFontData* fontDataToSubstitute); 82 83 // Also implemented by the platform. 84 void platformInit(); 85 86 PassRefPtr<SimpleFontData> getFontData(const FontDescription&, const AtomicString&, bool checkingAlternateName = false, ShouldRetain = Retain); 87 PassRefPtr<SimpleFontData> getLastResortFallbackFont(const FontDescription&, ShouldRetain = Retain); 88 SimpleFontData* getNonRetainedLastResortFallbackFont(const FontDescription&); 89 bool isPlatformFontAvailable(const FontDescription&, const AtomicString&); 90 91 void addClient(FontCacheClient*); 92 #if !ENABLE(OILPAN) 93 void removeClient(FontCacheClient*); 94 #endif 95 96 unsigned short generation(); 97 void invalidate(); 98 99 #if OS(WIN) 100 PassRefPtr<SimpleFontData> fontDataFromDescriptionAndLogFont(const FontDescription&, ShouldRetain, const LOGFONT&, wchar_t* outFontFamilyName); 101 #endif 102 103 #if OS(WIN) useSubpixelPositioning()104 bool useSubpixelPositioning() const { return s_useSubpixelPositioning; } fontManager()105 SkFontMgr* fontManager() { return m_fontManager.get(); } useDirectWrite()106 static bool useDirectWrite() { return s_useDirectWrite; } setUseDirectWrite(bool useDirectWrite)107 static void setUseDirectWrite(bool useDirectWrite) { s_useDirectWrite = useDirectWrite; } setDirectWriteFactory(IDWriteFactory * factory)108 static void setDirectWriteFactory(IDWriteFactory* factory) { s_directWriteFactory = factory; } setUseSubpixelPositioning(bool useSubpixelPositioning)109 static void setUseSubpixelPositioning(bool useSubpixelPositioning) { s_useSubpixelPositioning = useSubpixelPositioning; } 110 static void addSideloadedFontForTesting(SkTypeface*); 111 #endif 112 113 #if ENABLE(OPENTYPE_VERTICAL) 114 typedef uint32_t FontFileKey; 115 PassRefPtr<OpenTypeVerticalData> getVerticalData(const FontFileKey&, const FontPlatformData&); 116 #endif 117 118 #if OS(ANDROID) 119 static AtomicString getGenericFamilyNameForScript(const AtomicString& familyName, UScriptCode); 120 #else 121 struct PlatformFallbackFont { 122 String name; 123 CString filename; 124 int ttcIndex; 125 bool isBold; 126 bool isItalic; 127 }; 128 static void getFontForCharacter(UChar32, const char* preferredLocale, PlatformFallbackFont*); 129 #endif 130 131 private: 132 FontCache(); 133 ~FontCache(); 134 135 void purge(PurgeSeverity = PurgeIfNeeded); 136 disablePurging()137 void disablePurging() { m_purgePreventCount++; } enablePurging()138 void enablePurging() 139 { 140 ASSERT(m_purgePreventCount); 141 if (!--m_purgePreventCount) 142 purge(PurgeIfNeeded); 143 } 144 145 // FIXME: This method should eventually be removed. 146 FontPlatformData* getFontPlatformData(const FontDescription&, const AtomicString& family, bool checkingAlternateName = false); 147 148 // These methods are implemented by each platform. 149 FontPlatformData* createFontPlatformData(const FontDescription&, const AtomicString& family, float fontSize); 150 151 // Implemented on skia platforms. 152 PassRefPtr<SkTypeface> createTypeface(const FontDescription&, const AtomicString& family, CString& name); 153 154 PassRefPtr<SimpleFontData> fontDataFromFontPlatformData(const FontPlatformData*, ShouldRetain = Retain); 155 156 // Don't purge if this count is > 0; 157 int m_purgePreventCount; 158 159 #if OS(WIN) 160 OwnPtr<SkFontMgr> m_fontManager; 161 static bool s_useDirectWrite; 162 static IDWriteFactory* s_directWriteFactory; 163 static bool s_useSubpixelPositioning; 164 static HashMap<String, SkTypeface*>* s_sideloadedFonts; 165 #endif 166 167 #if OS(MACOSX) || OS(ANDROID) 168 friend class ComplexTextController; 169 #endif 170 friend class SimpleFontData; // For fontDataFromFontPlatformData 171 friend class FontFallbackList; 172 }; 173 174 class PLATFORM_EXPORT FontCachePurgePreventer { 175 public: FontCachePurgePreventer()176 FontCachePurgePreventer() { FontCache::fontCache()->disablePurging(); } ~FontCachePurgePreventer()177 ~FontCachePurgePreventer() { FontCache::fontCache()->enablePurging(); } 178 }; 179 180 } 181 182 #endif 183