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 "platform/fonts/Glyph.h"
34#include "platform/geometry/FloatSize.h"
35#include "platform/heap/Heap.h"
36#include "wtf/Vector.h"
37
38namespace blink {
39
40class SimpleFontData;
41
42class GlyphBuffer {
43    STACK_ALLOCATED();
44public:
45    GlyphBuffer() : m_hasVerticalAdvances(false) { }
46
47    bool isEmpty() const { return m_fontData.isEmpty(); }
48    unsigned size() const { return m_fontData.size(); }
49    bool hasVerticalAdvances() const { return m_hasVerticalAdvances; }
50
51    void clear()
52    {
53        m_fontData.clear();
54        m_glyphs.clear();
55        m_advances.clear();
56        m_hasVerticalAdvances = false;
57    }
58
59    const Glyph* glyphs(unsigned from) const { return m_glyphs.data() + from; }
60    const FloatSize* advances(unsigned from) const { return m_advances.data() + from; }
61
62    const SimpleFontData* fontDataAt(unsigned index) const { return m_fontData[index]; }
63
64    Glyph glyphAt(unsigned index) const
65    {
66        return m_glyphs[index];
67    }
68
69    FloatSize advanceAt(unsigned index) const
70    {
71        return m_advances[index];
72    }
73
74    void add(Glyph glyph, const SimpleFontData* font, float width)
75    {
76        m_fontData.append(font);
77        m_glyphs.append(glyph);
78        m_advances.append(FloatSize(width, 0));
79    }
80
81    void add(Glyph glyph, const SimpleFontData* font, const FloatSize& advance)
82    {
83        m_fontData.append(font);
84        m_glyphs.append(glyph);
85        m_advances.append(advance);
86        if (advance.height())
87            m_hasVerticalAdvances = true;
88    }
89
90    void reverse()
91    {
92        m_fontData.reverse();
93        m_glyphs.reverse();
94        m_advances.reverse();
95    }
96
97    void setAdvanceWidth(unsigned index, float newWidth)
98    {
99        m_advances[index].setWidth(newWidth);
100    }
101
102    void expandLastAdvance(float width)
103    {
104        ASSERT(!isEmpty());
105        FloatSize& lastAdvance = m_advances.last();
106        lastAdvance.setWidth(lastAdvance.width() + width);
107    }
108
109private:
110    Vector<const SimpleFontData*, 2048> m_fontData;
111    Vector<Glyph, 2048> m_glyphs;
112    Vector<FloatSize, 2048> m_advances;
113    bool m_hasVerticalAdvances;
114};
115
116} // namespace blink
117
118#endif
119