2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "platform/fonts/GlyphPageTreeNode.h"
33 #include "platform/fonts/SegmentedFontData.h"
34 #include "platform/fonts/SimpleFontData.h"
35 #include "platform/fonts/opentype/OpenTypeVerticalData.h"
36 #include "wtf/text/CString.h"
37 #include "wtf/text/WTFString.h"
38 #include "wtf/unicode/CharacterNames.h"
45 HashMap<int, GlyphPageTreeNode*>* GlyphPageTreeNode::roots = 0;
46 GlyphPageTreeNode* GlyphPageTreeNode::pageZeroRoot = 0;
48 GlyphPageTreeNode* GlyphPageTreeNode::getRoot(unsigned pageNumber)
50 static bool initialized;
53 roots = new HashMap<int, GlyphPageTreeNode*>;
54 pageZeroRoot = new GlyphPageTreeNode;
60 if (GlyphPageTreeNode* foundNode = roots->get(pageNumber))
63 GlyphPageTreeNode* node = new GlyphPageTreeNode;
65 node->m_pageNumber = pageNumber;
67 roots->set(pageNumber, node);
71 size_t GlyphPageTreeNode::treeGlyphPageCount()
75 HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
76 for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
77 count += it->value->pageCount();
81 count += pageZeroRoot->pageCount();
86 size_t GlyphPageTreeNode::pageCount() const
88 size_t count = m_page && m_page->owner() == this ? 1 : 0;
89 GlyphPageTreeNodeMap::const_iterator end = m_children.end();
90 for (GlyphPageTreeNodeMap::const_iterator it = m_children.begin(); it != end; ++it)
91 count += it->value->pageCount();
96 void GlyphPageTreeNode::pruneTreeCustomFontData(const FontData* fontData)
98 // Enumerate all the roots and prune any tree that contains our custom font data.
100 HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
101 for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
102 it->value->pruneCustomFontData(fontData);
106 pageZeroRoot->pruneCustomFontData(fontData);
109 void GlyphPageTreeNode::pruneTreeFontData(const SimpleFontData* fontData)
112 HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
113 for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
114 it->value->pruneFontData(fontData);
118 pageZeroRoot->pruneFontData(fontData);
121 static bool fill(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
123 #if ENABLE(SVG_FONTS)
124 if (fontData->isSVGFont())
125 return fontData->customFontData()->fillSVGGlyphPage(pageToFill, offset, length, buffer, bufferLength, fontData);
127 bool hasGlyphs = fontData->fillGlyphPage(pageToFill, offset, length, buffer, bufferLength);
128 #if ENABLE(OPENTYPE_VERTICAL)
129 if (hasGlyphs && fontData->verticalData())
130 fontData->verticalData()->substituteWithVerticalGlyphs(fontData, pageToFill, offset, length);
135 void GlyphPageTreeNode::initializePage(const FontData* fontData, unsigned pageNumber)
139 // This function must not be called for the root of the tree, because that
140 // level does not contain any glyphs.
141 ASSERT(m_level > 0 && m_parent);
143 // The parent's page will be 0 if we are level one or the parent's font data
144 // did not contain any glyphs for that page.
145 GlyphPage* parentPage = m_parent->page();
147 // NULL FontData means we're being asked for the system fallback font.
150 // Children of the root hold pure pages. These will cover only one
151 // font data's glyphs, and will have glyph index 0 if the font data does not
152 // contain the glyph.
153 unsigned start = pageNumber * GlyphPage::size;
154 UChar buffer[GlyphPage::size * 2 + 2];
155 unsigned bufferLength;
158 // Fill in a buffer with the entire "page" of characters that we want to look up glyphs for.
159 if (start < 0x10000) {
160 bufferLength = GlyphPage::size;
161 for (i = 0; i < GlyphPage::size; i++)
162 buffer[i] = start + i;
165 // Control characters must not render at all.
166 for (i = 0; i < 0x20; ++i)
167 buffer[i] = zeroWidthSpace;
168 for (i = 0x7F; i < 0xA0; i++)
169 buffer[i] = zeroWidthSpace;
170 buffer[softHyphen] = zeroWidthSpace;
172 // \n, \t, and nonbreaking space must render as a space.
173 buffer[newlineCharacter] = space;
174 buffer[characterTabulation] = space;
175 buffer[noBreakSpace] = space;
176 } else if (start == (arabicLetterMark & ~(GlyphPage::size - 1))) {
177 buffer[arabicLetterMark - start] = zeroWidthSpace;
178 } else if (start == (leftToRightMark & ~(GlyphPage::size - 1))) {
179 // LRM, RLM, LRE, RLE, ZWNJ, ZWJ, and PDF must not render at all.
180 buffer[leftToRightMark - start] = zeroWidthSpace;
181 buffer[rightToLeftMark - start] = zeroWidthSpace;
182 buffer[leftToRightEmbed - start] = zeroWidthSpace;
183 buffer[rightToLeftEmbed - start] = zeroWidthSpace;
184 buffer[leftToRightOverride - start] = zeroWidthSpace;
185 buffer[rightToLeftOverride - start] = zeroWidthSpace;
186 buffer[zeroWidthNonJoiner - start] = zeroWidthSpace;
187 buffer[zeroWidthJoiner - start] = zeroWidthSpace;
188 buffer[popDirectionalFormatting - start] = zeroWidthSpace;
189 buffer[activateArabicFormShaping - start] = zeroWidthSpace;
190 buffer[activateSymmetricSwapping - start] = zeroWidthSpace;
191 buffer[firstStrongIsolate - start] = zeroWidthSpace;
192 buffer[inhibitArabicFormShaping - start] = zeroWidthSpace;
193 buffer[inhibitSymmetricSwapping - start] = zeroWidthSpace;
194 buffer[leftToRightIsolate - start] = zeroWidthSpace;
195 buffer[nationalDigitShapes - start] = zeroWidthSpace;
196 buffer[nominalDigitShapes - start] = zeroWidthSpace;
197 buffer[popDirectionalIsolate - start] = zeroWidthSpace;
198 buffer[rightToLeftIsolate - start] = zeroWidthSpace;
199 } else if (start == (objectReplacementCharacter & ~(GlyphPage::size - 1))) {
200 // Object replacement character must not render at all.
201 buffer[objectReplacementCharacter - start] = zeroWidthSpace;
202 } else if (start == (zeroWidthNoBreakSpace & ~(GlyphPage::size - 1))) {
203 // ZWNBS/BOM must not render at all.
204 buffer[zeroWidthNoBreakSpace - start] = zeroWidthSpace;
207 bufferLength = GlyphPage::size * 2;
208 for (i = 0; i < GlyphPage::size; i++) {
210 buffer[i * 2] = U16_LEAD(c);
211 buffer[i * 2 + 1] = U16_TRAIL(c);
215 // Now that we have a buffer full of characters, we want to get back an array
216 // of glyph indices. This part involves calling into the platform-specific
217 // routine of our glyph map for actually filling in the page with the glyphs.
218 // Success is not guaranteed. For example, Times fails to fill page 260, giving glyph data
219 // for only 128 out of 256 characters.
221 if (!fontData->isSegmented()) {
222 m_page = GlyphPage::createForSingleFontData(this, toSimpleFontData(fontData));
223 haveGlyphs = fill(m_page.get(), 0, GlyphPage::size, buffer, bufferLength, toSimpleFontData(fontData));
225 m_page = GlyphPage::createForMixedFontData(this);
228 const SegmentedFontData* segmentedFontData = toSegmentedFontData(fontData);
229 for (int i = segmentedFontData->numRanges() - 1; i >= 0; i--) {
230 const FontDataRange& range = segmentedFontData->rangeAt(i);
231 // all this casting is to ensure all the parameters to min and max have the same type,
232 // to avoid ambiguous template parameter errors on Windows
233 int from = max(0, static_cast<int>(range.from()) - static_cast<int>(start));
234 int to = 1 + min(static_cast<int>(range.to()) - static_cast<int>(start), static_cast<int>(GlyphPage::size) - 1);
235 if (from >= static_cast<int>(GlyphPage::size) || to <= 0)
238 // If this is a custom font needs to be loaded, do not fill
239 // the page so that font fallback is used while loading.
240 RefPtr<CustomFontData> customData = range.fontData()->customFontData();
241 if (customData && customData->isLoadingFallback()) {
242 for (int j = from; j < to; j++) {
243 m_page->setCustomFontToLoad(j, customData.get());
249 haveGlyphs |= fill(m_page.get(), from, to - from, buffer + from * (start < 0x10000 ? 1 : 2), (to - from) * (start < 0x10000 ? 1 : 2), range.fontData().get());
255 } else if (parentPage && parentPage->owner() != m_parent) {
256 // The page we're overriding may not be owned by our parent node.
257 // This happens when our parent node provides no useful overrides
258 // and just copies the pointer to an already-existing page (see
261 // We want our override to be shared by all nodes that reference
262 // that page to avoid duplication, and so standardize on having the
263 // page's owner collect all the overrides. Call getChild on the
264 // page owner with the desired font data (this will populate
265 // the page) and then reference it.
266 m_page = parentPage->owner()->getChild(fontData, pageNumber)->page();
268 // Get the pure page for the fallback font (at level 1 with no
269 // overrides). getRootChild will always create a page if one
270 // doesn't exist, but the page doesn't necessarily have glyphs
271 // (this pointer may be 0).
272 GlyphPage* fallbackPage = getRootChild(fontData, pageNumber)->page();
274 // When the parent has no glyphs for this page, we can easily
275 // override it just by supplying the glyphs from our font.
276 m_page = fallbackPage;
277 } else if (!fallbackPage) {
278 // When our font has no glyphs for this page, we can just reference the
282 // Combine the parent's glyphs and ours to form a new more complete page.
283 m_page = GlyphPage::createForMixedFontData(this);
285 // Overlay the parent page on the fallback page. Check if the fallback font
286 // has added anything.
287 bool newGlyphs = false;
288 for (unsigned i = 0; i < GlyphPage::size; i++) {
289 if (parentPage->glyphAt(i)) {
290 m_page->setGlyphDataForIndex(i, parentPage->glyphDataForIndex(i));
291 } else if (fallbackPage->glyphAt(i)) {
292 m_page->setGlyphDataForIndex(i, fallbackPage->glyphDataForIndex(i));
296 if (parentPage->customFontToLoadAt(i)) {
297 m_page->setCustomFontToLoad(i, parentPage->customFontToLoadAt(i));
298 } else if (fallbackPage->customFontToLoadAt(i) && !parentPage->glyphAt(i)) {
299 m_page->setCustomFontToLoad(i, fallbackPage->customFontToLoadAt(i));
305 // We didn't override anything, so our override is just the parent page.
310 // System fallback. Initialized with the parent's page here, as individual
311 // entries may use different fonts depending on character. If the Font
312 // ever finds it needs a glyph out of the system fallback page, it will
313 // ask the system for the best font to use and fill that glyph in for us.
315 m_page = parentPage->createCopiedSystemFallbackPage(this);
317 m_page = GlyphPage::createForMixedFontData(this);
321 GlyphPageTreeNode* GlyphPageTreeNode::getChild(const FontData* fontData, unsigned pageNumber)
323 ASSERT(fontData || !m_isSystemFallback);
324 ASSERT(pageNumber == m_pageNumber);
326 if (GlyphPageTreeNode* foundChild = fontData ? m_children.get(fontData) : m_systemFallbackChild.get())
329 GlyphPageTreeNode* child = new GlyphPageTreeNode;
330 child->m_parent = this;
331 child->m_level = m_level + 1;
332 if (fontData && fontData->isCustomFont()) {
333 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
334 curr->m_customFontCount++;
338 child->m_pageNumber = m_pageNumber;
341 m_children.set(fontData, adoptPtr(child));
342 fontData->setMaxGlyphPageTreeLevel(max(fontData->maxGlyphPageTreeLevel(), child->m_level));
344 m_systemFallbackChild = adoptPtr(child);
345 child->m_isSystemFallback = true;
347 child->initializePage(fontData, pageNumber);
351 void GlyphPageTreeNode::pruneCustomFontData(const FontData* fontData)
353 if (!fontData || !m_customFontCount)
356 // Prune any branch that contains this FontData.
357 if (OwnPtr<GlyphPageTreeNode> node = m_children.take(fontData)) {
358 if (unsigned customFontCount = node->m_customFontCount + 1) {
359 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
360 curr->m_customFontCount -= customFontCount;
364 // Check any branches that remain that still have custom fonts underneath them.
365 if (!m_customFontCount)
368 GlyphPageTreeNodeMap::iterator end = m_children.end();
369 for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it)
370 it->value->pruneCustomFontData(fontData);
373 void GlyphPageTreeNode::pruneFontData(const SimpleFontData* fontData, unsigned level)
377 // Prune fall back child (if any) of this font.
378 if (m_systemFallbackChild && m_systemFallbackChild->m_page)
379 m_systemFallbackChild->m_page->removeFontDataFromSystemFallbackPage(fontData);
381 // Prune any branch that contains this FontData.
382 if (OwnPtr<GlyphPageTreeNode> node = m_children.take(fontData)) {
383 if (unsigned customFontCount = node->m_customFontCount) {
384 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
385 curr->m_customFontCount -= customFontCount;
390 if (level > fontData->maxGlyphPageTreeLevel())
393 GlyphPageTreeNodeMap::iterator end = m_children.end();
394 for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it)
395 it->value->pruneFontData(fontData, level);