| 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: |
|---|
| 11 | ReaderWriterFreeType() |
|---|
| 12 | { |
|---|
| 13 | supportsExtension("ttf","true type font format"); |
|---|
| 14 | supportsExtension("ttc","true type 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 | |
|---|
| 23 | supportsOption("monochrome","Select monochrome font."); |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | virtual const char* className() const { return "FreeType Font Reader/Writer"; } |
|---|
| 27 | |
|---|
| 28 | static unsigned int getFlags(const osgDB::ReaderWriter::Options* options) |
|---|
| 29 | { |
|---|
| 30 | unsigned int flags = 0; |
|---|
| 31 | if (options && options->getOptionString().find("monochrome") != std::string::npos) |
|---|
| 32 | { |
|---|
| 33 | flags |= FT_LOAD_MONOCHROME; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | return flags; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 40 | { |
|---|
| 41 | std::string ext = osgDB::getLowerCaseFileExtension(file); |
|---|
| 42 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 43 | |
|---|
| 44 | std::string fileName = osgDB::findDataFile( file, options ); |
|---|
| 45 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 46 | |
|---|
| 47 | FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); |
|---|
| 48 | if (!freeTypeLibrary) |
|---|
| 49 | { |
|---|
| 50 | osg::notify(osg::WARN)<<"Warning:: cannot create freetype font after freetype library has been deleted."<<std::endl; |
|---|
| 51 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | if ( (options != NULL) && (options->getPluginData("3D")) ) |
|---|
| 55 | return freeTypeLibrary->getFont3D(fileName,0,getFlags(options)); |
|---|
| 56 | else |
|---|
| 57 | return freeTypeLibrary->getFont(fileName,0,getFlags(options)); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options* options) const |
|---|
| 61 | { |
|---|
| 62 | FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); |
|---|
| 63 | if (!freeTypeLibrary) |
|---|
| 64 | { |
|---|
| 65 | osg::notify(osg::WARN)<<"Warning:: cannot create freetype font after freetype library has been deleted."<<std::endl; |
|---|
| 66 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | return freeTypeLibrary->getFont(stream, 0, getFlags(options)); |
|---|
| 70 | } |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | REGISTER_OSGPLUGIN(freetype, ReaderWriterFreeType) |
|---|