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 blink { 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 if (hasPerGlyphFontData()) { 147 m_perGlyphFontData[index] = fontData; 148 } else { 149 // A single-font GlyphPage already assigned m_fontDataForAllGlyphs in the constructor. 150 ASSERT(!glyph || fontData == m_fontDataForAllGlyphs); 151 } 152 } 153 setGlyphDataForIndex(unsigned index,const GlyphData & glyphData)154 void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData) 155 { 156 setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData); 157 } 158 customFontToLoadAt(unsigned index)159 const CustomFontData* customFontToLoadAt(unsigned index) const 160 { 161 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 162 return m_customFontToLoad ? m_customFontToLoad->at(index) : 0; 163 } 164 setCustomFontToLoad(unsigned index,const CustomFontData * customFontToLoad)165 void setCustomFontToLoad(unsigned index, const CustomFontData* customFontToLoad) 166 { 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 removeFontDataFromSystemFallbackPage(const SimpleFontData * fontData)176 void removeFontDataFromSystemFallbackPage(const SimpleFontData* fontData) 177 { 178 // This method should only be called on the system fallback page, which is never 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 owner()188 GlyphPageTreeNode* owner() const { return m_owner; } 189 190 private: 191 explicit GlyphPage(GlyphPageTreeNode* owner, const SimpleFontData* fontDataForAllGlyphs = 0) m_fontDataForAllGlyphs(fontDataForAllGlyphs)192 : m_fontDataForAllGlyphs(fontDataForAllGlyphs) 193 , m_owner(owner) 194 { 195 memset(m_glyphs, 0, sizeof(m_glyphs)); 196 if (hasPerGlyphFontData()) 197 memset(m_perGlyphFontData, 0, sizeof(SimpleFontData*) * GlyphPage::size); 198 } 199 hasPerGlyphFontData()200 bool hasPerGlyphFontData() const { return !m_fontDataForAllGlyphs; } 201 202 class CustomDataPage : public RefCounted<CustomDataPage> { 203 public: create()204 static RefPtr<CustomDataPage> create() { return adoptRef(new CustomDataPage()); } at(size_t index)205 const CustomFontData* at(size_t index) const { return m_customData[index]; } set(size_t index,const CustomFontData * data)206 void set(size_t index, const CustomFontData* data) { m_customData[index] = data; } 207 private: CustomDataPage()208 CustomDataPage() { memset(m_customData, 0, sizeof(m_customData)); } 209 const CustomFontData* m_customData[size]; 210 }; 211 212 const SimpleFontData* m_fontDataForAllGlyphs; 213 GlyphPageTreeNode* m_owner; 214 RefPtr<CustomDataPage> m_customFontToLoad; 215 Glyph m_glyphs[size]; 216 217 // NOTE: This array has (GlyphPage::size) elements if m_fontDataForAllGlyphs is null. 218 const SimpleFontData* m_perGlyphFontData[0]; 219 }; 220 221 #if COMPILER(MSVC) 222 #pragma warning(pop) 223 #endif 224 225 } // namespace blink 226 227 #endif // GlyphPage_h 228