OpenSceneGraph/include/osgText/Font

241 lines
9.4 KiB
Plaintext
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
2001-11-12 18:04:57 +08:00
#ifndef OSGTEXT_FONT
#define OSGTEXT_FONT 1
2001-11-12 18:04:57 +08:00
#include <string>
#include <istream>
#include <osg/TexEnv>
#include <osgText/Glyph>
#include <osgDB/Options>
2001-11-12 18:04:57 +08:00
#include <OpenThreads/Mutex>
namespace osgText {
2001-11-12 18:04:57 +08:00
// forward declare Font
class Font;
class TextBase;
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
#ifdef OSG_PROVIDE_READFILE
/** Read a font from specified file. The filename may contain a path.
* It will search for the font file in the following places in this order:
* - In the current directory
* - All paths defined in OSG_FILE_PATH or OSGFILEPATH environment variable
* - Filename with path stripped: In the current directory
* - Filename with path stripped: All paths defined in OSG_FILE_PATH or OSGFILEPATH
*
* Then the file will be searched in OS specific directories in the following order:
* - Again in the current directory
* - Windows: In C:/winnt/fonts
* - Windows: In C:/windows/fonts
* - Windows: In the fonts directory of the windows install directory
* - Other OS: In /usr/share/fonts/ttf
* - Other OS: In /usr/share/fonts/ttf/western
* - Other OS: In /usr/share/fonts/ttf/decoratives
*
* If the given file could not be found, the path part will be stripped and
* the file will be searched again in the OS specific directories.
*/
extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
/** read a font from specified stream.*/
extern OSGTEXT_EXPORT Font* readFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
Introduced CMake option OSG_PROVIDE_READFILE option that defaults to ON, but when switched to OFF disables the building of the osgDB::read*File() methods, forcing users to use osgDB::readRef*File() methods. The later is preferable as it closes a potential threading bug when using paging databases in conjunction with the osgDB::Registry Object Cache. This threading bug occurs when one thread gets an object from the Cache via an osgDB::read*File() call where only a pointer to the object is passed back, so taking a reference to the object is delayed till it gets reassigned to a ref_ptr<>, but at the same time another thread calls a flush of the Object Cache deleting this object as it's referenceCount is now zero. Using osgDB::readREf*File() makes sure the a ref_ptr<> is passed back and the referenceCount never goes to zero. To ensure the OSG builds when OSG_PROVIDE_READFILE is to OFF the many cases of osgDB::read*File() usage had to be replaced with a ref_ptr<> osgDB::readRef*File() usage. The avoid this change causing lots of other client code to be rewritten to handle the use of ref_ptr<> in place of C pointer I introduced a serious of templte methods in various class to adapt ref_ptr<> to the underly C pointer to be passed to old OSG API's, example of this is found in include/osg/Group: bool addChild(Node* child); // old method which can only be used with a Node* tempalte<class T> bool addChild(const osg::ref_ptr<T>& child) { return addChild(child.get()); } // adapter template method These changes together cover 149 modified files, so it's a large submission. This extent of changes are warrent to make use of the Object Cache and multi-threaded loaded more robust. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15164 16af8721-9629-0410-8352-f15c8da7e697
2015-10-22 21:42:19 +08:00
#endif
extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
extern OSGTEXT_EXPORT std::string findFontFile(const std::string& str);
/** Pure virtual base class for fonts.
* Concrete implementation are the DefaultFont found in src/osgText/DefaultFont.cpp
* and FreeTypeFont found in src/osgPlugins/freetype/FreeTypeFont.cpp*/
2001-11-12 18:04:57 +08:00
class OSGTEXT_EXPORT Font : public osg::Object
{
// declare the interface to a font.
public:
2001-11-12 18:04:57 +08:00
// forward declare nested classes.
class FontImplementation;
2001-11-12 18:04:57 +08:00
public:
Font(FontImplementation* implementation=0);
2001-11-12 18:04:57 +08:00
virtual osg::Object* cloneType() const { return 0; } // cloneType() not appropriate
virtual osg::Object* clone(const osg::CopyOp&) const { return 0; } // clone() not appropriate
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Font*>(obj)!=NULL; }
virtual const char* className() const { return "Font"; }
virtual const char* libraryName() const { return "osgText"; }
2001-11-12 18:04:57 +08:00
virtual std::string getFileName() const;
2001-11-12 18:04:57 +08:00
static osg::ref_ptr<Font> getDefaultFont();
typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSets;
StateSets& getCachedStateSets() { return _statesets; }
const StateSets& getCachedStateSets() const { return _statesets; }
/** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes and a font resolution.*/
virtual osg::Vec2 getKerning(const FontResolution& fontRes, unsigned int leftcharcode, unsigned int rightcharcode, KerningType kerningType);
/** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
virtual Glyph* getGlyph(const FontResolution& fontSize, unsigned int charcode);
/** Get a Glyph3D for specified charcode and a font size.*/
virtual Glyph3D* getGlyph3D(const FontResolution& fontSize, unsigned int charcode);
/** Return true if this font provides vertical alignments and spacing or glyphs.*/
virtual bool hasVertical() const;
/** Get the ascender and descender sizes of the font where supported by the FontImplementation,
* return true on success, return false when not supported.*/
virtual bool getVerticalSize(float& ascender, float& descender) const { return _implementation ? _implementation->getVerticalSize(ascender, descender) : false; }
/** Set the size of texture to create to store the glyph images when rendering.
* Note, this doesn't affect already created Texture Glhph's.*/
void setTextureSizeHint(unsigned int width,unsigned int height);
unsigned int getTextureWidthHint() const;
unsigned int getTextureHeightHint() const;
/** Set the minification texture filter to use when creating the texture to store the glyph images when rendering.
* Note, this doesn't affect already created Texture Glhph's.*/
void setMinFilterHint(osg::Texture::FilterMode mode);
osg::Texture::FilterMode getMinFilterHint() const;
/** Set the magnification texture filter to use when creating the texture to store the glyph images when rendering.
* Note, this doesn't affect already created Texture Glhph's.*/
void setMagFilterHint(osg::Texture::FilterMode mode);
osg::Texture::FilterMode getMagFilterHint() const;
2001-11-12 18:04:57 +08:00
void setMaxAnisotropy(float anis) { _maxAnisotropy = anis; }
float getMaxAnisotropy() const { return _maxAnisotropy; }
unsigned int getFontDepth() const { return _depth; }
void setNumberCurveSamples(unsigned int numSamples) { _numCurveSamples = numSamples; }
unsigned int getNumberCurveSamples() const { return _numCurveSamples; }
// make Text a friend to allow it add and remove its entry in the Font's _textList.
friend class FontImplementation;
void setImplementation(FontImplementation* implementation);
FontImplementation* getImplementation();
const FontImplementation* getImplementation() const;
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
virtual void releaseGLObjects(osg::State* state=0) const;
typedef OpenThreads::Mutex FontMutex;
typedef std::vector< osg::ref_ptr<GlyphTexture> > GlyphTextureList;
GlyphTextureList& getGlyphTextureList() { return _glyphTextureList; }
void assignGlyphToGlyphTexture(Glyph* glyph, ShaderTechnique shaderTechnique);
protected:
2001-11-12 18:04:57 +08:00
virtual ~Font();
void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph);
typedef std::map< unsigned int, osg::ref_ptr<Glyph> > GlyphMap;
typedef std::map< unsigned int, osg::ref_ptr<Glyph3D> > Glyph3DMap;
typedef std::map< FontResolution, GlyphMap > FontSizeGlyphMap;
typedef std::map< FontResolution, Glyph3DMap > FontSizeGlyph3DMap;
mutable OpenThreads::Mutex _glyphMapMutex;
StateSets _statesets;
FontSizeGlyphMap _sizeGlyphMap;
GlyphTextureList _glyphTextureList;
FontSizeGlyph3DMap _sizeGlyph3DMap;
// current active size of font
FontResolution _fontSize;
unsigned int _textureWidthHint;
unsigned int _textureHeightHint;
osg::Texture::FilterMode _minFilterHint;
osg::Texture::FilterMode _magFilterHint;
float _maxAnisotropy;
unsigned int _depth;
unsigned int _numCurveSamples;
osg::ref_ptr<FontImplementation> _implementation;
2001-11-12 18:04:57 +08:00
// declare the nested classes.
public:
class FontImplementation : public osg::Referenced
{
public:
FontImplementation():
2007-12-17 01:01:40 +08:00
osg::Referenced(true),
_facade(0) {}
virtual std::string getFileName() const = 0;
virtual bool supportsMultipleFontResolutions() const = 0;
/** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
virtual Glyph* getGlyph(const FontResolution& fontRes, unsigned int charcode) = 0;
/** Get a Glyph3D for specified charcode.*/
virtual Glyph3D* getGlyph3D(const FontResolution& fontRes, unsigned int charcode) = 0;
/** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/
virtual osg::Vec2 getKerning(const FontResolution& fontRes, unsigned int leftcharcode, unsigned int rightcharcode, KerningType kerningType) = 0;
/** Return true if this font provides vertical alignments and spacing or glyphs.*/
virtual bool hasVertical() const = 0;
void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
{
_facade->addGlyph(fontRes, charcode, glyph);
}
Font* _facade;
virtual bool getVerticalSize(float & /*ascender*/, float & /*descender*/) const { return false; }
};
2001-11-12 18:04:57 +08:00
};
}
2001-11-12 18:04:57 +08:00
#endif