2006-07-18 23:21:48 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2001-11-12 18:04:57 +08:00
*
2003-03-03 05:05:05 +08:00
* 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
# include <osgText/Font>
2003-03-04 20:34:42 +08:00
# include <osgText/Text>
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
# include <osg/State>
2001-11-19 18:40:42 +08:00
# include <osg/Notify>
2006-08-03 21:12:14 +08:00
# include <osg/ApplicationUsage>
2003-12-09 19:31:33 +08:00
2003-03-03 05:05:05 +08:00
# include <osgDB/ReadFile>
2003-03-04 00:42:48 +08:00
# include <osgDB/FileUtils>
2005-09-28 21:39:26 +08:00
# include <osgDB/FileNameUtils>
2003-03-03 05:05:05 +08:00
# include <osg/GLU>
2001-11-12 18:04:57 +08:00
2007-09-16 01:40:08 +08:00
# include <OpenThreads/ReentrantMutex>
2001-11-12 18:04:57 +08:00
using namespace osgText ;
2003-04-14 23:02:13 +08:00
using namespace std ;
2001-11-12 18:04:57 +08:00
2006-08-03 21:12:14 +08:00
static osg : : ApplicationUsageProxy Font_e0 ( osg : : ApplicationUsage : : ENVIRONMENTAL_VARIABLE , " OSG_TEXT_INCREMENTAL_SUBLOADING <type> " , " ON | OFF " ) ;
2007-09-16 01:40:08 +08:00
static OpenThreads : : ReentrantMutex s_FontFileMutex ;
2006-08-03 21:12:14 +08:00
2007-09-01 23:24:22 +08:00
Font : : FontMutex * osgText : : Font : : getSerializeFontCallsMutex ( )
{
2007-12-10 23:15:56 +08:00
static OpenThreads : : Mutex s_serializeFontCallsMutex ;
return & s_serializeFontCallsMutex ;
2007-09-01 23:24:22 +08:00
}
2005-11-02 18:57:42 +08:00
std : : string osgText : : findFontFile ( const std : : string & str )
2003-03-04 00:42:48 +08:00
{
// try looking in OSGFILEPATH etc first for fonts.
std : : string filename = osgDB : : findDataFile ( str ) ;
2006-07-04 21:56:29 +08:00
if ( ! filename . empty ( ) ) return filename ;
2003-03-04 00:42:48 +08:00
2007-09-16 01:40:08 +08:00
OpenThreads : : ScopedLock < OpenThreads : : ReentrantMutex > lock ( s_FontFileMutex ) ;
2003-03-04 00:42:48 +08:00
static osgDB : : FilePathList s_FontFilePath ;
static bool initialized = false ;
if ( ! initialized )
{
initialized = true ;
# if defined(WIN32)
2004-08-28 00:14:21 +08:00
osgDB : : convertStringPathIntoFilePathList (
2003-03-04 00:42:48 +08:00
" .;C:/winnt/fonts;C:/windows/fonts " ,
s_FontFilePath ) ;
char * ptr ;
if ( ( ptr = getenv ( " windir " ) ) )
{
2005-09-12 03:08:41 +08:00
std : : string winFontPath = ptr ;
winFontPath + = " \\ fonts " ;
s_FontFilePath . push_back ( winFontPath ) ;
2003-03-04 00:42:48 +08:00
}
# else
2004-08-28 00:14:21 +08:00
osgDB : : convertStringPathIntoFilePathList (
2003-03-04 00:42:48 +08:00
" .:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives " ,
s_FontFilePath ) ;
# endif
}
filename = osgDB : : findFileInPath ( str , s_FontFilePath ) ;
if ( ! filename . empty ( ) ) return filename ;
2005-09-28 21:39:26 +08:00
// Try filename without pathname, if it has a path
filename = osgDB : : getSimpleFileName ( str ) ;
if ( filename ! = str )
{
filename = osgDB : : findFileInPath ( filename , s_FontFilePath ) ;
if ( ! filename . empty ( ) ) return filename ;
}
2006-07-04 21:56:29 +08:00
else
{
filename = osgText : : findFontFile ( std : : string ( " fonts/ " ) + filename ) ;
if ( ! filename . empty ( ) ) return filename ;
}
2005-09-28 21:39:26 +08:00
// Not found, return empty string
2003-03-04 00:42:48 +08:00
osg : : notify ( osg : : WARN ) < < " Warning: font file \" " < < str < < " \" not found. " < < std : : endl ;
return std : : string ( ) ;
}
2007-07-02 20:30:14 +08:00
osgText : : Font * osgText : : readFontFile ( const std : : string & filename , const osgDB : : ReaderWriter : : Options * userOptions )
2001-11-12 18:04:57 +08:00
{
2006-07-04 21:56:29 +08:00
if ( filename = = " " ) return 0 ;
2003-03-04 00:42:48 +08:00
std : : string foundFile = findFontFile ( filename ) ;
if ( foundFile . empty ( ) ) return 0 ;
2004-11-22 22:10:12 +08:00
2007-09-01 02:56:22 +08:00
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( s_FontFileMutex ) ;
2007-07-02 20:30:14 +08:00
osg : : ref_ptr < osgDB : : ReaderWriter : : Options > localOptions ;
if ( ! userOptions )
{
localOptions = new osgDB : : ReaderWriter : : Options ;
localOptions - > setObjectCacheHint ( osgDB : : ReaderWriter : : Options : : CACHE_OBJECTS ) ;
}
2003-03-04 00:42:48 +08:00
2007-07-02 20:30:14 +08:00
osg : : Object * object = osgDB : : readObjectFile ( foundFile , userOptions ? userOptions : localOptions . get ( ) ) ;
2003-03-04 00:42:48 +08:00
2003-03-03 05:05:05 +08:00
// if the object is a font then return it.
osgText : : Font * font = dynamic_cast < osgText : : Font * > ( object ) ;
if ( font ) return font ;
2003-03-04 00:42:48 +08:00
2003-03-03 05:05:05 +08:00
// otherwise if the object has zero references then delete it by doing another unref().
if ( object & & object - > referenceCount ( ) = = 0 ) object - > unref ( ) ;
return 0 ;
2001-11-12 18:04:57 +08:00
}
2007-07-02 20:30:14 +08:00
osgText : : Font * osgText : : readFontStream ( std : : istream & stream , const osgDB : : ReaderWriter : : Options * userOptions )
2005-11-07 19:05:16 +08:00
{
2007-09-01 02:56:22 +08:00
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( s_FontFileMutex ) ;
2007-07-02 20:30:14 +08:00
osg : : ref_ptr < osgDB : : ReaderWriter : : Options > localOptions ;
if ( ! userOptions )
{
localOptions = new osgDB : : ReaderWriter : : Options ;
localOptions - > setObjectCacheHint ( osgDB : : ReaderWriter : : Options : : CACHE_OBJECTS ) ;
}
2005-11-07 19:05:16 +08:00
// there should be a better way to get the FreeType ReaderWriter by name...
osgDB : : ReaderWriter * reader = osgDB : : Registry : : instance ( ) - > getReaderWriterForExtension ( " ttf " ) ;
if ( reader = = 0 ) return 0 ;
2007-07-02 20:30:14 +08:00
osgDB : : ReaderWriter : : ReadResult rr = reader - > readObject ( stream , userOptions ? userOptions : localOptions . get ( ) ) ;
2005-11-07 19:05:16 +08:00
if ( rr . error ( ) )
{
osg : : notify ( osg : : WARN ) < < rr . message ( ) < < std : : endl ;
return 0 ;
}
if ( ! rr . validObject ( ) ) return 0 ;
osg : : Object * object = rr . takeObject ( ) ;
// if the object is a font then return it.
osgText : : Font * font = dynamic_cast < osgText : : Font * > ( object ) ;
if ( font ) return font ;
// otherwise if the object has zero references then delete it by doing another unref().
if ( object & & object - > referenceCount ( ) = = 0 ) object - > unref ( ) ;
return 0 ;
}
2007-12-13 01:04:48 +08:00
osg : : ref_ptr < Font > osgText : : readRefFontFile ( const std : : string & filename , const osgDB : : ReaderWriter : : Options * userOptions )
{
if ( filename = = " " ) return 0 ;
std : : string foundFile = findFontFile ( filename ) ;
if ( foundFile . empty ( ) ) return 0 ;
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( s_FontFileMutex ) ;
osg : : ref_ptr < osgDB : : ReaderWriter : : Options > localOptions ;
if ( ! userOptions )
{
localOptions = new osgDB : : ReaderWriter : : Options ;
localOptions - > setObjectCacheHint ( osgDB : : ReaderWriter : : Options : : CACHE_OBJECTS ) ;
}
osg : : ref_ptr < osg : : Object > object = osgDB : : readRefObjectFile ( foundFile , userOptions ? userOptions : localOptions . get ( ) ) ;
// if the object is a font then return it.
osgText : : Font * font = dynamic_cast < osgText : : Font * > ( object . get ( ) ) ;
if ( font ) return osg : : ref_ptr < Font > ( font ) ;
return 0 ;
}
osg : : ref_ptr < Font > osgText : : readRefFontStream ( std : : istream & stream , const osgDB : : ReaderWriter : : Options * userOptions )
{
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( s_FontFileMutex ) ;
osg : : ref_ptr < osgDB : : ReaderWriter : : Options > localOptions ;
if ( ! userOptions )
{
localOptions = new osgDB : : ReaderWriter : : Options ;
localOptions - > setObjectCacheHint ( osgDB : : ReaderWriter : : Options : : CACHE_OBJECTS ) ;
}
// there should be a better way to get the FreeType ReaderWriter by name...
osgDB : : ReaderWriter * reader = osgDB : : Registry : : instance ( ) - > getReaderWriterForExtension ( " ttf " ) ;
if ( reader = = 0 ) return 0 ;
osgDB : : ReaderWriter : : ReadResult rr = reader - > readObject ( stream , userOptions ? userOptions : localOptions . get ( ) ) ;
if ( rr . error ( ) )
{
osg : : notify ( osg : : WARN ) < < rr . message ( ) < < std : : endl ;
return 0 ;
}
if ( ! rr . validObject ( ) ) return 0 ;
// if the object is a font then return it.
osgText : : Font * font = dynamic_cast < osgText : : Font * > ( rr . getObject ( ) ) ;
if ( font ) return osg : : ref_ptr < Font > ( font ) ;
return 0 ;
}
2001-11-12 18:04:57 +08:00
2003-03-07 01:11:24 +08:00
Font : : Font ( FontImplementation * implementation ) :
2007-08-22 20:14:15 +08:00
osg : : Object ( true ) ,
2003-03-03 05:05:05 +08:00
_width ( 16 ) ,
2003-03-07 01:11:24 +08:00
_height ( 16 ) ,
2007-05-04 20:05:29 +08:00
_margin ( 1 ) ,
2007-05-09 15:54:32 +08:00
_marginRatio ( 0.02 ) ,
2007-05-09 18:42:56 +08:00
_textureWidthHint ( 1024 ) ,
_textureHeightHint ( 1024 ) ,
2003-03-07 01:11:24 +08:00
_minFilterHint ( osg : : Texture : : LINEAR_MIPMAP_LINEAR ) ,
_magFilterHint ( osg : : Texture : : LINEAR )
2001-11-12 18:04:57 +08:00
{
2003-03-07 01:11:24 +08:00
setImplementation ( implementation ) ;
2006-07-04 21:56:29 +08:00
2006-08-29 03:37:40 +08:00
_texenv = new osg : : TexEnv ;
2006-07-04 21:56:29 +08:00
_stateset = new osg : : StateSet ;
_stateset - > setRenderingHint ( osg : : StateSet : : TRANSPARENT_BIN ) ;
2007-05-09 18:31:06 +08:00
char * ptr ;
if ( ( ptr = getenv ( " OSG_MAX_TEXTURE_SIZE " ) ) ! = 0 )
{
2007-07-11 23:30:14 +08:00
unsigned int osg_max_size = atoi ( ptr ) ;
2007-05-09 18:31:06 +08:00
if ( osg_max_size < _textureWidthHint ) _textureWidthHint = osg_max_size ;
if ( osg_max_size < _textureHeightHint ) _textureHeightHint = osg_max_size ;
}
2001-11-12 18:04:57 +08:00
}
2003-03-03 05:05:05 +08:00
Font : : ~ Font ( )
2001-11-12 18:04:57 +08:00
{
2003-03-07 01:11:24 +08:00
if ( _implementation . valid ( ) ) _implementation - > _facade = 0 ;
}
void Font : : setImplementation ( FontImplementation * implementation )
{
if ( _implementation . valid ( ) ) _implementation - > _facade = 0 ;
_implementation = implementation ;
if ( _implementation . valid ( ) ) _implementation - > _facade = this ;
}
2003-03-10 17:15:59 +08:00
Font : : FontImplementation * Font : : getImplementation ( )
{
return _implementation . get ( ) ;
}
const Font : : FontImplementation * Font : : getImplementation ( ) const
{
return _implementation . get ( ) ;
}
2003-03-07 01:11:24 +08:00
std : : string Font : : getFileName ( ) const
{
if ( _implementation . valid ( ) ) return _implementation - > getFileName ( ) ;
return " " ;
}
2007-12-23 21:45:37 +08:00
void Font : : setFontResolution ( const FontSizePair & fontSize )
2003-03-07 01:11:24 +08:00
{
2007-12-23 21:45:37 +08:00
if ( _implementation . valid ( ) ) _implementation - > setFontResolution ( fontSize ) ;
2003-03-07 01:11:24 +08:00
}
2005-01-27 19:10:50 +08:00
unsigned int Font : : getFontWidth ( ) const
2003-03-10 17:15:59 +08:00
{
return _width ;
}
2005-01-27 19:10:50 +08:00
unsigned int Font : : getFontHeight ( ) const
2003-03-10 17:15:59 +08:00
{
return _height ;
}
void Font : : setGlyphImageMargin ( unsigned int margin )
{
_margin = margin ;
}
unsigned int Font : : getGlyphImageMargin ( ) const
{
return _margin ;
}
2007-05-04 20:05:29 +08:00
void Font : : setGlyphImageMarginRatio ( float ratio )
{
_marginRatio = ratio ;
}
float Font : : getGlyphImageMarginRatio ( ) const
{
return _marginRatio ;
}
2003-03-10 17:15:59 +08:00
void Font : : setTextureSizeHint ( unsigned int width , unsigned int height )
{
_textureWidthHint = width ;
_textureHeightHint = height ;
2007-05-09 18:31:06 +08:00
char * ptr ;
if ( ( ptr = getenv ( " OSG_MAX_TEXTURE_SIZE " ) ) ! = 0 )
{
2007-07-11 23:30:14 +08:00
unsigned int osg_max_size = atoi ( ptr ) ;
2007-05-09 18:31:06 +08:00
if ( osg_max_size < _textureWidthHint ) _textureWidthHint = osg_max_size ;
if ( osg_max_size < _textureHeightHint ) _textureHeightHint = osg_max_size ;
}
2003-03-10 17:15:59 +08:00
}
unsigned int Font : : getTextureWidthHint ( ) const
{
return _textureWidthHint ;
}
unsigned int Font : : getTextureHeightHint ( ) const
{
return _textureHeightHint ;
}
void Font : : setMinFilterHint ( osg : : Texture : : FilterMode mode )
{
_minFilterHint = mode ;
}
osg : : Texture : : FilterMode Font : : getMinFilterHint ( ) const
{
return _minFilterHint ;
}
/** 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 Font : : setMagFilterHint ( osg : : Texture : : FilterMode mode )
{
_magFilterHint = mode ;
}
osg : : Texture : : FilterMode Font : : getMagFilterHint ( ) const
{
return _magFilterHint ;
}
2003-03-07 01:11:24 +08:00
Font : : Glyph * Font : : getGlyph ( unsigned int charcode )
{
2007-12-23 21:18:40 +08:00
FontSizeGlyphMap : : iterator itr = _sizeGlyphMap . find ( FontSizePair ( _width , _height ) ) ;
2003-03-07 01:11:24 +08:00
if ( itr ! = _sizeGlyphMap . end ( ) )
2003-03-04 20:34:42 +08:00
{
2003-03-07 01:11:24 +08:00
GlyphMap & glyphmap = itr - > second ;
GlyphMap : : iterator gitr = glyphmap . find ( charcode ) ;
if ( gitr ! = glyphmap . end ( ) ) return gitr - > second . get ( ) ;
2003-03-04 20:34:42 +08:00
}
2003-03-07 01:11:24 +08:00
if ( _implementation . valid ( ) ) return _implementation - > getGlyph ( charcode ) ;
else return 0 ;
}
2007-01-09 04:40:29 +08:00
void Font : : setThreadSafeRefUnref ( bool threadSafe )
{
Object : : setThreadSafeRefUnref ( threadSafe ) ;
if ( _texenv . valid ( ) ) _texenv - > setThreadSafeRefUnref ( threadSafe ) ;
if ( _stateset . valid ( ) ) _stateset - > setThreadSafeRefUnref ( threadSafe ) ;
for ( GlyphTextureList : : const_iterator itr = _glyphTextureList . begin ( ) ;
itr ! = _glyphTextureList . end ( ) ;
+ + itr )
{
( * itr ) - > setThreadSafeRefUnref ( threadSafe ) ;
}
}
void Font : : resizeGLObjectBuffers ( unsigned int maxSize )
{
if ( _stateset . valid ( ) ) _stateset - > resizeGLObjectBuffers ( maxSize ) ;
for ( GlyphTextureList : : const_iterator itr = _glyphTextureList . begin ( ) ;
itr ! = _glyphTextureList . end ( ) ;
+ + itr )
{
( * itr ) - > resizeGLObjectBuffers ( maxSize ) ;
}
}
2005-05-07 17:17:55 +08:00
void Font : : releaseGLObjects ( osg : : State * state ) const
{
2007-01-09 04:40:29 +08:00
if ( _stateset . valid ( ) ) _stateset - > releaseGLObjects ( state ) ;
2006-10-03 19:14:34 +08:00
for ( GlyphTextureList : : const_iterator itr = _glyphTextureList . begin ( ) ;
itr ! = _glyphTextureList . end ( ) ;
+ + itr )
{
( * itr ) - > releaseGLObjects ( state ) ;
2007-01-09 04:40:29 +08:00
}
// const_cast<Font*>(this)->_glyphTextureList.clear();
// const_cast<Font*>(this)->_sizeGlyphMap.clear();
2005-05-07 17:17:55 +08:00
}
2003-06-27 00:21:49 +08:00
osg : : Vec2 Font : : getKerning ( unsigned int leftcharcode , unsigned int rightcharcode , KerningType kerningType )
2003-03-07 01:11:24 +08:00
{
2003-06-27 00:21:49 +08:00
if ( _implementation . valid ( ) ) return _implementation - > getKerning ( leftcharcode , rightcharcode , kerningType ) ;
2003-03-07 01:11:24 +08:00
else return osg : : Vec2 ( 0.0f , 0.0f ) ;
}
bool Font : : hasVertical ( ) const
{
if ( _implementation . valid ( ) ) return _implementation - > hasVertical ( ) ;
else return false ;
2001-11-12 18:04:57 +08:00
}
2003-03-07 01:11:24 +08:00
2003-03-04 20:34:42 +08:00
void Font : : addGlyph ( unsigned int width , unsigned int height , unsigned int charcode , Glyph * glyph )
2002-06-12 02:41:57 +08:00
{
2007-12-23 21:18:40 +08:00
_sizeGlyphMap [ FontSizePair ( width , height ) ] [ charcode ] = glyph ;
2003-03-03 05:05:05 +08:00
int posX = 0 , posY = 0 ;
GlyphTexture * glyphTexture = 0 ;
for ( GlyphTextureList : : iterator itr = _glyphTextureList . begin ( ) ;
itr ! = _glyphTextureList . end ( ) & & ! glyphTexture ;
+ + itr )
{
if ( ( * itr ) - > getSpaceForGlyph ( glyph , posX , posY ) ) glyphTexture = itr - > get ( ) ;
}
2003-03-11 21:30:03 +08:00
if ( glyphTexture )
{
//cout << " found space for texture "<<glyphTexture<<" posX="<<posX<<" posY="<<posY<<endl;
}
2003-03-03 05:05:05 +08:00
if ( ! glyphTexture )
{
2002-06-12 02:41:57 +08:00
2003-03-03 05:05:05 +08:00
glyphTexture = new GlyphTexture ;
2003-03-11 00:40:26 +08:00
2003-03-11 21:30:03 +08:00
static int numberOfTexturesAllocated = 0 ;
+ + numberOfTexturesAllocated ;
2005-04-29 03:50:07 +08:00
osg : : notify ( osg : : INFO ) < < " Font " < < this < < " , numberOfTexturesAllocated " < < numberOfTexturesAllocated < < std : : endl ;
2003-03-07 01:11:24 +08:00
2003-03-03 05:05:05 +08:00
// reserve enough space for the glyphs.
2003-03-07 01:11:24 +08:00
glyphTexture - > setGlyphImageMargin ( _margin ) ;
2007-05-04 20:05:29 +08:00
glyphTexture - > setGlyphImageMarginRatio ( _marginRatio ) ;
2003-03-07 01:11:24 +08:00
glyphTexture - > setTextureSize ( _textureWidthHint , _textureHeightHint ) ;
glyphTexture - > setFilter ( osg : : Texture : : MIN_FILTER , _minFilterHint ) ;
glyphTexture - > setFilter ( osg : : Texture : : MAG_FILTER , _magFilterHint ) ;
2003-03-03 05:05:05 +08:00
glyphTexture - > setMaxAnisotropy ( 8 ) ;
_glyphTextureList . push_back ( glyphTexture ) ;
if ( ! glyphTexture - > getSpaceForGlyph ( glyph , posX , posY ) )
{
osg : : notify ( osg : : WARN ) < < " Warning: unable to allocate texture big enough for glyph " < < std : : endl ;
return ;
}
}
// add the glyph into the texture.
glyphTexture - > addGlyph ( glyph , posX , posY ) ;
2001-11-12 18:04:57 +08:00
}
2003-03-03 05:05:05 +08:00
Font : : GlyphTexture : : GlyphTexture ( ) :
2007-05-04 20:05:29 +08:00
_margin ( 1 ) ,
2007-05-09 15:54:32 +08:00
_marginRatio ( 0.02f ) ,
2003-03-03 05:05:05 +08:00
_usedY ( 0 ) ,
_partUsedX ( 0 ) ,
_partUsedY ( 0 )
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
{
}
2002-01-19 06:25:51 +08:00
2003-03-03 05:05:05 +08:00
Font : : GlyphTexture : : ~ GlyphTexture ( )
2001-11-12 18:04:57 +08:00
{
}
2003-03-11 21:30:03 +08:00
// return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.
2003-03-12 05:42:55 +08:00
int Font : : GlyphTexture : : compare ( const osg : : StateAttribute & rhs ) const
2003-03-11 21:30:03 +08:00
{
if ( this < & rhs ) return - 1 ;
else if ( this > & rhs ) return 1 ;
return 0 ;
}
2003-03-03 05:05:05 +08:00
bool Font : : GlyphTexture : : getSpaceForGlyph ( Glyph * glyph , int & posX , int & posY )
2001-11-12 18:04:57 +08:00
{
2007-05-04 20:05:29 +08:00
int maxAxis = std : : max ( glyph - > s ( ) , glyph - > t ( ) ) ;
int margin = _margin + ( int ) ( ( float ) maxAxis * _marginRatio ) ;
int width = glyph - > s ( ) + 2 * margin ;
int height = glyph - > t ( ) + 2 * margin ;
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
// first check box (_partUsedX,_usedY) to (width,height)
if ( width < = ( getTextureWidth ( ) - _partUsedX ) & &
height < = ( getTextureHeight ( ) - _usedY ) )
2002-11-06 23:43:11 +08:00
{
2003-03-03 05:05:05 +08:00
// can fit in existing row.
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
// record the position in which the texture will be stored.
2007-05-04 20:05:29 +08:00
posX = _partUsedX + margin ;
posY = _usedY + margin ;
2003-03-03 05:05:05 +08:00
// move used markers on.
_partUsedX + = width ;
if ( _usedY + height > _partUsedY ) _partUsedY = _usedY + height ;
return true ;
}
2001-11-14 22:09:07 +08:00
2003-03-03 05:05:05 +08:00
// start an new row.
if ( width < = getTextureWidth ( ) & &
height < = ( getTextureHeight ( ) - _partUsedY ) )
2001-11-14 22:09:07 +08:00
{
2003-03-03 05:05:05 +08:00
// can fit next row.
_partUsedX = 0 ;
_usedY = _partUsedY ;
2007-05-04 20:05:29 +08:00
posX = _partUsedX + margin ;
posY = _usedY + margin ;
2003-03-03 05:05:05 +08:00
// move used markers on.
_partUsedX + = width ;
if ( _usedY + height > _partUsedY ) _partUsedY = _usedY + height ;
return true ;
2001-11-14 22:09:07 +08:00
}
2003-03-03 05:05:05 +08:00
// doesn't fit into glyph.
return false ;
2001-11-12 18:04:57 +08:00
}
2003-03-03 05:05:05 +08:00
void Font : : GlyphTexture : : addGlyph ( Glyph * glyph , int posX , int posY )
2001-11-12 18:04:57 +08:00
{
2007-09-04 20:32:47 +08:00
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( _mutex ) ;
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
_glyphs . push_back ( glyph ) ;
for ( unsigned int i = 0 ; i < _glyphsToSubload . size ( ) ; + + i )
{
_glyphsToSubload [ i ] . push_back ( glyph ) ;
}
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
// set up the details of where to place glyph's image in the texture.
glyph - > setTexture ( this ) ;
glyph - > setTexturePosition ( posX , posY ) ;
2007-05-04 20:05:29 +08:00
unsigned int sizeAdjustment = 1 ;
glyph - > setMinTexCoord ( osg : : Vec2 ( ( float ) ( posX ) / ( float ) ( getTextureWidth ( ) - sizeAdjustment ) , ( float ) ( posY ) / ( float ) ( getTextureHeight ( ) - sizeAdjustment ) ) ) ;
glyph - > setMaxTexCoord ( osg : : Vec2 ( ( float ) ( posX + glyph - > s ( ) ) / ( float ) ( getTextureWidth ( ) - sizeAdjustment ) , ( float ) ( posY + glyph - > t ( ) ) / ( float ) ( getTextureHeight ( ) - sizeAdjustment ) ) ) ;
2001-11-12 18:04:57 +08:00
}
2003-03-03 05:05:05 +08:00
void Font : : GlyphTexture : : apply ( osg : : State & state ) const
2001-11-12 18:04:57 +08:00
{
2003-03-03 05:05:05 +08:00
// get the contextID (user defined ID of 0 upwards) for the
// current OpenGL context.
const unsigned int contextID = state . getContextID ( ) ;
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
if ( contextID > = _glyphsToSubload . size ( ) )
2001-11-14 22:09:07 +08:00
{
2007-09-04 20:32:47 +08:00
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( _mutex ) ;
2003-03-03 05:05:05 +08:00
// graphics context is beyond the number of glyphsToSubloads, so
// we must now copy the glyph list across, this is a potential
// threading issue though is multiple applies are happening the
// same time on this object - to avoid this condition number of
// graphics contexts should be set before create text.
for ( unsigned int i = _glyphsToSubload . size ( ) ; i < = contextID ; + + i )
{
2003-03-04 00:12:00 +08:00
GlyphPtrList & glyphPtrs = _glyphsToSubload [ i ] ;
for ( GlyphRefList : : const_iterator itr = _glyphs . begin ( ) ;
itr ! = _glyphs . end ( ) ;
+ + itr )
{
glyphPtrs . push_back ( itr - > get ( ) ) ;
}
2003-03-03 05:05:05 +08:00
}
2001-11-14 22:09:07 +08:00
}
2001-11-12 18:04:57 +08:00
2003-03-04 23:47:28 +08:00
const Extensions * extensions = getExtensions ( contextID , true ) ;
bool generateMipMapSupported = extensions - > isGenerateMipMapSupported ( ) ;
2003-07-14 22:42:10 +08:00
// get the texture object for the current contextID.
TextureObject * textureObject = getTextureObject ( contextID ) ;
2004-07-19 22:34:12 +08:00
bool newTextureObject = ( textureObject = = 0 ) ;
2001-11-12 18:04:57 +08:00
2004-07-19 22:34:12 +08:00
if ( newTextureObject )
2001-11-14 22:09:07 +08:00
{
2007-05-09 18:41:56 +08:00
GLint maxTextureSize = 256 ;
glGetIntegerv ( GL_MAX_TEXTURE_SIZE , & maxTextureSize ) ;
if ( maxTextureSize < getTextureWidth ( ) | | maxTextureSize < getTextureHeight ( ) )
{
osg : : notify ( osg : : WARN ) < < " Warning: osgText::Font texture size of ( " < < getTextureWidth ( ) < < " , " < < getTextureHeight ( ) < < " ) too large, unable to create font texture. " < < std : : endl ;
osg : : notify ( osg : : WARN ) < < " Maximum supported by hardward by native OpenGL implementation is ( " < < maxTextureSize < < " , " < < maxTextureSize < < " ). " < < std : : endl ;
osg : : notify ( osg : : WARN ) < < " Please set OSG_MAX_TEXTURE_SIZE lenvironment variable to " < < maxTextureSize < < " and re-run application. " < < std : : endl ;
return ;
}
2003-07-14 22:42:10 +08:00
2003-03-03 05:05:05 +08:00
// being bound for the first time, need to allocate the texture
2003-07-14 22:42:10 +08:00
2004-09-22 05:33:52 +08:00
_textureObjectBuffer [ contextID ] = textureObject = osg : : Texture : : generateTextureObject (
2003-12-09 20:04:14 +08:00
contextID , GL_TEXTURE_2D , 1 , GL_ALPHA , getTextureWidth ( ) , getTextureHeight ( ) , 1 , 0 ) ;
2004-07-19 22:34:12 +08:00
2003-07-14 22:42:10 +08:00
textureObject - > bind ( ) ;
2003-03-03 05:05:05 +08:00
applyTexParameters ( GL_TEXTURE_2D , state ) ;
2003-04-02 04:32:57 +08:00
2003-03-03 05:05:05 +08:00
2003-03-04 23:47:28 +08:00
// need to look at generate mip map extension if mip mapping required.
switch ( _min_filter )
{
case NEAREST_MIPMAP_NEAREST :
case NEAREST_MIPMAP_LINEAR :
case LINEAR_MIPMAP_NEAREST :
case LINEAR_MIPMAP_LINEAR :
2003-04-02 04:32:57 +08:00
if ( generateMipMapSupported )
{
2003-03-04 23:47:28 +08:00
glTexParameteri ( GL_TEXTURE_2D , GL_GENERATE_MIPMAP_SGIS , GL_TRUE ) ;
}
else glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , LINEAR ) ;
break ;
default :
// not mip mapping so no problems.
break ;
}
2007-05-09 15:54:32 +08:00
unsigned int imageDataSize = getTextureHeight ( ) * getTextureWidth ( ) ;
unsigned char * imageData = new unsigned char [ imageDataSize ] ;
for ( unsigned int i = 0 ; i < imageDataSize ; + + i )
{
imageData [ i ] = 0 ;
}
2007-05-09 18:31:06 +08:00
2007-05-09 21:05:19 +08:00
// osg::notify(osg::NOTICE)<<"Texture width = "<<getTextureWidth()<<std::endl;
// osg::notify(osg::NOTICE)<<"Texture height = "<<getTextureHeight()<<std::endl;
2003-03-04 23:47:28 +08:00
2003-03-03 05:05:05 +08:00
// allocate the texture memory.
2003-12-09 20:04:14 +08:00
glTexImage2D ( GL_TEXTURE_2D , 0 , GL_ALPHA ,
getTextureWidth ( ) , getTextureHeight ( ) , 0 ,
GL_ALPHA ,
GL_UNSIGNED_BYTE ,
2007-05-09 15:54:32 +08:00
imageData ) ;
delete [ ] imageData ;
2001-11-14 22:09:07 +08:00
2003-03-03 05:05:05 +08:00
}
else
{
// reuse texture by binding.
2003-07-14 22:42:10 +08:00
textureObject - > bind ( ) ;
2003-03-03 05:05:05 +08:00
if ( getTextureParameterDirty ( contextID ) )
2003-03-04 23:47:28 +08:00
{
2003-03-03 05:05:05 +08:00
applyTexParameters ( GL_TEXTURE_2D , state ) ;
2003-04-02 04:32:57 +08:00
}
2001-11-12 18:04:57 +08:00
2003-03-04 23:47:28 +08:00
2003-03-03 05:05:05 +08:00
}
2001-11-14 22:09:07 +08:00
2003-06-24 23:40:09 +08:00
static const GLubyte * s_renderer = 0 ;
static bool s_subloadAllGlyphsTogether = false ;
if ( ! s_renderer )
{
2007-09-04 20:32:47 +08:00
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( _mutex ) ;
2003-06-24 23:40:09 +08:00
s_renderer = glGetString ( GL_RENDERER ) ;
osg : : notify ( osg : : INFO ) < < " glGetString(GL_RENDERER)== " < < s_renderer < < std : : endl ;
2007-08-25 00:10:43 +08:00
if ( s_renderer & & strstr ( ( const char * ) s_renderer , " IMPACT " ) ! = 0 )
2003-06-24 23:40:09 +08:00
{
// we're running on an Octane, so need to work around its
// subloading bugs by loading all at once.
s_subloadAllGlyphsTogether = true ;
}
2006-08-03 21:12:14 +08:00
2007-08-25 00:10:43 +08:00
if ( s_renderer & &
( strstr ( ( const char * ) s_renderer , " Radeon " ) ! = 0 ) | |
2007-12-11 01:30:18 +08:00
( strstr ( ( const char * ) s_renderer , " RADEON " ) ! = 0 ) | |
( strstr ( ( const char * ) s_renderer , " ALL-IN-WONDER " ) ! = 0 ) )
2007-08-25 00:10:43 +08:00
{
// we're running on an ATI, so need to work around its
// subloading bugs by loading all at once.
s_subloadAllGlyphsTogether = true ;
}
2007-12-21 22:16:40 +08:00
if ( s_renderer & & strstr ( ( const char * ) s_renderer , " Sun " ) ! = 0 )
{
// we're running on an solaris x server, so need to work around its
// subloading bugs by loading all at once.
s_subloadAllGlyphsTogether = true ;
}
2006-08-03 21:12:14 +08:00
const char * str = getenv ( " OSG_TEXT_INCREMENTAL_SUBLOADING " ) ;
2007-08-25 00:12:22 +08:00
if ( str )
2006-08-03 21:12:14 +08:00
{
2007-12-21 22:16:40 +08:00
s_subloadAllGlyphsTogether = strcmp ( str , " OFF " ) = = 0 | | strcmp ( str , " Off " ) = = 0 | | strcmp ( str , " off " ) = = 0 ;
2006-08-03 21:12:14 +08:00
}
2003-06-24 23:40:09 +08:00
}
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
// now subload the glyphs that are outstanding for this graphics context.
2003-03-04 00:12:00 +08:00
GlyphPtrList & glyphsWereSubloading = _glyphsToSubload [ contextID ] ;
2001-11-12 18:04:57 +08:00
2004-07-19 22:34:12 +08:00
if ( ! glyphsWereSubloading . empty ( ) | | newTextureObject )
2002-01-16 18:36:20 +08:00
{
2007-09-04 20:32:47 +08:00
OpenThreads : : ScopedLock < OpenThreads : : Mutex > lock ( _mutex ) ;
2003-06-24 23:40:09 +08:00
if ( ! s_subloadAllGlyphsTogether )
{
2004-07-19 22:34:12 +08:00
if ( newTextureObject )
2003-06-24 23:40:09 +08:00
{
2004-07-19 22:34:12 +08:00
for ( GlyphRefList : : const_iterator itr = _glyphs . begin ( ) ;
itr ! = _glyphs . end ( ) ;
+ + itr )
{
( * itr ) - > subload ( ) ;
}
2003-06-24 23:40:09 +08:00
}
2004-07-19 22:34:12 +08:00
else // just subload the new entries.
{
// default way of subloading as required.
//std::cout<<"subloading"<<std::endl;
for ( GlyphPtrList : : iterator itr = glyphsWereSubloading . begin ( ) ;
itr ! = glyphsWereSubloading . end ( ) ;
+ + itr )
{
( * itr ) - > subload ( ) ;
}
}
2003-06-24 23:40:09 +08:00
// clear the list since we have now subloaded them.
glyphsWereSubloading . clear ( ) ;
}
else
2003-03-03 05:05:05 +08:00
{
2006-08-03 21:12:14 +08:00
osg : : notify ( osg : : INFO ) < < " osgText::Font loading all glyphs as a single subload. " < < std : : endl ;
2003-06-24 23:40:09 +08:00
// Octane has bugs in OGL driver which mean that subloads smaller
// than 32x32 produce errors, and also cannot handle general alignment,
// so to get round this copy all glyphs into a temporary image and
// then subload the whole lot in one go.
2003-12-09 20:04:14 +08:00
int tsize = getTextureHeight ( ) * getTextureWidth ( ) ;
unsigned char * local_data = new unsigned char [ tsize ] ;
memset ( local_data , 0L , tsize ) ;
for ( GlyphRefList : : const_iterator itr = _glyphs . begin ( ) ;
itr ! = _glyphs . end ( ) ;
+ + itr )
{
//(*itr)->subload();
// Rather than subloading to graphics, we'll write the values
// of the glyphs into some intermediate data and subload the
// whole thing at the end
for ( int t = 0 ; t < ( * itr ) - > t ( ) ; t + + )
{
for ( int s = 0 ; s < ( * itr ) - > s ( ) ; s + + )
{
int sindex = ( t * ( * itr ) - > s ( ) + s ) ;
int dindex =
( ( ( ( * itr ) - > getTexturePositionY ( ) + t ) * getTextureWidth ( ) ) +
( ( * itr ) - > getTexturePositionX ( ) + s ) ) ;
const unsigned char * sptr = & ( * itr ) - > data ( ) [ sindex ] ;
unsigned char * dptr = & local_data [ dindex ] ;
( * dptr ) = ( * sptr ) ;
}
}
}
// clear the list since we have now subloaded them.
glyphsWereSubloading . clear ( ) ;
// Subload the image once
glTexSubImage2D ( GL_TEXTURE_2D , 0 , 0 , 0 ,
getTextureWidth ( ) ,
getTextureHeight ( ) ,
GL_ALPHA , GL_UNSIGNED_BYTE , local_data ) ;
delete [ ] local_data ;
2003-03-03 05:05:05 +08:00
}
}
else
2001-11-14 22:09:07 +08:00
{
2007-01-09 06:40:56 +08:00
// osg::notify(osg::INFO) << "no need to subload "<<std::endl;
2001-11-14 22:09:07 +08:00
}
2003-04-02 04:32:57 +08:00
2003-07-14 22:42:10 +08:00
// if (generateMipMapTurnedOn)
// {
// glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
// }
2003-04-02 04:32:57 +08:00
2001-11-12 18:04:57 +08:00
}
2007-01-09 04:46:56 +08:00
void Font : : GlyphTexture : : setThreadSafeRefUnref ( bool threadSafe )
{
osg : : Texture2D : : setThreadSafeRefUnref ( threadSafe ) ;
}
void Font : : GlyphTexture : : resizeGLObjectBuffers ( unsigned int maxSize )
{
osg : : Texture2D : : resizeGLObjectBuffers ( maxSize ) ;
_glyphsToSubload . resize ( maxSize ) ;
}
2003-03-04 06:18:33 +08:00
// all the methods in Font::Glyph have been made non inline because VisualStudio6.0 is STUPID, STUPID, STUPID PILE OF JUNK.
2007-12-17 01:01:40 +08:00
Font : : Glyph : : Glyph ( ) :
_font ( 0 ) ,
_glyphCode ( 0 ) ,
_horizontalBearing ( 0.0f , 0.f ) ,
_horizontalAdvance ( 0.f ) ,
_verticalBearing ( 0.0f , 0.f ) ,
_verticalAdvance ( 0.f ) ,
_texture ( 0 ) ,
_texturePosX ( 0 ) ,
_texturePosY ( 0 ) ,
_minTexCoord ( 0.0f , 0.0f ) ,
_maxTexCoord ( 0.0f , 0.0f )
{
setThreadSafeRefUnref ( true ) ;
}
Font : : Glyph : : ~ Glyph ( )
{
}
2001-11-12 18:04:57 +08:00
2003-03-04 06:18:33 +08:00
unsigned int Font : : Glyph : : getGlyphCode ( ) const { return _glyphCode ; }
2003-03-04 20:34:42 +08:00
// void Font::Glyph::setFont(Font* font) { _font = font; }
// Font* Font::Glyph::getFont() const { return _font; }
2003-03-04 06:18:33 +08:00
void Font : : Glyph : : setHorizontalBearing ( const osg : : Vec2 & bearing ) { _horizontalBearing = bearing ; }
const osg : : Vec2 & Font : : Glyph : : getHorizontalBearing ( ) const { return _horizontalBearing ; }
void Font : : Glyph : : setHorizontalAdvance ( float advance ) { _horizontalAdvance = advance ; }
float Font : : Glyph : : getHorizontalAdvance ( ) const { return _horizontalAdvance ; }
void Font : : Glyph : : setVerticalBearing ( const osg : : Vec2 & bearing ) { _verticalBearing = bearing ; }
const osg : : Vec2 & Font : : Glyph : : getVerticalBearing ( ) const { return _verticalBearing ; }
void Font : : Glyph : : setVerticalAdvance ( float advance ) { _verticalAdvance = advance ; }
float Font : : Glyph : : getVerticalAdvance ( ) const { return _verticalAdvance ; }
void Font : : Glyph : : setTexture ( GlyphTexture * texture ) { _texture = texture ; }
Font : : GlyphTexture * Font : : Glyph : : getTexture ( ) { return _texture ; }
const Font : : GlyphTexture * Font : : Glyph : : getTexture ( ) const { return _texture ; }
void Font : : Glyph : : setTexturePosition ( int posX , int posY ) { _texturePosX = posX ; _texturePosY = posY ; }
int Font : : Glyph : : getTexturePositionX ( ) const { return _texturePosX ; }
int Font : : Glyph : : getTexturePositionY ( ) const { return _texturePosY ; }
void Font : : Glyph : : setMinTexCoord ( const osg : : Vec2 & coord ) { _minTexCoord = coord ; }
const osg : : Vec2 & Font : : Glyph : : getMinTexCoord ( ) const { return _minTexCoord ; }
void Font : : Glyph : : setMaxTexCoord ( const osg : : Vec2 & coord ) { _maxTexCoord = coord ; }
const osg : : Vec2 & Font : : Glyph : : getMaxTexCoord ( ) const { return _maxTexCoord ; }
2003-03-04 00:12:00 +08:00
void Font : : Glyph : : subload ( ) const
2001-11-12 18:04:57 +08:00
{
2003-03-03 05:05:05 +08:00
GLenum errorNo = glGetError ( ) ;
if ( errorNo ! = GL_NO_ERROR )
2001-11-14 22:09:07 +08:00
{
2003-04-14 23:02:13 +08:00
osg : : notify ( osg : : WARN ) < < " before Font::Glyph::subload(): detected OpenGL error ' " < < gluErrorString ( errorNo ) < < std : : endl ;
2001-11-14 22:09:07 +08:00
}
2001-11-12 18:04:57 +08:00
2003-03-03 05:05:05 +08:00
glPixelStorei ( GL_UNPACK_ALIGNMENT , getPacking ( ) ) ;
glTexSubImage2D ( GL_TEXTURE_2D , 0 ,
_texturePosX , _texturePosY ,
s ( ) , t ( ) ,
( GLenum ) getPixelFormat ( ) ,
( GLenum ) getDataType ( ) ,
data ( ) ) ;
errorNo = glGetError ( ) ;
if ( errorNo ! = GL_NO_ERROR )
2002-01-19 06:25:51 +08:00
{
2003-04-14 23:02:13 +08:00
osg : : notify ( osg : : WARN ) < < " after Font::Glyph::subload() : detected OpenGL error ' " < < gluErrorString ( errorNo ) < < " ' " < < std : : endl ;
2004-03-03 21:27:21 +08:00
osg : : notify ( osg : : WARN ) < < " \t glTexSubImage2D(0x " < < hex < < GL_TEXTURE_2D < < dec < < " , " < < 0 < < " \t " < < std : : endl < <
" \t " < < _texturePosX < < " , " < < _texturePosY < < std : : endl < <
" \t " < < s ( ) < < " , " < < t ( ) < < std : : endl < < hex < <
" \t 0x " < < ( GLenum ) getPixelFormat ( ) < < std : : endl < <
" \t 0x " < < ( GLenum ) getDataType ( ) < < std : : endl < <
" \t 0x " < < ( unsigned long ) data ( ) < < " ); " < < dec < < std : : endl ;
2003-03-03 05:05:05 +08:00
}
2001-11-12 18:04:57 +08:00
}
2003-05-02 05:06:18 +08:00
void Font : : Glyph : : draw ( osg : : State & state ) const
{
GLuint & globj = _globjList [ state . getContextID ( ) ] ;
// call the globj if already set otherwise compile and execute.
if ( globj ! = 0 )
{
glCallList ( globj ) ;
}
else
{
globj = glGenLists ( 1 ) ;
glNewList ( globj , GL_COMPILE_AND_EXECUTE ) ;
glPixelStorei ( GL_UNPACK_ALIGNMENT , getPacking ( ) ) ;
glDrawPixels ( s ( ) , t ( ) ,
( GLenum ) getPixelFormat ( ) ,
( GLenum ) getDataType ( ) ,
data ( ) ) ;
glEndList ( ) ;
}
}