OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Font.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_FONT
15 #define OSGTEXT_FONT 1
16 
17 #include <string>
18 #include <istream>
19 
20 #include <osg/TexEnv>
21 #include <osgText/Glyph>
22 #include <osgDB/Options>
23 
24 #include <OpenThreads/Mutex>
25 
26 namespace osgText {
27 
28 // forward declare Font
29 class Font;
30 
50 extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
51 
53 extern OSGTEXT_EXPORT Font* readFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
54 
55 extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
56 
57 extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
58 
59 extern OSGTEXT_EXPORT std::string findFontFile(const std::string& str);
60 
65 {
66 // declare the interface to a font.
67 public:
68 
69  // forward declare nested classes.
70  class FontImplementation;
71 
72 public:
73  Font(FontImplementation* implementation=0);
74 
75  virtual osg::Object* cloneType() const { return 0; } // cloneType() not appropriate
76  virtual osg::Object* clone(const osg::CopyOp&) const { return 0; } // clone() not appropriate
77  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Font*>(obj)!=NULL; }
78  virtual const char* className() const { return "Font"; }
79  virtual const char* libraryName() const { return "osgText"; }
80 
81  virtual std::string getFileName() const;
82 
83  static osg::ref_ptr<Font>& getDefaultFont();
84 
85  void setTexEnv(osg::TexEnv* texenv) { if (texenv) _texenv = texenv; }
86  inline osg::TexEnv* getTexEnv() { return _texenv.get(); }
87  inline const osg::TexEnv* getTexEnv() const { return _texenv.get(); }
88 
89  void setStateSet(osg::StateSet* stateset) { _stateset = stateset; }
90  osg::StateSet* getStateSet() { return _stateset.get(); }
91  const osg::StateSet* getStateSet() const { return _stateset.get(); }
92 
93 
95  virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType);
96 
98  virtual Glyph* getGlyph(const FontResolution& fontSize, unsigned int charcode);
99 
100 
102  virtual Glyph3D* getGlyph3D(unsigned int charcode);
103 
105  virtual bool hasVertical() const;
106 
109  virtual bool getVerticalSize(float& ascender, float& descender) const { return _implementation ? _implementation->getVerticalSize(ascender, descender) : false; }
110 
114  void setGlyphImageMargin(unsigned int margin);
115  unsigned int getGlyphImageMargin() const;
116 
120  void setGlyphImageMarginRatio(float margin);
121  float getGlyphImageMarginRatio() const;
122 
123 
126  void setTextureSizeHint(unsigned int width,unsigned int height);
127 
128  unsigned int getTextureWidthHint() const;
129  unsigned int getTextureHeightHint() const;
130 
133  void setMinFilterHint(osg::Texture::FilterMode mode);
134  osg::Texture::FilterMode getMinFilterHint() const;
135 
138  void setMagFilterHint(osg::Texture::FilterMode mode);
139  osg::Texture::FilterMode getMagFilterHint() const;
140 
141  unsigned int getFontDepth() const { return _depth; }
142 
143  void setNumberCurveSamples(unsigned int numSamples) { _numCurveSamples = numSamples; }
144  unsigned int getNumberCurveSamples() const { return _numCurveSamples; }
145 
146 
147  // make Text a friend to allow it add and remove its entry in the Font's _textList.
148  friend class FontImplementation;
149 
150  void setImplementation(FontImplementation* implementation);
151 
152  FontImplementation* getImplementation();
153  const FontImplementation* getImplementation() const;
154 
156  virtual void setThreadSafeRefUnref(bool threadSafe);
157 
159  virtual void resizeGLObjectBuffers(unsigned int maxSize);
160 
164  virtual void releaseGLObjects(osg::State* state=0) const;
165 
167 
168  typedef std::vector< osg::ref_ptr<GlyphTexture> > GlyphTextureList;
169  GlyphTextureList& getGlyphTextureList() { return _glyphTextureList; }
170 
171 protected:
172 
173  virtual ~Font();
174 
175  void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph);
176 
177  typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
178  typedef std::map< unsigned int, osg::ref_ptr<Glyph> > GlyphMap;
179  typedef std::map< unsigned int, osg::ref_ptr<Glyph3D> > Glyph3DMap;
180 
181  typedef std::map< FontResolution, GlyphMap > FontSizeGlyphMap;
182 
184 
187  FontSizeGlyphMap _sizeGlyphMap;
188  GlyphTextureList _glyphTextureList;
189 
190 
191  Glyph3DMap _glyph3DMap;
192 
193  // current active size of font
195  unsigned int _margin;
197 
198  unsigned int _textureWidthHint;
199  unsigned int _textureHeightHint;
202 
203  unsigned int _depth;
204  unsigned int _numCurveSamples;
205 
206 
208 
209 
210 // declare the nested classes.
211 public:
212 
214  {
215  public:
216 
218  osg::Referenced(true),
219  _facade(0) {}
220 
221  virtual std::string getFileName() const = 0;
222 
223  virtual bool supportsMultipleFontResolutions() const = 0;
224 
226  virtual Glyph* getGlyph(const FontResolution& fontRes, unsigned int charcode) = 0;
227 
229  virtual Glyph3D* getGlyph3D(unsigned int charcode) = 0;
230 
232  virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType) = 0;
233 
235  virtual bool hasVertical() const = 0;
236 
237  void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
238  {
239  _facade->addGlyph(fontRes, charcode, glyph);
240  }
241 
243 
244  virtual bool getVerticalSize(float & /*ascender*/, float & /*descender*/) const { return false; }
245  };
246 
247 
248 
249 };
250 
251 }
252 
253 #endif
virtual bool isSameKindAs(const osg::Object *obj) const
Definition: Font.h:77
std::pair< unsigned int, unsigned int > FontResolution
Definition: KerningType.h:20
osg::Texture::FilterMode _minFilterHint
Definition: Font.h:200
unsigned int getNumberCurveSamples() const
Definition: Font.h:144
GlyphTextureList & getGlyphTextureList()
Definition: Font.h:169
#define NULL
Definition: Export.h:59
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
osg::TexEnv * getTexEnv()
Definition: Font.h:86
unsigned int _depth
Definition: Font.h:203
OSGTEXT_EXPORT osg::ref_ptr< Font > readRefFontStream(std::istream &stream, const osgDB::Options *userOptions=0)
osg::ref_ptr< osg::TexEnv > _texenv
Definition: Font.h:185
virtual const char * className() const
Definition: Font.h:78
std::vector< osg::ref_ptr< GlyphTexture > > GlyphTextureList
Definition: Font.h:168
osg::StateSet * getStateSet()
Definition: Font.h:90
osg::ref_ptr< osg::StateSet > _stateset
Definition: Font.h:186
FontResolution _fontSize
Definition: Font.h:194
unsigned int _textureWidthHint
Definition: Font.h:198
FontSizeGlyphMap _sizeGlyphMap
Definition: Font.h:187
virtual const char * libraryName() const
Definition: Font.h:79
unsigned int getFontDepth() const
Definition: Font.h:141
GLint GLenum GLsizei width
Definition: GLU.h:71
virtual osg::Object * cloneType() const
Definition: Font.h:75
const osg::StateSet * getStateSet() const
Definition: Font.h:91
OSGTEXT_EXPORT Font * readFontFile(const std::string &filename, const osgDB::Options *userOptions=0)
void setTexEnv(osg::TexEnv *texenv)
Definition: Font.h:85
const osg::TexEnv * getTexEnv() const
Definition: Font.h:87
unsigned int _textureHeightHint
Definition: Font.h:199
unsigned int _margin
Definition: Font.h:195
void setStateSet(osg::StateSet *stateset)
Definition: Font.h:89
unsigned int _numCurveSamples
Definition: Font.h:204
Glyph3DMap _glyph3DMap
Definition: Font.h:191
virtual bool getVerticalSize(float &ascender, float &descender) const
Definition: Font.h:109
GLint GLenum GLsizei GLsizei height
Definition: GLU.h:71
std::map< unsigned int, osg::ref_ptr< Glyph3D > > Glyph3DMap
Definition: Font.h:179
std::vector< osg::ref_ptr< osg::StateSet > > StateSetList
Definition: Font.h:177
GlyphTextureList _glyphTextureList
Definition: Font.h:188
OpenThreads::Mutex _glyphMapMutex
Definition: Font.h:183
void addGlyph(const FontResolution &fontRes, unsigned int charcode, Glyph *glyph)
Definition: Font.h:237
virtual osg::Object * clone(const osg::CopyOp &) const
Definition: Font.h:76
OSGTEXT_EXPORT Font * readFontStream(std::istream &stream, const osgDB::Options *userOptions=0)
void setNumberCurveSamples(unsigned int numSamples)
Definition: Font.h:143
OpenThreads::Mutex FontMutex
Definition: Font.h:166
Definition: AlphaFunc.h:19
std::map< unsigned int, osg::ref_ptr< Glyph > > GlyphMap
Definition: Font.h:178
OSGTEXT_EXPORT osg::ref_ptr< Font > readRefFontFile(const std::string &filename, const osgDB::Options *userOptions=0)
float _marginRatio
Definition: Font.h:196
osg::ref_ptr< FontImplementation > _implementation
Definition: Font.h:207
#define OSGTEXT_EXPORT
Definition: Export.h:39
osg::Texture::FilterMode _magFilterHint
Definition: Font.h:201
std::map< FontResolution, GlyphMap > FontSizeGlyphMap
Definition: Font.h:181
virtual bool getVerticalSize(float &, float &) const
Definition: Font.h:244
OSGTEXT_EXPORT std::string findFontFile(const std::string &str)