| 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 | supportsExtension("text3d","use 3D Font instead of 2D Font"); |
|---|
| 23 | |
|---|
| 24 | supportsOption("monochrome","Select monochrome font."); |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | virtual const char* className() const { return "FreeType Font Reader/Writer"; } |
|---|
| 28 | |
|---|
| 29 | static unsigned int getFlags(const osgDB::ReaderWriter::Options* options) |
|---|
| 30 | { |
|---|
| 31 | unsigned int flags = 0; |
|---|
| 32 | if (options && options->getOptionString().find("monochrome") != std::string::npos) |
|---|
| 33 | { |
|---|
| 34 | flags |= FT_LOAD_MONOCHROME; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | return flags; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const |
|---|
| 41 | { |
|---|
| 42 | std::string tmpFile = file; |
|---|
| 43 | bool needFont3D = false; |
|---|
| 44 | |
|---|
| 45 | std::string ext = osgDB::getLowerCaseFileExtension(tmpFile); |
|---|
| 46 | if (ext == "text3d") |
|---|
| 47 | { |
|---|
| 48 | needFont3D = true; |
|---|
| 49 | tmpFile.erase(tmpFile.size()-7, 7); |
|---|
| 50 | ext = osgDB::getLowerCaseFileExtension(tmpFile); |
|---|
| 51 | } |
|---|
| 52 | else if ((options != NULL) && (options->getPluginData("3D"))) |
|---|
| 53 | { |
|---|
| 54 | needFont3D = true; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; |
|---|
| 58 | |
|---|
| 59 | std::string fileName = osgDB::findDataFile( tmpFile, options ); |
|---|
| 60 | if (fileName.empty()) return ReadResult::FILE_NOT_FOUND; |
|---|
| 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 | if (needFont3D) |
|---|
| 70 | return freeTypeLibrary->getFont3D(fileName,0,getFlags(options)); |
|---|
| 71 | else |
|---|
| 72 | return freeTypeLibrary->getFont(fileName,0,getFlags(options)); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | virtual ReadResult readObject(std::istream& stream, const osgDB::ReaderWriter::Options* options) const |
|---|
| 76 | { |
|---|
| 77 | FreeTypeLibrary* freeTypeLibrary = FreeTypeLibrary::instance(); |
|---|
| 78 | if (!freeTypeLibrary) |
|---|
| 79 | { |
|---|
| 80 | osg::notify(osg::WARN)<<"Warning:: cannot create freetype font after freetype library has been deleted."<<std::endl; |
|---|
| 81 | return ReadResult::ERROR_IN_READING_FILE; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | return freeTypeLibrary->getFont(stream, 0, getFlags(options)); |
|---|
| 85 | } |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | REGISTER_OSGPLUGIN(freetype, ReaderWriterFreeType) |
|---|