Refactored to use a typedef of Font to Font3D rather than have a separate Font3D class

This commit is contained in:
Robert Osfield 2010-09-03 09:08:19 +00:00
parent 8c3e3055e7
commit ba10f56f86
20 changed files with 50 additions and 251 deletions

View File

@ -33,8 +33,7 @@ public:
void setFontResolution(const osgText::FontResolution& fontSize);
virtual osgText::Glyph* getGlyph(const osgText::FontResolution& fontRes, unsigned int charcode);
virtual osgText::Glyph3D* getGlyph3D(unsigned int charcode) { return 0; }
virtual osg::Vec2 getKerning(const osgText::FontResolution& fontRes, unsigned int leftcharcode,
unsigned int rightcharcode, osgText::KerningType kerningType);
virtual osg::Vec2 getKerning(unsigned int leftcharcode, unsigned int rightcharcode, osgText::KerningType kerningType);
virtual bool hasVertical() const;
virtual float getScale() const { return 1.0f; }

View File

@ -317,11 +317,12 @@ public:
/** 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& fontSize, unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType);
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(const FontResolution& fontSize, unsigned int charcode);
/** Get a Glyph3D for specified charcode.*/
virtual Glyph3D* getGlyph3D(unsigned int charcode);
@ -448,7 +449,7 @@ public:
virtual Glyph3D* getGlyph3D(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;
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;

View File

@ -14,169 +14,37 @@
#ifndef OSGTEXT_FONT3D
#define OSGTEXT_FONT3D 1
#include <string>
#include <istream>
#include <osg/Vec2>
#include <osg/Geometry>
#include <osgDB/ReaderWriter>
#include <osgText/Export>
#include <osgText/KerningType>
#include <osgText/Font>
#include <OpenThreads/Mutex>
namespace osgText {
class Font3D;
class Text3D;
typedef Font Font3D;
/** 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 Font3D* readFont3DFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0);
/** read a font from specified stream.*/
extern OSGTEXT_EXPORT Font3D* readFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0);
extern OSGTEXT_EXPORT osg::ref_ptr<Font3D> readRefFont3DFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0);
extern OSGTEXT_EXPORT osg::ref_ptr<Font3D> readRefFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0);
extern OSGTEXT_EXPORT std::string findFont3DFile(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*/
class OSGTEXT_EXPORT Font3D : public osg::Object
inline Font* readFont3DFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0)
{
// declare the interface to a font.
public:
return readFontFile(filename,userOptions);
}
// forward declare nested classes.
class Font3DImplementation;
inline Font* readFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0)
{
return readFontStream(stream, userOptions);
}
public:
Font3D(Font3DImplementation* implementation=0);
inline osg::ref_ptr<Font> readRefFont3DFile(const std::string& filename, const osgDB::ReaderWriter::Options* userOptions = 0)
{
return readRefFontFile(filename, userOptions);
}
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 Font3D*>(obj)!=NULL; }
virtual const char* className() const { return "Font3D"; }
virtual const char* libraryName() const { return "osgText"; }
inline osg::ref_ptr<Font> readRefFont3DStream(std::istream& stream, const osgDB::ReaderWriter::Options* userOptions = 0)
{
return readRefFontStream(stream, userOptions);
}
virtual std::string getFileName() const;
unsigned int getFontWidth() const { return _width; }
unsigned int getFontHeight() const { return _height; }
unsigned int getFontDepth() const { return _depth; }
void setNumberCurveSamples(unsigned int numSamples) { _numCurveSamples = numSamples; }
unsigned int getNumberCurveSamples() const { return _numCurveSamples; }
/** 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 Glyph3D* getGlyph(unsigned int charcode);
/** Return true if this font provides vertical alignments and spacing or glyphs.*/
virtual bool hasVertical() const;
/** Return the scale to apply on the glyph to have a charactere size equal to 1 */
virtual float getScale() const { return _implementation->getScale(); };
// make Text a friend to allow it add and remove its entry in the Font's _textList.
friend class Font3DImplementation;
void setImplementation(Font3DImplementation* implementation);
Font3DImplementation* getImplementation();
const Font3DImplementation* getImplementation() const;
/** Set whether to use a mutex to ensure ref() and unref() */
virtual void setThreadSafeRefUnref(bool threadSafe);
typedef OpenThreads::Mutex Font3DMutex;
protected:
virtual ~Font3D();
// void addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph);
void addGlyph(unsigned int charcode, Glyph3D* glyph);
// current active size of font
unsigned int _depth;
unsigned int _width;
unsigned int _height;
unsigned int _numCurveSamples;
// unsigned int _margin;
// float _marginRatio;
typedef std::map<char, osg::ref_ptr<Glyph3D> > Glyph3DMap;
Glyph3DMap _glyph3DMap;
osg::ref_ptr<Font3DImplementation> _implementation;
// declare the nested classes.
public:
class Font3DImplementation : public osg::Referenced
{
public:
Font3DImplementation():
osg::Referenced(true),
_facade(0) {}
virtual std::string getFileName() const = 0;
/** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
virtual Glyph3D* 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;
virtual float getScale() const = 0;
void setFontWidth(unsigned int width) { _facade->_width = width; }
void setFontHeight(unsigned int height) { _facade->_height = height; }
void setFontDepth(unsigned int depth) { _facade->_depth = depth; }
Font3D* _facade;
};
};
inline std::string findFont3DFile(const std::string& str)
{
return findFontFile(str);
}
}
#endif

View File

@ -124,7 +124,7 @@ public:
// // make Font a friend to allow it set the _font to 0 if the font is
// // forcefully unloaded.
friend class Font3D;
friend class Font;
virtual osg::BoundingBox computeBound() const;

View File

@ -10,12 +10,10 @@ ENDIF()
SET(TARGET_SRC
FreeTypeFont.cpp
FreeTypeFont3D.cpp
FreeTypeLibrary.cpp
ReaderWriterFreeType.cpp )
SET(TARGET_H
FreeTypeFont.h
FreeTypeFont3D.h
FreeTypeLibrary.h )
SET(TARGET_ADDED_LIBRARIES osgText )
SET(TARGET_LIBRARIES_VARS FREETYPE_LIBRARY )

View File

@ -620,14 +620,12 @@ osgText::Glyph3D * FreeTypeFont::getGlyph3D(unsigned int charcode)
return glyph3D.release();
}
osg::Vec2 FreeTypeFont::getKerning(const osgText::FontResolution& fontRes, unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType)
osg::Vec2 FreeTypeFont::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(FreeTypeLibrary::instance()->getMutex());
if (!FT_HAS_KERNING(_face) || (kerningType == osgText::KERNING_NONE)) return osg::Vec2(0.0f,0.0f);
setFontResolution(fontRes);
FT_Kerning_Mode mode = (kerningType==osgText::KERNING_DEFAULT) ? ft_kerning_default : ft_kerning_unfitted;
// convert character code to glyph index

View File

@ -35,7 +35,7 @@ public:
virtual osgText::Glyph3D* getGlyph3D(unsigned int charcode);
virtual osg::Vec2 getKerning(const osgText::FontResolution& fontRes, unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType _kerningType);
virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType _kerningType);
virtual bool hasVertical() const;

View File

@ -49,15 +49,6 @@ FreeTypeLibrary::~FreeTypeLibrary()
else fontImplementation->_facade = 0;
}
while(!_font3DImplementationSet.empty())
{
FreeTypeFont3D* font3DImplementation = *_font3DImplementationSet.begin();
_font3DImplementationSet.erase(_font3DImplementationSet.begin());
osgText::Font3D* font3D = font3DImplementation->_facade;
if (font3D) font3D->setImplementation(0);
else font3DImplementation->_facade = 0;
}
FT_Done_FreeType( _ftlibrary);
}
@ -189,37 +180,6 @@ osgText::Font* FreeTypeLibrary::getFont(std::istream& fontstream, unsigned int i
return font;
}
osgText::Font3D* FreeTypeLibrary::getFont3D(const std::string& fontfile, unsigned int index, unsigned int flags)
{
FT_Face face;
if (getFace(fontfile, index, face) == false) return (0);
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
FreeTypeFont3D* font3DImp = new FreeTypeFont3D(fontfile,face,flags);
osgText::Font3D* font3D = new osgText::Font3D(font3DImp);
_font3DImplementationSet.insert(font3DImp);
return font3D;
}
osgText::Font3D* FreeTypeLibrary::getFont3D(std::istream& fontstream, unsigned int index, unsigned int flags)
{
FT_Face face = 0;
FT_Byte * buffer = getFace(fontstream, index, face);
if (face == 0) return (0);
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(getMutex());
FreeTypeFont3D* font3DImp = new FreeTypeFont3D(buffer,face,flags);
osgText::Font3D* font3D = new osgText::Font3D(font3DImp);
_font3DImplementationSet.insert(font3DImp);
return font3D;
}
void FreeTypeLibrary::verifyCharacterMap(FT_Face face)
{
//

View File

@ -15,7 +15,6 @@
#define FREETYPE_LIBRARY
#include "FreeTypeFont.h"
#include "FreeTypeFont3D.h"
#include <set>
#include <istream>
@ -40,11 +39,7 @@ public:
osgText::Font* getFont(const std::string& fontfile,unsigned int index=0, unsigned int flags=0);
osgText::Font* getFont(std::istream& fontstream, unsigned int index=0, unsigned int flags=0);
osgText::Font3D* getFont3D(const std::string& fontfile, unsigned int index=0, unsigned int flags=0);
osgText::Font3D* getFont3D(std::istream& fontstream, unsigned int index=0, unsigned int flags=0);
void removeFontImplmentation(FreeTypeFont* fontImpl) { _fontImplementationSet.erase(fontImpl); }
void removeFont3DImplmentation(FreeTypeFont3D* font3DImpl) { _font3DImplementationSet.erase(font3DImpl); }
protected:
@ -61,12 +56,10 @@ protected:
FreeTypeLibrary();
typedef std::set< FreeTypeFont* > FontImplementationSet;
typedef std::set< FreeTypeFont3D* > Font3DImplementationSet;
mutable OpenThreads::Mutex _mutex;
FT_Library _ftlibrary;
FontImplementationSet _fontImplementationSet;
Font3DImplementationSet _font3DImplementationSet;
};

View File

@ -39,24 +39,10 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
{
std::string tmpFile = file;
bool needFont3D = false;
std::string ext = osgDB::getLowerCaseFileExtension(tmpFile);
if (ext == "text3d")
{
needFont3D = true;
tmpFile.erase(tmpFile.size()-7, 7);
ext = osgDB::getLowerCaseFileExtension(tmpFile);
}
else if ((options != NULL) && (options->getPluginData("3D")))
{
needFont3D = true;
}
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( tmpFile, options );
std::string fileName = osgDB::findDataFile( file, options );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance();
@ -66,9 +52,6 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
return ReadResult::ERROR_IN_READING_FILE;
}
if (needFont3D)
return freeTypeLibrary->getFont3D(fileName,0,getFlags(options));
else
return freeTypeLibrary->getFont(fileName,0,getFlags(options));
}

View File

@ -118,7 +118,7 @@ TXFFont::hasVertical() const
}
osg::Vec2
TXFFont::getKerning(const osgText::FontResolution&, unsigned int, unsigned int, osgText::KerningType)
TXFFont::getKerning(unsigned int, unsigned int, osgText::KerningType)
{
return osg::Vec2(0, 0);
}

View File

@ -34,7 +34,7 @@ public:
virtual bool hasVertical() const;
virtual osg::Vec2 getKerning(const osgText::FontResolution& fontRes, unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType);
virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType);
virtual float getScale() const { return 1.0; }

View File

@ -128,8 +128,7 @@ QFontImplementation::getGlyph(const osgText::FontResolution& fontRes, unsigned i
}
osg::Vec2
QFontImplementation::getKerning(const osgText::FontResolution& fontRes, unsigned int leftcharcode,
unsigned int rightcharcode, osgText::KerningType kerningType)
QFontImplementation::getKerning(unsigned int leftcharcode, unsigned int rightcharcode, osgText::KerningType kerningType)
{
return osg::Vec2(0, 0);
}

View File

@ -27,7 +27,6 @@ ADD_LIBRARY(${LIB_NAME}
DefaultFont.cpp
DefaultFont.h
Font.cpp
Font3D.cpp
String.cpp
FadeText.cpp
TextBase.cpp

View File

@ -74,7 +74,7 @@ osgText::Glyph* DefaultFont::getGlyph(const FontResolution& fontRes, unsigned in
}
osg::Vec2 DefaultFont::getKerning(const FontResolution&, unsigned int,unsigned int, KerningType)
osg::Vec2 DefaultFont::getKerning(unsigned int,unsigned int, KerningType)
{
// no kerning on default font.
return osg::Vec2(0.0f,0.0f);

View File

@ -37,7 +37,7 @@ public:
virtual osgText::Glyph3D* getGlyph3D(unsigned int charcode) { return 0; }
virtual osg::Vec2 getKerning(const FontResolution&, unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType);
virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType);
virtual bool hasVertical() const;

View File

@ -422,9 +422,9 @@ void Font::releaseGLObjects(osg::State* state) const
// const_cast<Font*>(this)->_sizeGlyphMap.clear();
}
osg::Vec2 Font::getKerning(const FontResolution& fontRes, unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType)
osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType)
{
if (_implementation.valid()) return _implementation->getKerning(fontRes, leftcharcode,rightcharcode,kerningType);
if (_implementation.valid()) return _implementation->getKerning(leftcharcode,rightcharcode,kerningType);
else return osg::Vec2(0.0f,0.0f);
}
@ -943,3 +943,9 @@ void Glyph::subload() const
"\t 0x"<<(unsigned long)data()<<");"<<dec<<std::endl;
}
}
void Glyph3D::setThreadSafeRefUnref(bool threadSafe)
{
if (_vertexArray.valid()) _vertexArray->setThreadSafeRefUnref(threadSafe);
if (_normalArray.valid()) _normalArray->setThreadSafeRefUnref(threadSafe);
}

View File

@ -312,9 +312,4 @@ bool Font3D::hasVertical() const
else return false;
}
void Glyph3D::setThreadSafeRefUnref(bool threadSafe)
{
if (_vertexArray.valid()) _vertexArray->setThreadSafeRefUnref(threadSafe);
if (_normalArray.valid()) _normalArray->setThreadSafeRefUnref(threadSafe);
}
}

View File

@ -149,14 +149,14 @@ String::iterator Text::computeLastCharacterOnLine(osg::Vec2& cursor, String::ite
{
case LEFT_TO_RIGHT:
{
osg::Vec2 delta(activefont->getKerning(_fontSize, previous_charcode,charcode,_kerningType));
osg::Vec2 delta(activefont->getKerning(previous_charcode,charcode,_kerningType));
cursor.x() += delta.x() * wr;
cursor.y() += delta.y() * hr;
break;
}
case RIGHT_TO_LEFT:
{
osg::Vec2 delta(activefont->getKerning(_fontSize, charcode,previous_charcode,_kerningType));
osg::Vec2 delta(activefont->getKerning(charcode,previous_charcode,_kerningType));
cursor.x() -= delta.x() * wr;
cursor.y() -= delta.y() * hr;
break;
@ -418,14 +418,14 @@ void Text::computeGlyphRepresentation()
{
case LEFT_TO_RIGHT:
{
osg::Vec2 delta(activefont->getKerning(_fontSize, previous_charcode,charcode,_kerningType));
osg::Vec2 delta(activefont->getKerning(previous_charcode,charcode,_kerningType));
cursor.x() += delta.x() * wr;
cursor.y() += delta.y() * hr;
break;
}
case RIGHT_TO_LEFT:
{
osg::Vec2 delta(activefont->getKerning(_fontSize, charcode,previous_charcode,_kerningType));
osg::Vec2 delta(activefont->getKerning(charcode,previous_charcode,_kerningType));
cursor.x() -= delta.x() * wr;
cursor.y() -= delta.y() * hr;
break;

View File

@ -135,7 +135,7 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
return lastChar;
}
Glyph3D* glyph = _font->getGlyph(charcode);
Glyph3D* glyph = _font->getGlyph3D(charcode);
if (glyph)
{
const osg::BoundingBox & bb = glyph->getBoundingBox();
@ -245,7 +245,7 @@ String::iterator Text3D::computeLastCharacterOnLine(osg::Vec2& cursor, String::i
// Subtract off glyphs from the cursor position (to correctly center text)
if(*prevChar != '-')
{
Glyph3D* glyph = _font->getGlyph(*prevChar);
Glyph3D* glyph = _font->getGlyph3D(*prevChar);
if (glyph)
{
switch(_layout)
@ -316,7 +316,7 @@ void Text3D::computeGlyphRepresentation()
{
unsigned int charcode = *itr;
Glyph3D* glyph = _font->getGlyph(charcode);
Glyph3D* glyph = _font->getGlyph3D(charcode);
if (glyph)
{
const osg::BoundingBox & bb = glyph->getBoundingBox();