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 blink {
     31 
     32 class GlyphPageTreeNode;
     33 class FontDescription;
     34 
     35 const int cAllFamiliesScanned = -1;
     36 
     37 class PLATFORM_EXPORT FontFallbackList : public RefCounted<FontFallbackList> {
     38     WTF_MAKE_NONCOPYABLE(FontFallbackList);
     39 public:
     40     typedef HashMap<int, GlyphPageTreeNode*, DefaultHash<int>::Hash> GlyphPages;
     41 
     42     class GlyphPagesStateSaver {
     43     public:
     44         GlyphPagesStateSaver(FontFallbackList& fallbackList)
     45             : m_fallbackList(fallbackList)
     46             , m_pages(fallbackList.m_pages)
     47             , m_pageZero(fallbackList.m_pageZero)
     48         {
     49         }
     50 
     51         ~GlyphPagesStateSaver()
     52         {
     53             m_fallbackList.m_pages = m_pages;
     54             m_fallbackList.m_pageZero = m_pageZero;
     55         }
     56 
     57     private:
     58         FontFallbackList& m_fallbackList;
     59         GlyphPages& m_pages;
     60         GlyphPageTreeNode* m_pageZero;
     61     };
     62 
     63     static PassRefPtr<FontFallbackList> create() { return adoptRef(new FontFallbackList()); }
     64 
     65     ~FontFallbackList() { releaseFontData(); }
     66     void invalidate(PassRefPtrWillBeRawPtr<FontSelector>);
     67 
     68     bool isFixedPitch(const FontDescription& fontDescription) const
     69     {
     70         if (m_pitch == UnknownPitch)
     71             determinePitch(fontDescription);
     72         return m_pitch == FixedPitch;
     73     }
     74     void determinePitch(const FontDescription&) const;
     75 
     76     bool loadingCustomFonts() const;
     77     bool shouldSkipDrawing() const;
     78 
     79     FontSelector* fontSelector() const { return m_fontSelector.get(); }
     80     // FIXME: It should be possible to combine fontSelectorVersion and generation.
     81     unsigned fontSelectorVersion() const { return m_fontSelectorVersion; }
     82     unsigned generation() const { return m_generation; }
     83 
     84     WidthCache& widthCache() const { return m_widthCache; }
     85 
     86     const SimpleFontData* primarySimpleFontData(const FontDescription& fontDescription)
     87     {
     88         ASSERT(isMainThread());
     89         if (!m_cachedPrimarySimpleFontData)
     90             m_cachedPrimarySimpleFontData = determinePrimarySimpleFontData(fontDescription);
     91         return m_cachedPrimarySimpleFontData;
     92     }
     93     const FontData* fontDataAt(const FontDescription&, unsigned index) const;
     94 
     95     GlyphPageTreeNode* getPageNode(unsigned pageNumber) const
     96     {
     97         return pageNumber ? m_pages.get(pageNumber) : m_pageZero;
     98     }
     99 
    100     void setPageNode(unsigned pageNumber, GlyphPageTreeNode* node)
    101     {
    102         if (pageNumber)
    103             m_pages.set(pageNumber, node);
    104         else
    105             m_pageZero = node;
    106     }
    107 
    108 private:
    109     FontFallbackList();
    110 
    111     PassRefPtr<FontData> getFontData(const FontDescription&, int& familyIndex) const;
    112 
    113     const SimpleFontData* determinePrimarySimpleFontData(const FontDescription&) const;
    114 
    115     void releaseFontData();
    116 
    117     mutable Vector<RefPtr<FontData>, 1> m_fontList;
    118     GlyphPages m_pages;
    119     GlyphPageTreeNode* m_pageZero;
    120     mutable const SimpleFontData* m_cachedPrimarySimpleFontData;
    121     RefPtrWillBePersistent<FontSelector> m_fontSelector;
    122     mutable WidthCache m_widthCache;
    123     unsigned m_fontSelectorVersion;
    124     mutable int m_familyIndex;
    125     unsigned short m_generation;
    126     mutable unsigned m_pitch : 3; // Pitch
    127     mutable bool m_hasLoadingFallback : 1;
    128 };
    129 
    130 } // namespace blink
    131 
    132 #endif
    133