OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Glyph.h
Go to the documentation of this file.
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 // GL_ALPHA is deprecated in GL3/GL4 core profile, use GL_RED and a shader in this case. See osgText example.
34 #if defined(OSG_GL3_AVAILABLE) && !defined(OSG_GL2_AVAILABLE) && !defined(OSG_GL1_AVAILABLE)
35 #define OSGTEXT_GLYPH_FORMAT GL_RED
36 #define OSGTEXT_GLYPH_INTERNALFORMAT GL_R8
37 #else
38 #define OSGTEXT_GLYPH_FORMAT GL_ALPHA
39 #define OSGTEXT_GLYPH_INTERNALFORMAT GL_ALPHA
40 #endif
41 
42 namespace osgText {
43 
44 class Font;
45 class Text;
46 class Glyph3D;
47 class GlyphGeometry;
48 class GlyphTexture;
49 
51 {
52 public:
53 
54  Glyph(Font* font, unsigned int glyphCode);
55 
56  Font* getFont() { return _font; }
57  const Font* getFont() const { return _font; }
58 
59  unsigned int getGlyphCode() const { return _glyphCode; }
60 
61  void setWidth(float width) { _width = width; }
62  float getWidth() const { return _width; }
63 
64  void setHeight(float height) { _height = height; }
65  float getHeight() const { return _height; }
66 
67  void setHorizontalBearing(const osg::Vec2& bearing);
68  const osg::Vec2& getHorizontalBearing() const;
69 
70  void setHorizontalAdvance(float advance);
71  float getHorizontalAdvance() const;
72 
73  void setVerticalBearing(const osg::Vec2& bearing);
74  const osg::Vec2& getVerticalBearing() const;
75 
76  void setVerticalAdvance(float advance);
77  float getVerticalAdvance() const;
78 
79  void setTexture(GlyphTexture* texture);
80  GlyphTexture* getTexture();
81  const GlyphTexture* getTexture() const;
82 
83  void setTexturePosition(int posX,int posY);
84  int getTexturePositionX() const;
85  int getTexturePositionY() const;
86 
87  void setMinTexCoord(const osg::Vec2& coord);
88  const osg::Vec2& getMinTexCoord() const;
89 
90  void setMaxTexCoord(const osg::Vec2& coord);
91  const osg::Vec2& getMaxTexCoord() const;
92 
93  void subload() const;
94 
95 protected:
96 
97  virtual ~Glyph();
98 
100  unsigned int _glyphCode;
101 
102  float _width;
103  float _height;
104 
107 
110 
116 
118  mutable GLObjectList _globjList;
119 
120 };
121 
123 {
124  public:
125 
126  GlyphGeometry();
127 
128  void setup(const Glyph3D* glyph, const Style* style);
129 
130  bool match(const Style* style) const;
131 
132  osg::Geode* getGeode() const { return _geode.get(); }
133  osg::Geometry* getGeometry() const { return _geometry.get(); }
134 
136  void setVertexArray(osg::Vec3Array * va) { _vertices = va; }
138  osg::Vec3Array * getVertexArray() const { return _vertices.get(); }
139 
141  void setNormalArray(osg::Vec3Array* na) { _normals = na; }
143  osg::Vec3Array* getNormalArray() const { return _normals.get(); }
144 
146  osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
148  osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
150  osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; }
151 
153  virtual void setThreadSafeRefUnref(bool threadSafe);
154 
155  protected:
156 
162 
166 };
167 
168 
170 {
171 public:
172 
173  Glyph3D(Font* font, unsigned int glyphCode);
174 
175  Font* getFont() { return _font; }
176  const Font* getFont() const { return _font; }
177 
178  unsigned int getGlyphCode() const { return _glyphCode; }
179 
180  void setWidth(float width) { _width = width; }
181  float getWidth() const { return _width; }
182 
183  void setHeight(float height) { _height = height; }
184  float getHeight() const { return _height; }
185 
186  void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
187  const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; }
188 
189  void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
190  float getHorizontalAdvance() const { return _horizontalAdvance; }
191 
192  void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
193  const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }
194 
195  void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
196  float getVerticalAdvance() const { return _verticalAdvance; }
197 
198  void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
199  const osg::BoundingBox & getBoundingBox() const { return _bb; }
200 
201 
203  virtual void setThreadSafeRefUnref(bool threadSafe);
204 
205 
206  void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
207  osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
208  const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); }
209 
211  osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; }
212  const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const { return _rawFacePrimitiveSetList; }
213 
214  GlyphGeometry* getGlyphGeometry(const Style* style);
215 
216 protected:
217 
218  virtual ~Glyph3D() {}
219 
221  unsigned int _glyphCode;
222 
223  float _width;
224  float _height;
225 
228 
231 
233 // osg::Vec2 _advance;
234 
235 
238 
239  typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries;
240  GlyphGeometries _glyphGeometries;
241 
242 };
243 
244 
246 {
247 public:
248 
249  GlyphTexture();
250 
251  const char* className() const { return "GlyphTexture"; }
252 
254  virtual int compare(const osg::StateAttribute& rhs) const;
255 
257  void setGlyphImageMargin(unsigned int margin) { _margin = margin; }
258  unsigned int getGlyphImageMargin() const { return _margin; }
259 
260  void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; }
261  float getGlyphImageMarginRatio() const { return _marginRatio; }
262 
263  bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
264 
265  void addGlyph(Glyph* glyph,int posX, int posY);
266 
267  virtual void apply(osg::State& state) const;
268 
270  virtual void setThreadSafeRefUnref(bool threadSafe);
271 
273  virtual void resizeGLObjectBuffers(unsigned int maxSize);
274 
276  osg::Image* createImage();
277 
278 protected:
279 
280  virtual ~GlyphTexture();
281 
282 
283  // parameter used to compute the size and position of empty space
284  // in the texture which could accommodate new glyphs.
285  int _margin;
287  int _usedY;
290 
291  typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
292  typedef std::vector< const Glyph* > GlyphPtrList;
294 
295  GlyphRefList _glyphs;
296  mutable GlyphBuffer _glyphsToSubload;
297 
299 
300 };
301 
302 }
303 
304 
305 #endif
void setRawVertexArray(osg::Vec3Array *vertices)
Definition: Glyph.h:206
const osg::Vec2 & getHorizontalBearing() const
Definition: Glyph.h:187
std::vector< ref_ptr< PrimitiveSet > > PrimitiveSetList
Definition: Geometry.h:102
osg::Vec3Array * getNormalArray() const
Definition: Glyph.h:143
float getHorizontalAdvance() const
Definition: Glyph.h:190
osg::ref_ptr< osg::Vec3Array > _rawVertexArray
Definition: Glyph.h:236
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
void setBoundingBox(osg::BoundingBox &bb)
Definition: Glyph.h:198
int _texturePosY
Definition: Glyph.h:113
float _verticalAdvance
Definition: Glyph.h:109
osg::buffered_value< GLuint > GLObjectList
Definition: Glyph.h:117
unsigned int _glyphCode
Definition: Glyph.h:221
osg::Vec2 _horizontalBearing
Definition: Glyph.h:105
float _verticalAdvance
Definition: Glyph.h:230
osg::Geometry * getGeometry() const
Definition: Glyph.h:133
osg::Geometry::PrimitiveSetList & getWallPrimitiveSetList()
Definition: Glyph.h:148
virtual ~Glyph3D()
Definition: Glyph.h:218
osg::Geometry::PrimitiveSetList & getFrontPrimitiveSetList()
Definition: Glyph.h:146
const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const
Definition: Glyph.h:212
void setNormalArray(osg::Vec3Array *na)
Definition: Glyph.h:141
osg::Vec2 _maxTexCoord
Definition: Glyph.h:115
const osg::Vec3Array * getRawVertexArray() const
Definition: Glyph.h:208
float getHeight() const
Definition: Glyph.h:184
float _height
Definition: Glyph.h:103
float getHeight() const
Definition: Glyph.h:65
GlyphTexture * _texture
Definition: Glyph.h:111
osg::Geode * getGeode() const
Definition: Glyph.h:132
float _width
Definition: Glyph.h:223
void setHorizontalBearing(const osg::Vec2 &bearing)
Definition: Glyph.h:186
Font * _font
Definition: Glyph.h:99
float getWidth() const
Definition: Glyph.h:181
osg::ref_ptr< osg::Vec3Array > _vertices
Definition: Glyph.h:160
void setVerticalAdvance(float advance)
Definition: Glyph.h:195
GlyphBuffer _glyphsToSubload
Definition: Glyph.h:296
const char * className() const
Definition: Glyph.h:251
osg::Vec2 _minTexCoord
Definition: Glyph.h:114
Font * _font
Definition: Glyph.h:220
void setWidth(float width)
Definition: Glyph.h:61
unsigned int getGlyphImageMargin() const
Definition: Glyph.h:258
osg::Vec2 _verticalBearing
Definition: Glyph.h:108
GlyphRefList _glyphs
Definition: Glyph.h:295
unsigned int getGlyphCode() const
Definition: Glyph.h:178
void setHeight(float height)
Definition: Glyph.h:64
std::vector< osg::ref_ptr< Glyph > > GlyphRefList
Definition: Glyph.h:291
std::list< osg::ref_ptr< GlyphGeometry > > GlyphGeometries
Definition: Glyph.h:239
void setGlyphImageMargin(unsigned int margin)
Definition: Glyph.h:257
const osg::BoundingBox & getBoundingBox() const
Definition: Glyph.h:199
float _width
Definition: Glyph.h:102
float getGlyphImageMarginRatio() const
Definition: Glyph.h:261
osg::ref_ptr< osg::Vec3Array > _normals
Definition: Glyph.h:161
osg::Vec2 _verticalBearing
Definition: Glyph.h:229
osg::Geometry::PrimitiveSetList _wallPrimitiveSetList
Definition: Glyph.h:164
GLint GLenum GLsizei width
Definition: GLU.h:71
OpenThreads::Mutex _mutex
Definition: Glyph.h:298
osg::ref_ptr< osg::Geometry > _geometry
Definition: Glyph.h:159
unsigned int getGlyphCode() const
Definition: Glyph.h:59
float _horizontalAdvance
Definition: Glyph.h:106
Font * getFont()
Definition: Glyph.h:56
const osg::Vec2 & getVerticalBearing() const
Definition: Glyph.h:193
void setHorizontalAdvance(float advance)
Definition: Glyph.h:189
osg::Vec2 _horizontalBearing
Definition: Glyph.h:226
unsigned int _glyphCode
Definition: Glyph.h:100
osg::buffered_object< GlyphPtrList > GlyphBuffer
Definition: Glyph.h:293
float _height
Definition: Glyph.h:224
osg::BoundingBox _bb
Definition: Glyph.h:232
osg::ref_ptr< Style > _style
Definition: Glyph.h:157
GLint GLenum GLsizei GLsizei height
Definition: GLU.h:71
osg::ref_ptr< osg::Geode > _geode
Definition: Glyph.h:158
GLObjectList _globjList
Definition: Glyph.h:118
void setGlyphImageMarginRatio(float margin)
Definition: Glyph.h:260
int _texturePosX
Definition: Glyph.h:112
std::vector< const Glyph * > GlyphPtrList
Definition: Glyph.h:292
osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList
Definition: Glyph.h:237
osg::Vec3Array * getRawVertexArray()
Definition: Glyph.h:207
const Font * getFont() const
Definition: Glyph.h:57
void setVerticalBearing(const osg::Vec2 &bearing)
Definition: Glyph.h:192
void setHeight(float height)
Definition: Glyph.h:183
osg::Geometry::PrimitiveSetList _backPrimitiveSetList
Definition: Glyph.h:165
osg::Geometry::PrimitiveSetList _frontPrimitiveSetList
Definition: Glyph.h:163
void setVertexArray(osg::Vec3Array *va)
Definition: Glyph.h:136
osg::Geometry::PrimitiveSetList & getBackPrimitiveSetList()
Definition: Glyph.h:150
osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList()
Definition: Glyph.h:211
const Font * getFont() const
Definition: Glyph.h:176
float getWidth() const
Definition: Glyph.h:62
void setWidth(float width)
Definition: Glyph.h:180
osg::Vec3Array * getVertexArray() const
Definition: Glyph.h:138
float getVerticalAdvance() const
Definition: Glyph.h:196
GlyphGeometries _glyphGeometries
Definition: Glyph.h:240
#define OSGTEXT_EXPORT
Definition: Export.h:39
Font * getFont()
Definition: Glyph.h:175
float _horizontalAdvance
Definition: Glyph.h:227