TextBase 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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_TEXTBASE
  14. #define OSGTEXT_TEXTBASE 1
  15. #include <osg/Drawable>
  16. #include <osgText/String>
  17. #include <osgText/KerningType>
  18. #include <osgText/Font>
  19. namespace osgText {
  20. #define NEW_APPROACH
  21. class OSGTEXT_EXPORT TextBase : public osg::Drawable
  22. {
  23. public:
  24. TextBase();
  25. TextBase(const TextBase& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
  26. //virtual osg::Object* cloneType() const { return new Text(); }
  27. //virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Text(*this,copyop); }
  28. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TextBase*>(obj)!=NULL; }
  29. virtual const char* className() const { return "TextBase"; }
  30. virtual const char* libraryName() const { return "osgText"; }
  31. void setColor(const osg::Vec4& color);
  32. const osg::Vec4& getColor() const { return _color; }
  33. /** Set the Font to use to render the text.
  34. * setFont(0) sets the use of the default font.*/
  35. virtual void setFont(Font* font=0) { setFont(osg::ref_ptr<Font>(font)); };
  36. /** Set the Font to use to render the text.*/
  37. virtual void setFont(osg::ref_ptr<Font> font);
  38. /** Set the font, loaded from the specified front file, to use to render the text,
  39. * setFont("") sets the use of the default font.
  40. * See the osgText::readFontFile function for how the font file will be located. */
  41. virtual void setFont(const std::string& fontfile);
  42. /** Get the font. Return 0 if default is being used.*/
  43. Font* getFont() { return _font.get(); }
  44. /** Get the const font. Return 0 if default is being used.*/
  45. const Font* getFont() const { return _font.get(); }
  46. /** Set the text style.*/
  47. void setStyle(Style* style) { _style = style; }
  48. /** Get the text style.*/
  49. Style* getStyle() { return _style.get(); }
  50. /** Get the const text style.*/
  51. const Style* getStyle() const { return _style.get(); }
  52. /** Get or create the text style.*/
  53. Style* getOrCreateStyle() { if (!_style) _style = new Style; return _style.get(); }
  54. /** Set the Font reference width and height resolution in texels.
  55. * Note, the size may not be supported by current font,
  56. * the closest supported font size will be selected.*/
  57. void setFontResolution(unsigned int width, unsigned int height);
  58. unsigned int getFontWidth() const { return _fontSize.first; }
  59. unsigned int getFontHeight() const { return _fontSize.second; }
  60. /** Set the text using a osgText::String.*/
  61. void setText(const String& text);
  62. /** Set the text using a std::string,
  63. * which is converted to an internal TextString.*/
  64. void setText(const std::string& text);
  65. /** Set the text using a Unicode encoded std::string, which is converted to an internal TextString.
  66. * The encoding parameter specificies which Unicode encodeding is used in the std::string. */
  67. void setText(const std::string& text,String::Encoding encoding);
  68. /** Set the text using a wchar_t string,
  69. * which is converted to an internal TextString.*/
  70. void setText(const wchar_t* text);
  71. /** Get the text string.
  72. * Note, if you modify the string you must call Text::update() for
  73. * the internal glyph reprentation to be updated.*/
  74. String& getText() { return _text; }
  75. /** Get the const text string.*/
  76. const String& getText() const { return _text; }
  77. /** update internal glyph respresentation used for rendering,
  78. * and bounding volume.*/
  79. void update() { computeGlyphRepresentation(); }
  80. /** Set the rendered character size in object coordinates.*/
  81. void setCharacterSize(float height);
  82. /** Set the rendered character size in object coordinates.*/
  83. void setCharacterSize(float height, float aspectRatio);
  84. float getCharacterHeight() const { return _characterHeight; }
  85. float getCharacterAspectRatio() const { return _style.valid()? _style->getWidthRatio() : 1.0f; }
  86. enum CharacterSizeMode
  87. {
  88. OBJECT_COORDS, /// default
  89. SCREEN_COORDS, /// internally scale the characters to be constant screen size.
  90. OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT /// text that behavaves like OBJECT_COORDS sized text when a long distance way, but has its screen sized capped automatically when the viewer gets near.
  91. };
  92. /** Set how the CharacterSize value relates to the final rendered character.*/
  93. void setCharacterSizeMode(CharacterSizeMode mode) { _characterSizeMode = mode; }
  94. /** Get the CharacterSizeMode.*/
  95. CharacterSizeMode getCharacterSizeMode() const { return _characterSizeMode; }
  96. /** Set the maximum width of the text box.
  97. * With horizontal layouts any characters which do not fit are wrapped around.
  98. * 0 or negative values indicate that no maximum width is set, lines can be as long as
  99. * they need be to fit thre required text*/
  100. void setMaximumWidth(float maximumWidth);
  101. /** Get the maximim width of the text box.*/
  102. float getMaximumWidth() const { return _maximumWidth; }
  103. /** Set the maximum height of the text box.
  104. * With horizontal layouts any characters which do not fit are wrapped around.
  105. * 0 or negative values indicate that no maximum height is set, lines can be as long as
  106. * they need be to fit the required text*/
  107. void setMaximumHeight(float maximumHeight);
  108. /** Get the maximum height of the text box.*/
  109. float getMaximumHeight() const { return _maximumHeight; }
  110. /** Set the line spacing of the text box, given as a percentage of
  111. * the character height. The default value is 0 for backward
  112. * compatibility. For longer paragraphs of text, a value of at
  113. * least 25% (i.e. set line spacing to 0.25) is recommended. */
  114. void setLineSpacing(float lineSpacing);
  115. /** Get the line spacing of the text box. */
  116. float getLineSpacing() const { return _lineSpacing; }
  117. /** Set the position of text.*/
  118. void setPosition(const osg::Vec3& pos);
  119. /** Get the position of text.*/
  120. const osg::Vec3& getPosition() const { return _position; }
  121. enum AlignmentType
  122. {
  123. LEFT_TOP,
  124. LEFT_CENTER,
  125. LEFT_BOTTOM,
  126. CENTER_TOP,
  127. CENTER_CENTER,
  128. CENTER_BOTTOM,
  129. RIGHT_TOP,
  130. RIGHT_CENTER,
  131. RIGHT_BOTTOM,
  132. LEFT_BASE_LINE,
  133. CENTER_BASE_LINE,
  134. RIGHT_BASE_LINE,
  135. LEFT_BOTTOM_BASE_LINE,
  136. CENTER_BOTTOM_BASE_LINE,
  137. RIGHT_BOTTOM_BASE_LINE,
  138. BASE_LINE = LEFT_BASE_LINE /// default.
  139. };
  140. void setAlignment(AlignmentType alignment);
  141. AlignmentType getAlignment() const { return _alignment; }
  142. enum AxisAlignment
  143. {
  144. XY_PLANE,
  145. REVERSED_XY_PLANE,
  146. XZ_PLANE,
  147. REVERSED_XZ_PLANE,
  148. YZ_PLANE,
  149. REVERSED_YZ_PLANE,
  150. SCREEN,
  151. USER_DEFINED_ROTATION
  152. };
  153. void setAxisAlignment(AxisAlignment axis);
  154. AxisAlignment getAxisAlignment() const { return _axisAlignment; }
  155. void setRotation(const osg::Quat& quat);
  156. const osg::Quat& getRotation() const { return _rotation; }
  157. void setAutoRotateToScreen(bool autoRotateToScreen);
  158. bool getAutoRotateToScreen() const { return _autoRotateToScreen; }
  159. enum Layout
  160. {
  161. LEFT_TO_RIGHT, /// default
  162. RIGHT_TO_LEFT,
  163. VERTICAL
  164. };
  165. void setLayout(Layout layout);
  166. Layout getLayout() const { return _layout; }
  167. enum DrawModeMask
  168. {
  169. TEXT = 1, /// default
  170. BOUNDINGBOX = 2,
  171. FILLEDBOUNDINGBOX = 4,
  172. ALIGNMENT = 8
  173. };
  174. void setDrawMode(unsigned int mode);
  175. unsigned int getDrawMode() const { return _drawMode; }
  176. void setBoundingBoxMargin(float margin);
  177. float getBoundingBoxMargin() const { return _textBBMargin; }
  178. void setBoundingBoxColor(const osg::Vec4& color){ _textBBColor = color; }
  179. const osg::Vec4& getBoundingBoxColor() const { return _textBBColor; }
  180. void setKerningType(KerningType kerningType) { _kerningType = kerningType; }
  181. KerningType getKerningType() const { return _kerningType; }
  182. /** Get the number of wrapped lines - only valid after computeGlyphRepresentation() has been called, returns 0 otherwise */
  183. unsigned int getLineCount() const { return _lineCount; }
  184. /** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
  185. * @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
  186. */
  187. virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
  188. /** Resize any per context GLObject buffers to specified size. */
  189. virtual void resizeGLObjectBuffers(unsigned int maxSize);
  190. /** If State is non-zero, this function releases OpenGL objects for
  191. * the specified graphics context. Otherwise, releases OpenGL objexts
  192. * for all graphics contexts. */
  193. virtual void releaseGLObjects(osg::State* state=0) const;
  194. virtual osg::BoundingBox computeBoundingBox() const;
  195. typedef osg::ref_ptr<osg::Vec3Array> Coords;
  196. Coords& getCoords() { return _coords; }
  197. const Coords& getCoords() const { return _coords; }
  198. void getCoord(unsigned int i, osg::Vec2& c) const { c.set((*_coords)[i].x(), (*_coords)[i].y()); }
  199. void getCoord(unsigned int i, osg::Vec3& c) const { c = (*_coords)[i]; }
  200. /** Get the cached internal matrix used to provide positioning of text. The cached matrix is originally computed by computeMatrix(..). */
  201. const osg::Matrix& getMatrix() const { return _matrix; }
  202. /** compute the matrix that positions the text in model space for the given viewpoint.*/
  203. bool computeMatrix(osg::Matrix& matrix, osg::State* state=0) const;
  204. protected:
  205. virtual ~TextBase();
  206. virtual osg::StateSet* createStateSet();
  207. virtual void assignStateSet();
  208. void initArraysAndBuffers();
  209. osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo) const;
  210. void positionCursor(const osg::Vec2 & endOfLine_coords, osg::Vec2 & cursor, unsigned int linelength);
  211. String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last);
  212. void computePositions();
  213. virtual void computePositionsImplementation();
  214. virtual void computeGlyphRepresentation() = 0;
  215. typedef osg::ref_ptr<osg::Vec2Array> TexCoords;
  216. typedef osg::ref_ptr<osg::Vec4Array> ColorCoords;
  217. typedef std::vector< osg::ref_ptr<osg::DrawElements> > Primitives;
  218. // members which have public access.
  219. osg::Vec4 _color;
  220. osg::ref_ptr<Font> _font;
  221. osg::ref_ptr<Font> _fontFallback;
  222. osg::ref_ptr<Style> _style;
  223. FontResolution _fontSize;
  224. float _characterHeight;
  225. CharacterSizeMode _characterSizeMode;
  226. float _maximumWidth;
  227. float _maximumHeight;
  228. float _lineSpacing;
  229. String _text;
  230. osg::Vec3 _position;
  231. AlignmentType _alignment;
  232. AxisAlignment _axisAlignment;
  233. osg::Quat _rotation;
  234. bool _autoRotateToScreen;
  235. Layout _layout;
  236. unsigned int _drawMode;
  237. float _textBBMargin;
  238. osg::Vec4 _textBBColor;
  239. KerningType _kerningType;
  240. unsigned int _lineCount;
  241. bool _glyphNormalized;
  242. osg::Vec3 _offset;
  243. osg::Vec3 _normal;
  244. osg::BoundingBox _textBB;
  245. osg::BoundingBox _textBBWithMargin;
  246. mutable osg::Matrix _matrix;
  247. Primitives _decorationPrimitives;
  248. void setupDecoration();
  249. osg::ref_ptr<osg::VertexBufferObject> _vbo;
  250. osg::ref_ptr<osg::ElementBufferObject> _ebo;
  251. Coords _coords;
  252. Coords _normals;
  253. ColorCoords _colorCoords;
  254. TexCoords _texcoords;
  255. unsigned int addCoord(const osg::Vec2& c) { unsigned int s = _coords->size(); _coords->push_back(osg::Vec3(c.x(), c.y(), 0.0f)); _coords->dirty(); return s; }
  256. unsigned int addCoord(const osg::Vec3& c) { unsigned int s = _coords->size(); _coords->push_back(c); _coords->dirty(); return s; }
  257. void addTexCoord(const osg::Vec2& tc) { _texcoords->push_back(tc); _texcoords->dirty(); }
  258. };
  259. }
  260. #endif