| 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 | |
|---|
| 31 | /** Read a font from specified file. The filename may contain a path. |
|---|
| 32 | * It will search for the font file in the following places in this order: |
|---|
| 33 | * - In the current directory |
|---|
| 34 | * - All paths defined in OSG_FILE_PATH or OSGFILEPATH environment variable |
|---|
| 35 | * - Filename with path stripped: In the current directory |
|---|
| 36 | * - Filename with path stripped: All paths defined in OSG_FILE_PATH or OSGFILEPATH |
|---|
| 37 | * |
|---|
| 38 | * Then the file will be searched in OS specific directories in the following order: |
|---|
| 39 | * - Again in the current directory |
|---|
| 40 | * - Windows: In C:/winnt/fonts |
|---|
| 41 | * - Windows: In C:/windows/fonts |
|---|
| 42 | * - Windows: In the fonts directory of the windows install directory |
|---|
| 43 | * - Other OS: In /usr/share/fonts/ttf |
|---|
| 44 | * - Other OS: In /usr/share/fonts/ttf/western |
|---|
| 45 | * - Other OS: In /usr/share/fonts/ttf/decoratives |
|---|
| 46 | * |
|---|
| 47 | * If the given file could not be found, the path part will be stripped and |
|---|
| 48 | * the file will be searched again in the OS specific directories. |
|---|
| 49 | */ |
|---|
| 50 | extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgDB::Options* userOptions = 0); |
|---|
| 51 | |
|---|
| 52 | /** read a font from specified stream.*/ |
|---|
| 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 | |
|---|
| 61 | /** Pure virtual base class for fonts. |
|---|
| 62 | * Concrete implementation are the DefaultFont found in src/osgText/DefaultFont.cpp |
|---|
| 63 | * and FreeTypeFont found in src/osgPlugins/freetype/FreeTypeFont.cpp*/ |
|---|
| 64 | class OSGTEXT_EXPORT Font : public osg::Object |
|---|
| 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 | |
|---|
| 94 | /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/ |
|---|
| 95 | virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType); |
|---|
| 96 | |
|---|
| 97 | /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/ |
|---|
| 98 | virtual Glyph* getGlyph(const FontResolution& fontSize, unsigned int charcode); |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /** Get a Glyph3D for specified charcode.*/ |
|---|
| 102 | virtual Glyph3D* getGlyph3D(unsigned int charcode); |
|---|
| 103 | |
|---|
| 104 | /** Return true if this font provides vertical alignments and spacing or glyphs.*/ |
|---|
| 105 | virtual bool hasVertical() const; |
|---|
| 106 | |
|---|
| 107 | /** Get the ascender and descender sizes of the font where supported by the FontImplementation, |
|---|
| 108 | * return true on success, return false when not supported.*/ |
|---|
| 109 | virtual bool getVerticalSize(float& ascender, float& descender) const { return _implementation ? _implementation->getVerticalSize(ascender, descender) : false; } |
|---|
| 110 | |
|---|
| 111 | /** Set the margin around each glyph, |
|---|
| 112 | * to ensure that texture filtering doesn't bleed adjacent glyph's into each other. |
|---|
| 113 | * Default margin is 1 texels.*/ |
|---|
| 114 | void setGlyphImageMargin(unsigned int margin); |
|---|
| 115 | unsigned int getGlyphImageMargin() const; |
|---|
| 116 | |
|---|
| 117 | /** Set the margin ratio around each glyph, relative to the glyph's size. |
|---|
| 118 | * to ensure that texture filtering doesn't bleed adjacent glyph's into each other. |
|---|
| 119 | * Default margin is 0.05.*/ |
|---|
| 120 | void setGlyphImageMarginRatio(float margin); |
|---|
| 121 | float getGlyphImageMarginRatio() const; |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /** Set the size of texture to create to store the glyph images when rendering. |
|---|
| 125 | * Note, this doesn't affect already created Texture Glhph's.*/ |
|---|
| 126 | void setTextureSizeHint(unsigned int width,unsigned int height); |
|---|
| 127 | |
|---|
| 128 | unsigned int getTextureWidthHint() const; |
|---|
| 129 | unsigned int getTextureHeightHint() const; |
|---|
| 130 | |
|---|
| 131 | /** Set the minification texture filter to use when creating the texture to store the glyph images when rendering. |
|---|
| 132 | * Note, this doesn't affect already created Texture Glhph's.*/ |
|---|
| 133 | void setMinFilterHint(osg::Texture::FilterMode mode); |
|---|
| 134 | osg::Texture::FilterMode getMinFilterHint() const; |
|---|
| 135 | |
|---|
| 136 | /** Set the magnification texture filter to use when creating the texture to store the glyph images when rendering. |
|---|
| 137 | * Note, this doesn't affect already created Texture Glhph's.*/ |
|---|
| 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 | |
|---|
| 155 | /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/ |
|---|
| 156 | virtual void setThreadSafeRefUnref(bool threadSafe); |
|---|
| 157 | |
|---|
| 158 | /** Resize any per context GLObject buffers to specified size. */ |
|---|
| 159 | virtual void resizeGLObjectBuffers(unsigned int maxSize); |
|---|
| 160 | |
|---|
| 161 | /** If State is non-zero, this function releases OpenGL objects for |
|---|
| 162 | * the specified graphics context. Otherwise, releases OpenGL objexts |
|---|
| 163 | * for all graphics contexts. */ |
|---|
| 164 | virtual void releaseGLObjects(osg::State* state=0) const; |
|---|
| 165 | |
|---|
| 166 | typedef OpenThreads::Mutex FontMutex; |
|---|
| 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 | |
|---|
| 183 | mutable OpenThreads::Mutex _glyphMapMutex; |
|---|
| 184 | |
|---|
| 185 | osg::ref_ptr<osg::TexEnv> _texenv; |
|---|
| 186 | osg::ref_ptr<osg::StateSet> _stateset; |
|---|
| 187 | FontSizeGlyphMap _sizeGlyphMap; |
|---|
| 188 | GlyphTextureList _glyphTextureList; |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | Glyph3DMap _glyph3DMap; |
|---|
| 192 | |
|---|
| 193 | // current active size of font |
|---|
| 194 | FontResolution _fontSize; |
|---|
| 195 | unsigned int _margin; |
|---|
| 196 | float _marginRatio; |
|---|
| 197 | |
|---|
| 198 | unsigned int _textureWidthHint; |
|---|
| 199 | unsigned int _textureHeightHint; |
|---|
| 200 | osg::Texture::FilterMode _minFilterHint; |
|---|
| 201 | osg::Texture::FilterMode _magFilterHint; |
|---|
| 202 | |
|---|
| 203 | unsigned int _depth; |
|---|
| 204 | unsigned int _numCurveSamples; |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | osg::ref_ptr<FontImplementation> _implementation; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 210 | // declare the nested classes. |
|---|
| 211 | public: |
|---|
| 212 | |
|---|
| 213 | class FontImplementation : public osg::Referenced |
|---|
| 214 | { |
|---|
| 215 | public: |
|---|
| 216 | |
|---|
| 217 | FontImplementation(): |
|---|
| 218 | osg::Referenced(true), |
|---|
| 219 | _facade(0) {} |
|---|
| 220 | |
|---|
| 221 | virtual std::string getFileName() const = 0; |
|---|
| 222 | |
|---|
| 223 | virtual bool supportsMultipleFontResolutions() const = 0; |
|---|
| 224 | |
|---|
| 225 | /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/ |
|---|
| 226 | virtual Glyph* getGlyph(const FontResolution& fontRes, unsigned int charcode) = 0; |
|---|
| 227 | |
|---|
| 228 | /** Get a Glyph3D for specified charcode.*/ |
|---|
| 229 | virtual Glyph3D* getGlyph3D(unsigned int charcode) = 0; |
|---|
| 230 | |
|---|
| 231 | /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/ |
|---|
| 232 | virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType) = 0; |
|---|
| 233 | |
|---|
| 234 | /** Return true if this font provides vertical alignments and spacing or glyphs.*/ |
|---|
| 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 | |
|---|
| 242 | Font* _facade; |
|---|
| 243 | |
|---|
| 244 | virtual bool getVerticalSize(float & ascender, float & descender) const { return false; } |
|---|
| 245 | }; |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | }; |
|---|
| 250 | |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | #endif |
|---|