Added new files to cvs.

This commit is contained in:
Robert Osfield 2001-11-12 10:04:57 +00:00
parent 8f6b7d04a4
commit 34555f61d6
4 changed files with 773 additions and 0 deletions

228
include/osgText/Font Normal file
View File

@ -0,0 +1,228 @@
//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield
//Distributed under the terms of the GNU Library General Public License (LGPL)
//as published by the Free Software Foundation.
/* --------------------------------------------------------------------------
*
* openscenegraph textLib / FTGL wrapper
*
* --------------------------------------------------------------------------
*
* prog: max rheiner;mrn@paus.ch
* date: 4/25/2001 (m/d/y)
*
* --------------------------------------------------------------------------
*
* --------------------------------------------------------------------------
*/
#ifndef OSGTEXT_FONT
#define OSGTEXT_FONT 1
#include <osg/Object>
#include <osgText/Export>
#include <string>
// http://homepages.paradise.net.nz/henryj/code/
class FTFont;
namespace osgText {
///////////////////////////////////////////////////////////////////////////////
// Font - FontBaseClass
class OSGTEXT_EXPORT Font : public osg::Object
{
public:
Font();
virtual bool open(const std::string& font);
virtual bool create(int pointSize, const unsigned int res = 72 );
virtual bool create();
virtual void output(const char* text);
virtual bool isOk(void) const { return _init; }
virtual bool isCreated(void) const { return isOk() && _created; }
virtual float getWidth(const char* text) const;
virtual int getHeight() const;
virtual int getDescender() const;
virtual int getAscender() const;
int getPointSize(void) const { return _pointSize; }
const std::string& getFontName();
FTFont* getFont(void) { return _font; }
protected:
virtual ~Font();
virtual void clear();
virtual FTFont* createFontObj(void)=0;
bool init(const std::string& font);
bool _init;
bool _created;
FTFont* _font;
std::string _fontName;
int _pointSize;
int _res;
};
// Font
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// RasterFont
class OSGTEXT_EXPORT RasterFont:public Font
{
public:
RasterFont():Font(){;}
RasterFont(const std::string& font):Font(){;}
protected:
};
// RasterFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// VectorFont
class OSGTEXT_EXPORT VectorFont:public Font
{
public:
VectorFont():Font(){;}
VectorFont(const std::string& font):Font(){;}
protected:
double _precision;
};
// VectorFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// BitmapFont
class OSGTEXT_EXPORT BitmapFont:public RasterFont
{
public:
BitmapFont() {;}
BitmapFont(const std::string& font,
int point_size);
META_Object(BitmapFont);
protected:
virtual FTFont* createFontObj(void);
};
// BitmapFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PixmapFont
class OSGTEXT_EXPORT PixmapFont:public RasterFont
{
public:
PixmapFont() {;}
PixmapFont(const std::string& font,
int point_size);
META_Object(PixmapFont);
protected:
virtual FTFont* createFontObj(void);
};
// PixmapFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// TextureFont
class OSGTEXT_EXPORT TextureFont:public RasterFont
{
public:
TextureFont() {;}
TextureFont(const std::string& font,
int point_size);
META_Object(TextureFont);
protected:
virtual FTFont* createFontObj(void);
};
// PixmapFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// OutlineFont
class OSGTEXT_EXPORT OutlineFont:public VectorFont
{
public:
OutlineFont() {;}
OutlineFont(const std::string& font,
int point_size,
double precision);
META_Object(OutlineFont);
protected:
virtual FTFont* createFontObj(void);
};
// OutlineFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PolygonFont
class OSGTEXT_EXPORT PolygonFont:public VectorFont
{
public:
PolygonFont() {;}
PolygonFont(const std::string& font,
int point_size,
double precision);
META_Object(PolygonFont);
protected:
virtual FTFont* createFontObj(void);
};
// PolygonFont
///////////////////////////////////////////////////////////////////////////////
};
#endif // OSGTEXT_FONT

60
include/osgText/Paragraph Normal file
View File

