From 79f9cfae8072b88befba2cdc2fed43c21ff5d498 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 7 Feb 2018 18:28:17 +0000 Subject: [PATCH] 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 fontOptions = new osgDB::Options; fontOptions->setObjectCacheHint(osgDB::Options::CACHE_OBJECTS); fontOptions->setOptionString("index=1"); text->setFont(osgText::readFontFile("Menlo.ttc", fontOptions)); " --- .../freetype/ReaderWriterFreeType.cpp | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/osgPlugins/freetype/ReaderWriterFreeType.cpp b/src/osgPlugins/freetype/ReaderWriterFreeType.cpp index bdb8f7cf3..934b76ee6 100644 --- a/src/osgPlugins/freetype/ReaderWriterFreeType.cpp +++ b/src/osgPlugins/freetype/ReaderWriterFreeType.cpp @@ -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=", "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)); } };