initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / GlyphPage.h
1 /*
2  * Copyright (C) 2006, 2007, 2008 Apple Inc. All rights reserved.
3  * Copyright (C) Research In Motion Limited 2011. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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. 
17  *
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.
28  */
29
30 #ifndef GlyphPage_h
31 #define GlyphPage_h
32
33 #include "Glyph.h"
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/unicode/Unicode.h>
37
38 namespace WebCore {
39
40 class SimpleFontData;
41 class GlyphPageTreeNode;
42
43 // Holds the glyph index and the corresponding SimpleFontData information for a given
44 // character.
45 struct GlyphData {
46     GlyphData(Glyph g = 0, const SimpleFontData* f = 0)
47         : glyph(g)
48         , fontData(f)
49     {
50     }
51     Glyph glyph;
52     const SimpleFontData* fontData;
53 };
54
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.
58 //
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> {
64 public:
65     static PassRefPtr<GlyphPage> create(GlyphPageTreeNode* owner)
66     {
67         return adoptRef(new GlyphPage(owner));
68     }
69
70     static const size_t size = 256; // Covers Latin-1 in a single page.
71
72     unsigned indexForCharacter(UChar32 c) const { return c % size; }
73     GlyphData glyphDataForCharacter(UChar32 c) const
74     {
75         unsigned index = indexForCharacter(c);
76         return GlyphData(m_glyphs[index], m_glyphFontData[index]);
77     }
78
79     GlyphData glyphDataForIndex(unsigned index) const
80     {
81         ASSERT(index < size);
82         return GlyphData(m_glyphs[index], m_glyphFontData[index]);
83     }
84
85     Glyph glyphAt(unsigned index) const
86     {
87         ASSERT(index < size);
88         return m_glyphs[index];
89     }
90
91     const SimpleFontData* fontDataForCharacter(UChar32 c) const
92     {
93         return m_glyphFontData[indexForCharacter(c)];
94     }
95
96     void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f)
97     {
98         setGlyphDataForIndex(indexForCharacter(c), g, f);
99     }
100
101     void setGlyphDataForIndex(unsigned index, Glyph g, const SimpleFontData* f)
102     {
103         ASSERT(index < size);
104         m_glyphs[index] = g;
105         m_glyphFontData[index] = f;
106     }
107
108     void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData)
109     {
110         setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData);
111     }
112     
113     void copyFrom(const GlyphPage& other)
114     {
115         memcpy(m_glyphs, other.m_glyphs, sizeof(m_glyphs));
116         memcpy(m_glyphFontData, other.m_glyphFontData, sizeof(m_glyphFontData));
117     }
118
119     void clear()
120     {
121         memset(m_glyphs, 0, sizeof(m_glyphs));
122         memset(m_glyphFontData, 0, sizeof(m_glyphFontData));
123     }
124
125     void clearForFontData(const SimpleFontData* fontData)
126     {
127         for (size_t i = 0; i < size; ++i) {
128             if (m_glyphFontData[i] == fontData) {
129                 m_glyphs[i] = 0;
130                 m_glyphFontData[i] = 0;
131             }
132         }
133     }
134
135     GlyphPageTreeNode* owner() const { return m_owner; }
136
137     // Implemented by the platform.
138     bool fill(unsigned offset, unsigned length, UChar* characterBuffer, unsigned bufferLength, const SimpleFontData*);
139
140 private:
141     GlyphPage(GlyphPageTreeNode* owner)
142         : m_owner(owner)
143     {
144     }
145
146     // Separate arrays, rather than array of GlyphData, to save space.
147     Glyph m_glyphs[size];
148     const SimpleFontData* m_glyphFontData[size];
149
150     GlyphPageTreeNode* m_owner;
151 };
152
153 } // namespace WebCore
154
155 #endif // GlyphPageTreeNode_h