root/OpenSceneGraph/trunk/src/osgPlugins/freetype/ReaderWriterFreeType.cpp @ 9881

Revision 9881, 3.4 kB (checked in by robert, 5 years ago)

From David Callu, "Problem:

osgText::Text and osgText::Text3D use the same font file.
The first really load the file and obtain an osgText::Font object,
the second use the cache created during the first load of the
font file, and so obtain an osgText::Font object instead of
osgText::Font3D object. To obtain an osgText::Font3D object,
osgText::Text3D call osgDB::readObjectFile(...) with an option
to specify the plugin we want an osgText::Font3D instead of
osgText::Font.

Generalised Problem:

In osgDB::Registry, loaded file cache is referenced by the name
of this file, so if I load a file with some options, and the cache
already contain object for this filename, I obtain an object
potentially not loaded with my options.

Behaviours:

Cache management is delegate to osgDB::Registry, but cache
coherence (load a file with option then reuse it, deactivate the
cache when load a specific file or don't cached the loaded file)
is user's responsibility.

Text3D solution:

Postfix the font file name by .text3d or something similar and then have the freetype plugin return
osgText::Font3D when it detects this.
This operation is done by osgText::readFont3DFile() which unsure the filename have .text3d as extension.
This is totaly transparent for user, and backward compatible.

BTW, I fix the bug about the Normal of 3D text. Currently, the front and wall face have
the same normal (0,0,1) in the Text3D object coordinate. Now the wall face have its own
normal array computed by the plugin.

BTW 2, I implement
- void Text3D::accept(osg::Drawable::ConstAttributeFunctor?& af) const
- void Text3D::accept(osg::PrimitiveFunctor?& pf) const
so now statistics are well reported.
"

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osgDB/FileNameUtils>
2#include <osgDB/FileUtils>
3#include <osgDB/Registry>
4#include <osg/Notify>
5
6#include "FreeTypeLibrary.h"
7
8class 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// now register with Registry to instantiate the above
89// reader/writer.
90REGISTER_OSGPLUGIN(freetype, ReaderWriterFreeType)
Note: See TracBrowser for help on using the browser.