@ -0,0 +1,60 @@
//C++ header - Open Scene Graph - Copyright (C) 1998-2001 Robert Osfield
//Distributed under the terms of the GNU Library General Public License (LGPL)
//as published by the Free Software Foundation.
#ifndef OSGTEXT_PARAGRAPH
#define OSGTEXT_PARAGRAPH
#include <osg/Geode>
#include <osgText/Text>
namespace osgText {
class OSGTEXT_EXPORT Paragraph : public osg::Geode
{
public:
Paragraph();
Paragraph(const osg::Vec3& position,const std::string& text,osgText::Font* font);
META_Node(Paragraph)
void setFont(osgText::Font* font);
osgText::Font* getFont() { return _font.get(); }
const osgText::Font* getFont() const { return _font.get(); }
void setMaximumNoCharactersPerLine(unsigned int maxCharsPerLine);
const unsigned int getMaximumNoCharactersPerLine() const { return _maxCharsPerLine; }
void setText(const std::string& text);
std::string& getText() { return _text; }
const std::string& getText() const { return _text; }
void setPosition(const osg::Vec3& position);
const osg::Vec3& getPosition() const { return _position; }
void setAlignment(int alignment);
int getAlignment() { return _alignment; }
float getHeight() const;
protected:
virtual ~Paragraph() {}
void createDrawables();
osg::Vec3 _position;
std::string _text;
osg::ref_ptr<osgText::Font> _font;
int _alignment;
unsigned int _maxCharsPerLine;
};
};
#endif

321
src/osgText/Font.cpp Normal file
View File

@ -0,0 +1,321 @@
/* --------------------------------------------------------------------------
*
* openscenegraph textLib / FTGL
*
* --------------------------------------------------------------------------
*
* prog: max rheiner;mrn@paus.ch
* date: 4/25/2001 (m/d/y)
*
* ----------------------------------------------------------------------------
*
* --------------------------------------------------------------------------
*/
#include <osgText/Font>
#include <osgDB/FileUtils>
#include "FTFace.h"
#include "FTGLBitmapFont.h"
#include "FTGLPixmapFont.h"
#include "FTGLOutlineFont.h"
#include "FTGLPolygonFont.h"
#include "FTGLTextureFont.h"
using namespace osg;
using namespace osgText;
// define the default paths to look for fonts.
// note delimator is : for unix, ; for windows.
#if defined(__linux) || defined(__FreeBSD__) || defined (__sgi)
static char* s_FontFilePath = ".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives";
#elif defined(WIN32)
static char* s_FontFilePath = ".;C:/windows/fonts";
#else
static char* s_FontFilePath = ".:";
#endif
std::string findFontFile(const std::string& str)
{
// try looking in OSGFILEPATH etc first for fonts.
char* filename = osgDB::findFile(str.c_str());
if (filename) return std::string(filename);
// else fallback into the standard font file paths.
if (s_FontFilePath)
{
filename = osgDB::findFileInPath(str.c_str(),s_FontFilePath);
if (filename) return std::string(filename);
}
return std::string();
}
///////////////////////////////////////////////////////////////////////////////
// Font
Font::
Font()
{
_init=false;
_font=NULL;
_created=false;
_pointSize=14;
_res=72;
}
bool Font::
init(const std::string& font)
{
_font=NULL;
_created=false;
open(font);
if(_font!=NULL)
return true;
else
return false;
}
Font::
~Font()
{
clear();
}
bool Font::
open(const std::string& font)
{
clear();
std::string filename = findFontFile(font);
if (filename.empty()) return false;
_font=createFontObj();
if( _font!=NULL && _font->Open(filename.c_str()) )
{
_init=true;
_fontName=font;
return true;
}
else
return false;
}
bool Font::
create(int pointSize,const unsigned int res)
{
_pointSize=pointSize;
_res=res;
return create();
}
bool Font::
create()
{
if(_init)
{
if(_font->FaceSize(_pointSize,_res))
{
_created=true;
return true;
}
else
return false;
}
else
return false;
}
void Font::
output(const char* text)
{
if(_created)
_font->render(text);
else
create(_pointSize);
}
void Font::
clear()
{
_init=false;
if(_font)
{
delete _font;
_font=NULL;
}
_fontName="";
}
float Font::
getWidth(const char* text) const
{
if(_init && _created)
return _font->Advance(text);
else
return -1;
}
int Font::
getHeight() const
{
if(_init && _created)
return _pointSize;
else
return -1;
}
int Font::
getDescender() const
{
if(_init && _created)
return _font->Descender();
else
return -1;
}
int Font::
getAscender() const
{
if(_init && _created)
return _font->Ascender();
else
return -1;
}
// Font
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// BitmapFont
BitmapFont::
BitmapFont(const std::string& font,
int point_size):
RasterFont()
{
if(init(font))
{
}
_pointSize=point_size;
}
FTFont* BitmapFont::
createFontObj(void)
{
return (FTFont*)(new FTGLBitmapFont);
}
// BitmapFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PixmapFont
PixmapFont::
PixmapFont(const std::string& font,
int point_size):
RasterFont(font)
{
if(init(font))
{
}
_pointSize=point_size;
}
FTFont* PixmapFont::
createFontObj(void)
{
return (FTFont*)(new FTGLPixmapFont);
}
// PixmapFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PixmapFont
TextureFont::
TextureFont(const std::string& font,
int point_size):
RasterFont(font)
{
if(init(font))
{
}
_pointSize=point_size;
}
FTFont* TextureFont::
createFontObj(void)
{
return (FTFont*)(new FTGLTextureFont);
}
// PixmapFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// _FTGLOutlineFont
OutlineFont::
OutlineFont(const std::string& font,
int point_size,
double precision):
VectorFont(font)
{
if(init(font))
{
}
_pointSize=point_size;
_precision=precision;
}
FTFont* OutlineFont::
createFontObj(void)
{
return (FTFont*)(new FTGLOutlineFont);
}
// _FTGLOutlineFont
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// PolygonFont
PolygonFont::
PolygonFont(const std::string& font,
int point_size,
double precision):
VectorFont(font)
{
if(init(font))
{
}
_pointSize=point_size;
_precision=precision;
}
FTFont* PolygonFont::
createFontObj(void)
{
return (FTFont*)(new FTGLPolygonFont);
}
// PolygonFont
///////////////////////////////////////////////////////////////////////////////

