openscenegraph
Glyph
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/ReentrantMutex>
32
33namespace osgText {
34
35class Font;
36class Text;
37class Glyph3D;
38class GlyphGeometry;
39class GlyphTexture;
40
41enum ShaderTechnique
42{
43 NO_TEXT_SHADER = 0x0,
44 GREYSCALE = 0x1,
45 SIGNED_DISTANCE_FIELD = 0x2,
46 ALL_FEATURES = GREYSCALE | SIGNED_DISTANCE_FIELD
47};
48
49class OSGTEXT_EXPORT Glyph : public osg::Image
50{
51public:
52
53 Glyph(Font* font, unsigned int glyphCode);
54
55 Font* getFont() { return _font; }
56 const Font* getFont() const { return _font; }
57
58 unsigned int getGlyphCode() const { return _glyphCode; }
59
60 void setFontResolution(const FontResolution& fontRes) { _fontResolution = fontRes; }
61 const FontResolution& getFontResolution() const { return _fontResolution; }
62
63 void setWidth(float width) { _width = width; }
64 float getWidth() const { return _width; }
65
66 void setHeight(float height) { _height = height; }
67 float getHeight() const { return _height; }
68
69 void setHorizontalBearing(const osg::Vec2& bearing);
70 const osg::Vec2& getHorizontalBearing() const;
71
72 void setHorizontalAdvance(float advance);
73 float getHorizontalAdvance() const;
74
75 void setVerticalBearing(const osg::Vec2& bearing);
76 const osg::Vec2& getVerticalBearing() const;
77
78 void setVerticalAdvance(float advance);
79 float getVerticalAdvance() const;
80
81 struct TextureInfo : public osg::Referenced
82 {
83 TextureInfo():
84 texture(0),
85 texelMargin(0.0f) {}
86
87 TextureInfo(GlyphTexture* tex, int x, int y, const osg::Vec2& mintc, const osg::Vec2& maxtc, float margin):
88 texture(tex),
89 texturePositionX(x),
90 texturePositionY(y),
91 minTexCoord(mintc),
92 maxTexCoord(maxtc),
93 texelMargin(margin) {}
94
95 GlyphTexture* texture;
96 int texturePositionX;
97 int texturePositionY;
98 osg::Vec2 minTexCoord;
99 osg::Vec2 maxTexCoord;
100 float texelMargin;
101 };
102
103 void setTextureInfo(ShaderTechnique technique, TextureInfo* info);
104
105 const TextureInfo* getTextureInfo(ShaderTechnique technique) const;
106
107 TextureInfo* getOrCreateTextureInfo(ShaderTechnique technique);
108
109protected:
110
111 virtual ~Glyph();
112
113 Font* _font;
114 unsigned int _glyphCode;
115
116 FontResolution _fontResolution;
117
118 float _width;
119 float _height;
120
121 osg::Vec2 _horizontalBearing;
122 float _horizontalAdvance;
123
124 osg::Vec2 _verticalBearing;
125 float _verticalAdvance;
126
127 typedef std::vector< osg::ref_ptr<TextureInfo> > TextureInfoList;
128 TextureInfoList _textureInfoList;
129
130 mutable OpenThreads::ReentrantMutex _textureInfoListMutex;
131};
132
133class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
134{
135 public:
136
137 GlyphGeometry();
138
139 void setup(const Glyph3D* glyph, const Style* style);
140
141 bool match(const Style* style) const;
142
143 osg::Geode* getGeode() const { return _geode.get(); }
144 osg::Geometry* getGeometry() const { return _geometry.get(); }
145
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(); }
150
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(); }
155
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; }
162
163 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
164 virtual void setThreadSafeRefUnref(bool threadSafe);
165
166 protected:
167
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;
173
174 osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
175 osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
176 osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
177};
178
179
180class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
181{
182public:
183
184 Glyph3D(Font* font, unsigned int glyphCode);
185
186 Font* getFont() { return _font; }
187 const Font* getFont() const { return _font; }
188
189 unsigned int getGlyphCode() const { return _glyphCode; }
190
191 void setWidth(float width) { _width = width; }
192 float getWidth() const { return _width; }
193
194 void setHeight(float height) { _height = height; }
195 float getHeight() const { return _height; }
196
197 void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
198 const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; }
199
200 void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
201 float getHorizontalAdvance() const { return _horizontalAdvance; }
202
203 void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
204 const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }
205
206 void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
207 float getVerticalAdvance() const { return _verticalAdvance; }
208
209 void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
210 const osg::BoundingBox & getBoundingBox() const { return _bb; }
211
212
213 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
214 virtual void setThreadSafeRefUnref(bool threadSafe);
215
216
217 void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
218 osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
219 const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); }
220
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; }
224
225 GlyphGeometry* getGlyphGeometry(const Style* style);
226
227protected:
228
229 virtual ~Glyph3D() {}
230
231 Font* _font;
232 unsigned int _glyphCode;
233
234 float _width;
235 float _height;
236
237 osg::Vec2 _horizontalBearing;
238 float _horizontalAdvance;
239
240 osg::Vec2 _verticalBearing;
241 float _verticalAdvance;
242
243 osg::BoundingBox _bb;
244// osg::Vec2 _advance;
245
246
247 osg::ref_ptr<osg::Vec3Array> _rawVertexArray;
248 osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;
249
250 typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries;
251 GlyphGeometries _glyphGeometries;
252
253};
254
255
256class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
257{
258public:
259
260 GlyphTexture();
261
262 const char* className() const { return "GlyphTexture"; }
263
264 /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
265 virtual int compare(const osg::StateAttribute& rhs) const;
266
267 void setShaderTechnique(ShaderTechnique technique) { _shaderTechnique = technique; }
268
269 ShaderTechnique getShaderTechnique() const { return _shaderTechnique; }
270
271
272 int getEffectMargin(const Glyph* glyph);
273 int getTexelMargin(const Glyph* glyph);
274
275 bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
276
277 void addGlyph(Glyph* glyph,int posX, int posY);
278
279 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
280 virtual void setThreadSafeRefUnref(bool threadSafe);
281
282 /** Resize any per context GLObject buffers to specified size. */
283 virtual void resizeGLObjectBuffers(unsigned int maxSize);
284
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();
287
288protected:
289
290 virtual ~GlyphTexture();
291
292 void copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info);
293
294 ShaderTechnique _shaderTechnique;
295
296 int _usedY;
297 int _partUsedX;
298 int _partUsedY;
299
300 typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
301 typedef std::vector< const Glyph* > GlyphPtrList;
302 typedef osg::buffered_object< GlyphPtrList > GlyphBuffer;
303
304 GlyphRefList _glyphs;
305 mutable GlyphBuffer _glyphsToSubload;
306
307 mutable OpenThreads::Mutex _mutex;
308
309};
310
311}
312
313
314#endif