From Ravi Mathur, "he freetype plugin currently forces the use of the first font within a truetype collection (.ttc index 0). I made a slight modification such that users can specify any font index via the userOptions input to osgText::readFontFile(). Specifically, the freetype plugin now accepts a new string option of the format "index=< unsigned int >". Example usage:

Code:
// Chooses the second font within the Menlo font collection
osg::ref_ptr<osgDB::Options> fontOptions = new osgDB::Options;
fontOptions->setObjectCacheHint(osgDB::Options::CACHE_OBJECTS);
fontOptions->setOptionString("index=1");
text->setFont(osgText::readFontFile("Menlo.ttc", fontOptions));

"
This commit is contained in:
Robert Osfield 2018-02-07 18:28:17 +00:00
parent f767ab0cc9
commit 79f9cfae80

View File

@ -11,7 +11,7 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
ReaderWriterFreeType()
{
supportsExtension("ttf","true type font format");
supportsExtension("ttc","true type format");
supportsExtension("ttc","true type collection format");
supportsExtension("pfb","type1 binary format");
supportsExtension("pfa","type2 ascii format");
supportsExtension("cid","Postscript CID-Fonts format");
@ -23,6 +23,7 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
supportsExtension("woff","web open font format");
supportsOption("monochrome","Select monochrome font.");
supportsOption("index=<uint>", "Select index of font within ttc collection. Defaults to 0.");
}
virtual const char* className() const { return "FreeType Font Reader/Writer"; }
@ -38,6 +39,20 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
return flags;
}
static unsigned int getIndex(const osgDB::ReaderWriter::Options* options)
{
if(!options) return 0;
std::string indexstr = options->getPluginStringData("index");
int index = std::atoi(indexstr.c_str());
if(index < 0)
{
OSG_WARN<< "Warning: invalid index string (" << indexstr << ") when loading freetype font. Attempting to use default index 0." << std::endl;
return 0;
}
else return (unsigned int)index;
}
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
@ -53,7 +68,7 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
return ReadResult::ERROR_IN_READING_FILE;
}
return freeTypeLibrary->getFont(fileName,0,getFlags(options));
return freeTypeLibrary->getFont(fileName, getIndex(options), getFlags(options));
}
virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options* options) const
@ -65,7 +80,7 @@ class ReaderWriterFreeType : public osgDB::ReaderWriter
return ReadResult::ERROR_IN_READING_FILE;
}
return freeTypeLibrary->getFont(stream, 0, getFlags(options));
return freeTypeLibrary->getFont(stream, getIndex(options), getFlags(options));
}
};