1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef GlyphPageTreeNode_h
30 #define GlyphPageTreeNode_h
31 
32 #include "GlyphPage.h"
33 #include <string.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/PassRefPtr.h>
36 #include <wtf/RefCounted.h>
37 #include <wtf/unicode/Unicode.h>
38 
39 #ifndef NDEBUG
40 void showGlyphPageTrees();
41 void showGlyphPageTree(unsigned pageNumber);
42 #endif
43 
44 namespace WebCore {
45 
46 class FontData;
47 class SimpleFontData;
48 
49 // The glyph page tree is a data structure that maps (FontData, glyph page number)
50 // to a GlyphPage.  Level 0 (the "root") is special. There is one root
51 // GlyphPageTreeNode for each glyph page number.  The roots do not have a
52 // GlyphPage associated with them, and their initializePage() function is never
53 // called to fill the glyphs.
54 //
55 // Each root node maps a FontData pointer to another GlyphPageTreeNode at
56 // level 1 (the "root child") that stores the actual glyphs for a specific font data.
57 // These nodes will only have a GlyphPage if they have glyphs for that range.
58 //
59 // Levels greater than one correspond to subsequent levels of the fallback list
60 // for that font. These levels override their parent's page of glyphs by
61 // filling in holes with the new font (thus making a more complete page).
62 //
63 // A NULL FontData pointer corresponds to the system fallback
64 // font. It is tracked separately from the regular pages and overrides so that
65 // the glyph pages do not get polluted with these last-resort glyphs. The
66 // system fallback page is not populated at construction like the other pages,
67 // but on demand for each glyph, because the system may need to use different
68 // fallback fonts for each. This lazy population is done by the Font.
69 class GlyphPageTreeNode {
70 public:
GlyphPageTreeNode()71     GlyphPageTreeNode()
72         : m_parent(0)
73         , m_level(0)
74         , m_isSystemFallback(false)
75         , m_customFontCount(0)
76         , m_systemFallbackChild(0)
77 #ifndef NDEBUG
78         , m_pageNumber(0)
79 #endif
80     {
81     }
82 
83     ~GlyphPageTreeNode();
84 
85     static HashMap<int, GlyphPageTreeNode*>* roots;
86     static GlyphPageTreeNode* pageZeroRoot;
87 
getRootChild(const FontData * fontData,unsigned pageNumber)88     static GlyphPageTreeNode* getRootChild(const FontData* fontData, unsigned pageNumber)
89     {
90         return getRoot(pageNumber)->getChild(fontData, pageNumber);
91     }
92 
93     static void pruneTreeCustomFontData(const FontData*);
94     static void pruneTreeFontData(const SimpleFontData*);
95 
96     void pruneCustomFontData(const FontData*);
97     void pruneFontData(const SimpleFontData*, unsigned level = 0);
98 
parent()99     GlyphPageTreeNode* parent() const { return m_parent; }
100     GlyphPageTreeNode* getChild(const FontData*, unsigned pageNumber);
101 
102     // Returns a page of glyphs (or NULL if there are no glyphs in this page's character range).
page()103     GlyphPage* page() const { return m_page.get(); }
104 
105     // Returns the level of this node. See class-level comment.
level()106     unsigned level() const { return m_level; }
107 
108     // The system fallback font has special rules (see above).
isSystemFallback()109     bool isSystemFallback() const { return m_isSystemFallback; }
110 
111     static size_t treeGlyphPageCount();
112     size_t pageCount() const;
113 
114 private:
115     static GlyphPageTreeNode* getRoot(unsigned pageNumber);
116     void initializePage(const FontData*, unsigned pageNumber);
117 
118 #ifndef NDEBUG
119     void showSubtree();
120 #endif
121 
122     GlyphPageTreeNode* m_parent;
123     RefPtr<GlyphPage> m_page;
124     unsigned m_level : 31;
125     bool m_isSystemFallback : 1;
126     unsigned m_customFontCount;
127     HashMap<const FontData*, GlyphPageTreeNode*> m_children;
128     GlyphPageTreeNode* m_systemFallbackChild;
129 
130 #ifndef NDEBUG
131     unsigned m_pageNumber;
132 
133     friend void ::showGlyphPageTree(unsigned pageNumber);
134 #endif
135 };
136 
137 } // namespace WebCore
138 
139 #endif // GlyphPageTreeNode_h
140