Home | History | Annotate | Download | only in graphics
      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 "core/platform/graphics/FloatSize.h"
     34 #include "core/platform/graphics/Glyph.h"
     35 #include "wtf/UnusedParam.h"
     36 #include "wtf/Vector.h"
     37 
     38 #if OS(DARWIN)
     39 #include <ApplicationServices/ApplicationServices.h>
     40 #endif
     41 
     42 namespace WebCore {
     43 
     44 class SimpleFontData;
     45 
     46 typedef Glyph GlyphBufferGlyph;
     47 
     48 // CG uses CGSize instead of FloatSize so that the result of advances()
     49 // can be passed directly to CGContextShowGlyphsWithAdvances in FontMac.mm
     50 #if OS(DARWIN)
     51 struct GlyphBufferAdvance : CGSize {
     52 public:
     53     GlyphBufferAdvance(CGSize size) : CGSize(size)
     54     {
     55     }
     56 
     57     void setWidth(CGFloat width) { this->CGSize::width = width; }
     58     CGFloat width() const { return this->CGSize::width; }
     59     CGFloat height() const { return this->CGSize::height; }
     60 };
     61 #else
     62 typedef FloatSize GlyphBufferAdvance;
     63 #endif
     64 
     65 class GlyphBuffer {
     66 public:
     67     bool isEmpty() const { return m_fontData.isEmpty(); }
     68     int size() const { return m_fontData.size(); }
     69 
     70     void clear()
     71     {
     72         m_fontData.clear();
     73         m_glyphs.clear();
     74         m_advances.clear();
     75     }
     76 
     77     GlyphBufferGlyph* glyphs(int from) { return m_glyphs.data() + from; }
     78     GlyphBufferAdvance* advances(int from) { return m_advances.data() + from; }
     79     const GlyphBufferGlyph* glyphs(int from) const { return m_glyphs.data() + from; }
     80     const GlyphBufferAdvance* advances(int from) const { return m_advances.data() + from; }
     81 
     82     const SimpleFontData* fontDataAt(int index) const { return m_fontData[index]; }
     83 
     84     Glyph glyphAt(int index) const
     85     {
     86         return m_glyphs[index];
     87     }
     88 
     89     float advanceAt(int index) const
     90     {
     91         return m_advances[index].width();
     92     }
     93 
     94     void add(Glyph glyph, const SimpleFontData* font, float width)
     95     {
     96         m_fontData.append(font);
     97         m_glyphs.append(glyph);
     98 
     99 #if OS(DARWIN)
    100         CGSize advance = { width, 0 };
    101         m_advances.append(advance);
    102 #else
    103         m_advances.append(FloatSize(width, 0));
    104 #endif
    105     }
    106 
    107     void add(Glyph glyph, const SimpleFontData* font, GlyphBufferAdvance advance)
    108     {
    109         m_fontData.append(font);
    110         m_glyphs.append(glyph);
    111         m_advances.append(advance);
    112     }
    113 
    114     void reverse(int from, int length)
    115     {
    116         for (int i = from, end = from + length - 1; i < end; ++i, --end)
    117             swap(i, end);
    118     }
    119 
    120     void expandLastAdvance(float width)
    121     {
    122         ASSERT(!isEmpty());
    123         GlyphBufferAdvance& lastAdvance = m_advances.last();
    124         lastAdvance.setWidth(lastAdvance.width() + width);
    125     }
    126 
    127 private:
    128     void swap(int index1, int index2)
    129     {
    130         const SimpleFontData* f = m_fontData[index1];
    131         m_fontData[index1] = m_fontData[index2];
    132         m_fontData[index2] = f;
    133 
    134         GlyphBufferGlyph g = m_glyphs[index1];
    135         m_glyphs[index1] = m_glyphs[index2];
    136         m_glyphs[index2] = g;
    137 
    138         GlyphBufferAdvance s = m_advances[index1];
    139         m_advances[index1] = m_advances[index2];
    140         m_advances[index2] = s;
    141     }
    142 
    143     Vector<const SimpleFontData*, 2048> m_fontData;
    144     Vector<GlyphBufferGlyph, 2048> m_glyphs;
    145     Vector<GlyphBufferAdvance, 2048> m_advances;
    146 };
    147 
    148 }
    149 #endif
    150