1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
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.
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.
15#define OSGTEXT_GLYPH 1
22#include <osg/Texture2D>
23#include <osg/StateSet>
24#include <osg/Geometry>
27#include <osgText/Export>
28#include <osgText/KerningType>
29#include <osgText/Style>
31#include <OpenThreads/ReentrantMutex>
45 SIGNED_DISTANCE_FIELD = 0x2,
46 ALL_FEATURES = GREYSCALE | SIGNED_DISTANCE_FIELD
49class OSGTEXT_EXPORT Glyph : public osg::Image
53 Glyph(Font* font, unsigned int glyphCode);
55 Font* getFont() { return _font; }
56 const Font* getFont() const { return _font; }
58 unsigned int getGlyphCode() const { return _glyphCode; }
60 void setFontResolution(const FontResolution& fontRes) { _fontResolution = fontRes; }
61 const FontResolution& getFontResolution() const { return _fontResolution; }
63 void setWidth(float width) { _width = width; }
64 float getWidth() const { return _width; }
66 void setHeight(float height) { _height = height; }
67 float getHeight() const { return _height; }
69 void setHorizontalBearing(const osg::Vec2& bearing);
70 const osg::Vec2& getHorizontalBearing() const;
72 void setHorizontalAdvance(float advance);
73 float getHorizontalAdvance() const;
75 void setVerticalBearing(const osg::Vec2& bearing);
76 const osg::Vec2& getVerticalBearing() const;
78 void setVerticalAdvance(float advance);
79 float getVerticalAdvance() const;
81 struct TextureInfo : public osg::Referenced
87 TextureInfo(GlyphTexture* tex, int x, int y, const osg::Vec2& mintc, const osg::Vec2& maxtc, float margin):
93 texelMargin(margin) {}
95 GlyphTexture* texture;
98 osg::Vec2 minTexCoord;
99 osg::Vec2 maxTexCoord;
103 void setTextureInfo(ShaderTechnique technique, TextureInfo* info);
105 const TextureInfo* getTextureInfo(ShaderTechnique technique) const;
107 TextureInfo* getOrCreateTextureInfo(ShaderTechnique technique);
114 unsigned int _glyphCode;
116 FontResolution _fontResolution;
121 osg::Vec2 _horizontalBearing;
122 float _horizontalAdvance;
124 osg::Vec2 _verticalBearing;
125 float _verticalAdvance;
127 typedef std::vector< osg::ref_ptr<TextureInfo> > TextureInfoList;
128 TextureInfoList _textureInfoList;
130 mutable OpenThreads::ReentrantMutex _textureInfoListMutex;
133class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
139 void setup(const Glyph3D* glyph, const Style* style);
141 bool match(const Style* style) const;
143 osg::Geode* getGeode() const { return _geode.get(); }
144 osg::Geometry* getGeometry() const { return _geometry.get(); }
146 /** Set the VertexArray of the glyph. */
147 void setVertexArray(osg::Vec3Array * va) { _vertices = va; }
148 /** Get the VertexArray of the glyph. */
149 osg::Vec3Array * getVertexArray() const { return _vertices.get(); }
151 /** Set the VertexArray of the glyph. */
152 void setNormalArray(osg::Vec3Array* na) { _normals = na; }
153 /** Get the NormalArray for the wall face. */
154 osg::Vec3Array* getNormalArray() const { return _normals.get(); }
156 /** Get the PrimitiveSetList for the front face. */
157 osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
158 /** Get the PrimitiveSetList for the wall face. */
159 osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
160 /** Get et the PrimitiveSetList for the back face. */
161 osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; }
163 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
164 virtual void setThreadSafeRefUnref(bool threadSafe);
168 osg::ref_ptr<Style> _style;
169 osg::ref_ptr<osg::Geode> _geode;
170 osg::ref_ptr<osg::Geometry> _geometry;
171 osg::ref_ptr<osg::Vec3Array> _vertices;
172 osg::ref_ptr<osg::Vec3Array> _normals;
174 osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
175 osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
176 osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
180class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
184 Glyph3D(Font* font, unsigned int glyphCode);
186 Font* getFont() { return _font; }
187 const Font* getFont() const { return _font; }
189 unsigned int getGlyphCode() const { return _glyphCode; }
191 void setWidth(float width) { _width = width; }
192 float getWidth() const { return _width; }
194 void setHeight(float height) { _height = height; }
195 float getHeight() const { return _height; }
197 void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
198 const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; }
200 void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
201 float getHorizontalAdvance() const { return _horizontalAdvance; }
203 void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
204 const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }
206 void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
207 float getVerticalAdvance() const { return _verticalAdvance; }
209 void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
210 const osg::BoundingBox & getBoundingBox() const { return _bb; }
213 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
214 virtual void setThreadSafeRefUnref(bool threadSafe);
217 void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
218 osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
219 const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); }
221 /** Get the PrimitiveSetList for the raw face which hasn't been tessellated. */
222 osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; }
223 const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const { return _rawFacePrimitiveSetList; }
225 GlyphGeometry* getGlyphGeometry(const Style* style);
229 virtual ~Glyph3D() {}
232 unsigned int _glyphCode;
237 osg::Vec2 _horizontalBearing;
238 float _horizontalAdvance;
240 osg::Vec2 _verticalBearing;
241 float _verticalAdvance;
243 osg::BoundingBox _bb;
244// osg::Vec2 _advance;
247 osg::ref_ptr<osg::Vec3Array> _rawVertexArray;
248 osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;
250 typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries;
251 GlyphGeometries _glyphGeometries;
256class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
262 const char* className() const { return "GlyphTexture"; }
264 /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
265 virtual int compare(const osg::StateAttribute& rhs) const;
267 void setShaderTechnique(ShaderTechnique technique) { _shaderTechnique = technique; }
269 ShaderTechnique getShaderTechnique() const { return _shaderTechnique; }
272 int getEffectMargin(const Glyph* glyph);
273 int getTexelMargin(const Glyph* glyph);
275 bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
277 void addGlyph(Glyph* glyph,int posX, int posY);
279 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
280 virtual void setThreadSafeRefUnref(bool threadSafe);
282 /** Resize any per context GLObject buffers to specified size. */
283 virtual void resizeGLObjectBuffers(unsigned int maxSize);
285 /** create an image that maps all the associated Glyph's onto a single image, that is equivalent to what will be downloaded to the texture.*/
286 osg::Image* createImage();
290 virtual ~GlyphTexture();
292 void copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info);
294 ShaderTechnique _shaderTechnique;
300 typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
301 typedef std::vector< const Glyph* > GlyphPtrList;
302 typedef osg::buffered_object< GlyphPtrList > GlyphBuffer;
304 GlyphRefList _glyphs;
305 mutable GlyphBuffer _glyphsToSubload;
307 mutable OpenThreads::Mutex _mutex;