Font 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGTEXT_FONT
  14. #define OSGTEXT_FONT 1
  15. #include <string>
  16. #include <istream>
  17. #include <osg/TexEnv>
  18. #include <osgText/Glyph>
  19. #include <osgDB/Options>
  20. #include <OpenThreads/Mutex>
  21. namespace osgText {
  22. // forward declare Font
  23. class Font;
  24. class TextBase;
  25. #ifdef OSG_PROVIDE_READFILE
  26. /** Read a font from specified file. The filename may contain a path.
  27. * It will search for the font file in the following places in this order:
  28. * - In the current directory
  29. * - All paths defined in OSG_FILE_PATH or OSGFILEPATH environment variable
  30. * - Filename with path stripped: In the current directory
  31. * - Filename with path stripped: All paths defined in OSG_FILE_PATH or OSGFILEPATH
  32. *
  33. * Then the file will be searched in OS specific directories in the following order:
  34. * - Again in the current directory
  35. * - Windows: In C:/winnt/fonts
  36. * - Windows: In C:/windows/fonts
  37. * - Windows: In the fonts directory of the windows install directory
  38. * - Other OS: In /usr/share/fonts/ttf
  39. * - Other OS: In /usr/share/fonts/ttf/western
  40. * - Other OS: In /usr/share/fonts/ttf/decoratives
  41. *
  42. * If the given file could not be found, the path part will be stripped and
  43. * the file will be searched again in the OS specific directories.
  44. */
  45. extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
  46. /** read a font from specified stream.*/
  47. extern OSGTEXT_EXPORT Font* readFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
  48. #endif
  49. extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
  50. extern OSGTEXT_EXPORT osg::ref_ptr<Font> readRefFontStream(std::istream& stream, const osgDB::Options* userOptions = 0);
  51. extern OSGTEXT_EXPORT std::string findFontFile(const std::string& str);
  52. /** Pure virtual base class for fonts.
  53. * Concrete implementation are the DefaultFont found in src/osgText/DefaultFont.cpp
  54. * and FreeTypeFont found in src/osgPlugins/freetype/FreeTypeFont.cpp*/
  55. class OSGTEXT_EXPORT Font : public osg::Object
  56. {
  57. // declare the interface to a font.
  58. public:
  59. // forward declare nested classes.
  60. class FontImplementation;
  61. public:
  62. Font(FontImplementation* implementation=0);
  63. virtual osg::Object* cloneType() const { return 0; } // cloneType() not appropriate
  64. virtual osg::Object* clone(const osg::CopyOp&) const { return 0; } // clone() not appropriate
  65. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Font*>(obj)!=NULL; }
  66. virtual const char* className() const { return "Font"; }
  67. virtual const char* libraryName() const { return "osgText"; }
  68. virtual std::string getFileName() const;
  69. static osg::ref_ptr<Font> getDefaultFont();
  70. typedef std::vector< osg::ref_ptr<osg::StateSet> > StateSets;
  71. StateSets& getCachedStateSets() { return _statesets; }
  72. const StateSets& getCachedStateSets() const { return _statesets; }
  73. /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes and a font resolution.*/
  74. virtual osg::Vec2 getKerning(const FontResolution& fontRes, unsigned int leftcharcode, unsigned int rightcharcode, KerningType kerningType);
  75. /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
  76. virtual Glyph* getGlyph(const FontResolution& fontSize, unsigned int charcode);
  77. /** Get a Glyph3D for specified charcode and a font size.*/
  78. virtual Glyph3D* getGlyph3D(const FontResolution& fontSize, unsigned int charcode);
  79. /** Return true if this font provides vertical alignments and spacing or glyphs.*/
  80. virtual bool hasVertical() const;
  81. /** Get the ascender and descender sizes of the font where supported by the FontImplementation,
  82. * return true on success, return false when not supported.*/
  83. virtual bool getVerticalSize(float& ascender, float& descender) const { return _implementation ? _implementation->getVerticalSize(ascender, descender) : false; }
  84. /** Set the size of texture to create to store the glyph images when rendering.
  85. * Note, this doesn't affect already created Texture Glhph's.*/
  86. void setTextureSizeHint(unsigned int width,unsigned int height);
  87. unsigned int getTextureWidthHint() const;
  88. unsigned int getTextureHeightHint() const;
  89. /** Set the minification texture filter to use when creating the texture to store the glyph images when rendering.
  90. * Note, this doesn't affect already created Texture Glhph's.*/
  91. void setMinFilterHint(osg::Texture::FilterMode mode);
  92. osg::Texture::FilterMode getMinFilterHint() const;
  93. /** Set the magnification texture filter to use when creating the texture to store the glyph images when rendering.
  94. * Note, this doesn't affect already created Texture Glhph's.*/
  95. void setMagFilterHint(osg::Texture::FilterMode mode);
  96. osg::Texture::FilterMode getMagFilterHint() const;
  97. void setMaxAnisotropy(float anis) { _maxAnisotropy = anis; }
  98. float getMaxAnisotropy() const { return _maxAnisotropy; }
  99. unsigned int getFontDepth() const { return _depth; }
  100. void setNumberCurveSamples(unsigned int numSamples) { _numCurveSamples = numSamples; }
  101. unsigned int getNumberCurveSamples() const { return _numCurveSamples; }
  102. // make Text a friend to allow it add and remove its entry in the Font's _textList.
  103. friend class FontImplementation;
  104. void setImplementation(FontImplementation* implementation);
  105. FontImplementation* getImplementation();
  106. const FontImplementation* getImplementation() const;
  107. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  108. virtual void setThreadSafeRefUnref(bool threadSafe);
  109. /** Resize any per context GLObject buffers to specified size. */
  110. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  111. /** If State is non-zero, this function releases OpenGL objects for
  112. * the specified graphics context. Otherwise, releases OpenGL objexts
  113. * for all graphics contexts. */
  114. virtual void releaseGLObjects(osg::State* state=0) const;
  115. typedef OpenThreads::Mutex FontMutex;
  116. typedef std::vector< osg::ref_ptr<GlyphTexture> > GlyphTextureList;
  117. GlyphTextureList& getGlyphTextureList() { return _glyphTextureList; }
  118. void assignGlyphToGlyphTexture(Glyph* glyph, ShaderTechnique shaderTechnique);
  119. protected:
  120. virtual ~Font();
  121. void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph);
  122. typedef std::map< unsigned int, osg::ref_ptr<Glyph> > GlyphMap;
  123. typedef std::map< unsigned int, osg::ref_ptr<Glyph3D> > Glyph3DMap;
  124. typedef std::map< FontResolution, GlyphMap > FontSizeGlyphMap;
  125. typedef std::map< FontResolution, Glyph3DMap > FontSizeGlyph3DMap;
  126. mutable OpenThreads::Mutex _glyphMapMutex;
  127. StateSets _statesets;
  128. FontSizeGlyphMap _sizeGlyphMap;
  129. GlyphTextureList _glyphTextureList;
  130. FontSizeGlyph3DMap _sizeGlyph3DMap;
  131. // current active size of font
  132. FontResolution _fontSize;
  133. unsigned int _textureWidthHint;
  134. unsigned int _textureHeightHint;
  135. osg::Texture::FilterMode _minFilterHint;
  136. osg::Texture::FilterMode _magFilterHint;
  137. float _maxAnisotropy;
  138. unsigned int _depth;
  139. unsigned int _numCurveSamples;
  140. osg::ref_ptr<FontImplementation> _implementation;
  141. // declare the nested classes.
  142. public:
  143. class FontImplementation : public osg::Referenced
  144. {
  145. public:
  146. FontImplementation():
  147. osg::Referenced(true),
  148. _facade(0) {}
  149. virtual std::string getFileName() const = 0;
  150. virtual bool supportsMultipleFontResolutions() const = 0;
  151. /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/
  152. virtual Glyph* getGlyph(const FontResolution& fontRes, unsigned int charcode) = 0;
  153. /** Get a Glyph3D for specified charcode.*/
  154. virtual Glyph3D* getGlyph3D(const FontResolution& fontRes, unsigned int charcode) = 0;
  155. /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/
  156. virtual osg::Vec2 getKerning(const FontResolution& fontRes, unsigned int leftcharcode, unsigned int rightcharcode, KerningType kerningType) = 0;
  157. /** Return true if this font provides vertical alignments and spacing or glyphs.*/
  158. virtual bool hasVertical() const = 0;
  159. void addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph* glyph)
  160. {
  161. _facade->addGlyph(fontRes, charcode, glyph);
  162. }
  163. Font* _facade;
  164. virtual bool getVerticalSize(float & /*ascender*/, float & /*descender*/) const { return false; }
  165. };
  166. };
  167. }
  168. #endif