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 <string.h> 34 #include "core/platform/graphics/Glyph.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 glyphAt(unsigned index) const 118 { 119 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 120 return m_glyphs[index]; 121 } 122 123 ALWAYS_INLINE const SimpleFontData* fontDataForCharacter(UChar32 c) const 124 { 125 unsigned index = indexForCharacter(c); 126 if (hasPerGlyphFontData()) 127 return m_perGlyphFontData[index]; 128 return m_glyphs[index] ? m_fontDataForAllGlyphs : 0; 129 } 130 131 void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f) 132 { 133 setGlyphDataForIndex(indexForCharacter(c), g, f); 134 } 135 136 void setGlyphDataForIndex(unsigned index, Glyph glyph, const SimpleFontData* fontData) 137 { 138 ASSERT_WITH_SECURITY_IMPLICATION(index < size); 139 m_glyphs[index] = glyph; 140 141 // GlyphPage getters will always return a null SimpleFontData* for glyph #0 if there's no per-glyph font array. 142 if (hasPerGlyphFontData()) { 143 m_perGlyphFontData[index] = glyph ? fontData : 0; 144 return; 145 } 146 147 // A single-font GlyphPage already assigned m_fontDataForAllGlyphs in the constructor. 148 ASSERT(!glyph || fontData == m_fontDataForAllGlyphs); 149 } 150 151 void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData) 152 { 153 setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData); 154 } 155 156 void removeFontDataFromSystemFallbackPage(const SimpleFontData* fontData) 157 { 158 // This method should only be called on the system fallback page, which is never single-font. 159 ASSERT(hasPerGlyphFontData()); 160 for (size_t i = 0; i < size; ++i) { 161 if (m_perGlyphFontData[i] == fontData) { 162 m_glyphs[i] = 0; 163 m_perGlyphFontData[i] = 0; 164 } 165 } 166 } 167 168 GlyphPageTreeNode* owner() const { return m_owner; } 169 170 // Implemented by the platform. 171 bool fill(unsigned offset, unsigned length, UChar* characterBuffer, unsigned bufferLength, const SimpleFontData*); 172 173 private: 174 explicit GlyphPage(GlyphPageTreeNode* owner, const SimpleFontData* fontDataForAllGlyphs = 0) 175 : m_fontDataForAllGlyphs(fontDataForAllGlyphs) 176 , m_owner(owner) 177 { 178 memset(m_glyphs, 0, sizeof(m_glyphs)); 179 if (hasPerGlyphFontData()) 180 memset(m_perGlyphFontData, 0, sizeof(SimpleFontData*) * GlyphPage::size); 181 } 182 183 bool hasPerGlyphFontData() const { return !m_fontDataForAllGlyphs; } 184 185 const SimpleFontData* m_fontDataForAllGlyphs; 186 GlyphPageTreeNode* m_owner; 187 Glyph m_glyphs[size]; 188 189 // NOTE: This array has (GlyphPage::size) elements if m_fontDataForAllGlyphs is null. 190 const SimpleFontData* m_perGlyphFontData[0]; 191 }; 192 193 #if COMPILER(MSVC) 194 #pragma warning(pop) 195 #endif 196 197 } // namespace WebCore 198 199 #endif // GlyphPage_h 200