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