OLD | NEW |
| (Empty) |
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 "platform/fonts/GlyphPage.h" | |
33 #include "wtf/Allocator.h" | |
34 #include "wtf/HashMap.h" | |
35 #include "wtf/PassRefPtr.h" | |
36 #include "wtf/text/Unicode.h" | |
37 #include <memory> | |
38 #include <string.h> | |
39 #include <unicode/uscript.h> | |
40 | |
41 namespace blink { | |
42 | |
43 class FontData; | |
44 class SimpleFontData; | |
45 | |
46 // The glyph page tree is a data structure that maps (FontData, glyph page | |
47 // number) to a GlyphPage. Level 0 (the "root") is special. There is one root | |
48 // GlyphPageTreeNode for each glyph page number. The roots do not have a | |
49 // GlyphPage associated with them, and their initializePage() function is never | |
50 // called to fill the glyphs. | |
51 // | |
52 // Each root node maps a FontData pointer to another GlyphPageTreeNode at | |
53 // level 1 (the "root child") that stores the actual glyphs for a specific font | |
54 // data. These nodes will only have a GlyphPage if they have glyphs for that | |
55 // range. | |
56 // | |
57 // Levels greater than one correspond to subsequent levels of the fallback list | |
58 // for that font. These levels override their parent's page of glyphs by | |
59 // filling in holes with the new font (thus making a more complete page). | |
60 // | |
61 // A SystemFallbackGlyphPageTreeNode is a special leaf node of the glyph tree | |
62 // which is for tracking the glyph page for a system fallback font. | |
63 // The glyph page is tracked separately from the regular pages and overrides | |
64 // so that the glyph pages do not get polluted with these last-resort glyphs. | |
65 // The system fallback page is not populated at construction like the other | |
66 // pages, but on demand for each glyph, because the system may need to use | |
67 // different fallback fonts for each. This lazy population is done by the Font. | |
68 | |
69 class GlyphPageTreeNode; | |
70 class SystemFallbackGlyphPageTreeNode; | |
71 | |
72 class PLATFORM_EXPORT GlyphPageTreeNodeBase { | |
73 USING_FAST_MALLOC(GlyphPageTreeNodeBase); | |
74 WTF_MAKE_NONCOPYABLE(GlyphPageTreeNodeBase); | |
75 | |
76 public: | |
77 GlyphPageTreeNode* parent() const { return m_parent; } | |
78 | |
79 // Returns the level of this node. See class-level comment. | |
80 unsigned level() const { return m_level; } | |
81 | |
82 // The system fallback font has special rules (see above). | |
83 bool isSystemFallback() const { return m_isSystemFallback; } | |
84 | |
85 // Returns a page of glyphs (or null if there are no glyphs in this page's | |
86 // character range). | |
87 virtual GlyphPage* page(UScriptCode = USCRIPT_COMMON) = 0; | |
88 | |
89 protected: | |
90 GlyphPageTreeNodeBase(GlyphPageTreeNode* parent, bool isSystemFallback); | |
91 virtual ~GlyphPageTreeNodeBase() {} | |
92 | |
93 GlyphPageTreeNode* m_parent; | |
94 unsigned m_level : 31; | |
95 unsigned m_isSystemFallback : 1; | |
96 unsigned m_customFontCount; | |
97 | |
98 #if ENABLE(ASSERT) | |
99 unsigned m_pageNumber; | |
100 #endif | |
101 }; | |
102 | |
103 class PLATFORM_EXPORT GlyphPageTreeNode : public GlyphPageTreeNodeBase { | |
104 public: | |
105 static GlyphPageTreeNodeBase* getRootChild(const FontData* fontData, | |
106 unsigned pageNumber) { | |
107 return getRoot(pageNumber)->getChild(fontData, pageNumber); | |
108 } | |
109 static GlyphPageTreeNode* getNormalRootChild(const FontData* fontData, | |
110 unsigned pageNumber) { | |
111 return getRoot(pageNumber)->getNormalChild(fontData, pageNumber); | |
112 } | |
113 | |
114 static void pruneTreeCustomFontData(const FontData*); | |
115 static void pruneTreeFontData(const SimpleFontData*); | |
116 | |
117 void pruneCustomFontData(const FontData*); | |
118 void pruneFontData(const SimpleFontData*, unsigned level = 0); | |
119 | |
120 GlyphPage* page(UScriptCode = USCRIPT_COMMON) final { return m_page.get(); } | |
121 | |
122 GlyphPageTreeNodeBase* getChild(const FontData*, unsigned pageNumber); | |
123 GlyphPageTreeNode* getNormalChild(const FontData*, unsigned pageNumber); | |
124 SystemFallbackGlyphPageTreeNode* getSystemFallbackChild(unsigned pageNumber); | |
125 | |
126 static size_t treeGlyphPageCount(); | |
127 size_t pageCount() const; | |
128 | |
129 private: | |
130 GlyphPageTreeNode(GlyphPageTreeNode* parent = nullptr) | |
131 : GlyphPageTreeNodeBase(parent, false) {} | |
132 | |
133 static GlyphPageTreeNode* getRoot(unsigned pageNumber); | |
134 | |
135 void initializePage(const FontData*, unsigned pageNumber); | |
136 void initializePurePage(const FontData*, unsigned pageNumber); | |
137 void initializeOverridePage(const FontData*, unsigned pageNumber); | |
138 | |
139 #ifndef NDEBUG | |
140 void showSubtree(); | |
141 #endif | |
142 | |
143 static HashMap<int, GlyphPageTreeNode*>* roots; | |
144 static GlyphPageTreeNode* pageZeroRoot; | |
145 | |
146 RefPtr<GlyphPage> m_page; | |
147 typedef HashMap<const FontData*, std::unique_ptr<GlyphPageTreeNode>> | |
148 GlyphPageTreeNodeMap; | |
149 GlyphPageTreeNodeMap m_children; | |
150 std::unique_ptr<SystemFallbackGlyphPageTreeNode> m_systemFallbackChild; | |
151 }; | |
152 | |
153 class PLATFORM_EXPORT SystemFallbackGlyphPageTreeNode | |
154 : public GlyphPageTreeNodeBase { | |
155 public: | |
156 GlyphPage* page(UScriptCode = USCRIPT_COMMON) final; | |
157 | |
158 private: | |
159 friend class GlyphPageTreeNode; | |
160 | |
161 SystemFallbackGlyphPageTreeNode(GlyphPageTreeNode* parent) | |
162 : GlyphPageTreeNodeBase(parent, true) {} | |
163 | |
164 void pruneFontData(const SimpleFontData*); | |
165 PassRefPtr<GlyphPage> initializePage(); | |
166 | |
167 struct UScriptCodeHashTraits : WTF::GenericHashTraits<UScriptCode> { | |
168 STATIC_ONLY(UScriptCodeHashTraits); | |
169 static UScriptCode emptyValue() { return USCRIPT_CODE_LIMIT; } | |
170 static void constructDeletedValue(UScriptCode& slot, bool) { | |
171 slot = USCRIPT_INVALID_CODE; | |
172 } | |
173 static bool isDeletedValue(UScriptCode value) { | |
174 return value == USCRIPT_INVALID_CODE; | |
175 } | |
176 }; | |
177 typedef HashMap<UScriptCode, | |
178 RefPtr<GlyphPage>, | |
179 WTF::IntHash<UScriptCode>, | |
180 UScriptCodeHashTraits> | |
181 PageByScriptMap; | |
182 PageByScriptMap m_pagesByScript; | |
183 }; | |
184 | |
185 inline GlyphPageTreeNodeBase::GlyphPageTreeNodeBase(GlyphPageTreeNode* parent, | |
186 bool isSystemFallback) | |
187 : m_parent(parent), | |
188 m_level(parent ? parent->m_level + 1 : 0), | |
189 m_isSystemFallback(isSystemFallback), | |
190 m_customFontCount(0) | |
191 #if ENABLE(ASSERT) | |
192 , | |
193 m_pageNumber(parent ? parent->m_pageNumber : 0) | |
194 #endif | |
195 { | |
196 } | |
197 | |
198 inline GlyphPageTreeNode* toGlyphPageTreeNode(GlyphPageTreeNodeBase* node) { | |
199 ASSERT(!node->isSystemFallback()); | |
200 return static_cast<GlyphPageTreeNode*>(node); | |
201 } | |
202 | |
203 inline SystemFallbackGlyphPageTreeNode* toSystemFallbackGlyphPageTreeNode( | |
204 GlyphPageTreeNodeBase* node) { | |
205 ASSERT(node->isSystemFallback()); | |
206 return static_cast<SystemFallbackGlyphPageTreeNode*>(node); | |
207 } | |
208 | |
209 inline GlyphPageTreeNodeBase* GlyphPageTreeNode::getChild( | |
210 const FontData* fontData, | |
211 unsigned pageNumber) { | |
212 if (fontData) | |
213 return getNormalChild(fontData, pageNumber); | |
214 return getSystemFallbackChild(pageNumber); | |
215 } | |
216 | |
217 } // namespace blink | |
218 | |
219 #endif // GlyphPageTreeNode_h | |
OLD | NEW |