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 "platform/fonts/GlyphPageTreeNode.h" 31 32 #include <stdio.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" 39 40 namespace WebCore { 41 42 using std::max; 43 using std::min; 44 45 HashMap<int, GlyphPageTreeNode*>* GlyphPageTreeNode::roots = 0; 46 GlyphPageTreeNode* GlyphPageTreeNode::pageZeroRoot = 0; 47 48 GlyphPageTreeNode* GlyphPageTreeNode::getRoot(unsigned pageNumber) 49 { 50 static bool initialized; 51 if (!initialized) { 52 initialized = true; 53 roots = new HashMap<int, GlyphPageTreeNode*>; 54 pageZeroRoot = new GlyphPageTreeNode; 55 } 56 57 if (!pageNumber) 58 return pageZeroRoot; 59 60 if (GlyphPageTreeNode* foundNode = roots->get(pageNumber)) 61 return foundNode; 62 63 GlyphPageTreeNode* node = new GlyphPageTreeNode; 64 #ifndef NDEBUG 65 node->m_pageNumber = pageNumber; 66 #endif 67 roots->set(pageNumber, node); 68 return node; 69 } 70 71 size_t GlyphPageTreeNode::treeGlyphPageCount() 72 { 73 size_t count = 0; 74 if (roots) { 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(); 78 } 79 80 if (pageZeroRoot) 81 count += pageZeroRoot->pageCount(); 82 83 return count; 84 } 85 86 size_t GlyphPageTreeNode::pageCount() const 87 { 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(); 92 93 return count; 94 } 95 96 void GlyphPageTreeNode::pruneTreeCustomFontData(const FontData* fontData) 97 { 98 // Enumerate all the roots and prune any tree that contains our custom font data. 99 if (roots) { 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); 103 } 104 105 if (pageZeroRoot) 106 pageZeroRoot->pruneCustomFontData(fontData); 107 } 108 109 void GlyphPageTreeNode::pruneTreeFontData(const SimpleFontData* fontData) 110 { 111 if (roots) { 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); 115 } 116 117 if (pageZeroRoot) 118 pageZeroRoot->pruneFontData(fontData); 119 } 120 121 static bool fill(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData) 122 { 123 #if ENABLE(SVG_FONTS) 124 if (fontData->isSVGFont()) 125 return fontData->customFontData()->fillSVGGlyphPage(pageToFill, offset, length, buffer, bufferLength, fontData); 126 #endif 127 bool hasGlyphs = pageToFill->fill(offset, length, buffer, bufferLength, fontData); 128 #if ENABLE(OPENTYPE_VERTICAL) 129 if (hasGlyphs && fontData->verticalData()) 130 fontData->verticalData()->substituteWithVerticalGlyphs(fontData, pageToFill, offset, length); 131 #endif 132 return hasGlyphs; 133 } 134 135 void GlyphPageTreeNode::initializePage(const FontData* fontData, unsigned pageNumber) 136 { 137 ASSERT(!m_page); 138 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); 142 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(); 146 147 // NULL FontData means we're being asked for the system fallback font. 148 if (fontData) { 149 if (m_level == 1) { 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; 156 unsigned i; 157 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; 163 164 if (start == 0) { 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; 171 172 // \n, \t, and nonbreaking space must render as a space. 173 buffer[(int)'\n'] = ' '; 174 buffer[(int)'\t'] = ' '; 175 buffer[noBreakSpace] = ' '; 176 } else if (start == (leftToRightMark & ~(GlyphPage::size - 1))) { 177 // LRM, RLM, LRE, RLE, ZWNJ, ZWJ, and PDF must not render at all. 178 buffer[leftToRightMark - start] = zeroWidthSpace; 179 buffer[rightToLeftMark - start] = zeroWidthSpace; 180 buffer[leftToRightEmbed - start] = zeroWidthSpace; 181 buffer[rightToLeftEmbed - start] = zeroWidthSpace; 182 buffer[leftToRightOverride - start] = zeroWidthSpace; 183 buffer[rightToLeftOverride - start] = zeroWidthSpace; 184 buffer[zeroWidthNonJoiner - start] = zeroWidthSpace; 185 buffer[zeroWidthJoiner - start] = zeroWidthSpace; 186 buffer[popDirectionalFormatting - start] = zeroWidthSpace; 187 } else if (start == (objectReplacementCharacter & ~(GlyphPage::size - 1))) { 188 // Object replacement character must not render at all. 189 buffer[objectReplacementCharacter - start] = zeroWidthSpace; 190 } else if (start == (zeroWidthNoBreakSpace & ~(GlyphPage::size - 1))) { 191 // ZWNBS/BOM must not render at all. 192 buffer[zeroWidthNoBreakSpace - start] = zeroWidthSpace; 193 } 194 } else { 195 bufferLength = GlyphPage::size * 2; 196 for (i = 0; i < GlyphPage::size; i++) { 197 int c = i + start; 198 buffer[i * 2] = U16_LEAD(c); 199 buffer[i * 2 + 1] = U16_TRAIL(c); 200 } 201 } 202 203 // Now that we have a buffer full of characters, we want to get back an array 204 // of glyph indices. This part involves calling into the platform-specific 205 // routine of our glyph map for actually filling in the page with the glyphs. 206 // Success is not guaranteed. For example, Times fails to fill page 260, giving glyph data 207 // for only 128 out of 256 characters. 208 bool haveGlyphs; 209 if (!fontData->isSegmented()) { 210 m_page = GlyphPage::createForSingleFontData(this, static_cast<const SimpleFontData*>(fontData)); 211 haveGlyphs = fill(m_page.get(), 0, GlyphPage::size, buffer, bufferLength, static_cast<const SimpleFontData*>(fontData)); 212 } else { 213 m_page = GlyphPage::createForMixedFontData(this); 214 haveGlyphs = false; 215 216 const SegmentedFontData* segmentedFontData = static_cast<const SegmentedFontData*>(fontData); 217 unsigned numRanges = segmentedFontData->numRanges(); 218 bool zeroFilled = false; 219 RefPtr<GlyphPage> scratchPage; 220 GlyphPage* pageToFill = m_page.get(); 221 for (unsigned i = 0; i < numRanges; i++) { 222 const FontDataRange& range = segmentedFontData->rangeAt(i); 223 // all this casting is to ensure all the parameters to min and max have the same type, 224 // to avoid ambiguous template parameter errors on Windows 225 int from = max(0, static_cast<int>(range.from()) - static_cast<int>(start)); 226 int to = 1 + min(static_cast<int>(range.to()) - static_cast<int>(start), static_cast<int>(GlyphPage::size) - 1); 227 if (from < static_cast<int>(GlyphPage::size) && to > 0) { 228 // If this is a custom font needs to be loaded, kick off 229 // the load here, and do not fill the page so that 230 // font fallback is used while loading. 231 RefPtr<CustomFontData> customData = range.fontData()->customFontData(); 232 if (customData && customData->isLoadingFallback()) { 233 customData->beginLoadIfNeeded(); 234 continue; 235 } 236 237 if (haveGlyphs && !scratchPage) { 238 scratchPage = GlyphPage::createForMixedFontData(this); 239 pageToFill = scratchPage.get(); 240 } 241 242 if (!zeroFilled) { 243 if (from > 0 || to < static_cast<int>(GlyphPage::size)) { 244 for (unsigned i = 0; i < GlyphPage::size; i++) 245 pageToFill->setGlyphDataForIndex(i, 0, 0); 246 } 247 zeroFilled = true; 248 } 249 haveGlyphs |= fill(pageToFill, from, to - from, buffer + from * (start < 0x10000 ? 1 : 2), (to - from) * (start < 0x10000 ? 1 : 2), range.fontData().get()); 250 if (scratchPage) { 251 ASSERT_WITH_SECURITY_IMPLICATION(to <= static_cast<int>(GlyphPage::size)); 252 for (int j = from; j < to; j++) { 253 if (!m_page->glyphAt(j) && pageToFill->glyphAt(j)) 254 m_page->setGlyphDataForIndex(j, pageToFill->glyphDataForIndex(j)); 255 } 256 } 257 } 258 } 259 } 260 261 if (!haveGlyphs) 262 m_page = 0; 263 } else if (parentPage && parentPage->owner() != m_parent) { 264 // The page we're overriding may not be owned by our parent node. 265 // This happens when our parent node provides no useful overrides 266 // and just copies the pointer to an already-existing page (see 267 // below). 268 // 269 // We want our override to be shared by all nodes that reference 270 // that page to avoid duplication, and so standardize on having the 271 // page's owner collect all the overrides. Call getChild on the 272 // page owner with the desired font data (this will populate 273 // the page) and then reference it. 274 m_page = parentPage->owner()->getChild(fontData, pageNumber)->page(); 275 } else { 276 // Get the pure page for the fallback font (at level 1 with no 277 // overrides). getRootChild will always create a page if one 278 // doesn't exist, but the page doesn't necessarily have glyphs 279 // (this pointer may be 0). 280 GlyphPage* fallbackPage = getRootChild(fontData, pageNumber)->page(); 281 if (!parentPage) { 282 // When the parent has no glyphs for this page, we can easily 283 // override it just by supplying the glyphs from our font. 284 m_page = fallbackPage; 285 } else if (!fallbackPage) { 286 // When our font has no glyphs for this page, we can just reference the 287 // parent page. 288 m_page = parentPage; 289 } else { 290 // Combine the parent's glyphs and ours to form a new more complete page. 291 m_page = GlyphPage::createForMixedFontData(this); 292 293 // Overlay the parent page on the fallback page. Check if the fallback font 294 // has added anything. 295 bool newGlyphs = false; 296 for (unsigned i = 0; i < GlyphPage::size; i++) { 297 if (parentPage->glyphAt(i)) 298 m_page->setGlyphDataForIndex(i, parentPage->glyphDataForIndex(i)); 299 else if (fallbackPage->glyphAt(i)) { 300 m_page->setGlyphDataForIndex(i, fallbackPage->glyphDataForIndex(i)); 301 newGlyphs = true; 302 } else 303 m_page->setGlyphDataForIndex(i, 0, 0); 304 } 305 306 if (!newGlyphs) 307 // We didn't override anything, so our override is just the parent page. 308 m_page = parentPage; 309 } 310 } 311 } else { 312 // System fallback. Initialized with the parent's page here, as individual 313 // entries may use different fonts depending on character. If the Font 314 // ever finds it needs a glyph out of the system fallback page, it will 315 // ask the system for the best font to use and fill that glyph in for us. 316 if (parentPage) 317 m_page = parentPage->createCopiedSystemFallbackPage(this); 318 else 319 m_page = GlyphPage::createForMixedFontData(this); 320 } 321 } 322 323 GlyphPageTreeNode* GlyphPageTreeNode::getChild(const FontData* fontData, unsigned pageNumber) 324 { 325 ASSERT(fontData || !m_isSystemFallback); 326 ASSERT(pageNumber == m_pageNumber); 327 328 if (GlyphPageTreeNode* foundChild = fontData ? m_children.get(fontData) : m_systemFallbackChild.get()) 329 return foundChild; 330 331 GlyphPageTreeNode* child = new GlyphPageTreeNode; 332 child->m_parent = this; 333 child->m_level = m_level + 1; 334 if (fontData && fontData->isCustomFont()) { 335 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent) 336 curr->m_customFontCount++; 337 } 338 339 #ifndef NDEBUG 340 child->m_pageNumber = m_pageNumber; 341 #endif 342 if (fontData) { 343 m_children.set(fontData, adoptPtr(child)); 344 fontData->setMaxGlyphPageTreeLevel(max(fontData->maxGlyphPageTreeLevel(), child->m_level)); 345 } else { 346 m_systemFallbackChild = adoptPtr(child); 347 child->m_isSystemFallback = true; 348 } 349 child->initializePage(fontData, pageNumber); 350 return child; 351 } 352 353 void GlyphPageTreeNode::pruneCustomFontData(const FontData* fontData) 354 { 355 if (!fontData || !m_customFontCount) 356 return; 357 358 // Prune any branch that contains this FontData. 359 if (OwnPtr<GlyphPageTreeNode> node = m_children.take(fontData)) { 360 if (unsigned customFontCount = node->m_customFontCount + 1) { 361 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent) 362 curr->m_customFontCount -= customFontCount; 363 } 364 } 365 366 // Check any branches that remain that still have custom fonts underneath them. 367 if (!m_customFontCount) 368 return; 369 370 GlyphPageTreeNodeMap::iterator end = m_children.end(); 371 for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it) 372 it->value->pruneCustomFontData(fontData); 373 } 374 375 void GlyphPageTreeNode::pruneFontData(const SimpleFontData* fontData, unsigned level) 376 { 377 ASSERT(fontData); 378 379 // Prune fall back child (if any) of this font. 380 if (m_systemFallbackChild && m_systemFallbackChild->m_page) 381 m_systemFallbackChild->m_page->removeFontDataFromSystemFallbackPage(fontData); 382 383 // Prune any branch that contains this FontData. 384 if (OwnPtr<GlyphPageTreeNode> node = m_children.take(fontData)) { 385 if (unsigned customFontCount = node->m_customFontCount) { 386 for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent) 387 curr->m_customFontCount -= customFontCount; 388 } 389 } 390 391 level++; 392 if (level > fontData->maxGlyphPageTreeLevel()) 393 return; 394 395 GlyphPageTreeNodeMap::iterator end = m_children.end(); 396 for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it) 397 it->value->pruneFontData(fontData, level); 398 } 399 400 #ifndef NDEBUG 401 void GlyphPageTreeNode::showSubtree() 402 { 403 Vector<char> indent(level()); 404 indent.fill('\t', level()); 405 indent.append(0); 406 407 GlyphPageTreeNodeMap::iterator end = m_children.end(); 408 for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it) { 409 printf("%s\t%p %s\n", indent.data(), it->key, it->key->description().utf8().data()); 410 it->value->showSubtree(); 411 } 412 if (m_systemFallbackChild) { 413 printf("%s\t* fallback\n", indent.data()); 414 m_systemFallbackChild->showSubtree(); 415 } 416 } 417 #endif 418 419 } 420 421 #ifndef NDEBUG 422 void showGlyphPageTrees() 423 { 424 printf("Page 0:\n"); 425 showGlyphPageTree(0); 426 HashMap<int, WebCore::GlyphPageTreeNode*>::iterator end = WebCore::GlyphPageTreeNode::roots->end(); 427 for (HashMap<int, WebCore::GlyphPageTreeNode*>::iterator it = WebCore::GlyphPageTreeNode::roots->begin(); it != end; ++it) { 428 printf("\nPage %d:\n", it->key); 429 showGlyphPageTree(it->key); 430 } 431 } 432 433 void showGlyphPageTree(unsigned pageNumber) 434 { 435 WebCore::GlyphPageTreeNode::getRoot(pageNumber)->showSubtree(); 436 } 437 #endif 438