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