Home | History | Annotate | Download | only in graphics
      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 "core/platform/graphics/FontSelector.h"
     25 #include "core/platform/graphics/SimpleFontData.h"
     26 #include "core/platform/graphics/WidthCache.h"
     27 #include "wtf/Forward.h"
     28 #include "wtf/MainThread.h"
     29 
     30 namespace WebCore {
     31 
     32 class Font;
     33 class GlyphPageTreeNode;
     34 class GraphicsContext;
     35 class IntRect;
     36 class FontDescription;
     37 class FontPlatformData;
     38 class FontSelector;
     39 
     40 const int cAllFamiliesScanned = -1;
     41 
     42 class FontFallbackList : public RefCounted<FontFallbackList> {
     43     WTF_MAKE_NONCOPYABLE(FontFallbackList);
     44 public:
     45     typedef HashMap<int, GlyphPageTreeNode*, DefaultHash<int>::Hash> GlyphPages;
     46 
     47     class GlyphPagesStateSaver {
     48     public:
     49         GlyphPagesStateSaver(FontFallbackList& fallbackList)
     50             : m_fallbackList(fallbackList)
     51             , m_pages(fallbackList.m_pages)
     52             , m_pageZero(fallbackList.m_pageZero)
     53         {
     54         }
     55 
     56         ~GlyphPagesStateSaver()
     57         {
     58             m_fallbackList.m_pages = m_pages;
     59             m_fallbackList.m_pageZero = m_pageZero;
     60         }
     61 
     62     private:
     63         FontFallbackList& m_fallbackList;
     64         GlyphPages& m_pages;
     65         GlyphPageTreeNode* m_pageZero;
     66     };
     67 
     68     static PassRefPtr<FontFallbackList> create() { return adoptRef(new FontFallbackList()); }
     69 
     70     ~FontFallbackList() { releaseFontData(); }
     71     void invalidate(PassRefPtr<FontSelector>);
     72 
     73     bool isFixedPitch(const Font* f) const { if (m_pitch == UnknownPitch) determinePitch(f); return m_pitch == FixedPitch; };
     74     void determinePitch(const Font*) const;
     75 
     76     bool loadingCustomFonts() const { return m_loadingCustomFonts; }
     77 
     78     FontSelector* fontSelector() const { return m_fontSelector.get(); }
     79     // FIXME: It should be possible to combine fontSelectorVersion and generation.
     80     unsigned fontSelectorVersion() const { return m_fontSelectorVersion; }
     81     unsigned generation() const { return m_generation; }
     82 
     83     WidthCache& widthCache() const { return m_widthCache; }
     84 
     85 private:
     86     FontFallbackList();
     87 
     88     const SimpleFontData* primarySimpleFontData(const Font* f)
     89     {
     90         ASSERT(isMainThread());
     91         if (!m_cachedPrimarySimpleFontData)
     92             m_cachedPrimarySimpleFontData = primaryFontData(f)->fontDataForCharacter(' ');
     93         return m_cachedPrimarySimpleFontData;
     94     }
     95 
     96     const FontData* primaryFontData(const Font*) const;
     97     const FontData* fontDataAt(const Font*, unsigned index) const;
     98 
     99     void setPlatformFont(const FontPlatformData&);
    100 
    101     void releaseFontData();
    102 
    103     mutable Vector<RefPtr<FontData>, 1> m_fontList;
    104     mutable GlyphPages m_pages;
    105     mutable GlyphPageTreeNode* m_pageZero;
    106     mutable const SimpleFontData* m_cachedPrimarySimpleFontData;
    107     RefPtr<FontSelector> m_fontSelector;
    108     mutable WidthCache m_widthCache;
    109     unsigned m_fontSelectorVersion;
    110     mutable int m_familyIndex;
    111     unsigned short m_generation;
    112     mutable unsigned m_pitch : 3; // Pitch
    113     mutable bool m_loadingCustomFonts : 1;
    114 
    115     friend class Font;
    116 };
    117 
    118 }
    119 
    120 #endif
    121