This source file includes following definitions.
- getRoot
- treeGlyphPageCount
- pageCount
- pruneTreeCustomFontData
- pruneTreeFontData
- fill
- initializePage
- getChild
- pruneCustomFontData
- pruneFontData
- showSubtree
- showGlyphPageTrees
- showGlyphPageTree
#include "config.h"
#include "platform/fonts/GlyphPageTreeNode.h"
#include <stdio.h>
#include "platform/fonts/SegmentedFontData.h"
#include "platform/fonts/SimpleFontData.h"
#include "platform/fonts/opentype/OpenTypeVerticalData.h"
#include "wtf/text/CString.h"
#include "wtf/text/WTFString.h"
#include "wtf/unicode/CharacterNames.h"
namespace WebCore {
using std::max;
using std::min;
HashMap<int, GlyphPageTreeNode*>* GlyphPageTreeNode::roots = 0;
GlyphPageTreeNode* GlyphPageTreeNode::pageZeroRoot = 0;
GlyphPageTreeNode* GlyphPageTreeNode::getRoot(unsigned pageNumber)
{
static bool initialized;
if (!initialized) {
initialized = true;
roots = new HashMap<int, GlyphPageTreeNode*>;
pageZeroRoot = new GlyphPageTreeNode;
}
if (!pageNumber)
return pageZeroRoot;
if (GlyphPageTreeNode* foundNode = roots->get(pageNumber))
return foundNode;
GlyphPageTreeNode* node = new GlyphPageTreeNode;
#ifndef NDEBUG
node->m_pageNumber = pageNumber;
#endif
roots->set(pageNumber, node);
return node;
}
size_t GlyphPageTreeNode::treeGlyphPageCount()
{
size_t count = 0;
if (roots) {
HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
count += it->value->pageCount();
}
if (pageZeroRoot)
count += pageZeroRoot->pageCount();
return count;
}
size_t GlyphPageTreeNode::pageCount() const
{
size_t count = m_page && m_page->owner() == this ? 1 : 0;
GlyphPageTreeNodeMap::const_iterator end = m_children.end();
for (GlyphPageTreeNodeMap::const_iterator it = m_children.begin(); it != end; ++it)
count += it->value->pageCount();
return count;
}
void GlyphPageTreeNode::pruneTreeCustomFontData(const FontData* fontData)
{
if (roots) {
HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
it->value->pruneCustomFontData(fontData);
}
if (pageZeroRoot)
pageZeroRoot->pruneCustomFontData(fontData);
}
void GlyphPageTreeNode::pruneTreeFontData(const SimpleFontData* fontData)
{
if (roots) {
HashMap<int, GlyphPageTreeNode*>::iterator end = roots->end();
for (HashMap<int, GlyphPageTreeNode*>::iterator it = roots->begin(); it != end; ++it)
it->value->pruneFontData(fontData);
}
if (pageZeroRoot)
pageZeroRoot->pruneFontData(fontData);
}
static bool fill(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData* fontData)
{
#if ENABLE(SVG_FONTS)
if (fontData->isSVGFont())
return fontData->customFontData()->fillSVGGlyphPage(pageToFill, offset, length, buffer, bufferLength, fontData);
#endif
bool hasGlyphs = pageToFill->fill(offset, length, buffer, bufferLength, fontData);
#if ENABLE(OPENTYPE_VERTICAL)
if (hasGlyphs && fontData->verticalData())
fontData->verticalData()->substituteWithVerticalGlyphs(fontData, pageToFill, offset, length);
#endif
return hasGlyphs;
}
void GlyphPageTreeNode::initializePage(const FontData* fontData, unsigned pageNumber)
{
ASSERT(!m_page);
ASSERT(m_level > 0 && m_parent);
GlyphPage* parentPage = m_parent->page();
if (fontData) {
if (m_level == 1) {
unsigned start = pageNumber * GlyphPage::size;
UChar buffer[GlyphPage::size * 2 + 2];
unsigned bufferLength;
unsigned i;
if (start < 0x10000) {
bufferLength = GlyphPage::size;
for (i = 0; i < GlyphPage::size; i++)
buffer[i] = start + i;
if (start == 0) {
for (i = 0; i < 0x20; ++i)
buffer[i] = zeroWidthSpace;
for (i = 0x7F; i < 0xA0; i++)
buffer[i] = zeroWidthSpace;
buffer[softHyphen] = zeroWidthSpace;
buffer[(int)'\n'] = ' ';
buffer[(int)'\t'] = ' ';
buffer[noBreakSpace] = ' ';
} else if (start == (leftToRightMark & ~(GlyphPage::size - 1))) {
buffer[leftToRightMark - start] = zeroWidthSpace;
buffer[rightToLeftMark - start] = zeroWidthSpace;
buffer[leftToRightEmbed - start] = zeroWidthSpace;
buffer[rightToLeftEmbed - start] = zeroWidthSpace;
buffer[leftToRightOverride - start] = zeroWidthSpace;
buffer[rightToLeftOverride - start] = zeroWidthSpace;
buffer[zeroWidthNonJoiner - start] = zeroWidthSpace;
buffer[zeroWidthJoiner - start] = zeroWidthSpace;
buffer[popDirectionalFormatting - start] = zeroWidthSpace;
} else if (start == (objectReplacementCharacter & ~(GlyphPage::size - 1))) {
buffer[objectReplacementCharacter - start] = zeroWidthSpace;
} else if (start == (zeroWidthNoBreakSpace & ~(GlyphPage::size - 1))) {
buffer[zeroWidthNoBreakSpace - start] = zeroWidthSpace;
}
} else {
bufferLength = GlyphPage::size * 2;
for (i = 0; i < GlyphPage::size; i++) {
int c = i + start;
buffer[i * 2] = U16_LEAD(c);
buffer[i * 2 + 1] = U16_TRAIL(c);
}
}
bool haveGlyphs;
if (!fontData->isSegmented()) {
m_page = GlyphPage::createForSingleFontData(this, static_cast<const SimpleFontData*>(fontData));
haveGlyphs = fill(m_page.get(), 0, GlyphPage::size, buffer, bufferLength, static_cast<const SimpleFontData*>(fontData));
} else {
m_page = GlyphPage::createForMixedFontData(this);
haveGlyphs = false;
const SegmentedFontData* segmentedFontData = static_cast<const SegmentedFontData*>(fontData);
unsigned numRanges = segmentedFontData->numRanges();
bool zeroFilled = false;
RefPtr<GlyphPage> scratchPage;
GlyphPage* pageToFill = m_page.get();
for (unsigned i = 0; i < numRanges; i++) {
const FontDataRange& range = segmentedFontData->rangeAt(i);
int from = max(0, static_cast<int>(range.from()) - static_cast<int>(start));
int to = 1 + min(static_cast<int>(range.to()) - static_cast<int>(start), static_cast<int>(GlyphPage::size) - 1);
if (from < static_cast<int>(GlyphPage::size) && to > 0) {
RefPtr<CustomFontData> customData = range.fontData()->customFontData();
if (customData && customData->isLoadingFallback()) {
customData->beginLoadIfNeeded();
continue;
}
if (haveGlyphs && !scratchPage) {
scratchPage = GlyphPage::createForMixedFontData(this);
pageToFill = scratchPage.get();
}
if (!zeroFilled) {
if (from > 0 || to < static_cast<int>(GlyphPage::size)) {
for (unsigned i = 0; i < GlyphPage::size; i++)
pageToFill->setGlyphDataForIndex(i, 0, 0);
}
zeroFilled = true;
}
haveGlyphs |= fill(pageToFill, from, to - from, buffer + from * (start < 0x10000 ? 1 : 2), (to - from) * (start < 0x10000 ? 1 : 2), range.fontData().get());
if (scratchPage) {
ASSERT_WITH_SECURITY_IMPLICATION(to <= static_cast<int>(GlyphPage::size));
for (int j = from; j < to; j++) {
if (!m_page->glyphAt(j) && pageToFill->glyphAt(j))
m_page->setGlyphDataForIndex(j, pageToFill->glyphDataForIndex(j));
}
}
}
}
}
if (!haveGlyphs)
m_page = nullptr;
} else if (parentPage && parentPage->owner() != m_parent) {
m_page = parentPage->owner()->getChild(fontData, pageNumber)->page();
} else {
GlyphPage* fallbackPage = getRootChild(fontData, pageNumber)->page();
if (!parentPage) {
m_page = fallbackPage;
} else if (!fallbackPage) {
m_page = parentPage;
} else {
m_page = GlyphPage::createForMixedFontData(this);
bool newGlyphs = false;
for (unsigned i = 0; i < GlyphPage::size; i++) {
if (parentPage->glyphAt(i))
m_page->setGlyphDataForIndex(i, parentPage->glyphDataForIndex(i));
else if (fallbackPage->glyphAt(i)) {
m_page->setGlyphDataForIndex(i, fallbackPage->glyphDataForIndex(i));
newGlyphs = true;
} else
m_page->setGlyphDataForIndex(i, 0, 0);
}
if (!newGlyphs)
m_page = parentPage;
}
}
} else {
if (parentPage)
m_page = parentPage->createCopiedSystemFallbackPage(this);
else
m_page = GlyphPage::createForMixedFontData(this);
}
}
GlyphPageTreeNode* GlyphPageTreeNode::getChild(const FontData* fontData, unsigned pageNumber)
{
ASSERT(fontData || !m_isSystemFallback);
ASSERT(pageNumber == m_pageNumber);
if (GlyphPageTreeNode* foundChild = fontData ? m_children.get(fontData) : m_systemFallbackChild.get())
return foundChild;
GlyphPageTreeNode* child = new GlyphPageTreeNode;
child->m_parent = this;
child->m_level = m_level + 1;
if (fontData && fontData->isCustomFont()) {
for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
curr->m_customFontCount++;
}
#ifndef NDEBUG
child->m_pageNumber = m_pageNumber;
#endif
if (fontData) {
m_children.set(fontData, adoptPtr(child));
fontData->setMaxGlyphPageTreeLevel(max(fontData->maxGlyphPageTreeLevel(), child->m_level));
} else {
m_systemFallbackChild = adoptPtr(child);
child->m_isSystemFallback = true;
}
child->initializePage(fontData, pageNumber);
return child;
}
void GlyphPageTreeNode::pruneCustomFontData(const FontData* fontData)
{
if (!fontData || !m_customFontCount)
return;
if (OwnPtr<GlyphPageTreeNode> node = m_children.take(fontData)) {
if (unsigned customFontCount = node->m_customFontCount + 1) {
for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
curr->m_customFontCount -= customFontCount;
}
}
if (!m_customFontCount)
return;
GlyphPageTreeNodeMap::iterator end = m_children.end();
for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it)
it->value->pruneCustomFontData(fontData);
}
void GlyphPageTreeNode::pruneFontData(const SimpleFontData* fontData, unsigned level)
{
ASSERT(fontData);
if (m_systemFallbackChild && m_systemFallbackChild->m_page)
m_systemFallbackChild->m_page->removeFontDataFromSystemFallbackPage(fontData);
if (OwnPtr<GlyphPageTreeNode> node = m_children.take(fontData)) {
if (unsigned customFontCount = node->m_customFontCount) {
for (GlyphPageTreeNode* curr = this; curr; curr = curr->m_parent)
curr->m_customFontCount -= customFontCount;
}
}
level++;
if (level > fontData->maxGlyphPageTreeLevel())
return;
GlyphPageTreeNodeMap::iterator end = m_children.end();
for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it)
it->value->pruneFontData(fontData, level);
}
#ifndef NDEBUG
void GlyphPageTreeNode::showSubtree()
{
Vector<char> indent(level());
indent.fill('\t', level());
indent.append(0);
GlyphPageTreeNodeMap::iterator end = m_children.end();
for (GlyphPageTreeNodeMap::iterator it = m_children.begin(); it != end; ++it) {
printf("%s\t%p %s\n", indent.data(), it->key, it->key->description().utf8().data());
it->value->showSubtree();
}
if (m_systemFallbackChild) {
printf("%s\t* fallback\n", indent.data());
m_systemFallbackChild->showSubtree();
}
}
#endif
}
#ifndef NDEBUG
void showGlyphPageTrees()
{
printf("Page 0:\n");
showGlyphPageTree(0);
HashMap<int, WebCore::GlyphPageTreeNode*>::iterator end = WebCore::GlyphPageTreeNode::roots->end();
for (HashMap<int, WebCore::GlyphPageTreeNode*>::iterator it = WebCore::GlyphPageTreeNode::roots->begin(); it != end; ++it) {
printf("\nPage %d:\n", it->key);
showGlyphPageTree(it->key);
}
}
void showGlyphPageTree(unsigned pageNumber)
{
WebCore::GlyphPageTreeNode::getRoot(pageNumber)->showSubtree();
}
#endif