Files
OpenSceneGraph/include/osgText/Font
2003-03-02 21:05:05 +00:00

198 lines
6.2 KiB
C++

/* -*-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.
*/
#ifndef OSGTEXT_FONT
#define OSGTEXT_FONT 1
#include <osg/Vec2>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/StateSet>
#include <osg/buffered_value>
#include <osgText/Export>
#include <string>
namespace osgText {
class Font;
/** read a font from specified file.*/
extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename);
/** 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*/
class OSGTEXT_EXPORT Font : public osg::Object
{
// declare the interface to a font.
public:
// forward declare nested classes.
class Glyph;
class GlyphTexture;
Font();
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"; }
virtual std::string getFileName() const = 0;
/** Set the pixel width and height */
virtual void setSize(unsigned int width, unsigned int height) = 0;
unsigned int getWidth() { return _width; }
unsigned int getHeight() { return _height; }
virtual Glyph* getGlyph(unsigned int charcode) = 0;
virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode) = 0;
virtual bool hasVertical() const = 0;
protected:
virtual ~Font();
void addGlyph(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;
GlyphMap _glyphMap;
GlyphTextureList _glyphTextureList;
StateSetList _stateSetList;
// current active size of font
unsigned int _width;
unsigned int _height;
// declare the nested classes.
public:
class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
{
public:
GlyphTexture();
void setStateSet(osg::StateSet* stateset) { _stateset = stateset; }
osg::StateSet* getStateSet() { return _stateset; }
const osg::StateSet* getStateSet() const { return _stateset; }
bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
void addGlyph(Glyph* glyph,int posX, int posY);
virtual void apply(osg::State& state) const;
protected:
virtual ~GlyphTexture();
osg::StateSet* _stateset;
// parameter used to compute the size and position of empty space
// in the texture which could accomodate new glyphs.
int _usedY;
int _partUsedX;
int _partUsedY;
typedef std::vector<Glyph*> GlyphList;
typedef osg::buffered_object<GlyphList> GlyphBuffer;
GlyphList _glyphs;
mutable GlyphBuffer _glyphsToSubload;
};
class OSGTEXT_EXPORT Glyph : public osg::Image
{
public:
Glyph();
virtual ~Glyph();
unsigned int getGlyphCode() const { return _glyphCode; }
void setFont(Font* font) { _font = font; }
Font* getFont() const { return _font; }
void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
const osg::Vec2& getHorizontalBearing() const { return _horizontalBearing; }
void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
float getHorizontalAdvance() const { return _horizontalAdvance; }
void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }
void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
float getVerticalAdvance() const { return _verticalAdvance; }
void setTexture(GlyphTexture* texture) { _texture = texture; }
GlyphTexture* getTexture() { return _texture.get(); }
const GlyphTexture* getTexture() const { return _texture.get(); }
osg::StateSet* getStateSet() { return _texture.valid()?_texture->getStateSet():0; }
const osg::StateSet* getStateSet() const { return _texture.valid()?_texture->getStateSet():0; }
void setTexturePosition(int posX,int posY) { _texturePosX = posX; _texturePosY = posY; }
int getTexturePositionX() const { return _texturePosX; }
int getTexturePositionY() const { return _texturePosY; }
void setMinTexCoord(const osg::Vec2& coord) { _minTexCoord=coord; }
const osg::Vec2& getMinTexCoord() const { return _minTexCoord; }
void setMaxTexCoord(const osg::Vec2& coord) { _maxTexCoord=coord; }
const osg::Vec2& getMaxTexCoord() const { return _maxTexCoord; }
void subload();
protected:
Font* _font;
unsigned int _glyphCode;
osg::Vec2 _horizontalBearing;
float _horizontalAdvance;
osg::Vec2 _verticalBearing;
float _verticalAdvance;
osg::ref_ptr<GlyphTexture> _texture;
int _texturePosX;
int _texturePosY;
osg::Vec2 _minTexCoord;
osg::Vec2 _maxTexCoord;
};
};
}
#endif