- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / GlyphPage.h
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2013 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 "platform/fonts/Glyph.h"
34 #include <string.h>
35 #include "wtf/PassRefPtr.h"
36 #include "wtf/RefCounted.h"
37 #include "wtf/RefPtr.h"
38 #include "wtf/unicode/Unicode.h"
39
40 namespace WebCore {
41
42 class SimpleFontData;
43 class GlyphPageTreeNode;
44
45 // Holds the glyph index and the corresponding SimpleFontData information for a given
46 // character.
47 struct GlyphData {
48     GlyphData(Glyph g = 0, const SimpleFontData* f = 0)
49         : glyph(g)
50         , fontData(f)
51     {
52     }
53     Glyph glyph;
54     const SimpleFontData* fontData;
55 };
56
57 #if COMPILER(MSVC)
58 #pragma warning(push)
59 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
60 #endif
61
62 // A GlyphPage contains a fixed-size set of GlyphData mappings for a contiguous
63 // range of characters in the Unicode code space. GlyphPages are indexed
64 // starting from 0 and incrementing for each 256 glyphs.
65 //
66 // One page may actually include glyphs from other fonts if the characters are
67 // missing in the primary font. It is owned by exactly one GlyphPageTreeNode,
68 // although multiple nodes may reference it as their "page" if they are supposed
69 // to be overriding the parent's node, but provide no additional information.
70 class GlyphPage : public RefCounted<GlyphPage> {
71 public:
72     static PassRefPtr<GlyphPage> createForMixedFontData(GlyphPageTreeNode* owner)
73     {
74         void* slot = fastMalloc(sizeof(GlyphPage) + sizeof(SimpleFontData*) * GlyphPage::size);
75         return adoptRef(new (slot) GlyphPage(owner));
76     }
77
78     static PassRefPtr<GlyphPage> createForSingleFontData(GlyphPageTreeNode* owner, const SimpleFontData* fontData)
79     {
80         ASSERT(fontData);
81         return adoptRef(new GlyphPage(owner, fontData));
82     }
83
84     PassRefPtr<GlyphPage> createCopiedSystemFallbackPage(GlyphPageTreeNode* owner) const
85     {
86         RefPtr<GlyphPage> page = GlyphPage::createForMixedFontData(owner);
87         memcpy(page->m_glyphs, m_glyphs, sizeof(m_glyphs));
88         if (hasPerGlyphFontData())
89             memcpy(page->m_perGlyphFontData, m_perGlyphFontData, sizeof(SimpleFontData*) * GlyphPage::size);
90         else {
91             for (size_t i = 0; i < GlyphPage::size; ++i) {
92                 page->m_perGlyphFontData[i] = m_glyphs[i] ? m_fontDataForAllGlyphs : 0;
93             }
94         }
95         return page.release();
96     }
97
98     ~GlyphPage() { }
99
100     static const size_t size = 256; // Covers Latin-1 in a single page.
101     static unsigned indexForCharacter(UChar32 c) { return c % GlyphPage::size; }
102
103     ALWAYS_INLINE GlyphData glyphDataForCharacter(UChar32 c) const
104     {
105         return glyphDataForIndex(indexForCharacter(c));
106     }
107
108     ALWAYS_INLINE GlyphData glyphDataForIndex(unsigned index) const
109     {
110         ASSERT_WITH_SECURITY_IMPLICATION(index < size);
111         Glyph glyph = m_glyphs[index];
112         if (hasPerGlyphFontData())
113             return GlyphData(glyph, m_perGlyphFontData[index]);
114         return GlyphData(glyph, glyph ? m_fontDataForAllGlyphs : 0);
115     }
116
117     ALWAYS_INLINE Glyph glyphForCharacter(UChar32 c) const
118     {
119         return glyphAt(indexForCharacter(c));
120     }
121
122     ALWAYS_INLINE Glyph glyphAt(unsigned index) const
123     {
124         ASSERT_WITH_SECURITY_IMPLICATION(index < size);
125         return m_glyphs[index];
126     }
127
128     ALWAYS_INLINE const SimpleFontData* fontDataForCharacter(UChar32 c) const
129     {
130         unsigned index = indexForCharacter(c);
131         if (hasPerGlyphFontData())
132             return m_perGlyphFontData[index];
133         return m_glyphs[index] ? m_fontDataForAllGlyphs : 0;
134     }
135
136     void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f)
137     {
138         setGlyphDataForIndex(indexForCharacter(c), g, f);
139     }
140
141     void setGlyphDataForIndex(unsigned index, Glyph glyph, const SimpleFontData* fontData)
142     {
143         ASSERT_WITH_SECURITY_IMPLICATION(index < size);
144         m_glyphs[index] = glyph;
145
146         // GlyphPage getters will always return a null SimpleFontData* for glyph #0 if there's no per-glyph font array.
147         if (hasPerGlyphFontData()) {
148             m_perGlyphFontData[index] = glyph ? fontData : 0;
149             return;
150         }
151
152         // A single-font GlyphPage already assigned m_fontDataForAllGlyphs in the constructor.
153         ASSERT(!glyph || fontData == m_fontDataForAllGlyphs);
154     }
155
156     void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData)
157     {
158         setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData);
159     }
160
161     void removeFontDataFromSystemFallbackPage(const SimpleFontData* fontData)
162     {
163         // This method should only be called on the system fallback page, which is never single-font.
164         ASSERT(hasPerGlyphFontData());
165         for (size_t i = 0; i < size; ++i) {
166             if (m_perGlyphFontData[i] == fontData) {
167                 m_glyphs[i] = 0;
168                 m_perGlyphFontData[i] = 0;
169             }
170         }
171     }
172
173     GlyphPageTreeNode* owner() const { return m_owner; }
174
175     // Implemented by the platform.
176     bool fill(unsigned offset, unsigned length, UChar* characterBuffer, unsigned bufferLength, const SimpleFontData*);
177
178 private:
179     explicit GlyphPage(GlyphPageTreeNode* owner, const SimpleFontData* fontDataForAllGlyphs = 0)
180         : m_fontDataForAllGlyphs(fontDataForAllGlyphs)
181         , m_owner(owner)
182     {
183         memset(m_glyphs, 0, sizeof(m_glyphs));
184         if (hasPerGlyphFontData())
185             memset(m_perGlyphFontData, 0, sizeof(SimpleFontData*) * GlyphPage::size);
186     }
187
188     bool hasPerGlyphFontData() const { return !m_fontDataForAllGlyphs; }
189
190     const SimpleFontData* m_fontDataForAllGlyphs;
191     GlyphPageTreeNode* m_owner;
192     Glyph m_glyphs[size];
193
194     // NOTE: This array has (GlyphPage::size) elements if m_fontDataForAllGlyphs is null.
195     const SimpleFontData* m_perGlyphFontData[0];
196 };
197
198 #if COMPILER(MSVC)
199 #pragma warning(pop)
200 #endif
201
202 } // namespace WebCore
203
204 #endif // GlyphPage_h