Glyph 9.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSGTEXT_GLYPH
#define OSGTEXT_GLYPH 1

#include <string>
#include <istream>

#include <osg/Vec2>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/StateSet>
#include <osg/Geometry>
#include <osg/Geode>

#include <osgText/Export>
#include <osgText/KerningType>
#include <osgText/Style>

#include <OpenThreads/Mutex>

namespace osgText {

class Font;
class Text;
class Glyph3D;
class GlyphGeometry;
class GlyphTexture;

class OSGTEXT_EXPORT Glyph : public osg::Image
{
public:

    Glyph(Font* font, unsigned int glyphCode);

    Font* getFont() { return _font; }
    const Font* getFont() const { return _font; }

    unsigned int getGlyphCode() const { return _glyphCode; }

    void setWidth(float width) { _width = width; }
    float getWidth() const { return _width; }

    void setHeight(float height) { _height = height; }
    float getHeight() const { return _height; }

    void setHorizontalBearing(const osg::Vec2& bearing);
    const osg::Vec2& getHorizontalBearing() const;

    void setHorizontalAdvance(float advance);
    float getHorizontalAdvance() const;

    void setVerticalBearing(const osg::Vec2& bearing);
    const osg::Vec2& getVerticalBearing() const;

    void setVerticalAdvance(float advance);
    float getVerticalAdvance() const;

    void setTexture(GlyphTexture* texture);
    GlyphTexture* getTexture();
    const GlyphTexture* getTexture() const;

    void setTexturePosition(int posX,int posY);
    int getTexturePositionX() const;
    int getTexturePositionY() const;

    void setMinTexCoord(const osg::Vec2& coord);
    const osg::Vec2& getMinTexCoord() const;

    void setMaxTexCoord(const osg::Vec2& coord);
    const osg::Vec2& getMaxTexCoord() const;

    void subload() const;

protected:

    virtual ~Glyph();

    Font*                       _font;
    unsigned int                _glyphCode;

    float                       _width;
    float                       _height;

    osg::Vec2                   _horizontalBearing;
    float                       _horizontalAdvance;

    osg::Vec2                   _verticalBearing;
    float                       _verticalAdvance;

    GlyphTexture*               _texture;
    int                         _texturePosX;
    int                         _texturePosY;
    osg::Vec2                   _minTexCoord;
    osg::Vec2                   _maxTexCoord;

    typedef osg::buffered_value<GLuint> GLObjectList;
    mutable GLObjectList _globjList;

};

class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
{
    public:

        GlyphGeometry();

        void setup(const Glyph3D* glyph, const Style* style);

        bool match(const Style* style) const;

        osg::Geode* getGeode() const { return _geode.get(); }
        osg::Geometry* getGeometry() const { return _geometry.get(); }

        /** Set the VertexArray of the glyph. */
        void setVertexArray(osg::Vec3Array * va) { _vertices = va; }
        /** Get the VertexArray of the glyph. */
        osg::Vec3Array * getVertexArray() const { return _vertices.get(); }

        /** Set the VertexArray of the glyph. */
        void setNormalArray(osg::Vec3Array* na) { _normals = na; }
        /** Get the NormalArray for the wall face. */
        osg::Vec3Array* getNormalArray() const { return _normals.get(); }

        /** Get the PrimitiveSetList for the front face. */
        osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
        /** Get the PrimitiveSetList for the wall face. */
        osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
        /** Get et the PrimitiveSetList for the back face. */
        osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; }

        /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
        virtual void setThreadSafeRefUnref(bool threadSafe);

    protected:

        osg::ref_ptr<Style>             _style;
        osg::ref_ptr<osg::Geode>        _geode;
        osg::ref_ptr<osg::Geometry>     _geometry;
        osg::ref_ptr<osg::Vec3Array>    _vertices;
        osg::ref_ptr<osg::Vec3Array>    _normals;

        osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
        osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
        osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
};


class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
{
public:

    Glyph3D(Font* font, unsigned int glyphCode);

    Font* getFont() { return _font; }
    const Font* getFont() const { return _font; }

    unsigned int getGlyphCode() const { return _glyphCode; }

    void setWidth(float width) { _width = width; }
    float getWidth() const { return _width; }

    void setHeight(float height) { _height = height; }
    float getHeight() const { return _height; }

    void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
    const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; }

    void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
    float getHorizontalAdvance() const { return _horizontalAdvance; }

    void setVerticalBearing(const osg::Vec2& bearing) {  _verticalBearing=bearing; }
    const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }

    void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
    float getVerticalAdvance() const { return _verticalAdvance; }

    void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
    const osg::BoundingBox & getBoundingBox() const { return _bb; }


    /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
    virtual void setThreadSafeRefUnref(bool threadSafe);


    void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
    osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
    const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); }

    /** Get the PrimitiveSetList for the raw face which hasn't been tessellated. */
    osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; }
    const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const { return _rawFacePrimitiveSetList; }

    GlyphGeometry* getGlyphGeometry(const Style* style);

protected:

    virtual ~Glyph3D() {}

    Font*                       _font;
    unsigned int                _glyphCode;

    float                       _width;
    float                       _height;

    osg::Vec2                   _horizontalBearing;
    float                       _horizontalAdvance;

    osg::Vec2                   _verticalBearing;
    float                       _verticalAdvance;

    osg::BoundingBox            _bb;
//        osg::Vec2                   _advance;


    osg::ref_ptr<osg::Vec3Array>    _rawVertexArray;
    osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;

    typedef std::list< osg::ref_ptr<GlyphGeometry> >  GlyphGeometries;
    GlyphGeometries _glyphGeometries;

};


class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
{
public:

    GlyphTexture();

    const char* className() const { return "GlyphTexture"; }

    /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
    virtual int compare(const osg::StateAttribute& rhs) const;

    /** Set the margin around each glyph, to ensure that texture filtering doesn't bleed adjacent glyph's into each other.*/
    void setGlyphImageMargin(unsigned int margin) { _margin = margin; }
    unsigned int getGlyphImageMargin() const { return _margin; }

    void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; }
    float getGlyphImageMarginRatio() const { return _marginRatio; }

    bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);

    void addGlyph(Glyph* glyph,int posX, int posY);

    virtual void apply(osg::State& state) const;

    /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
    virtual void setThreadSafeRefUnref(bool threadSafe);

    /** Resize any per context GLObject buffers to specified size. */
    virtual void resizeGLObjectBuffers(unsigned int maxSize);

    /** create an image that maps all the associated Glyph's onto a single image, that is equivilant to what will be downloaded to the texture.*/
    osg::Image* createImage();
    
protected:

    virtual ~GlyphTexture();


    // parameter used to compute the size and position of empty space
    // in the texture which could accommodate new glyphs.
    int _margin;
    float _marginRatio;
    int _usedY;
    int _partUsedX;
    int _partUsedY;

    typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
    typedef std::vector< const Glyph* > GlyphPtrList;
    typedef osg::buffered_object< GlyphPtrList > GlyphBuffer;

    GlyphRefList                _glyphs;
    mutable GlyphBuffer         _glyphsToSubload;

    mutable OpenThreads::Mutex  _mutex;

};

}


#endif