Moved TextNode into osgText.

Cleaned up freetype plugin so it no longer does tesselation - instead Glyph and TextNode do this.
This commit is contained in:
Robert Osfield
2010-09-07 18:18:35 +00:00
parent a6abbb545e
commit 50be800787
16 changed files with 2000 additions and 968 deletions

View File

@@ -17,241 +17,16 @@
#include <string>
#include <istream>
#include <osg/Vec2>
#include <osg/Image>
#include <osg/Texture2D>
#include <osg/StateSet>
#include <osg/buffered_value>
#include <osg/TexEnv>
#include <osg/Geometry>
#include <osgDB/ReaderWriter>
#include <osgText/Export>
#include <osgText/KerningType>
#include <osgText/Glyph>
#include <osgDB/Options>
#include <OpenThreads/Mutex>
namespace osgText {
// forward declare Font
class Font;
class Text;
class Glyph3D;
class GlyphTexture;
class OSGTEXT_EXPORT Glyph : public osg::Image
{
public:
Glyph(unsigned int glyphCode);
unsigned int getGlyphCode() const { return _glyphCode; }
void setHorizontalBearing(const osg::Vec2& bearing);
const osg::Vec2& getHorizontalBearing() const;
void setHorizontalAdvance(float advance);
float getHorizontalAdvance() const;
void setVerticalBearing(const osg::Vec2& bearing);
const osg::Vec2& getVerticalBearing() const;
void setVerticalAdvance(float advance);
float getVerticalAdvance() const;
void setTexture(GlyphTexture* texture);
GlyphTexture* getTexture();
const GlyphTexture* getTexture() const;
void setTexturePosition(int posX,int posY);
int getTexturePositionX() const;
int getTexturePositionY() const;
void setMinTexCoord(const osg::Vec2& coord);
const osg::Vec2& getMinTexCoord() const;
void setMaxTexCoord(const osg::Vec2& coord);
const osg::Vec2& getMaxTexCoord() const;
void subload() const;
protected:
virtual ~Glyph();
Font* _font;
unsigned int _glyphCode;
osg::Vec2 _horizontalBearing;
float _horizontalAdvance;
osg::Vec2 _verticalBearing;
float _verticalAdvance;
GlyphTexture* _texture;
int _texturePosX;
int _texturePosY;
osg::Vec2 _minTexCoord;
osg::Vec2 _maxTexCoord;
typedef osg::buffered_value<GLuint> GLObjectList;
mutable GLObjectList _globjList;
};
class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
{
public:
Glyph3D(unsigned int glyphCode):
osg::Referenced(true),
_glyphCode(glyphCode),
_horizontalBearing(0,0),
_horizontalAdvance(0),
_verticalBearing(0,0),
_verticalAdvance(0)
{}
unsigned int getGlyphCode() const { return _glyphCode; }
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 setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
const osg::BoundingBox & getBoundingBox() const { return _bb; }
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
virtual void setThreadSafeRefUnref(bool threadSafe);
void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
/** Get the PrimitiveSetList for the raw face which hasn't been tessellated. */
osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; }
/** Get the PrimitiveSetList for the front face. */
osg::Geometry::PrimitiveSetList & getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
/** Get the PrimitiveSetList for the wall face. */
osg::Geometry::PrimitiveSetList & getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
/** Get et the PrimitiveSetList for the back face. */
osg::Geometry::PrimitiveSetList & getBackPrimitiveSetList() { return _backPrimitiveSetList; }
/** Set the VertexArray of the glyph. */
void setVertexArray(osg::Vec3Array * va) { _vertexArray = va; }
/** Get the VertexArray of the glyph. */
osg::Vec3Array * getVertexArray() { return _vertexArray.get(); }
/** Set the VertexArray of the glyph. */
void setNormalArray(osg::Vec3Array * na) { _normalArray = na; }
/** Get the NormalArray for the wall face. */
osg::Vec3Array * getNormalArray() { return _normalArray.get(); }
float getHorizontalWidth() { return (-_horizontalBearing.x() + _horizontalAdvance); }
float getHorizontalHeight() { return (-_horizontalBearing.y() + _bb.yMax()); }
float getVerticalWidth() { return (-_verticalBearing.x() + _bb.xMax()); }
float getVerticalHeight() { return (-_verticalBearing.y() + _verticalAdvance); }
void setWidth(float width) { _width = width; }
float getWidth() { return _width; }
void setHeight(float height) { _height = height; }
float getHeight() { return _height; }
protected:
virtual ~Glyph3D() {}
unsigned int _glyphCode;
osg::Vec2 _horizontalBearing;
float _horizontalAdvance;
osg::Vec2 _verticalBearing;
float _verticalAdvance;
osg::BoundingBox _bb;
// osg::Vec2 _advance;
float _width;
float _height;
osg::ref_ptr<osg::Vec3Array> _vertexArray;
osg::ref_ptr<osg::Vec3Array> _normalArray;
osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
osg::ref_ptr<osg::Vec3Array> _rawVertexArray;
osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;
};
class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
{
public:
GlyphTexture();
const char* className() const { return "GlyphTexture"; }
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
virtual int compare(const osg::StateAttribute& rhs) const;
/** 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; }
void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; }
float getGlyphImageMarginRatio() const { return _marginRatio; }
bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
void addGlyph(Glyph* glyph,int posX, int posY);
virtual void apply(osg::State& state) 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);
protected:
virtual ~GlyphTexture();
// parameter used to compute the size and position of empty space
// in the texture which could accommodate new glyphs.
int _margin;
float _marginRatio;
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;
mutable OpenThreads::Mutex _mutex;
};
/** 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:
@@ -272,14 +47,14 @@ protected:
* 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::ReaderWriter::Options* userOptions = 0);
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::ReaderWriter::Options* userOptions = 0);
extern OSGTEXT_EXPORT Font* readFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0);
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::ReaderWriter::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);

229
include/osgText/TextNode Normal file
View File

@@ -0,0 +1,229 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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_TEXTNODE
#define OSGTEXT_TEXTNODE 1
#include <osg/Group>
#include <osg/Quat>
#include <osgUtil/CullVisitor>
#include <osgText/Font>
#include <osgText/String>
namespace osgText {
// forward declare
class TextNode;
class Glyph;
class Bevel : public osg::Object
{
public:
Bevel();
Bevel(const Bevel& bevel, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, Bevel)
void setBevelThickness(float thickness) { _thickness = thickness; }
float getBevelThickness() const { return _thickness; }
void flatBevel(float width=0.25f);
void roundedBevel(float width=0.5f, unsigned int numSteps=10);
void roundedBevel2(float width=0.5f, unsigned int numSteps=10);
typedef std::vector<osg::Vec2> Vertices;
void setVertices(const Vertices& vertices) { _vertices = vertices; }
Vertices& getVertices() { return _vertices; }
const Vertices& getVertices() const { return _vertices; }
void print(std::ostream& fout);
protected:
float _thickness;
Vertices _vertices;
};
class Style : public osg::Object
{
public:
Style();
Style(const Style& style, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, Style)
/// default Layout implementation used if no other is specified on TextNode
static osg::ref_ptr<Style>& getDefaultStyle();
/// NULL is no bevel
void setBevel(Bevel* bevel) { _bevel = bevel; }
const Bevel* getBevel() const { return _bevel.get(); }
/// 1 is the default width of the text
void setWidthRatio(float widthRatio) { _widthRatio = widthRatio; }
float getWidthRatio() const { return _widthRatio; }
/// 0 is 2D text
void setThicknessRatio(float thicknessRatio) { _thicknessRatio = thicknessRatio; }
float getThicknessRatio() const { return _thicknessRatio; }
/// 0 is off
void setOutlineRatio(float outlineRatio) { _outlineRatio = outlineRatio; }
float getOutlineRatio() const { return _outlineRatio; }
/// 1.0 is default number of samples
void setSampleDensity(float sd) { _sampleDensity = sd; }
float getSampleDensity() const { return _sampleDensity; }
protected:
osg::ref_ptr<Bevel> _bevel;
float _widthRatio;
float _thicknessRatio;
float _outlineRatio;
float _sampleDensity;
};
class Layout : public osg::Object
{
public:
Layout();
Layout(const Layout& layout, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText,Layout)
/// default Layout implementation used if no other is specified on TextNode
static osg::ref_ptr<Layout>& getDefaultLayout();
virtual void layout(TextNode& text) const;
protected:
};
class TextTechnique : public osg::Object
{
public:
TextTechnique();
TextTechnique(const TextTechnique& technique, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osgText, TextTechnique)
TextNode* getTextNode() { return _textNode; }
const TextNode* getTextNode() const { return _textNode; }
/// default TextTechnique implementation used if no other is specified on TextNode
static osg::ref_ptr<TextTechnique>& getDefaultTextTechinque();
/// start building a new charater layout
virtual void start();
/// called by Layout engine to place individual characters
virtual void addCharacter(const osg::Vec3& position, const osg::Vec3& size, Glyph* glyph, Style* style);
/// called by Layout engine to place individual characters
virtual void addCharacter(const osg::Vec3& position, const osg::Vec3& size, Glyph3D* glyph, Style* style);
/// finish building new charater layout
virtual void finish();
/// provide traversal control
virtual void traverse(osg::NodeVisitor& nv);
protected:
friend class TextNode;
void setTextNode(TextNode* textNode) { _textNode = textNode; }
TextNode* _textNode;
};
class TextNode : public osg::Group
{
public:
TextNode();
TextNode(const TextNode& text, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Node(osgText, TextNode)
virtual void traverse(osg::NodeVisitor& nv);
void setFont(Font* font) { _font = font; }
Font* getFont() { return _font.get(); }
const Font* getFont() const { return _font.get(); }
Font* getActiveFont() { return _font.valid() ? _font.get() : Font::getDefaultFont().get(); }
const Font* getActiveFont() const { return _font.valid() ? _font.get() : Font::getDefaultFont().get(); }
void setStyle(Style* style) { _style = style; }
Style* getStyle() { return _style.get(); }
const Style* getStyle() const { return _style.get(); }
Style* getActiveStyle() { return _style.valid() ? _style.get() : Style::getDefaultStyle().get(); }
const Style* getActiveStyle() const { return _style.valid() ? _style.get() : Style::getDefaultStyle().get(); }
void setLayout(Layout* layout) { _layout = layout; }
Layout* getLayout() { return _layout.get(); }
const Layout* getLayout() const { return _layout.get(); }
const Layout* getActiveLayout() const { return _layout.valid() ? _layout.get() : Layout::getDefaultLayout().get(); }
void setTextTechnique(TextTechnique* technique);
TextTechnique* getTextTechnique() { return _technique.get(); }
const TextTechnique* getTextTechnique() const { return _technique.get(); }
void setText(const std::string& str);
void setText(const String& str) { _string = str; }
String& getText() { return _string; }
const String& getText() const { return _string; }
void setPosition(const osg::Vec3d& position) { _position = position; }
const osg::Vec3d& getPosition() const { return _position; }
void setRotation(const osg::Quat& rotation) { _rotation = rotation; }
const osg::Quat& getRotation() const { return _rotation; }
void setCharacterSize(float characterSize) { _characterSize = characterSize; }
float getCharacterSize() const { return _characterSize; }
/// force a regeneration of the rendering backend required to represent the text.
virtual void update();
protected:
virtual ~TextNode();
osg::ref_ptr<Font> _font;
osg::ref_ptr<Style> _style;
osg::ref_ptr<Layout> _layout;
osg::ref_ptr<TextTechnique> _technique;
String _string;
osg::Vec3d _position;
osg::Quat _rotation;
float _characterSize;
};
}
#endif