]> git.saurik.com Git - iphone-api.git/blob - WebCore/GlyphBuffer.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / GlyphBuffer.h
1 /*
2 * Copyright (C) 2006, 2009 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef GlyphBuffer_h
30 #define GlyphBuffer_h
31
32 #include "FloatSize.h"
33 #include <wtf/UnusedParam.h>
34 #include <wtf/Vector.h>
35
36 #if PLATFORM(CG)
37 #if USE(CORE_TEXT)
38 #include <CoreText/CoreText.h>
39 #endif
40 #include <CoreGraphics/CoreGraphics.h>
41 #endif
42
43 #if PLATFORM(CAIRO)
44 #include <cairo.h>
45 #endif
46
47 namespace WebCore {
48
49 typedef unsigned short Glyph;
50 class SimpleFontData;
51
52 #if PLATFORM(CAIRO)
53 // FIXME: Why does Cairo use such a huge struct instead of just an offset into an array?
54 typedef cairo_glyph_t GlyphBufferGlyph;
55 #else
56 typedef Glyph GlyphBufferGlyph;
57 #endif
58
59 // CG uses CGSize instead of FloatSize so that the result of advances()
60 // can be passed directly to CGContextShowGlyphsWithAdvances in FontMac.mm
61 #if PLATFORM(CG)
62 typedef CGSize GlyphBufferAdvance;
63 #else
64 typedef FloatSize GlyphBufferAdvance;
65 #endif
66
67 class GlyphBuffer {
68 public:
69 bool isEmpty() const { return m_fontData.isEmpty(); }
70 int size() const { return m_fontData.size(); }
71
72 void clear()
73 {
74 m_fontData.clear();
75 m_glyphs.clear();
76 m_advances.clear();
77 #if PLATFORM(WIN)
78 m_offsets.clear();
79 #endif
80 }
81
82 GlyphBufferGlyph* glyphs(int from) { return m_glyphs.data() + from; }
83 GlyphBufferAdvance* advances(int from) { return m_advances.data() + from; }
84 const GlyphBufferGlyph* glyphs(int from) const { return m_glyphs.data() + from; }
85 const GlyphBufferAdvance* advances(int from) const { return m_advances.data() + from; }
86
87 const SimpleFontData* fontDataAt(int index) const { return m_fontData[index]; }
88
89 void swap(int index1, int index2)
90 {
91 const SimpleFontData* f = m_fontData[index1];
92 m_fontData[index1] = m_fontData[index2];
93 m_fontData[index2] = f;
94
95 GlyphBufferGlyph g = m_glyphs[index1];
96 m_glyphs[index1] = m_glyphs[index2];
97 m_glyphs[index2] = g;
98
99 GlyphBufferAdvance s = m_advances[index1];
100 m_advances[index1] = m_advances[index2];
101 m_advances[index2] = s;
102
103 #if PLATFORM(WIN)
104 FloatSize offset = m_offsets[index1];
105 m_offsets[index1] = m_offsets[index2];
106 m_offsets[index2] = offset;
107 #endif
108 }
109
110 Glyph glyphAt(int index) const
111 {
112 #if PLATFORM(CAIRO)
113 return m_glyphs[index].index;
114 #else
115 return m_glyphs[index];
116 #endif
117 }
118
119 float advanceAt(int index) const
120 {
121 #if PLATFORM(CG)
122 return m_advances[index].width;
123 #else
124 return m_advances[index].width();
125 #endif
126 }
127
128 FloatSize offsetAt(int index) const
129 {
130 #if PLATFORM(WIN)
131 return m_offsets[index];
132 #else
133 UNUSED_PARAM(index);
134 return FloatSize();
135 #endif
136 }
137
138 void add(Glyph glyph, const SimpleFontData* font, float width, const FloatSize* offset = 0)
139 {
140 m_fontData.append(font);
141
142 #if PLATFORM(CAIRO)
143 cairo_glyph_t cairoGlyph;
144 cairoGlyph.index = glyph;
145 m_glyphs.append(cairoGlyph);
146 #else
147 m_glyphs.append(glyph);
148 #endif
149
150 #if PLATFORM(CG)
151 CGSize advance = { width, 0 };
152 m_advances.append(advance);
153 #else
154 m_advances.append(FloatSize(width, 0));
155 #endif
156
157 #if PLATFORM(WIN)
158 if (offset)
159 m_offsets.append(*offset);
160 else
161 m_offsets.append(FloatSize());
162 #else
163 UNUSED_PARAM(offset);
164 #endif
165 }
166
167 void add(Glyph glyph, const SimpleFontData* font, GlyphBufferAdvance advance)
168 {
169 m_fontData.append(font);
170 #if PLATFORM(CAIRO)
171 cairo_glyph_t cairoGlyph;
172 cairoGlyph.index = glyph;
173 m_glyphs.append(cairoGlyph);
174 #else
175 m_glyphs.append(glyph);
176 #endif
177
178 m_advances.append(advance);
179 }
180
181 private:
182 Vector<const SimpleFontData*, 2048> m_fontData;
183 Vector<GlyphBufferGlyph, 2048> m_glyphs;
184 Vector<GlyphBufferAdvance, 2048> m_advances;
185 #if PLATFORM(WIN)
186 Vector<FloatSize, 2048> m_offsets;
187 #endif
188 };
189
190 }
191 #endif