#ifndef GlyphBuffer_h
#define GlyphBuffer_h
#include "FloatSize.h"
#include "Glyph.h"
#include <climits>
#include <wtf/Vector.h>
#if USE(CG)
#include <CoreGraphics/CGGeometry.h>
#endif
#if USE(CAIRO)
#include <cairo.h>
#endif
namespace WebCore {
class Font;
#if USE(CAIRO)
typedef cairo_glyph_t GlyphBufferGlyph;
#elif USE(WINGDI)
typedef wchar_t GlyphBufferGlyph;
#else
typedef Glyph GlyphBufferGlyph;
#endif
#if USE(CG)
struct GlyphBufferAdvance : CGSize {
public:
GlyphBufferAdvance() : CGSize(CGSizeZero) { }
GlyphBufferAdvance(CGSize size) : CGSize(size)
{
}
void setWidth(CGFloat width) { this->CGSize::width = width; }
CGFloat width() const { return this->CGSize::width; }
CGFloat height() const { return this->CGSize::height; }
};
#else
typedef FloatSize GlyphBufferAdvance;
#endif
class GlyphBuffer {
public:
bool isEmpty() const { return m_font.isEmpty(); }
int size() const { return m_font.size(); }
void clear()
{
m_font.clear();
m_glyphs.clear();
m_advances.clear();
if (m_offsetsInString)
m_offsetsInString->clear();
#if PLATFORM(WIN)
m_offsets.clear();
#endif
}
GlyphBufferGlyph* glyphs(int from) { return m_glyphs.data() + from; }
GlyphBufferAdvance* advances(int from) { return m_advances.data() + from; }
const GlyphBufferGlyph* glyphs(int from) const { return m_glyphs.data() + from; }
const GlyphBufferAdvance* advances(int from) const { return m_advances.data() + from; }
const Font* fontAt(int index) const { return m_font[index]; }
void setInitialAdvance(GlyphBufferAdvance initialAdvance) { m_initialAdvance = initialAdvance; }
const GlyphBufferAdvance& initialAdvance() const { return m_initialAdvance; }
void setLeadingExpansion(float leadingExpansion) { m_leadingExpansion = leadingExpansion; }
float leadingExpansion() const { return m_leadingExpansion; }
Glyph glyphAt(int index) const
{
#if USE(CAIRO)
return m_glyphs[index].index;
#else
return m_glyphs[index];
#endif
}
GlyphBufferAdvance advanceAt(int index) const
{
return m_advances[index];
}
FloatSize offsetAt(int index) const
{
#if PLATFORM(WIN)
return m_offsets[index];
#else
UNUSED_PARAM(index);
return FloatSize();
#endif
}
static const unsigned noOffset = UINT_MAX;
void add(Glyph glyph, const Font* font, float width, unsigned offsetInString = noOffset, const FloatSize* offset = 0)
{
m_font.append(font);
#if USE(CAIRO)
cairo_glyph_t cairoGlyph;
cairoGlyph.index = glyph;
m_glyphs.append(cairoGlyph);
#else
m_glyphs.append(glyph);
#endif
#if USE(CG)
CGSize advance = { width, 0 };
m_advances.append(advance);
#else
m_advances.append(FloatSize(width, 0));
#endif
#if PLATFORM(WIN)
if (offset)
m_offsets.append(*offset);
else
m_offsets.append(FloatSize());
#else
UNUSED_PARAM(offset);
#endif
if (offsetInString != noOffset && m_offsetsInString)
m_offsetsInString->append(offsetInString);
}
#if !USE(WINGDI)
void add(Glyph glyph, const Font* font, GlyphBufferAdvance advance, unsigned offsetInString = noOffset)
{
m_font.append(font);
#if USE(CAIRO)
cairo_glyph_t cairoGlyph;
cairoGlyph.index = glyph;
m_glyphs.append(cairoGlyph);
#else
m_glyphs.append(glyph);
#endif
m_advances.append(advance);
if (offsetInString != noOffset && m_offsetsInString)
m_offsetsInString->append(offsetInString);
}
#endif
void reverse(int from, int length)
{
for (int i = from, end = from + length - 1; i < end; ++i, --end)
swap(i, end);
}
void expandLastAdvance(float width)
{
ASSERT(!isEmpty());
GlyphBufferAdvance& lastAdvance = m_advances.last();
lastAdvance.setWidth(lastAdvance.width() + width);
}
void saveOffsetsInString()
{
m_offsetsInString.reset(new Vector<unsigned, 2048>());
}
int offsetInString(int index) const
{
ASSERT(m_offsetsInString);
return (*m_offsetsInString)[index];
}
void shrink(int truncationPoint)
{
m_font.shrink(truncationPoint);
m_glyphs.shrink(truncationPoint);
m_advances.shrink(truncationPoint);
if (m_offsetsInString)
m_offsetsInString->shrink(truncationPoint);
#if PLATFORM(WIN)
m_offsets.shrink(truncationPoint);
#endif
}
private:
void swap(int index1, int index2)
{
const Font* f = m_font[index1];
m_font[index1] = m_font[index2];
m_font[index2] = f;
GlyphBufferGlyph g = m_glyphs[index1];
m_glyphs[index1] = m_glyphs[index2];
m_glyphs[index2] = g;
GlyphBufferAdvance s = m_advances[index1];
m_advances[index1] = m_advances[index2];
m_advances[index2] = s;
#if PLATFORM(WIN)
FloatSize offset = m_offsets[index1];
m_offsets[index1] = m_offsets[index2];
m_offsets[index2] = offset;
#endif
}
Vector<const Font*, 2048> m_font;
Vector<GlyphBufferGlyph, 2048> m_glyphs;
Vector<GlyphBufferAdvance, 2048> m_advances;
GlyphBufferAdvance m_initialAdvance;
std::unique_ptr<Vector<unsigned, 2048>> m_offsetsInString;
#if PLATFORM(WIN)
Vector<FloatSize, 2048> m_offsets;
#endif
float m_leadingExpansion;
};
}
#endif