OLD | NEW |
| (Empty) |
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/PlatformExport.h" | |
34 #include "platform/fonts/CustomFontData.h" | |
35 #include "platform/fonts/Glyph.h" | |
36 #include "wtf/Allocator.h" | |
37 #include "wtf/PassRefPtr.h" | |
38 #include "wtf/RefCounted.h" | |
39 #include "wtf/RefPtr.h" | |
40 #include "wtf/allocator/Partitions.h" | |
41 #include "wtf/text/Unicode.h" | |
42 #include <string.h> | |
43 | |
44 namespace blink { | |
45 | |
46 class SimpleFontData; | |
47 class GlyphPageTreeNodeBase; | |
48 | |
49 // Holds the glyph index and the corresponding SimpleFontData information for a | |
50 // given character. | |
51 struct GlyphData { | |
52 DISALLOW_NEW(); | |
53 GlyphData(Glyph g = 0, const SimpleFontData* f = 0) : glyph(g), fontData(f) {} | |
54 Glyph glyph; | |
55 const SimpleFontData* fontData; | |
56 }; | |
57 | |
58 #if COMPILER(MSVC) | |
59 #pragma warning(push) | |
60 #pragma warning( \ | |
61 disable : 4200) // Disable "zero-sized array in struct/union" warning | |
62 #endif | |
63 | |
64 // A GlyphPage contains a fixed-size set of GlyphData mappings for a contiguous | |
65 // range of characters in the Unicode code space. GlyphPages are indexed | |
66 // starting from 0 and incrementing for each 256 glyphs. | |
67 // | |
68 // One page may actually include glyphs from other fonts if the characters are | |
69 // missing in the primary font. It is owned by exactly one GlyphPageTreeNode, | |
70 // although multiple nodes may reference it as their "page" if they are supposed | |
71 // to be overriding the parent's node, but provide no additional information. | |
72 class PLATFORM_EXPORT GlyphPage : public RefCounted<GlyphPage> { | |
73 public: | |
74 static PassRefPtr<GlyphPage> createForMixedFontData( | |
75 GlyphPageTreeNodeBase* owner) { | |
76 void* slot = WTF::Partitions::fastMalloc( | |
77 sizeof(GlyphPage) + sizeof(SimpleFontData*) * GlyphPage::size, | |
78 WTF_HEAP_PROFILER_TYPE_NAME(GlyphPage)); | |
79 return adoptRef(new (slot) GlyphPage(owner)); | |
80 } | |
81 | |
82 static PassRefPtr<GlyphPage> createForSingleFontData( | |
83 GlyphPageTreeNodeBase* owner, | |
84 const SimpleFontData* fontData) { | |
85 ASSERT(fontData); | |
86 return adoptRef(new GlyphPage(owner, fontData)); | |
87 } | |
88 | |
89 PassRefPtr<GlyphPage> createCopiedSystemFallbackPage( | |
90 GlyphPageTreeNodeBase* owner) const { | |
91 RefPtr<GlyphPage> page = GlyphPage::createForMixedFontData(owner); | |
92 memcpy(page->m_glyphs, m_glyphs, sizeof(m_glyphs)); | |
93 if (hasPerGlyphFontData()) | |
94 memcpy(page->m_perGlyphFontData, m_perGlyphFontData, | |
95 sizeof(SimpleFontData*) * GlyphPage::size); | |
96 else { | |
97 for (size_t i = 0; i < GlyphPage::size; ++i) { | |
98 page->m_perGlyphFontData[i] = m_glyphs[i] ? m_fontDataForAllGlyphs : 0; | |
99 } | |
100 } | |
101 page->m_customFontToLoad = m_customFontToLoad; | |
102 return page.release(); | |
103 } | |
104 | |
105 ~GlyphPage() {} | |
106 | |
107 static const unsigned char sizeBits = 8; | |
108 static const size_t size = | |
109 (1 << GlyphPage::sizeBits); // Covers Latin-1 in a single page. | |
110 static unsigned indexForCharacter(UChar32 c) { return c & 0xFF; } | |
111 | |
112 ALWAYS_INLINE GlyphData glyphDataForCharacter(UChar32 c) const { | |
113 unsigned index = indexForCharacter(c); | |
114 if (const CustomFontData* customData = customFontToLoadAt(index)) | |
115 customData->beginLoadIfNeeded(); | |
116 return glyphDataForIndex(index); | |
117 } | |
118 | |
119 ALWAYS_INLINE GlyphData glyphDataForIndex(unsigned index) const { | |
120 ASSERT_WITH_SECURITY_IMPLICATION(index < size); | |
121 Glyph glyph = m_glyphs[index]; | |
122 if (hasPerGlyphFontData()) | |
123 return GlyphData(glyph, m_perGlyphFontData[index]); | |
124 return GlyphData(glyph, glyph ? m_fontDataForAllGlyphs : 0); | |
125 } | |
126 | |
127 ALWAYS_INLINE Glyph glyphForCharacter(UChar32 c) const { | |
128 return glyphAt(indexForCharacter(c)); | |
129 } | |
130 | |
131 ALWAYS_INLINE Glyph glyphAt(unsigned index) const { | |
132 ASSERT_WITH_SECURITY_IMPLICATION(index < size); | |
133 return m_glyphs[index]; | |
134 } | |
135 | |
136 void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f) { | |
137 setGlyphDataForIndex(indexForCharacter(c), g, f); | |
138 } | |
139 | |
140 void setGlyphDataForIndex(unsigned index, | |
141 Glyph glyph, | |
142 const SimpleFontData* fontData) { | |
143 ASSERT_WITH_SECURITY_IMPLICATION(index < size); | |
144 m_glyphs[index] = glyph; | |
145 setCustomFontToLoad(index, 0); | |
146 | |
147 if (hasPerGlyphFontData()) { | |
148 m_perGlyphFontData[index] = fontData; | |
149 } else { | |
150 // A single-font GlyphPage already assigned m_fontDataForAllGlyphs in the | |
151 // constructor. | |
152 ASSERT(!glyph || fontData == m_fontDataForAllGlyphs); | |
153 } | |
154 } | |
155 | |
156 void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData) { | |
157 setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData); | |
158 } | |
159 | |
160 const CustomFontData* customFontToLoadAt(unsigned index) const { | |
161 ASSERT_WITH_SECURITY_IMPLICATION(index < size); | |
162 return m_customFontToLoad ? m_customFontToLoad->at(index) : 0; | |
163 } | |
164 | |
165 void setCustomFontToLoad(unsigned index, | |
166 const CustomFontData* customFontToLoad) { | |
167 if (!m_customFontToLoad) { | |
168 if (!customFontToLoad) | |
169 return; | |
170 m_customFontToLoad = CustomDataPage::create(); | |
171 } | |
172 ASSERT_WITH_SECURITY_IMPLICATION(index < size); | |
173 m_customFontToLoad->set(index, customFontToLoad); | |
174 } | |
175 | |
176 void removePerGlyphFontData(const SimpleFontData* fontData) { | |
177 // This method should only be called on the mixed page, which is never | |
178 // single-font. | |
179 ASSERT(hasPerGlyphFontData()); | |
180 for (size_t i = 0; i < size; ++i) { | |
181 if (m_perGlyphFontData[i] == fontData) { | |
182 m_glyphs[i] = 0; | |
183 m_perGlyphFontData[i] = 0; | |
184 } | |
185 } | |
186 } | |
187 | |
188 GlyphPageTreeNodeBase* owner() const { return m_owner; } | |
189 | |
190 bool hasPerGlyphFontData() const { return !m_fontDataForAllGlyphs; } | |
191 | |
192 private: | |
193 explicit GlyphPage(GlyphPageTreeNodeBase* owner, | |
194 const SimpleFontData* fontDataForAllGlyphs = 0) | |
195 : m_fontDataForAllGlyphs(fontDataForAllGlyphs), m_owner(owner) { | |
196 memset(m_glyphs, 0, sizeof(m_glyphs)); | |
197 if (hasPerGlyphFontData()) | |
198 memset(m_perGlyphFontData, 0, sizeof(SimpleFontData*) * GlyphPage::size); | |
199 } | |
200 | |
201 class CustomDataPage : public RefCounted<CustomDataPage> { | |
202 public: | |
203 static RefPtr<CustomDataPage> create() { | |
204 return adoptRef(new CustomDataPage()); | |
205 } | |
206 const CustomFontData* at(size_t index) const { return m_customData[index]; } | |
207 void set(size_t index, const CustomFontData* data) { | |
208 m_customData[index] = data; | |
209 } | |
210 | |
211 private: | |
212 CustomDataPage() { memset(m_customData, 0, sizeof(m_customData)); } | |
213 const CustomFontData* m_customData[size]; | |
214 }; | |
215 | |
216 const SimpleFontData* m_fontDataForAllGlyphs; | |
217 GlyphPageTreeNodeBase* m_owner; | |
218 RefPtr<CustomDataPage> m_customFontToLoad; | |
219 Glyph m_glyphs[size]; | |
220 | |
221 // NOTE: This array has (GlyphPage::size) elements if m_fontDataForAllGlyphs | |
222 // is null. | |
223 const SimpleFontData* m_perGlyphFontData[0]; | |
224 }; | |
225 | |
226 #if COMPILER(MSVC) | |
227 #pragma warning(pop) | |
228 #endif | |
229 | |
230 } // namespace blink | |
231 | |
232 #endif // GlyphPage_h | |
OLD | NEW |