1 #include <osgDB/FileNameUtils> 2 #include <osgDB/FileUtils> 3 #include <osgDB/Registry> 4 #include <osg/Notify> 5 6 #include "FreeTypeLibrary.h" 7 8 class ReaderWriterFreeType : public osgDB::ReaderWriter 9 { 10 public: ReaderWriterFreeType()11 ReaderWriterFreeType() 12 { 13 supportsExtension("ttf","true type font format"); 14 supportsExtension("ttc","true type collection format"); 15 supportsExtension("pfb","type1 binary format"); 16 supportsExtension("pfa","type2 ascii format"); 17 supportsExtension("cid","Postscript CID-Fonts format"); 18 supportsExtension("cff","OpenType format"); 19 supportsExtension("cef","OpenType format"); 20 supportsExtension("fon","Windows bitmap fonts format"); 21 supportsExtension("fnt","Windows bitmap fonts format"); 22 supportsExtension("text3d","use 3D Font instead of 2D Font"); 23 supportsExtension("woff","web open font format"); 24 25 supportsOption("monochrome","Select monochrome font."); 26 supportsOption("index=<uint>", "Select index of font within ttc collection. Defaults to 0."); 27 } 28 className() const29 virtual const char* className() const { return "FreeType Font Reader/Writer"; } 30 getFlags(const osgDB::ReaderWriter::Options * options)31 static unsigned int getFlags(const osgDB::ReaderWriter::Options* options) 32 { 33 unsigned int flags = 0; 34 if (options && options->getOptionString().find("monochrome") != std::string::npos) 35 { 36 flags |= FT_LOAD_MONOCHROME; 37 } 38 39 return flags; 40 } 41 getIndex(const osgDB::ReaderWriter::Options * options)42 static unsigned int getIndex(const osgDB::ReaderWriter::Options* options) 43 { 44 if(!options) return 0; 45 46 std::string indexstr = options->getPluginStringData("index"); 47 int index = std::atoi(indexstr.c_str()); 48 if(index < 0) 49 { 50 OSG_WARN<< "Warning: invalid index string (" << indexstr << ") when loading freetype font. Attempting to use default index 0." << std::endl; 51 return 0; 52 } 53 else return (unsigned int)index; 54 } 55 readObject(const std::string & file,const osgDB::ReaderWriter::Options * options) const56 virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const 57 { 58 std::string ext = osgDB::getLowerCaseFileExtension(file); 59 if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; 60 61 std::string fileName = osgDB::findDataFile( file, options ); 62 if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; 63 64 FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); 65 if (!freeTypeLibrary) 66 { 67 OSG_WARN<<"Warning:: cannot create freetype font after freetype library has been deleted."<<std::endl; 68 return ReadResult::ERROR_IN_READING_FILE; 69 } 70 71 return freeTypeLibrary->getFont(fileName, getIndex(options), getFlags(options)); 72 } 73 readObject(std::istream & stream,const osgDB::ReaderWriter::Options * options) const74 virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options* options) const 75 { 76 FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); 77 if (!freeTypeLibrary) 78 { 79 OSG_WARN<<"Warning:: cannot create freetype font after freetype library has been deleted."<<std::endl; 80 return ReadResult::ERROR_IN_READING_FILE; 81 } 82 83 return freeTypeLibrary->getFont(stream, getIndex(options), getFlags(options)); 84 } 85 }; 86 87 // now register with Registry to instantiate the above 88 // reader/writer. 89 REGISTER_OSGPLUGIN(freetype, ReaderWriterFreeType) 90