| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGTEXT_GLYPH |
|---|
| 15 | #define OSGTEXT_GLYPH 1 |
|---|
| 16 | |
|---|
| 17 | #include <string> |
|---|
| 18 | #include <istream> |
|---|
| 19 | |
|---|
| 20 | #include <osg/Vec2> |
|---|
| 21 | #include <osg/Image> |
|---|
| 22 | #include <osg/Texture2D> |
|---|
| 23 | #include <osg/StateSet> |
|---|
| 24 | #include <osg/Geometry> |
|---|
| 25 | #include <osg/Geode> |
|---|
| 26 | |
|---|
| 27 | #include <osgText/Export> |
|---|
| 28 | #include <osgText/KerningType> |
|---|
| 29 | #include <osgText/Style> |
|---|
| 30 | |
|---|
| 31 | #include <OpenThreads/Mutex> |
|---|
| 32 | |
|---|
| 33 | namespace osgText { |
|---|
| 34 | |
|---|
| 35 | class Font; |
|---|
| 36 | class Text; |
|---|
| 37 | class Glyph3D; |
|---|
| 38 | class GlyphGeometry; |
|---|
| 39 | class GlyphTexture; |
|---|
| 40 | |
|---|
| 41 | class OSGTEXT_EXPORT Glyph : public osg::Image |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | |
|---|
| 45 | Glyph(Font* font, unsigned int glyphCode); |
|---|
| 46 | |
|---|
| 47 | Font* getFont() { return _font; } |
|---|
| 48 | const Font* getFont() const { return _font; } |
|---|
| 49 | |
|---|
| 50 | unsigned int getGlyphCode() const { return _glyphCode; } |
|---|
| 51 | |
|---|
| 52 | void setWidth(float width) { _width = width; } |
|---|
| 53 | float getWidth() const { return _width; } |
|---|
| 54 | |
|---|
| 55 | void setHeight(float height) { _height = height; } |
|---|
| 56 | float getHeight() const { return _height; } |
|---|
| 57 | |
|---|
| 58 | void setHorizontalBearing(const osg::Vec2& bearing); |
|---|
| 59 | const osg::Vec2& getHorizontalBearing() const; |
|---|
| 60 | |
|---|
| 61 | void setHorizontalAdvance(float advance); |
|---|
| 62 | float getHorizontalAdvance() const; |
|---|
| 63 | |
|---|
| 64 | void setVerticalBearing(const osg::Vec2& bearing); |
|---|
| 65 | const osg::Vec2& getVerticalBearing() const; |
|---|
| 66 | |
|---|
| 67 | void setVerticalAdvance(float advance); |
|---|
| 68 | float getVerticalAdvance() const; |
|---|
| 69 | |
|---|
| 70 | void setTexture(GlyphTexture* texture); |
|---|
| 71 | GlyphTexture* getTexture(); |
|---|
| 72 | const GlyphTexture* getTexture() const; |
|---|
| 73 | |
|---|
| 74 | void setTexturePosition(int posX,int posY); |
|---|
| 75 | int getTexturePositionX() const; |
|---|
| 76 | int getTexturePositionY() const; |
|---|
| 77 | |
|---|
| 78 | void setMinTexCoord(const osg::Vec2& coord); |
|---|
| 79 | const osg::Vec2& getMinTexCoord() const; |
|---|
| 80 | |
|---|
| 81 | void setMaxTexCoord(const osg::Vec2& coord); |
|---|
| 82 | const osg::Vec2& getMaxTexCoord() const; |
|---|
| 83 | |
|---|
| 84 | void subload() const; |
|---|
| 85 | |
|---|
| 86 | protected: |
|---|
| 87 | |
|---|
| 88 | virtual ~Glyph(); |
|---|
| 89 | |
|---|
| 90 | Font* _font; |
|---|
| 91 | unsigned int _glyphCode; |
|---|
| 92 | |
|---|
| 93 | float _width; |
|---|
| 94 | float _height; |
|---|
| 95 | |
|---|
| 96 | osg::Vec2 _horizontalBearing; |
|---|
| 97 | float _horizontalAdvance; |
|---|
| 98 | |
|---|
| 99 | osg::Vec2 _verticalBearing; |
|---|
| 100 | float _verticalAdvance; |
|---|
| 101 | |
|---|
| 102 | GlyphTexture* _texture; |
|---|
| 103 | int _texturePosX; |
|---|
| 104 | int _texturePosY; |
|---|
| 105 | osg::Vec2 _minTexCoord; |
|---|
| 106 | osg::Vec2 _maxTexCoord; |
|---|
| 107 | |
|---|
| 108 | typedef osg::buffered_value<GLuint> GLObjectList; |
|---|
| 109 | mutable GLObjectList _globjList; |
|---|
| 110 | |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced |
|---|
| 114 | { |
|---|
| 115 | public: |
|---|
| 116 | |
|---|
| 117 | GlyphGeometry(); |
|---|
| 118 | |
|---|
| 119 | void setup(const Glyph3D* glyph, const Style* style); |
|---|
| 120 | |
|---|
| 121 | bool match(const Style* style) const; |
|---|
| 122 | |
|---|
| 123 | osg::Geode* getGeode() const { return _geode.get(); } |
|---|
| 124 | osg::Geometry* getGeometry() const { return _geometry.get(); } |
|---|
| 125 | |
|---|
| 126 | /** Set the VertexArray of the glyph. */ |
|---|
| 127 | void setVertexArray(osg::Vec3Array * va) { _vertices = va; } |
|---|
| 128 | /** Get the VertexArray of the glyph. */ |
|---|
| 129 | osg::Vec3Array * getVertexArray() const { return _vertices.get(); } |
|---|
| 130 | |
|---|
| 131 | /** Set the VertexArray of the glyph. */ |
|---|
| 132 | void setNormalArray(osg::Vec3Array* na) { _normals = na; } |
|---|
| 133 | /** Get the NormalArray for the wall face. */ |
|---|
| 134 | osg::Vec3Array* getNormalArray() const { return _normals.get(); } |
|---|
| 135 | |
|---|
| 136 | /** Get the PrimitiveSetList for the front face. */ |
|---|
| 137 | osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; } |
|---|
| 138 | /** Get the PrimitiveSetList for the wall face. */ |
|---|
| 139 | osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; } |
|---|
| 140 | /** Get et the PrimitiveSetList for the back face. */ |
|---|
| 141 | osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; } |
|---|
| 142 | |
|---|
| 143 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 144 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 145 | |
|---|
| 146 | protected: |
|---|
| 147 | |
|---|
| 148 | osg::ref_ptr<Style> _style; |
|---|
| 149 | osg::ref_ptr<osg::Geode> _geode; |
|---|
| 150 | osg::ref_ptr<osg::Geometry> _geometry; |
|---|
| 151 | osg::ref_ptr<osg::Vec3Array> _vertices; |
|---|
| 152 | osg::ref_ptr<osg::Vec3Array> _normals; |
|---|
| 153 | |
|---|
| 154 | osg::Geometry::PrimitiveSetList _frontPrimitiveSetList; |
|---|
| 155 | osg::Geometry::PrimitiveSetList _wallPrimitiveSetList; |
|---|
| 156 | osg::Geometry::PrimitiveSetList _backPrimitiveSetList; |
|---|
| 157 | }; |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | class OSGTEXT_EXPORT Glyph3D : public osg::Referenced |
|---|
| 161 | { |
|---|
| 162 | public: |
|---|
| 163 | |
|---|
| 164 | Glyph3D(Font* font, unsigned int glyphCode); |
|---|
| 165 | |
|---|
| 166 | Font* getFont() { return _font; } |
|---|
| 167 | const Font* getFont() const { return _font; } |
|---|
| 168 | |
|---|
| 169 | unsigned int getGlyphCode() const { return _glyphCode; } |
|---|
| 170 | |
|---|
| 171 | void setWidth(float width) { _width = width; } |
|---|
| 172 | float getWidth() const { return _width; } |
|---|
| 173 | |
|---|
| 174 | void setHeight(float height) { _height = height; } |
|---|
| 175 | float getHeight() const { return _height; } |
|---|
| 176 | |
|---|
| 177 | void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; } |
|---|
| 178 | const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; } |
|---|
| 179 | |
|---|
| 180 | void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; } |
|---|
| 181 | float getHorizontalAdvance() const { return _horizontalAdvance; } |
|---|
| 182 | |
|---|
| 183 | void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; } |
|---|
| 184 | const osg::Vec2& getVerticalBearing() const { return _verticalBearing; } |
|---|
| 185 | |
|---|
| 186 | void setVerticalAdvance(float advance) { _verticalAdvance=advance; } |
|---|
| 187 | float getVerticalAdvance() const { return _verticalAdvance; } |
|---|
| 188 | |
|---|
| 189 | void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; } |
|---|
| 190 | const osg::BoundingBox & getBoundingBox() const { return _bb; } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 194 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; } |
|---|
| 198 | osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); } |
|---|
| 199 | const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); } |
|---|
| 200 | |
|---|
| 201 | /** Get the PrimitiveSetList for the raw face which hasn't been tessellated. */ |
|---|
| 202 | osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; } |
|---|
| 203 | const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const { return _rawFacePrimitiveSetList; } |
|---|
| 204 | |
|---|
| 205 | GlyphGeometry* getGlyphGeometry(const Style* style); |
|---|
| 206 | |
|---|
| 207 | protected: |
|---|
| 208 | |
|---|
| 209 | virtual ~Glyph3D() {} |
|---|
| 210 | |
|---|
| 211 | Font* _font; |
|---|
| 212 | unsigned int _glyphCode; |
|---|
| 213 | |
|---|
| 214 | float _width; |
|---|
| 215 | float _height; |
|---|
| 216 | |
|---|
| 217 | osg::Vec2 _horizontalBearing; |
|---|
| 218 | float _horizontalAdvance; |
|---|
| 219 | |
|---|
| 220 | osg::Vec2 _verticalBearing; |
|---|
| 221 | float _verticalAdvance; |
|---|
| 222 | |
|---|
| 223 | osg::BoundingBox _bb; |
|---|
| 224 | // osg::Vec2 _advance; |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | osg::ref_ptr<osg::Vec3Array> _rawVertexArray; |
|---|
| 228 | osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList; |
|---|
| 229 | |
|---|
| 230 | typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries; |
|---|
| 231 | GlyphGeometries _glyphGeometries; |
|---|
| 232 | |
|---|
| 233 | }; |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D |
|---|
| 237 | { |
|---|
| 238 | public: |
|---|
| 239 | |
|---|
| 240 | GlyphTexture(); |
|---|
| 241 | |
|---|
| 242 | const char* className() const { return "GlyphTexture"; } |
|---|
| 243 | |
|---|
| 244 | /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/ |
|---|
| 245 | virtual int compare(const osg::StateAttribute& rhs) const; |
|---|
| 246 | |
|---|
| 247 | /** Set the margin around each glyph, to ensure that texture filtering doesn't bleed adjacent glyph's into each other.*/ |
|---|
| 248 | void setGlyphImageMargin(unsigned int margin) { _margin = margin; } |
|---|
| 249 | unsigned int getGlyphImageMargin() const { return _margin; } |
|---|
| 250 | |
|---|
| 251 | void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; } |
|---|
| 252 | float getGlyphImageMarginRatio() const { return _marginRatio; } |
|---|
| 253 | |
|---|
| 254 | bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY); |
|---|
| 255 | |
|---|
| 256 | void addGlyph(Glyph* glyph,int posX, int posY); |
|---|
| 257 | |
|---|
| 258 | virtual void apply(osg::State& state) const; |
|---|
| 259 | |
|---|
| 260 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 261 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 262 | |
|---|
| 263 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 264 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 265 | |
|---|
| 266 | /** 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.*/ |
|---|
| 267 | osg::Image* createImage(); |
|---|
| 268 | |
|---|
| 269 | protected: |
|---|
| 270 | |
|---|
| 271 | virtual ~GlyphTexture(); |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | // parameter used to compute the size and position of empty space |
|---|
| 275 | // in the texture which could accommodate new glyphs. |
|---|
| 276 | int _margin; |
|---|
| 277 | float _marginRatio; |
|---|
| 278 | int _usedY; |
|---|
| 279 | int _partUsedX; |
|---|
| 280 | int _partUsedY; |
|---|
| 281 | |
|---|
| 282 | typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList; |
|---|
| 283 | typedef std::vector< const Glyph* > GlyphPtrList; |
|---|
| 284 | typedef osg::buffered_object< GlyphPtrList > GlyphBuffer; |
|---|
| 285 | |
|---|
| 286 | GlyphRefList _glyphs; |
|---|
| 287 | mutable GlyphBuffer _glyphsToSubload; |
|---|
| 288 | |
|---|
| 289 | mutable OpenThreads::Mutex _mutex; |
|---|
| 290 | |
|---|
| 291 | }; |
|---|
| 292 | |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | #endif |
|---|