Glyph 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 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_GLYPH
  14. #define OSGTEXT_GLYPH 1
  15. #include <string>
  16. #include <istream>
  17. #include <osg/Vec2>
  18. #include <osg/Image>
  19. #include <osg/Texture2D>
  20. #include <osg/StateSet>
  21. #include <osg/Geometry>
  22. #include <osg/Geode>
  23. #include <osgText/Export>
  24. #include <osgText/KerningType>
  25. #include <osgText/Style>
  26. #include <OpenThreads/ReentrantMutex>
  27. namespace osgText {
  28. class Font;
  29. class Text;
  30. class Glyph3D;
  31. class GlyphGeometry;
  32. class GlyphTexture;
  33. enum ShaderTechnique
  34. {
  35. NO_TEXT_SHADER = 0x0,
  36. GREYSCALE = 0x1,
  37. SIGNED_DISTANCE_FIELD = 0x2,
  38. ALL_FEATURES = GREYSCALE | SIGNED_DISTANCE_FIELD
  39. };
  40. class OSGTEXT_EXPORT Glyph : public osg::Image
  41. {
  42. public:
  43. Glyph(Font* font, unsigned int glyphCode);
  44. Font* getFont() { return _font; }
  45. const Font* getFont() const { return _font; }
  46. unsigned int getGlyphCode() const { return _glyphCode; }
  47. void setFontResolution(const FontResolution& fontRes) { _fontResolution = fontRes; }
  48. const FontResolution& getFontResolution() const { return _fontResolution; }
  49. void setWidth(float width) { _width = width; }
  50. float getWidth() const { return _width; }
  51. void setHeight(float height) { _height = height; }
  52. float getHeight() const { return _height; }
  53. void setHorizontalBearing(const osg::Vec2& bearing);
  54. const osg::Vec2& getHorizontalBearing() const;
  55. void setHorizontalAdvance(float advance);
  56. float getHorizontalAdvance() const;
  57. void setVerticalBearing(const osg::Vec2& bearing);
  58. const osg::Vec2& getVerticalBearing() const;
  59. void setVerticalAdvance(float advance);
  60. float getVerticalAdvance() const;
  61. struct TextureInfo : public osg::Referenced
  62. {
  63. TextureInfo():
  64. texture(0),
  65. texelMargin(0.0f) {}
  66. TextureInfo(GlyphTexture* tex, int x, int y, const osg::Vec2& mintc, const osg::Vec2& maxtc, float margin):
  67. texture(tex),
  68. texturePositionX(x),
  69. texturePositionY(y),
  70. minTexCoord(mintc),
  71. maxTexCoord(maxtc),
  72. texelMargin(margin) {}
  73. GlyphTexture* texture;
  74. int texturePositionX;
  75. int texturePositionY;
  76. osg::Vec2 minTexCoord;
  77. osg::Vec2 maxTexCoord;
  78. float texelMargin;
  79. };
  80. void setTextureInfo(ShaderTechnique technique, TextureInfo* info);
  81. const TextureInfo* getTextureInfo(ShaderTechnique technique) const;
  82. TextureInfo* getOrCreateTextureInfo(ShaderTechnique technique);
  83. protected:
  84. virtual ~Glyph();
  85. Font* _font;
  86. unsigned int _glyphCode;
  87. FontResolution _fontResolution;
  88. float _width;
  89. float _height;
  90. osg::Vec2 _horizontalBearing;
  91. float _horizontalAdvance;
  92. osg::Vec2 _verticalBearing;
  93. float _verticalAdvance;
  94. typedef std::vector< osg::ref_ptr<TextureInfo> > TextureInfoList;
  95. TextureInfoList _textureInfoList;
  96. mutable OpenThreads::ReentrantMutex _textureInfoListMutex;
  97. };
  98. class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
  99. {
  100. public:
  101. GlyphGeometry();
  102. void setup(const Glyph3D* glyph, const Style* style);
  103. bool match(const Style* style) const;
  104. osg::Geode* getGeode() const { return _geode.get(); }
  105. osg::Geometry* getGeometry() const { return _geometry.get(); }
  106. /** Set the VertexArray of the glyph. */
  107. void setVertexArray(osg::Vec3Array * va) { _vertices = va; }
  108. /** Get the VertexArray of the glyph. */
  109. osg::Vec3Array * getVertexArray() const { return _vertices.get(); }
  110. /** Set the VertexArray of the glyph. */
  111. void setNormalArray(osg::Vec3Array* na) { _normals = na; }
  112. /** Get the NormalArray for the wall face. */
  113. osg::Vec3Array* getNormalArray() const { return _normals.get(); }
  114. /** Get the PrimitiveSetList for the front face. */
  115. osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
  116. /** Get the PrimitiveSetList for the wall face. */
  117. osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
  118. /** Get et the PrimitiveSetList for the back face. */
  119. osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; }
  120. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  121. virtual void setThreadSafeRefUnref(bool threadSafe);
  122. protected:
  123. osg::ref_ptr<Style> _style;
  124. osg::ref_ptr<osg::Geode> _geode;
  125. osg::ref_ptr<osg::Geometry> _geometry;
  126. osg::ref_ptr<osg::Vec3Array> _vertices;
  127. osg::ref_ptr<osg::Vec3Array> _normals;
  128. osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
  129. osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
  130. osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
  131. };
  132. class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
  133. {
  134. public:
  135. Glyph3D(Font* font, unsigned int glyphCode);
  136. Font* getFont() { return _font; }
  137. const Font* getFont() const { return _font; }
  138. unsigned int getGlyphCode() const { return _glyphCode; }
  139. void setWidth(float width) { _width = width; }
  140. float getWidth() const { return _width; }
  141. void setHeight(float height) { _height = height; }
  142. float getHeight() const { return _height; }
  143. void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
  144. const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; }
  145. void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
  146. float getHorizontalAdvance() const { return _horizontalAdvance; }
  147. void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
  148. const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }
  149. void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
  150. float getVerticalAdvance() const { return _verticalAdvance; }
  151. void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
  152. const osg::BoundingBox & getBoundingBox() const { return _bb; }
  153. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  154. virtual void setThreadSafeRefUnref(bool threadSafe);
  155. void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
  156. osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
  157. const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); }
  158. /** Get the PrimitiveSetList for the raw face which hasn't been tessellated. */
  159. osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; }
  160. const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const { return _rawFacePrimitiveSetList; }
  161. GlyphGeometry* getGlyphGeometry(const Style* style);
  162. protected:
  163. virtual ~Glyph3D() {}
  164. Font* _font;
  165. unsigned int _glyphCode;
  166. float _width;
  167. float _height;
  168. osg::Vec2 _horizontalBearing;
  169. float _horizontalAdvance;
  170. osg::Vec2 _verticalBearing;
  171. float _verticalAdvance;
  172. osg::BoundingBox _bb;
  173. // osg::Vec2 _advance;
  174. osg::ref_ptr<osg::Vec3Array> _rawVertexArray;
  175. osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;
  176. typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries;
  177. GlyphGeometries _glyphGeometries;
  178. };
  179. class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
  180. {
  181. public:
  182. GlyphTexture();
  183. const char* className() const { return "GlyphTexture"; }
  184. /** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
  185. virtual int compare(const osg::StateAttribute& rhs) const;
  186. void setShaderTechnique(ShaderTechnique technique) { _shaderTechnique = technique; }
  187. ShaderTechnique getShaderTechnique() const { return _shaderTechnique; }
  188. int getEffectMargin(const Glyph* glyph);
  189. int getTexelMargin(const Glyph* glyph);
  190. bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
  191. void addGlyph(Glyph* glyph,int posX, int posY);
  192. /** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
  193. virtual void setThreadSafeRefUnref(bool threadSafe);
  194. /** Resize any per context GLObject buffers to specified size. */
  195. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  196. /** create an image that maps all the associated Glyph's onto a single image, that is equivalent to what will be downloaded to the texture.*/
  197. osg::Image* createImage();
  198. protected:
  199. virtual ~GlyphTexture();
  200. void copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info);
  201. ShaderTechnique _shaderTechnique;
  202. int _usedY;
  203. int _partUsedX;
  204. int _partUsedY;
  205. typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
  206. typedef std::vector< const Glyph* > GlyphPtrList;
  207. typedef osg::buffered_object< GlyphPtrList > GlyphBuffer;
  208. GlyphRefList _glyphs;
  209. mutable GlyphBuffer _glyphsToSubload;
  210. mutable OpenThreads::Mutex _mutex;
  211. };
  212. }
  213. #endif