GlyphPageTreeNode.h [plain text]
#ifndef GlyphPageTreeNode_h
#define GlyphPageTreeNode_h
#include <string.h>
#include <wtf/HashMap.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/unicode/Unicode.h>
#ifndef NDEBUG
void showGlyphPageTrees();
void showGlyphPageTree(unsigned pageNumber);
#endif
namespace WebCore {
class FontData;
class GlyphPageTreeNode;
class SimpleFontData;
typedef unsigned short Glyph;
struct GlyphData {
GlyphData(Glyph g = 0, const SimpleFontData* f = 0)
: glyph(g)
, fontData(f)
{
}
Glyph glyph;
const SimpleFontData* fontData;
};
class GlyphPage : public RefCounted<GlyphPage> {
public:
static PassRefPtr<GlyphPage> create(GlyphPageTreeNode* owner)
{
return adoptRef(new GlyphPage(owner));
}
static const size_t size = 256;
unsigned indexForCharacter(UChar32 c) const { return c % size; }
GlyphData glyphDataForCharacter(UChar32 c) const
{
unsigned index = indexForCharacter(c);
return GlyphData(m_glyphs[index], m_glyphFontData[index]);
}
GlyphData glyphDataForIndex(unsigned index) const
{
ASSERT(index < size);
return GlyphData(m_glyphs[index], m_glyphFontData[index]);
}
Glyph glyphAt(unsigned index) const
{
ASSERT(index < size);
return m_glyphs[index];
}
const SimpleFontData* fontDataForCharacter(UChar32 c) const
{
return m_glyphFontData[indexForCharacter(c)];
}
void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f)
{
setGlyphDataForIndex(indexForCharacter(c), g, f);
}
void setGlyphDataForIndex(unsigned index, Glyph g, const SimpleFontData* f)
{
ASSERT(index < size);
m_glyphs[index] = g;
m_glyphFontData[index] = f;
}
void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData)
{
setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData);
}
void copyFrom(const GlyphPage& other)
{
memcpy(m_glyphs, other.m_glyphs, sizeof(m_glyphs));
memcpy(m_glyphFontData, other.m_glyphFontData, sizeof(m_glyphFontData));
}
void clear()
{
memset(m_glyphs, 0, sizeof(m_glyphs));
memset(m_glyphFontData, 0, sizeof(m_glyphFontData));
}
GlyphPageTreeNode* owner() const { return m_owner; }
bool fill(unsigned offset, unsigned length, UChar* characterBuffer, unsigned bufferLength, const SimpleFontData*);
private:
GlyphPage(GlyphPageTreeNode* owner)
: m_owner(owner)
{
}
Glyph m_glyphs[size];
const SimpleFontData* m_glyphFontData[size];
GlyphPageTreeNode* m_owner;
};
class GlyphPageTreeNode {
public:
GlyphPageTreeNode()
: m_parent(0)
, m_level(0)
, m_isSystemFallback(false)
, m_customFontCount(0)
, m_systemFallbackChild(0)
#ifndef NDEBUG
, m_pageNumber(0)
#endif
{
}
~GlyphPageTreeNode();
static HashMap<int, GlyphPageTreeNode*>* roots;
static GlyphPageTreeNode* pageZeroRoot;
static GlyphPageTreeNode* getRootChild(const FontData* fontData, unsigned pageNumber)
{
return getRoot(pageNumber)->getChild(fontData, pageNumber);
}
static void pruneTreeCustomFontData(const FontData*);
static void pruneTreeFontData(const SimpleFontData*);
void pruneCustomFontData(const FontData*);
void pruneFontData(const SimpleFontData*, unsigned level = 0);
GlyphPageTreeNode* parent() const { return m_parent; }
GlyphPageTreeNode* getChild(const FontData*, unsigned pageNumber);
GlyphPage* page() const { return m_page.get(); }
unsigned level() const { return m_level; }
bool isSystemFallback() const { return m_isSystemFallback; }
static size_t treeGlyphPageCount();
size_t pageCount() const;
private:
static GlyphPageTreeNode* getRoot(unsigned pageNumber);
void initializePage(const FontData*, unsigned pageNumber);
#ifndef NDEBUG
void showSubtree();
#endif
GlyphPageTreeNode* m_parent;
RefPtr<GlyphPage> m_page;
unsigned m_level : 31;
bool m_isSystemFallback : 1;
unsigned m_customFontCount;
HashMap<const FontData*, GlyphPageTreeNode*> m_children;
GlyphPageTreeNode* m_systemFallbackChild;
#ifndef NDEBUG
unsigned m_pageNumber;
friend void ::showGlyphPageTree(unsigned pageNumber);
#endif
};
}
#endif // GlyphPageTreeNode_h