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 #include "config.h"
30 #include "GlyphPageTreeNode.h"
31
32 #include "CharacterNames.h"
33 #include "SegmentedFontData.h"
34 #include "SimpleFontData.h"
35 #include <wtf/unicode/Unicode.h>
36
37 namespace WebCore {
38
39 using std::max;
40 using std::min;
41
42 HashMap<int, GlyphPageTreeNode*>* GlyphPageTreeNode::roots = 0;
43 GlyphPageTreeNode* GlyphPageTreeNode::pageZeroRoot = 0;
44
getRoot(unsigned pageNumber)45 GlyphPageTreeNode* GlyphPageTreeNode::getRoot(unsigned pageNumber)
46 {
47 static bool initialized;
48 if (!initialized) {
49 initialized = true;
50 roots = new HashMap<int, GlyphPageTreeNode*>;
51 pageZeroRoot = new GlyphPageTreeNode;
52 }
53
54 GlyphPageTreeNode* node = pageNumber ? roots->get(pageNumber) : pageZeroRoot;
55 if (!node) {
56 node = new GlyphPageTreeNode;
57 #ifndef NDEBUG
58 node->m_pageNumber = pageNumber;
59 #endif
60 if (pageNumber)
61 roots->set(pageNumber, node);
62 else
63 pageZeroRoot = node;
64 }
65 return node;
66 }
67
treeGlyphPageCount()68 size_t GlyphPageTreeNode::treeGlyphPageCount()
69 {
70 size_t count = 0;
71 if (roots) {
72 HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
73 for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
74 count += it->second->pageCount();
75 }
76
77 if (pageZeroRoot)
78 count += pageZeroRoot->pageCount();
79
80 return count;
81 }
82
pageCount() const83 size_t GlyphPageTreeNode::pageCount() const
84 {
85 size_t count = m_page && m_page->owner() == this ? 1 : 0;
86 HashMap<const FontData*, GlyphPageTreeNode*>::const_iterator end = m_children.end();
87 for (HashMap<const FontData*, GlyphPageTreeNode*>::const_iterator it = m_children.begin(); it != end; ++it)
88 count += it->second->pageCount();
89
90 return count;
91 }
92
pruneTreeCustomFontData(const FontData * fontData)93 void GlyphPageTreeNode::pruneTreeCustomFontData(const FontData* fontData)
94 {
95 // Enumerate all the roots and prune any tree that contains our custom font data.
96 if (roots) {
97 HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
98 for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
99 it->second->pruneCustomFontData(fontData);
100 }
101
102 if (pageZeroRoot)
103 pageZeroRoot->pruneCustomFontData(fontData);
104 }
105
pruneTreeFontData(const SimpleFontData * fontData)106 void GlyphPageTreeNode::pruneTreeFontData(const SimpleFontData* fontData)
107 {
108 if (roots) {
109 HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
110 for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
111 it->second->pruneFontData(fontData);
112 }
113
114 if (pageZeroRoot)
115 pageZeroRoot->pruneFontData(fontData);
116 }
117
~GlyphPageTreeNode()118 GlyphPageTreeNode::~GlyphPageTreeNode()
119 {
120 deleteAllValues(m_children);
121 delete m_systemFallbackChild;
122 }
123
initializePage(const FontData * fontData,unsigned pageNumber)124 void GlyphPageTreeNode::initializePage(const FontData* fontData, unsigned pageNumber)
125 {
126 ASSERT(!m_page);
127
128 // This function must not be called for the root of the tree, because that
129 // level does not contain any glyphs.
130 ASSERT(m_level > 0 && m_parent);
131
132 // The parent's page will be 0 if we are level one or the parent's font data
133 // did not contain any glyphs for that page.
134 GlyphPage* parentPage = m_parent->page();
135
136 // NULL FontData means we're being asked for the system fallback font.
137 if (fontData) {
138 if (m_level == 1) {
139 // Children of the root hold pure pages. These will cover only one
140 // font data's glyphs, and will have glyph index 0 if the font data does not
141 // contain the glyph.
142 unsigned start = pageNumber * GlyphPage::size;
143 UChar buffer[GlyphPage::size * 2 + 2];
144 unsigned bufferLength;
145 unsigned i;
146
147 // Fill in a buffer with the entire "page" of characters that we want to look up glyphs for.
148 if (start < 0x10000) {
149 bufferLength = GlyphPage::size;
150 for (i = 0; i < GlyphPage::size; i++)
151 buffer[i] = start + i;
152
153 if (start == 0) {
154 // Control characters must not render at all.
155 for (i = 0; i < 0x20; ++i)
156 buffer[i] = zeroWidthSpace;
157 for (i = 0x7F; i < 0xA0; i++)
158 buffer[i] = zeroWidthSpace;
159
160 // \n, \t, and nonbreaking space must render as a space.
161 buffer[(int)'\n'] = ' ';
162 buffer[(int)'\t'] = ' ';
163 buffer[noBreakSpace] = ' ';
164 } else if (start == (leftToRightMark & ~(GlyphPage::size - 1))) {
165 // LRM, RLM, LRE, RLE and PDF must not render at all.
166 buffer[leftToRightMark - start] = zeroWidthSpace;
167 buffer[rightToLeftMark - start] = zeroWidthSpace;
168 buffer[leftToRightEmbed - start] = zeroWidthSpace;
169 buffer[rightToLeftEmbed - start] = zeroWidthSpace;
170 buffer[leftToRightOverride - start] = zeroWidthSpace;
171 buffer[rightToLeftOverride - start] = zeroWidthSpace;
172 buffer[popDirectionalFormatting - start] = zeroWidthSpace;
173 } else if (start == (objectReplacementCharacter & ~(GlyphPage::size - 1))) {
174 // Object replacement character must not render at all.
175 buffer[objectReplacementCharacter - start] = zeroWidthSpace;
176 }
177 } else {
178 bufferLength = GlyphPage::size * 2;
179 for (i = 0; i < GlyphPage::size; i++) {
180 int c = i + start;
181 buffer[i * 2] = U16_LEAD(c);
182 buffer[i * 2 + 1] = U16_TRAIL(c);
183 }
184 }
185
186 m_page = GlyphPage::create(this);
187
188 // Now that we have a buffer full of characters, we want to get back an array
189 // of glyph indices. This part involves calling into the platform-specific
190 // routine of our glyph map for actually filling in the page with the glyphs.
191 // Success is not guaranteed. For example, Times fails to fill page 260, giving glyph data
192 // for only 128 out of 256 characters.
193 bool haveGlyphs;
194 if (fontData->isSegmented()) {
195 haveGlyphs = false;
196
197 const SegmentedFontData* segmentedFontData = static_cast<const SegmentedFontData*>(fontData);
198 unsigned numRanges = segmentedFontData->numRanges();
199 bool zeroFilled = false;
200 RefPtr<GlyphPage> scratchPage;
201 GlyphPage* pageToFill = m_page.get();
202 for (unsigned i = 0; i < numRanges; i++) {
203 const FontDataRange& range = segmentedFontData->rangeAt(i);
204 int from = max(0, range.from() - static_cast<int>(start));
205 int to = 1 + min(range.to() - static_cast<int>(start), static_cast<int>(GlyphPage::size) - 1);
206 if (from < static_cast<int>(GlyphPage::size) && to > 0) {
207 if (haveGlyphs && !scratchPage) {
208 scratchPage = GlyphPage::create(this);
209 pageToFill = scratchPage.get();
210 }
211
212 if (!zeroFilled) {
213 if (from > 0 || to < static_cast<int>(GlyphPage::size)) {
214 for (unsigned i = 0; i < GlyphPage::size; i++)
215 pageToFill->setGlyphDataForIndex(i, 0, 0);
216 }
217 zeroFilled = true;
218 }
219 haveGlyphs |= pageToFill->fill(from, to - from, buffer + from * (start < 0x10000 ? 1 : 2), (to - from) * (start < 0x10000 ? 1 : 2), range.fontData());
220 if (scratchPage) {
221 for (int j = from; j < to; j++) {
222 if (!m_page->m_glyphs[j].glyph && pageToFill->m_glyphs[j].glyph)
223 m_page->m_glyphs[j] = pageToFill->m_glyphs[j];
224 }
225 }
226 }
227 }
228 } else
229 haveGlyphs = m_page->fill(0, GlyphPage::size, buffer, bufferLength, static_cast<const SimpleFontData*>(fontData));
230
231 if (!haveGlyphs)
232 m_page = 0;
233 } else if (parentPage && parentPage->owner() != m_parent) {
234 // The page we're overriding may not be owned by our parent node.
235 // This happens when our parent node provides no useful overrides
236 // and just copies the pointer to an already-existing page (see
237 // below).
238 //
239 // We want our override to be shared by all nodes that reference
240 // that page to avoid duplication, and so standardize on having the
241 // page's owner collect all the overrides. Call getChild on the
242 // page owner with the desired font data (this will populate
243 // the page) and then reference it.
244 m_page = parentPage->owner()->getChild(fontData, pageNumber)->page();
245 } else {
246 // Get the pure page for the fallback font (at level 1 with no
247 // overrides). getRootChild will always create a page if one
248 // doesn't exist, but the page doesn't necessarily have glyphs
249 // (this pointer may be 0).
250 GlyphPage* fallbackPage = getRootChild(fontData, pageNumber)->page();
251 if (!parentPage) {
252 // When the parent has no glyphs for this page, we can easily
253 // override it just by supplying the glyphs from our font.
254 m_page = fallbackPage;
255 } else if (!fallbackPage) {
256 // When our font has no glyphs for this page, we can just reference the
257 // parent page.
258 m_page = parentPage;
259 } else {
260 // Combine the parent's glyphs and ours to form a new more complete page.
261 m_page = GlyphPage::create(this);
262
263 // Overlay the parent page on the fallback page. Check if the fallback font
264 // has added anything.
265 bool newGlyphs = false;
266 for (unsigned i = 0; i < GlyphPage::size; i++) {
267 if (parentPage->m_glyphs[i].glyph)
268 m_page->m_glyphs[i] = parentPage->m_glyphs[i];
269 else if (fallbackPage->m_glyphs[i].glyph) {
270 m_page->m_glyphs[i] = fallbackPage->m_glyphs[i];
271 newGlyphs = true;
272 } else {
273 const GlyphData data = { 0, 0 };
274 m_page->m_glyphs[i] = data;
275 }
276 }
277
278 if (!newGlyphs)
279 // We didn't override anything, so our override is just the parent page.
280 m_page = parentPage;
281 }
282 }
283 } else {
284 m_page = GlyphPage::create(this);
285 // System fallback. Initialized with the parent's page here, as individual
286 // entries may use different fonts depending on character. If the Font
287 // ever finds it needs a glyph out of the system fallback page, it will
288 // ask the system for the best font to use and fill that glyph in for us.
289 if (parentPage)
290 memcpy(m_page->m_glyphs, parentPage->m_glyphs, GlyphPage::size * sizeof(m_page->m_glyphs[0]));
291 else {
292 const GlyphData data = { 0, 0 };
293 for (unsigned i = 0; i < GlyphPage::size; i++)
294 m_page->m_glyphs[i] = data;
295 }
296 }
297 }
298
getChild(const FontData * fontData,unsigned pageNumber)299 GlyphPageTreeNode* GlyphPageTreeNode::getChild(const FontData* fontData, unsigned pageNumber)
300 {
301 ASSERT(fontData || !m_isSystemFallback);
302 ASSERT(pageNumber == m_pageNumber);
303
304 GlyphPageTreeNode* child = fontData ? m_children.get(fontData) : m_systemFallbackChild;
305 if (!child) {
306 child = new GlyphPageTreeNode;
307 child->m_parent = this;
308 child->m_level = m_level + 1;
309 if (fontData && fontData->isCustomFont()) {
310 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
311 curr->m_customFontCount++;
312 }
313
314 #ifndef NDEBUG
315 child->m_pageNumber = m_pageNumber;
316 #endif
317 if (fontData) {
318 m_children.set(fontData, child);
319 fontData->setMaxGlyphPageTreeLevel(max(fontData->maxGlyphPageTreeLevel(), child->m_level));
320 } else {
321 m_systemFallbackChild = child;
322 child->m_isSystemFallback = true;
323 }
324 child->initializePage(fontData, pageNumber);
325 }
326 return child;
327 }
328
pruneCustomFontData(const FontData * fontData)329 void GlyphPageTreeNode::pruneCustomFontData(const FontData* fontData)
330 {
331 if (!fontData || !m_customFontCount)
332 return;
333
334 // Prune any branch that contains this FontData.
335 GlyphPageTreeNode* node = m_children.get(fontData);
336 if (node) {
337 m_children.remove(fontData);
338 unsigned fontCount = node->m_customFontCount + 1;
339 delete node;
340 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
341 curr->m_customFontCount -= fontCount;
342 }
343
344 // Check any branches that remain that still have custom fonts underneath them.
345 if (!m_customFontCount)
346 return;
347 HashMap<const FontData*, GlyphPageTreeNode*>::iterator end = m_children.end();
348 for (HashMap<const FontData*, GlyphPageTreeNode*>::iterator it = m_children.begin(); it != end; ++it)
349 it->second->pruneCustomFontData(fontData);
350 }
351
pruneFontData(const SimpleFontData * fontData,unsigned level)352 void GlyphPageTreeNode::pruneFontData(const SimpleFontData* fontData, unsigned level)
353 {
354 ASSERT(fontData);
355 if (!fontData)
356 return;
357
358 // Prune any branch that contains this FontData.
359 HashMap<const FontData*, GlyphPageTreeNode*>::iterator child = m_children.find(fontData);
360 if (child == m_children.end()) {
361 // If there is no level-1 node for fontData, then there is no deeper node for it in this tree.
362 if (!level)
363 return;
364 } else {
365 GlyphPageTreeNode* node = child->second;
366 m_children.remove(fontData);
367 unsigned customFontCount = node->m_customFontCount;
368 delete node;
369 if (customFontCount) {
370 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
371 curr->m_customFontCount -= customFontCount;
372 }
373 }
374
375 level++;
376 if (level > fontData->maxGlyphPageTreeLevel())
377 return;
378
379 HashMap<const FontData*, GlyphPageTreeNode*>::iterator end = m_children.end();
380 for (HashMap<const FontData*, GlyphPageTreeNode*>::iterator it = m_children.begin(); it != end; ++it)
381 it->second->pruneFontData(fontData, level);
382 }
383
384 }
385