Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (C) 2006, 2010 Apple Inc. All rights reserved.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef FontFallbackList_h
     22 #define FontFallbackList_h
     23 
     24 #include "platform/fonts/FontSelector.h"
     25 #include "platform/fonts/SimpleFontData.h"
     26 #include "platform/fonts/WidthCache.h"
     27 #include "wtf/Forward.h"
     28 #include "wtf/MainThread.h"
     29 
     30 namespace WebCore {
     31 
     32 class GlyphPageTreeNode;
     33 class GraphicsContext;
     34 class IntRect;
     35 class FontDescription;
     36 class FontPlatformData;
     37 class FontSelector;
     38 
     39 const int cAllFamiliesScanned = -1;
     40 
     41 class PLATFORM_EXPORT FontFallbackList : public RefCounted<FontFallbackList> {
     42     WTF_MAKE_NONCOPYABLE(FontFallbackList);
     43 public:
     44     typedef HashMap<int, GlyphPageTreeNode*, DefaultHash<int>::Hash> GlyphPages;
     45 
     46     class GlyphPagesStateSaver {
     47     public:
     48         GlyphPagesStateSaver(FontFallbackList& fallbackList)
     49             : m_fallbackList(fallbackList)
     50             , m_pages(fallbackList.m_pages)
     51             , m_pageZero(fallbackList.m_pageZero)
     52         {
     53         }
     54 
     55         ~GlyphPagesStateSaver()
     56         {
     57             m_fallbackList.m_pages = m_pages;
     58             m_fallbackList.m_pageZero = m_pageZero;
     59         }
     60 
     61     private:
     62         FontFallbackList& m_fallbackList;
     63         GlyphPages& m_pages;
     64         GlyphPageTreeNode* m_pageZero;
     65     };
     66 
     67     static PassRefPtr<FontFallbackList> create() { return adoptRef(new FontFallbackList()); }
     68 
     69     ~FontFallbackList() { releaseFontData(); }
     70     void invalidate(PassRefPtrWillBeRawPtr<FontSelector>);
     71 
     72     bool isFixedPitch(const FontDescription& fontDescription) const
     73     {
     74         if (m_pitch == UnknownPitch)
     75             determinePitch(fontDescription);
     76         return m_pitch == FixedPitch;
     77     }
     78     void determinePitch(const FontDescription&) const;
     79 
     80     bool loadingCustomFonts() const;
     81     bool shouldSkipDrawing() const;
     82 
     83     FontSelector* fontSelector() const { return m_fontSelector.get(); }
     84     // FIXME: It should be possible to combine fontSelectorVersion and generation.
     85     unsigned fontSelectorVersion() const { return m_fontSelectorVersion; }
     86     unsigned generation() const { return m_generation; }
     87 
     88     WidthCache& widthCache() const { return m_widthCache; }
     89 
     90 private:
     91     FontFallbackList();
     92 
     93     const SimpleFontData* primarySimpleFontData(const FontDescription& fontDescription)
     94     {
     95         ASSERT(isMainThread());
     96         if (!m_cachedPrimarySimpleFontData)
     97             m_cachedPrimarySimpleFontData = primaryFontData(fontDescription)->fontDataForCharacter(' ');
     98         return m_cachedPrimarySimpleFontData;
     99     }
    100 
    101     PassRefPtr<FontData> getFontData(const FontDescription&, int& familyIndex) const;
    102 
    103     const FontData* primaryFontData(const FontDescription&) const;
    104     const FontData* fontDataAt(const FontDescription&, unsigned index) const;
    105 
    106     void releaseFontData();
    107 
    108     mutable Vector<RefPtr<FontData>, 1> m_fontList;
    109     mutable GlyphPages m_pages;
    110     mutable GlyphPageTreeNode* m_pageZero;
    111     mutable const SimpleFontData* m_cachedPrimarySimpleFontData;
    112     RefPtrWillBePersistent<FontSelector> m_fontSelector;
    113     mutable WidthCache m_widthCache;
    114     unsigned m_fontSelectorVersion;
    115     mutable int m_familyIndex;
    116     unsigned short m_generation;
    117     mutable unsigned m_pitch : 3; // Pitch
    118     mutable bool m_hasLoadingFallback : 1;
    119 
    120     friend class Font;
    121 };
    122 
    123 }
    124 
    125 #endif
    126