2 * Copyright (C) 2006, 2009, 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2007-2008 Torch Mobile Inc.
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 "FloatSize.h"
35 #include <wtf/UnusedParam.h>
36 #include <wtf/Vector.h>
38 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
39 #include <CoreGraphics/CGGeometry.h>
42 #if PLATFORM(WX) && OS(DARWIN)
43 #include <ApplicationServices/ApplicationServices.h>
46 #if USE(CAIRO) || (PLATFORM(WX) && defined(wxUSE_CAIRO) && wxUSE_CAIRO)
54 #if USE(CAIRO) || (PLATFORM(WX) && defined(wxUSE_CAIRO) && wxUSE_CAIRO)
55 // FIXME: Why does Cairo use such a huge struct instead of just an offset into an array?
56 typedef cairo_glyph_t GlyphBufferGlyph;
58 typedef wchar_t GlyphBufferGlyph;
60 typedef Glyph GlyphBufferGlyph;
63 // CG uses CGSize instead of FloatSize so that the result of advances()
64 // can be passed directly to CGContextShowGlyphsWithAdvances in FontMac.mm
65 #if USE(CG) || (PLATFORM(WX) && OS(DARWIN)) || USE(SKIA_ON_MAC_CHROMIUM)
66 typedef CGSize GlyphBufferAdvance;
68 // There is no cross-platform code that uses the height of GlyphBufferAdvance,
69 // so we can save memory space on embedded devices by storing only the width
70 typedef float GlyphBufferAdvance;
72 typedef FloatSize GlyphBufferAdvance;
77 bool isEmpty() const { return m_fontData.isEmpty(); }
78 int size() const { return m_fontData.size(); }
90 GlyphBufferGlyph* glyphs(int from) { return m_glyphs.data() + from; }
91 GlyphBufferAdvance* advances(int from) { return m_advances.data() + from; }
92 const GlyphBufferGlyph* glyphs(int from) const { return m_glyphs.data() + from; }
93 const GlyphBufferAdvance* advances(int from) const { return m_advances.data() + from; }
95 const SimpleFontData* fontDataAt(int index) const { return m_fontData[index]; }
97 void swap(int index1, int index2)
99 const SimpleFontData* f = m_fontData[index1];
100 m_fontData[index1] = m_fontData[index2];
101 m_fontData[index2] = f;
103 GlyphBufferGlyph g = m_glyphs[index1];
104 m_glyphs[index1] = m_glyphs[index2];
105 m_glyphs[index2] = g;
107 GlyphBufferAdvance s = m_advances[index1];
108 m_advances[index1] = m_advances[index2];
109 m_advances[index2] = s;
112 FloatSize offset = m_offsets[index1];
113 m_offsets[index1] = m_offsets[index2];
114 m_offsets[index2] = offset;
118 Glyph glyphAt(int index) const
120 #if USE(CAIRO) || (PLATFORM(WX) && defined(wxUSE_CAIRO) && wxUSE_CAIRO)
121 return m_glyphs[index].index;
123 return m_glyphs[index];
127 float advanceAt(int index) const
129 #if USE(CG) || (PLATFORM(WX) && OS(DARWIN)) || USE(SKIA_ON_MAC_CHROMIUM)
130 return m_advances[index].width;
132 return m_advances[index];
134 return m_advances[index].width();
138 FloatSize offsetAt(int index) const
141 return m_offsets[index];
148 void add(Glyph glyph, const SimpleFontData* font, float width, const FloatSize* offset = 0)
150 m_fontData.append(font);
152 #if USE(CAIRO) || (PLATFORM(WX) && defined(wxUSE_CAIRO) && wxUSE_CAIRO)
153 cairo_glyph_t cairoGlyph;
154 cairoGlyph.index = glyph;
155 m_glyphs.append(cairoGlyph);
157 m_glyphs.append(glyph);
160 #if USE(CG) || (PLATFORM(WX) && OS(DARWIN)) || USE(SKIA_ON_MAC_CHROMIUM)
161 CGSize advance = { width, 0 };
162 m_advances.append(advance);
164 m_advances.append(width);
166 m_advances.append(FloatSize(width, 0));
171 m_offsets.append(*offset);
173 m_offsets.append(FloatSize());
175 UNUSED_PARAM(offset);
180 void add(Glyph glyph, const SimpleFontData* font, GlyphBufferAdvance advance)
182 m_fontData.append(font);
183 #if USE(CAIRO) || (PLATFORM(WX) && defined(wxUSE_CAIRO) && wxUSE_CAIRO)
184 cairo_glyph_t cairoGlyph;
185 cairoGlyph.index = glyph;
186 m_glyphs.append(cairoGlyph);
188 m_glyphs.append(glyph);
191 m_advances.append(advance);
195 void expandLastAdvance(float width)
198 GlyphBufferAdvance& lastAdvance = m_advances.last();
199 #if USE(CG) || (PLATFORM(WX) && OS(DARWIN)) || USE(SKIA_ON_MAC_CHROMIUM)
200 lastAdvance.width += width;
202 lastAdvance += width;
204 lastAdvance += FloatSize(width, 0);
209 Vector<const SimpleFontData*, 2048> m_fontData;
210 Vector<GlyphBufferGlyph, 2048> m_glyphs;
211 Vector<GlyphBufferAdvance, 2048> m_advances;
213 Vector<FloatSize, 2048> m_offsets;