Home | History | Annotate | Download | only in fonts
      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 "platform/fonts/FontFaceCreationParams.h"
     36 #include "wtf/Forward.h"
     37 #include "wtf/HashMap.h"
     38 #include "wtf/PassRefPtr.h"
     39 #include "wtf/RefPtr.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 blink {
     59 
     60 class FontCacheClient;
     61 class FontFaceCreationParams;
     62 class FontPlatformData;
     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)
    104     bool useSubpixelPositioning() const { return s_useSubpixelPositioning; }
    105     SkFontMgr* fontManager() { return m_fontManager.get(); }
    106     static bool useDirectWrite() { return s_useDirectWrite; }
    107     static float deviceScaleFactor() { return s_deviceScaleFactor; }
    108     static void setUseDirectWrite(bool useDirectWrite) { s_useDirectWrite = useDirectWrite; }
    109     static void setDirectWriteFactory(IDWriteFactory* factory) { s_directWriteFactory = factory; }
    110     static void setDeviceScaleFactor(float deviceScaleFactor) { s_deviceScaleFactor = deviceScaleFactor; }
    111     static void setUseSubpixelPositioning(bool useSubpixelPositioning) { s_useSubpixelPositioning = useSubpixelPositioning; }
    112     static void addSideloadedFontForTesting(SkTypeface*);
    113 #endif
    114 
    115 #if ENABLE(OPENTYPE_VERTICAL)
    116     typedef uint32_t FontFileKey;
    117     PassRefPtr<OpenTypeVerticalData> getVerticalData(const FontFileKey&, const FontPlatformData&);
    118 #endif
    119 
    120 #if OS(ANDROID)
    121     static AtomicString getGenericFamilyNameForScript(const AtomicString& familyName, const FontDescription&);
    122 #else
    123     struct PlatformFallbackFont {
    124         String name;
    125         CString filename;
    126         int fontconfigInterfaceId;
    127         int ttcIndex;
    128         bool isBold;
    129         bool isItalic;
    130     };
    131     static void getFontForCharacter(UChar32, const char* preferredLocale, PlatformFallbackFont*);
    132 #endif
    133 
    134 private:
    135     FontCache();
    136     ~FontCache();
    137 
    138     void purge(PurgeSeverity = PurgeIfNeeded);
    139 
    140     void disablePurging() { m_purgePreventCount++; }
    141     void enablePurging()
    142     {
    143         ASSERT(m_purgePreventCount);
    144         if (!--m_purgePreventCount)
    145             purge(PurgeIfNeeded);
    146     }
    147 
    148     // FIXME: This method should eventually be removed.
    149     FontPlatformData* getFontPlatformData(const FontDescription&, const FontFaceCreationParams&, bool checkingAlternateName = false);
    150 
    151     // These methods are implemented by each platform.
    152     FontPlatformData* createFontPlatformData(const FontDescription&, const FontFaceCreationParams&, float fontSize);
    153 
    154     // Implemented on skia platforms.
    155     PassRefPtr<SkTypeface> createTypeface(const FontDescription&, const FontFaceCreationParams&, CString& name);
    156 
    157     PassRefPtr<SimpleFontData> fontDataFromFontPlatformData(const FontPlatformData*, ShouldRetain = Retain);
    158     PassRefPtr<SimpleFontData> fallbackOnStandardFontStyle(const FontDescription&, UChar32);
    159 
    160     // Don't purge if this count is > 0;
    161     int m_purgePreventCount;
    162 
    163 #if OS(WIN)
    164     OwnPtr<SkFontMgr> m_fontManager;
    165     static bool s_useDirectWrite;
    166     static IDWriteFactory* s_directWriteFactory;
    167     static float s_deviceScaleFactor;
    168     static bool s_useSubpixelPositioning;
    169     static HashMap<String, RefPtr<SkTypeface> >* s_sideloadedFonts;
    170 #endif
    171 
    172 #if OS(MACOSX) || OS(ANDROID)
    173     friend class ComplexTextController;
    174 #endif
    175     friend class SimpleFontData; // For fontDataFromFontPlatformData
    176     friend class FontFallbackList;
    177 };
    178 
    179 class PLATFORM_EXPORT FontCachePurgePreventer {
    180 public:
    181     FontCachePurgePreventer() { FontCache::fontCache()->disablePurging(); }
    182     ~FontCachePurgePreventer() { FontCache::fontCache()->enablePurging(); }
    183 };
    184 
    185 } // namespace blink
    186 
    187 #endif
    188