164
src/osgText/Paragraph.cpp Normal file
View File

@ -0,0 +1,164 @@
#include <osgText/Paragraph>
using namespace osgText;
Paragraph::Paragraph()
{
_alignment = osgText::Text::LEFT_TOP;
_maxCharsPerLine = 80;
}
Paragraph::Paragraph(const osg::Vec3& position,const std::string& text,osgText::Font* font)
{
_maxCharsPerLine = 80;
_position = position;
_font = font;
setText(text);
}
void Paragraph::setPosition(const osg::Vec3& position)
{
if (_position==position) return;
osg::Vec3 delta = position-_position;
_position = position;
for(osg::Geode::DrawableList::iterator itr=_drawables.begin();
itr!=_drawables.end();
++itr)
{
osgText::Text* text = dynamic_cast<osgText::Text*>(itr->get());
if (text) text->setPosition(text->getPosition()+delta);
}
}
void Paragraph::setFont(osgText::Font* font)
{
if (_font==font) return;
_font = font;
for(osg::Geode::DrawableList::iterator itr=_drawables.begin();
itr!=_drawables.end();
++itr)
{
osgText::Text* text = dynamic_cast<osgText::Text*>(itr->get());
if (text) text->setFont(font);
}
}
void Paragraph::setMaximumNoCharactersPerLine(unsigned int maxCharsPerLine)
{
if (_maxCharsPerLine==maxCharsPerLine) return;
if (maxCharsPerLine<1) maxCharsPerLine=1;
else _maxCharsPerLine=maxCharsPerLine;
createDrawables();
}
void Paragraph::setAlignment(int alignment)
{
if (_alignment==alignment) return;
_alignment=alignment;
for(osg::Geode::DrawableList::iterator itr=_drawables.begin();
itr!=_drawables.end();
++itr)
{
osgText::Text* text = dynamic_cast<osgText::Text*>(itr->get());
if (text) text->setAlignment(_alignment);
}
}
void Paragraph::setText(const std::string& text)
{
if (text==_text) return;
_text = text;
createDrawables();
}
float Paragraph::getHeight() const
{
if (_font.valid()) return (_font->getPointSize()+1)*getNumDrawables();
else return 0;
}
void Paragraph::createDrawables()
{
_drawables.clear();
osg::Vec3 pos = _position;
typedef vector<std::string> TextList;
TextList formatedText;
std::string::size_type start = 0;
std::string::size_type last_space = 0;
std::string::size_type current_line_length = 0;
for(std::string::size_type current=0;
current<_text.size();
++current)
{
const char c = _text[current];
if (c==' ') last_space = current;
if (c=='\n')
{
formatedText.push_back(std::string(_text,start,current-start));
start = current+1;
last_space = start;
current_line_length = 0;
}
else if (current_line_length==_maxCharsPerLine)
{
if (last_space>start)
{
formatedText.push_back(std::string(_text,start,last_space-start));
start = last_space+1;
}
else
{
formatedText.push_back(std::string(_text,start,current-start));
start = current+1;
}
last_space = start;
current_line_length = 0;
}
else ++current_line_length;
}
if (start<_text.size())
{
formatedText.push_back(std::string(_text,start,_text.size()-start));
}
// now create the text drawables from the formate text list.
for(TextList::iterator itr=formatedText.begin();
itr!=formatedText.end();
++itr)
{
osgText::Text* textDrawable = new osgText::Text(_font.get());
textDrawable->setAlignment(_alignment);
textDrawable->setPosition(pos);
textDrawable->setText(*itr);
// textDrawable->setDrawMode( osgText::Text::TEXT |
// osgText::Text::BOUNDINGBOX |
// osgText::Text::ALIGNEMENT );
addDrawable(textDrawable);
pos.y() -= (_font->getPointSize()+1);
}
}