2 * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15 * its contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/unicode/Unicode.h>
41 class GlyphPageTreeNode;
43 // Holds the glyph index and the corresponding SimpleFontData information for a given
46 GlyphData(Glyph g = 0, const SimpleFontData* f = 0)
52 const SimpleFontData* fontData;
55 // A GlyphPage contains a fixed-size set of GlyphData mappings for a contiguous
56 // range of characters in the Unicode code space. GlyphPages are indexed
57 // starting from 0 and incrementing for each 256 glyphs.
59 // One page may actually include glyphs from other fonts if the characters are
60 // missing in the primary font. It is owned by exactly one GlyphPageTreeNode,
61 // although multiple nodes may reference it as their "page" if they are supposed
62 // to be overriding the parent's node, but provide no additional information.
63 class GlyphPage : public RefCounted<GlyphPage> {
65 static PassRefPtr<GlyphPage> create(GlyphPageTreeNode* owner)
67 return adoptRef(new GlyphPage(owner));
70 static const size_t size = 256; // Covers Latin-1 in a single page.
72 unsigned indexForCharacter(UChar32 c) const { return c % size; }
73 GlyphData glyphDataForCharacter(UChar32 c) const
75 unsigned index = indexForCharacter(c);
76 return GlyphData(m_glyphs[index], m_glyphFontData[index]);
79 GlyphData glyphDataForIndex(unsigned index) const
82 return GlyphData(m_glyphs[index], m_glyphFontData[index]);
85 Glyph glyphAt(unsigned index) const
88 return m_glyphs[index];
91 const SimpleFontData* fontDataForCharacter(UChar32 c) const
93 return m_glyphFontData[indexForCharacter(c)];
96 void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f)
98 setGlyphDataForIndex(indexForCharacter(c), g, f);
101 void setGlyphDataForIndex(unsigned index, Glyph g, const SimpleFontData* f)
103 ASSERT(index < size);
105 m_glyphFontData[index] = f;
108 void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData)
110 setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData);
113 void copyFrom(const GlyphPage& other)
115 memcpy(m_glyphs, other.m_glyphs, sizeof(m_glyphs));
116 memcpy(m_glyphFontData, other.m_glyphFontData, sizeof(m_glyphFontData));
121 memset(m_glyphs, 0, sizeof(m_glyphs));
122 memset(m_glyphFontData, 0, sizeof(m_glyphFontData));
125 void clearForFontData(const SimpleFontData* fontData)
127 for (size_t i = 0; i < size; ++i) {
128 if (m_glyphFontData[i] == fontData) {
130 m_glyphFontData[i] = 0;
135 GlyphPageTreeNode* owner() const { return m_owner; }
137 // Implemented by the platform.
138 bool fill(unsigned offset, unsigned length, UChar* characterBuffer, unsigned bufferLength, const SimpleFontData*);
141 GlyphPage(GlyphPageTreeNode* owner)
146 // Separate arrays, rather than array of GlyphData, to save space.
147 Glyph m_glyphs[size];
148 const SimpleFontData* m_glyphFontData[size];
150 GlyphPageTreeNode* m_owner;
153 } // namespace WebCore
155 #endif // GlyphPageTreeNode_h