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 <string.h> 37 #include "wtf/PassRefPtr.h" 38 #include "wtf/RefCounted.h" 39 #include "wtf/RefPtr.h" 40 #include "wtf/unicode/Unicode.h" 41 42 namespace WebCore { 43 44 class SimpleFontData; 45 class GlyphPageTreeNode; 46 47 // Holds the glyph index and the corresponding SimpleFontData information for a given 48 // character. 49 struct GlyphData { 50 GlyphData(Glyph g = 0, const SimpleFontData* f = 0) glyphGlyphData51 : glyph(g) 52 , fontData(f) 53 { 54 } 55 Glyph glyph; 56 const SimpleFontData* fontData; 57 }; 58 59 #if COMPILER(MSVC) 60 #pragma warning(push) 61 #pragma warning(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: createForMixedFontData(GlyphPageTreeNode * owner)74 static PassRefPtr<GlyphPage> createForMixedFontData(GlyphPageTreeNode* owner) 75 { 76 void* slot = fastMalloc(sizeof(GlyphPage) + sizeof(SimpleFontData*) * GlyphPage::size); 77 return adoptRef(new (slot) GlyphPage(owner)); 78 } 79 createForSingleFontData(GlyphPageTreeNode * owner,const SimpleFontData * fontData)80 static PassRefPtr<GlyphPage> createForSingleFontData(GlyphPageTreeNode* owner, const SimpleFontData* fontData) 81 { 82 ASSERT(fontData); 83 return adoptRef(new GlyphPage(owner, fontData)); 84 } 85 createCopiedSystemFallbackPage(GlyphPageTreeNode * owner)86 PassRefPtr<GlyphPage> createCopiedSystemFallbackPage(GlyphPageTreeNode* owner) const 87 { 88 RefPtr<GlyphPage> page = GlyphPage::createForMixedFontData(owner); 89 memcpy(page->m_glyphs, m_glyphs, sizeof(m_glyphs)); 90 if (hasPerGlyphFontData()) 91 memcpy(page->m_perGlyphFontData, m_perGlyphFontData, sizeof(SimpleFontData*) * GlyphPage::size); 92 else { 93 for (size_t i = 0; i < GlyphPage::size; ++i) { 94 page->m_perGlyphFontData[i] = m_glyphs[i] ? m_fontDataForAllGlyphs : 0; 95 } 96 } 97 page->m_customFontToLoad = m_customFontToLoad; 98 return page.release(); 99 } 100 ~GlyphPage()101 ~GlyphPage() { } 102 103 static const unsigned char sizeBits = 8; 104 static const size_t size = (1 << GlyphPage::sizeBits); // Covers Latin-1 in a single page. indexForCharacter(UChar32 c)105 static unsigned indexForCharacter(UChar32 c) { return c & 0xFF; } 106 glyphDataForCharacter(UChar32 c)107 ALWAYS_INLINE GlyphData glyphDataForCharacter(UChar32 c) const 108 { 109 unsigned index = indexForCharacter(c); 110 if (const CustomFontData* customData = customFontToLoadAt(index)) 111 customData->beginLoadIfNeeded(); 112 return glyphDataForIndex(index); 113 } 114 glyphDataForIndex(unsigned index)115 ALWAYS_INLINE GlyphData glyphDataForIndex(unsigned index) const 116 { 117 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 118 Glyph glyph = m_glyphs[index]; 119 if (hasPerGlyphFontData()) 120 return GlyphData(glyph, m_perGlyphFontData[index]); 121 return GlyphData(glyph, glyph ? m_fontDataForAllGlyphs : 0); 122 } 123 glyphForCharacter(UChar32 c)124 ALWAYS_INLINE Glyph glyphForCharacter(UChar32 c) const 125 { 126 return glyphAt(indexForCharacter(c)); 127 } 128 glyphAt(unsigned index)129 ALWAYS_INLINE Glyph glyphAt(unsigned index) const 130 { 131 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 132 return m_glyphs[index]; 133 } 134 setGlyphDataForCharacter(UChar32 c,Glyph g,const SimpleFontData * f)135 void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f) 136 { 137 setGlyphDataForIndex(indexForCharacter(c), g, f); 138 } 139 setGlyphDataForIndex(unsigned index,Glyph glyph,const SimpleFontData * fontData)140 void setGlyphDataForIndex(unsigned index, Glyph glyph, const SimpleFontData* fontData) 141 { 142 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 143 m_glyphs[index] = glyph; 144 setCustomFontToLoad(index, 0); 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 setGlyphDataForIndex(unsigned index,const GlyphData & glyphData)156 void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData) 157 { 158 setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData); 159 } 160 customFontToLoadAt(unsigned index)161 const CustomFontData* customFontToLoadAt(unsigned index) const 162 { 163 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 164 return m_customFontToLoad ? m_customFontToLoad->at(index) : 0; 165 } 166 setCustomFontToLoad(unsigned index,const CustomFontData * customFontToLoad)167 void setCustomFontToLoad(unsigned index, const CustomFontData* customFontToLoad) 168 { 169 if (!m_customFontToLoad) { 170 if (!customFontToLoad) 171 return; 172 m_customFontToLoad = CustomDataPage::create(); 173 } 174 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 175 m_customFontToLoad->set(index, customFontToLoad); 176 } 177 removeFontDataFromSystemFallbackPage(const SimpleFontData * fontData)178 void removeFontDataFromSystemFallbackPage(const SimpleFontData* fontData) 179 { 180 // This method should only be called on the system fallback page, which is never single-font. 181 ASSERT(hasPerGlyphFontData()); 182 for (size_t i = 0; i < size; ++i) { 183 if (m_perGlyphFontData[i] == fontData) { 184 m_glyphs[i] = 0; 185 m_perGlyphFontData[i] = 0; 186 } 187 } 188 } 189 owner()190 GlyphPageTreeNode* owner() const { return m_owner; } 191 192 private: 193 explicit GlyphPage(GlyphPageTreeNode* owner, const SimpleFontData* fontDataForAllGlyphs = 0) m_fontDataForAllGlyphs(fontDataForAllGlyphs)194 : m_fontDataForAllGlyphs(fontDataForAllGlyphs) 195 , m_owner(owner) 196 { 197 memset(m_glyphs, 0, sizeof(m_glyphs)); 198 if (hasPerGlyphFontData()) 199 memset(m_perGlyphFontData, 0, sizeof(SimpleFontData*) * GlyphPage::size); 200 } 201 hasPerGlyphFontData()202 bool hasPerGlyphFontData() const { return !m_fontDataForAllGlyphs; } 203 204 class CustomDataPage : public RefCounted<CustomDataPage> { 205 public: create()206 static RefPtr<CustomDataPage> create() { return adoptRef(new CustomDataPage()); } at(size_t index)207 const CustomFontData* at(size_t index) const { return m_customData[index]; } set(size_t index,const CustomFontData * data)208 void set(size_t index, const CustomFontData* data) { m_customData[index] = data; } 209 private: CustomDataPage()210 CustomDataPage() { memset(m_customData, 0, sizeof(m_customData)); } 211 const CustomFontData* m_customData[size]; 212 }; 213 214 const SimpleFontData* m_fontDataForAllGlyphs; 215 GlyphPageTreeNode* m_owner; 216 RefPtr<CustomDataPage> m_customFontToLoad; 217 Glyph m_glyphs[size]; 218 219 // NOTE: This array has (GlyphPage::size) elements if m_fontDataForAllGlyphs is null. 220 const SimpleFontData* m_perGlyphFontData[0]; 221 }; 222 223 #if COMPILER(MSVC) 224 #pragma warning(pop) 225 #endif 226 227 } // namespace WebCore 228 229 #endif // GlyphPage_h 230