1/* 2 * Copyright (C) 2006, 2009, 2011 Apple Inc. All rights reserved. 3 * Copyright (C) 2007-2008 Torch Mobile Inc. 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 GlyphBuffer_h 31#define GlyphBuffer_h 32 33#include "FloatSize.h" 34#include "Glyph.h" 35#include <wtf/Vector.h> 36 37#if USE(CG) 38#include <CoreGraphics/CGGeometry.h> 39#endif 40 41#if USE(CAIRO) 42#include <cairo.h> 43#endif 44 45namespace WebCore { 46 47class SimpleFontData; 48 49#if USE(CAIRO) 50// FIXME: Why does Cairo use such a huge struct instead of just an offset into an array? 51typedef cairo_glyph_t GlyphBufferGlyph; 52#elif USE(WINGDI) 53typedef wchar_t GlyphBufferGlyph; 54#elif PLATFORM(QT) 55typedef quint32 GlyphBufferGlyph; 56#elif PLATFORM(BLACKBERRY) 57typedef unsigned GlyphBufferGlyph; 58#else 59typedef Glyph GlyphBufferGlyph; 60#endif 61 62// CG uses CGSize instead of FloatSize so that the result of advances() 63// can be passed directly to CGContextShowGlyphsWithAdvances in FontMac.mm 64#if USE(CG) 65struct GlyphBufferAdvance : CGSize { 66public: 67 GlyphBufferAdvance() : CGSize(CGSizeZero) { } 68 GlyphBufferAdvance(CGSize size) : CGSize(size) 69 { 70 } 71 72 void setWidth(CGFloat width) { this->CGSize::width = width; } 73 CGFloat width() const { return this->CGSize::width; } 74 CGFloat height() const { return this->CGSize::height; } 75}; 76#elif PLATFORM(QT) 77struct GlyphBufferAdvance : public QPointF { 78public: 79 GlyphBufferAdvance() : QPointF() { } 80 GlyphBufferAdvance(const QPointF& advance) 81 : QPointF(advance) 82 { 83 } 84 85 void setWidth(qreal width) { QPointF::setX(width); } 86 qreal width() const { return QPointF::x(); } 87 qreal height() const { return QPointF::y(); } 88}; 89#else 90typedef FloatSize GlyphBufferAdvance; 91#endif 92 93class GlyphBuffer { 94public: 95 bool isEmpty() const { return m_fontData.isEmpty(); } 96 int size() const { return m_fontData.size(); } 97 98 void clear() 99 { 100 m_fontData.clear(); 101 m_glyphs.clear(); 102 m_advances.clear(); 103#if PLATFORM(WIN) 104 m_offsets.clear(); 105#endif 106 } 107 108 GlyphBufferGlyph* glyphs(int from) { return m_glyphs.data() + from; } 109 GlyphBufferAdvance* advances(int from) { return m_advances.data() + from; } 110 const GlyphBufferGlyph* glyphs(int from) const { return m_glyphs.data() + from; } 111 const GlyphBufferAdvance* advances(int from) const { return m_advances.data() + from; } 112 113 const SimpleFontData* fontDataAt(int index) const { return m_fontData[index]; } 114 115 void setInitialAdvance(GlyphBufferAdvance initialAdvance) { m_initialAdvance = initialAdvance; } 116 const GlyphBufferAdvance& initialAdvance() const { return m_initialAdvance; } 117 118 Glyph glyphAt(int index) const 119 { 120#if USE(CAIRO) 121 return m_glyphs[index].index; 122#else 123 return m_glyphs[index]; 124#endif 125 } 126 127 GlyphBufferAdvance advanceAt(int index) const 128 { 129 return m_advances[index]; 130 } 131 132 FloatSize offsetAt(int index) const 133 { 134#if PLATFORM(WIN) 135 return m_offsets[index]; 136#else 137 UNUSED_PARAM(index); 138 return FloatSize(); 139#endif 140 } 141 142 void add(Glyph glyph, const SimpleFontData* font, float width, const FloatSize* offset = 0) 143 { 144 m_fontData.append(font); 145 146#if USE(CAIRO) 147 cairo_glyph_t cairoGlyph; 148 cairoGlyph.index = glyph; 149 m_glyphs.append(cairoGlyph); 150#else 151 m_glyphs.append(glyph); 152#endif 153 154#if USE(CG) 155 CGSize advance = { width, 0 }; 156 m_advances.append(advance); 157#elif PLATFORM(QT) 158 m_advances.append(QPointF(width, 0)); 159#else 160 m_advances.append(FloatSize(width, 0)); 161#endif 162 163#if PLATFORM(WIN) 164 if (offset) 165 m_offsets.append(*offset); 166 else 167 m_offsets.append(FloatSize()); 168#else 169 UNUSED_PARAM(offset); 170#endif 171 } 172 173#if !USE(WINGDI) 174 void add(Glyph glyph, const SimpleFontData* font, GlyphBufferAdvance advance) 175 { 176 m_fontData.append(font); 177#if USE(CAIRO) 178 cairo_glyph_t cairoGlyph; 179 cairoGlyph.index = glyph; 180 m_glyphs.append(cairoGlyph); 181#else 182 m_glyphs.append(glyph); 183#endif 184 185 m_advances.append(advance); 186 } 187#endif 188 189 void reverse(int from, int length) 190 { 191 for (int i = from, end = from + length - 1; i < end; ++i, --end) 192 swap(i, end); 193 } 194 195 void expandLastAdvance(float width) 196 { 197 ASSERT(!isEmpty()); 198 GlyphBufferAdvance& lastAdvance = m_advances.last(); 199 lastAdvance.setWidth(lastAdvance.width() + width); 200 } 201 202private: 203 void swap(int index1, int index2) 204 { 205 const SimpleFontData* f = m_fontData[index1]; 206 m_fontData[index1] = m_fontData[index2]; 207 m_fontData[index2] = f; 208 209 GlyphBufferGlyph g = m_glyphs[index1]; 210 m_glyphs[index1] = m_glyphs[index2]; 211 m_glyphs[index2] = g; 212 213 GlyphBufferAdvance s = m_advances[index1]; 214 m_advances[index1] = m_advances[index2]; 215 m_advances[index2] = s; 216 217#if PLATFORM(WIN) 218 FloatSize offset = m_offsets[index1]; 219 m_offsets[index1] = m_offsets[index2]; 220 m_offsets[index2] = offset; 221#endif 222 } 223 224 Vector<const SimpleFontData*, 2048> m_fontData; 225 Vector<GlyphBufferGlyph, 2048> m_glyphs; 226 Vector<GlyphBufferAdvance, 2048> m_advances; 227 GlyphBufferAdvance m_initialAdvance; 228#if PLATFORM(WIN) 229 Vector<FloatSize, 2048> m_offsets; 230#endif 231}; 232 233} 234#endif 235