2 * Copyright (C) 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
33 #include "platform/fonts/Glyph.h"
35 #include "wtf/PassRefPtr.h"
36 #include "wtf/RefCounted.h"
37 #include "wtf/RefPtr.h"
38 #include "wtf/unicode/Unicode.h"
43 class GlyphPageTreeNode;
45 // Holds the glyph index and the corresponding SimpleFontData information for a given
48 GlyphData(Glyph g = 0, const SimpleFontData* f = 0)
54 const SimpleFontData* fontData;
59 #pragma warning(disable: 4200) // Disable "zero-sized array in struct/union" warning
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.
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> {
72 static PassRefPtr<GlyphPage> createForMixedFontData(GlyphPageTreeNode* owner)
74 void* slot = fastMalloc(sizeof(GlyphPage) + sizeof(SimpleFontData*) * GlyphPage::size);
75 return adoptRef(new (slot) GlyphPage(owner));
78 static PassRefPtr<GlyphPage> createForSingleFontData(GlyphPageTreeNode* owner, const SimpleFontData* fontData)
81 return adoptRef(new GlyphPage(owner, fontData));
84 PassRefPtr<GlyphPage> createCopiedSystemFallbackPage(GlyphPageTreeNode* owner) const
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);
91 for (size_t i = 0; i < GlyphPage::size; ++i) {
92 page->m_perGlyphFontData[i] = m_glyphs[i] ? m_fontDataForAllGlyphs : 0;
95 return page.release();
100 static const size_t size = 256; // Covers Latin-1 in a single page.
101 static unsigned indexForCharacter(UChar32 c) { return c % GlyphPage::size; }
103 ALWAYS_INLINE GlyphData glyphDataForCharacter(UChar32 c) const
105 return glyphDataForIndex(indexForCharacter(c));
108 ALWAYS_INLINE GlyphData glyphDataForIndex(unsigned index) const
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);
117 ALWAYS_INLINE Glyph glyphForCharacter(UChar32 c) const
119 return glyphAt(indexForCharacter(c));
122 ALWAYS_INLINE Glyph glyphAt(unsigned index) const
124 ASSERT_WITH_SECURITY_IMPLICATION(index < size);
125 return m_glyphs[index];
128 ALWAYS_INLINE const SimpleFontData* fontDataForCharacter(UChar32 c) const
130 unsigned index = indexForCharacter(c);
131 if (hasPerGlyphFontData())
132 return m_perGlyphFontData[index];
133 return m_glyphs[index] ? m_fontDataForAllGlyphs : 0;
136 void setGlyphDataForCharacter(UChar32 c, Glyph g, const SimpleFontData* f)
138 setGlyphDataForIndex(indexForCharacter(c), g, f);
141 void setGlyphDataForIndex(unsigned index, Glyph glyph, const SimpleFontData* fontData)
143 ASSERT_WITH_SECURITY_IMPLICATION(index < size);
144 m_glyphs[index] = glyph;
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;
152 // A single-font GlyphPage already assigned m_fontDataForAllGlyphs in the constructor.
153 ASSERT(!glyph || fontData == m_fontDataForAllGlyphs);
156 void setGlyphDataForIndex(unsigned index, const GlyphData& glyphData)
158 setGlyphDataForIndex(index, glyphData.glyph, glyphData.fontData);
161 void removeFontDataFromSystemFallbackPage(const SimpleFontData* fontData)
163 // This method should only be called on the system fallback page, which is never single-font.
164 ASSERT(hasPerGlyphFontData());
165 for (size_t i = 0; i < size; ++i) {
166 if (m_perGlyphFontData[i] == fontData) {
168 m_perGlyphFontData[i] = 0;
173 GlyphPageTreeNode* owner() const { return m_owner; }
175 // Implemented by the platform.
176 bool fill(unsigned offset, unsigned length, UChar* characterBuffer, unsigned bufferLength, const SimpleFontData*);
179 explicit GlyphPage(GlyphPageTreeNode* owner, const SimpleFontData* fontDataForAllGlyphs = 0)
180 : m_fontDataForAllGlyphs(fontDataForAllGlyphs)
183 memset(m_glyphs, 0, sizeof(m_glyphs));
184 if (hasPerGlyphFontData())
185 memset(m_perGlyphFontData, 0, sizeof(SimpleFontData*) * GlyphPage::size);
188 bool hasPerGlyphFontData() const { return !m_fontDataForAllGlyphs; }
190 const SimpleFontData* m_fontDataForAllGlyphs;
191 GlyphPageTreeNode* m_owner;
192 Glyph m_glyphs[size];
194 // NOTE: This array has (GlyphPage::size) elements if m_fontDataForAllGlyphs is null.
195 const SimpleFontData* m_perGlyphFontData[0];
202 } // namespace WebCore
204 #endif // GlyphPage_h