From Wang Lam,"The changes allow users of OpenSceneGraph to call a new function

readFontStream() to load fonts from a std::istream, rather than from the
local filesystem by name.  Such a call may be used, for example, if the
user has a font fetched over a network, or a font available in memory
without a correspondng filename.

The changes implement the new function by following the corresponding code
for readFontFile().  readFontStream() reads a stream into memory, and
holds that memory for FreeType.

As a basic test, I mangled the osgtext example to use
readFontStream(std::ifstream("font")) in lieu of a readFontFile call, and
the modified example ran completely."
This commit is contained in:
Robert Osfield
2005-11-07 11:05:16 +00:00
parent 416f908f29
commit 2773d45f2a
7 changed files with 109 additions and 0 deletions

View File

@@ -93,6 +93,33 @@ osgText::Font* osgText::readFontFile(const std::string& filename)
return 0;
}
osgText::Font* osgText::readFontStream(std::istream& stream)
{
osg::ref_ptr<osgDB::ReaderWriter::Options> options = new osgDB::ReaderWriter::Options;
options->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, options.get());
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;
}
Font::Font(FontImplementation* implementation):
_width(16),