From Serge Lages, introduce readRef*File() methods which pass back ref_ptr<> rather than C pointers.

This commit is contained in:
Robert Osfield
2007-12-12 17:04:48 +00:00
parent 88f3a864ac
commit 256391c3b4
10 changed files with 248 additions and 8 deletions

View File

@@ -151,6 +151,59 @@ osgText::Font* osgText::readFontStream(std::istream& stream, const osgDB::Reader
return 0;
}
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;
}
Font::Font(FontImplementation* implementation):
osg::Object(true),