OpenSceneGraph/include/osgText/Font

298 lines
10 KiB
Plaintext
Raw Normal View History

/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 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 <osg/Vec2>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/StateSet>
#include <osg/buffered_value>
#include <osg/TexEnv>
2001-11-12 18:04:57 +08:00
#include <osgText/Export>
namespace osgText {
2001-11-12 18:04:57 +08:00
class Font;
class Text;
2001-11-12 18:04:57 +08:00
enum KerningType
{
KERNING_DEFAULT, //default locked to integer kerning values
2004-01-25 04:50:33 +08:00
KERNING_UNFITTED, //use floating point value for kerning
KERNING_NONE //no kerning
};
/** read a font from specified file.*/
extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename);
2001-11-12 18:04:57 +08:00
/** 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 Glyph;
class GlyphTexture;
class FontImplementation;
2001-11-12 18:04:57 +08:00
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
/** Set the pixel width and height hint.*/
virtual void setSize(unsigned int width, unsigned int height);
2003-11-07 17:00:22 +08:00
unsigned int getWidth() const;
unsigned int getHeight() const;
2001-11-12 18:04:57 +08:00
/** 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(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(unsigned int charcode);
/** Return true if this font provides vertical alignments and spacing or glyphs.*/
virtual bool hasVertical() const;
/** Set the margin around each glyph,
* to ensure that texture filtering doesn't bleed adjacent glyph's into each other.
* Default margin is 2 texels.*/
void setGlyphImageMargin(unsigned int margin);
unsigned int getGlyphImageMargin() const;
/** 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
// 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;
protected:
2001-11-12 18:04:57 +08:00
virtual ~Font();
void addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph);
typedef std::vector< osg::ref_ptr<GlyphTexture> > GlyphTextureList;
typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSetList;
typedef std::map< unsigned int, osg::ref_ptr<Glyph> > GlyphMap;
typedef std::pair< unsigned int, unsigned int > SizePair;
typedef std::map< SizePair, GlyphMap > SizeGlyphMap;
SizeGlyphMap _sizeGlyphMap;
GlyphTextureList _glyphTextureList;
StateSetList _stateSetList;
// current active size of font
unsigned int _width;
unsigned int _height;
unsigned int _margin;
unsigned int _textureWidthHint;
unsigned int _textureHeightHint;
osg::Texture::FilterMode _minFilterHint;
osg::Texture::FilterMode _magFilterHint;
osg::ref_ptr<osg::TexEnv > _texEnv;
osg::ref_ptr<FontImplementation> _implementation;
2001-11-12 18:04:57 +08:00
// declare the nested classes.
public:
class FontImplementation : public osg::Referenced
{
public:
virtual std::string getFileName() const = 0;
/** Set the pixel width and height hint.*/
virtual void setSize(unsigned int width, unsigned int height) = 0;
/** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
virtual Glyph* getGlyph(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(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 setWidth(unsigned int width) { _facade->_width = width; }
void setHeight(unsigned int height) { _facade->_height = height; }
void addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph)
{
_facade->addGlyph(width, height, charcode, glyph);
}
Font* _facade;
};
class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
{
public:
2001-11-12 18:04:57 +08:00
GlyphTexture();
const char* className() const { return "GlyphTexture"; }
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
2003-03-12 05:42:55 +08:00
virtual int compare(const osg::StateAttribute& rhs) const;
void setStateSet(osg::StateSet* stateset) { _stateset = stateset; }
osg::StateSet* getStateSet() { return _stateset; }
const osg::StateSet* getStateSet() const { return _stateset; }
2001-11-12 18:04:57 +08:00
/** Set the margin around each glyph, to ensure that texture filtering doesn't bleed adjacent glyph's into each other.*/
void setGlyphImageMargin(unsigned int margin) { _margin = margin; }
unsigned int getGlyphImageMargin() const { return _margin; }
bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
2002-07-23 00:01:00 +08:00
void addGlyph(Glyph* glyph,int posX, int posY);
virtual void apply(osg::State& state) const;
2001-11-12 18:04:57 +08:00
protected:
virtual ~GlyphTexture();
2001-11-12 18:04:57 +08:00
osg::StateSet* _stateset;
// parameter used to compute the size and position of empty space
// in the texture which could accomodate new glyphs.
int _margin;
int _usedY;
int _partUsedX;
int _partUsedY;
typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
typedef std::vector< const Glyph* > GlyphPtrList;
typedef osg::buffered_object< GlyphPtrList > GlyphBuffer;
GlyphRefList _glyphs;
mutable GlyphBuffer _glyphsToSubload;
};
2001-11-12 18:04:57 +08:00
class OSGTEXT_EXPORT Glyph : public osg::Image
{
public:
2001-11-12 18:04:57 +08:00
Glyph();
virtual ~Glyph();
2001-11-12 18:04:57 +08:00
unsigned int getGlyphCode() const;
2001-11-12 18:04:57 +08:00
void setHorizontalBearing(const osg::Vec2& bearing);
const osg::Vec2& getHorizontalBearing() const;
2001-11-12 18:04:57 +08:00
void setHorizontalAdvance(float advance);
float getHorizontalAdvance() const;
2001-11-12 18:04:57 +08:00
void setVerticalBearing(const osg::Vec2& bearing);
const osg::Vec2& getVerticalBearing() const;
void setVerticalAdvance(float advance);
float getVerticalAdvance() const;
2001-11-12 18:04:57 +08:00
void setTexture(GlyphTexture* texture);
GlyphTexture* getTexture();
const GlyphTexture* getTexture() const;
2001-11-12 18:04:57 +08:00
osg::StateSet* getStateSet();
const osg::StateSet* getStateSet() const;
2001-11-12 18:04:57 +08:00
void setTexturePosition(int posX,int posY);
int getTexturePositionX() const;
int getTexturePositionY() const;
2001-11-12 18:04:57 +08:00
void setMinTexCoord(const osg::Vec2& coord);
const osg::Vec2& getMinTexCoord() const;
2001-11-12 18:04:57 +08:00
void setMaxTexCoord(const osg::Vec2& coord);
const osg::Vec2& getMaxTexCoord() const;
void subload() const;
void draw(osg::State& state) const;
protected:
Font* _font;
unsigned int _glyphCode;
2001-11-12 18:04:57 +08:00
osg::Vec2 _horizontalBearing;
float _horizontalAdvance;
2001-11-12 18:04:57 +08:00
osg::Vec2 _verticalBearing;
float _verticalAdvance;
2001-11-12 18:04:57 +08:00
GlyphTexture* _texture;
int _texturePosX;
int _texturePosY;
osg::Vec2 _minTexCoord;
osg::Vec2 _maxTexCoord;
typedef osg::buffered_value<GLuint> GLObjectList;
mutable GLObjectList _globjList;
2001-11-12 18:04:57 +08:00
};
2001-11-12 18:04:57 +08:00
};
}
2001-11-12 18:04:57 +08:00
#endif