1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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.
21#include <osgText/Glyph>
22#include <osgDB/Options>
24#include <OpenThreads/Mutex>
28// forward declare Font
32#ifdef OSG_PROVIDE_READFILE
33/** Read a font from specified file. The filename may contain a path.
34 * It will search for the font file in the following places in this order:
35 * - In the current directory
36 * - All paths defined in OSG_FILE_PATH or OSGFILEPATH environment variable
37 * - Filename with path stripped: In the current directory
38 * - Filename with path stripped: All paths defined in OSG_FILE_PATH or OSGFILEPATH
40 * Then the file will be searched in OS specific directories in the following order:
41 * - Again in the current directory
42 * - Windows: In C:/winnt/fonts
43 * - Windows: In C:/windows/fonts
44 * - Windows: In the fonts directory of the windows install directory
45 * - Other OS: In /usr/share/fonts/ttf
46 * - Other OS: In /usr/share/fonts/ttf/western
47 * - Other OS: In /usr/share/fonts/ttf/decoratives
49 * If the given file could not be found, the path part will be stripped and
50 * the file will be searched again in the OS specific directories.
52extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
54/** read a font from specified stream.*/
55extern OSGTEXT_EXPORT Font* readFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
58extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
60extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
62extern OSGTEXT_EXPORT std::string findFontFile(const std::string& str);
64/** Pure virtual base class for fonts.
65 * Concrete implementation are the DefaultFont found in src/osgText/DefaultFont.cpp
66 * and FreeTypeFont found in src/osgPlugins/freetype/FreeTypeFont.cpp*/
67class OSGTEXT_EXPORT Font : public osg::Object
69// declare the interface to a font.
72 // forward declare nested classes.
73 class FontImplementation;
76 Font(FontImplementation* implementation=0);
78 virtual osg::Object* cloneType() const { return 0; } // cloneType() not appropriate
79 virtual osg::Object* clone(const osg::CopyOp&) const { return 0; } // clone() not appropriate
80 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Font*>(obj)!=NULL; }
81 virtual const char* className() const { return "Font"; }
82 virtual const char* libraryName() const { return "osgText"; }
84 virtual std::string getFileName() const;
86 static osg::ref_ptr<Font> getDefaultFont();
88 typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSets;
89 StateSets& getCachedStateSets() { return _statesets; }
90 const StateSets& getCachedStateSets() const { return _statesets; }
93 /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes and a font resolution.*/
94 virtual osg::Vec2 getKerning(const FontResolution& fontRes, unsigned int leftcharcode, unsigned int rightcharcode, KerningType kerningType);
96 /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
97 virtual Glyph* getGlyph(const FontResolution& fontSize, unsigned int charcode);
100 /** Get a Glyph3D for specified charcode and a font size.*/
101 virtual Glyph3D* getGlyph3D(const FontResolution& fontSize, unsigned int charcode);
103 /** Return true if this font provides vertical alignments and spacing or glyphs.*/
104 virtual bool hasVertical() const;
106 /** Get the ascender and descender sizes of the font where supported by the FontImplementation,
107 * return true on success, return false when not supported.*/
108 virtual bool getVerticalSize(float& ascender, float& descender) const { return _implementation ? _implementation->getVerticalSize(ascender, descender) : false; }
110 /** Set the size of texture to create to store the glyph images when rendering.
111 * Note, this doesn't affect already created Texture Glhph's.*/
112 void setTextureSizeHint(unsigned int width,unsigned int height);
114 unsigned int getTextureWidthHint() const;
115 unsigned int getTextureHeightHint() const;
117 /** Set the minification texture filter to use when creating the texture to store the glyph images when rendering.
118 * Note, this doesn't affect already created Texture Glhph's.*/
119 void setMinFilterHint(osg::Texture::FilterMode mode);
120 osg::Texture::FilterMode getMinFilterHint() const;
122 /** Set the magnification texture filter to use when creating the texture to store the glyph images when rendering.
123 * Note, this doesn't affect already created Texture Glhph's.*/
124 void setMagFilterHint(osg::Texture::FilterMode mode);
125 osg::Texture::FilterMode getMagFilterHint() const;
127 void setMaxAnisotropy(float anis) { _maxAnisotropy = anis; }
128 float getMaxAnisotropy() const { return _maxAnisotropy; }
130 unsigned int getFontDepth() const { return _depth; }
132 void setNumberCurveSamples(unsigned int numSamples) { _numCurveSamples = numSamples; }
133 unsigned int getNumberCurveSamples() const { return _numCurveSamples; }
136 // make Text a friend to allow it add and remove its entry in the Font's _textList.
137 friend class FontImplementation;
139 void setImplementation(FontImplementation* implementation);
141 FontImplementation* getImplementation();
142 const FontImplementation* getImplementation() const;
144 /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
145 virtual void setThreadSafeRefUnref(bool threadSafe);
147 /** Resize any per context GLObject buffers to specified size. */
148 virtual void resizeGLObjectBuffers(unsigned int maxSize);
150 /** If State is non-zero, this function releases OpenGL objects for
151 * the specified graphics context. Otherwise, releases OpenGL objexts
152 * for all graphics contexts. */
153 virtual void releaseGLObjects(osg::State* state=0) const;
155 typedef OpenThreads::Mutex FontMutex;
157 typedef std::vector< osg::ref_ptr<GlyphTexture> > GlyphTextureList;
158 GlyphTextureList& getGlyphTextureList() { return _glyphTextureList; }
160 void assignGlyphToGlyphTexture(Glyph* glyph, ShaderTechnique shaderTechnique);
166 void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph);
168 typedef std::map< unsigned int, osg::ref_ptr<Glyph> > GlyphMap;
169 typedef std::map< unsigned int, osg::ref_ptr<Glyph3D> > Glyph3DMap;
171 typedef std::map< FontResolution, GlyphMap > FontSizeGlyphMap;
172 typedef std::map< FontResolution, Glyph3DMap > FontSizeGlyph3DMap;
174 mutable OpenThreads::Mutex _glyphMapMutex;
176 StateSets _statesets;
177 FontSizeGlyphMap _sizeGlyphMap;
178 GlyphTextureList _glyphTextureList;
181 FontSizeGlyph3DMap _sizeGlyph3DMap;
183 // current active size of font
184 FontResolution _fontSize;
186 unsigned int _textureWidthHint;
187 unsigned int _textureHeightHint;
188 osg::Texture::FilterMode _minFilterHint;
189 osg::Texture::FilterMode _magFilterHint;
190 float _maxAnisotropy;
193 unsigned int _numCurveSamples;
196 osg::ref_ptr<FontImplementation> _implementation;
199// declare the nested classes.
202 class FontImplementation : public osg::Referenced
206 FontImplementation():
207 osg::Referenced(true),
210 virtual std::string getFileName() const = 0;
212 virtual bool supportsMultipleFontResolutions() const = 0;
214 /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
215 virtual Glyph* getGlyph(const FontResolution& fontRes, unsigned int charcode) = 0;
217 /** Get a Glyph3D for specified charcode.*/
218 virtual Glyph3D* getGlyph3D(const FontResolution& fontRes, unsigned int charcode) = 0;
220 /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/
221 virtual osg::Vec2 getKerning(const FontResolution& fontRes, unsigned int leftcharcode, unsigned int rightcharcode, KerningType kerningType) = 0;
223 /** Return true if this font provides vertical alignments and spacing or glyphs.*/
224 virtual bool hasVertical() const = 0;
226 void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
228 _facade->addGlyph(fontRes, charcode, glyph);
233 virtual bool getVerticalSize(float & /*ascender*/, float & /*descender*/) const { return false; }