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
|
|
|
|
2008-12-13 02:47:30 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-09-16 01:40:08 +08:00
|
|
|
#include <OpenThreads/ReentrantMutex>
|
|
|
|
|
2010-09-03 16:26:46 +08:00
|
|
|
#include "DefaultFont.h"
|
|
|
|
|
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");
|
|
|
|
|
2010-09-03 16:26:46 +08:00
|
|
|
|
|
|
|
osg::ref_ptr<Font>& Font::getDefaultFont()
|
|
|
|
{
|
|
|
|
static OpenThreads::Mutex s_DefaultFontMutex;
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_DefaultFontMutex);
|
|
|
|
|
|
|
|
static osg::ref_ptr<Font> s_defaultFont = new DefaultFont;
|
|
|
|
return s_defaultFont;
|
|
|
|
}
|
|
|
|
|
2007-09-16 01:40:08 +08:00
|
|
|
static OpenThreads::ReentrantMutex s_FontFileMutex;
|
2006-08-03 21:12:14 +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
|
2010-05-29 00:00:45 +08:00
|
|
|
OSG_INFO<<"Warning: font file \""<<str<<"\" not found."<<std::endl;
|
2003-03-04 00:42:48 +08:00
|
|
|
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
|
|
|
{
|
2010-03-05 23:46:17 +08:00
|
|
|
if (filename.empty()) return 0;
|
2006-07-04 21:56:29 +08:00
|
|
|
|
2003-03-04 00:42:48 +08:00
|
|
|
std::string foundFile = findFontFile(filename);
|
2010-03-05 23:46:17 +08:00
|
|
|
if (foundFile.empty())
|
|
|
|
foundFile = filename;
|
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())
|
|
|
|
{
|
2010-05-29 00:00:45 +08:00
|
|
|
OSG_WARN << rr.message() << std::endl;
|
2005-11-07 19:05:16 +08:00
|
|
|
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)
|
|
|
|
{
|
2010-03-05 23:46:17 +08:00
|
|
|
if (filename.empty()) return 0;
|
2007-12-13 01:04:48 +08:00
|
|
|
|
|
|
|
std::string foundFile = findFontFile(filename);
|
2010-03-05 23:46:17 +08:00
|
|
|
if (foundFile.empty())
|
|
|
|
foundFile = filename;
|
2007-12-13 01:04:48 +08:00
|
|
|
|
|
|
|
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())
|
|
|
|
{
|
2010-05-29 00:00:45 +08:00
|
|
|
OSG_WARN << rr.message() << std::endl;
|
2007-12-13 01:04:48 +08:00
|
|
|
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),
|
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),
|
2010-09-03 16:26:46 +08:00
|
|
|
_magFilterHint(osg::Texture::LINEAR),
|
|
|
|
_depth(1),
|
|
|
|
_numCurveSamples(10)
|
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();
|
2010-03-05 23:46:17 +08:00
|
|
|
return std::string();
|
2003-03-07 01:11:24 +08:00
|
|
|
}
|
|
|
|
|
2003-03-10 17:15:59 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-03 16:26:46 +08:00
|
|
|
Glyph* Font::getGlyph(const FontResolution& fontRes, unsigned int charcode)
|
2003-03-07 01:11:24 +08:00
|
|
|
{
|
2003-03-04 20:34:42 +08:00
|
|
|
{
|
2008-02-25 20:54:54 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
|
|
|
|
FontSizeGlyphMap::iterator itr = _sizeGlyphMap.find(fontRes);
|
|
|
|
if (itr!=_sizeGlyphMap.end())
|
|
|
|
{
|
2010-09-03 16:26:46 +08:00
|
|
|
GlyphMap& glyphmap = itr->second;
|
2008-02-25 20:54:54 +08:00
|
|
|
GlyphMap::iterator gitr = glyphmap.find(charcode);
|
|
|
|
if (gitr!=glyphmap.end()) return gitr->second.get();
|
|
|
|
}
|
2003-03-04 20:34:42 +08:00
|
|
|
}
|
2010-09-03 16:26:46 +08:00
|
|
|
|
|
|
|
Glyph* glyph = _implementation.valid() ? _implementation->getGlyph(fontRes, charcode) : 0;
|
|
|
|
if (glyph)
|
|
|
|
{
|
|
|
|
addGlyph(fontRes, charcode, glyph);
|
|
|
|
return glyph;
|
|
|
|
}
|
2003-03-07 01:11:24 +08:00
|
|
|
else return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-03 16:26:46 +08:00
|
|
|
Glyph3D* Font::getGlyph3D(unsigned int charcode)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
|
|
|
|
Glyph3DMap::iterator itr = _glyph3DMap.find(charcode);
|
|
|
|
if (itr!=_glyph3DMap.end()) return itr->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Glyph3D* glyph = _implementation.valid() ? _implementation->getGlyph3D(charcode) : 0;
|
|
|
|
if (glyph)
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
|
|
|
|
_glyph3DMap[charcode] = glyph;
|
|
|
|
return glyph;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-09 04:40:29 +08:00
|
|
|
void Font::setThreadSafeRefUnref(bool threadSafe)
|
|
|
|
{
|
2008-09-18 23:16:57 +08:00
|
|
|
osg::Object::setThreadSafeRefUnref(threadSafe);
|
2007-01-09 04:40:29 +08:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-09-03 17:08:19 +08:00
|
|
|
osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType)
|
2003-03-07 01:11:24 +08:00
|
|
|
{
|
2010-09-03 17:08:19 +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
|
|
|
|
|
|
|
|
2007-12-24 02:15:54 +08:00
|
|
|
void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
|
2002-06-12 02:41:57 +08:00
|
|
|
{
|
2008-02-25 20:54:54 +08:00
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_glyphMapMutex);
|
|
|
|
|
2007-12-24 02:15:54 +08:00
|
|
|
_sizeGlyphMap[fontRes][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
|
|
|
|
2010-05-29 00:00:45 +08:00
|
|
|
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))
|
|
|
|
{
|
2010-05-29 00:00:45 +08:00
|
|
|
OSG_WARN<<"Warning: unable to allocate texture big enough for glyph"<<std::endl;
|
2003-03-03 05:05:05 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the glyph into the texture.
|
|
|
|
glyphTexture->addGlyph(glyph,posX,posY);
|
|
|
|
|
2001-11-12 18:04:57 +08:00
|
|
|
}
|