2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2007-12-10 23:15:56 +08:00
*
2012-03-22 01:36:20 +08:00
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
2007-12-10 23:15:56 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2007-12-10 23:15:56 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-22 01:36:20 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2007-12-10 23:15:56 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGTEXT_TEXTBASE
#define OSGTEXT_TEXTBASE 1
#include <osg/Drawable>
#include <osgText/String>
#include <osgText/KerningType>
2010-09-29 19:34:55 +08:00
#include <osgText/Font>
2007-12-10 23:15:56 +08:00
namespace osgText {
2017-03-10 01:01:33 +08:00
#define NEW_APPROACH
2017-03-02 19:12:55 +08:00
2007-12-10 23:15:56 +08:00
class OSGTEXT_EXPORT TextBase : public osg::Drawable
{
public:
TextBase();
TextBase(const TextBase& text,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
//virtual osg::Object* cloneType() const { return new Text(); }
//virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new Text(*this,copyop); }
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const TextBase*>(obj)!=NULL; }
virtual const char* className() const { return "TextBase"; }
virtual const char* libraryName() const { return "osgText"; }
2010-11-19 01:59:55 +08:00
void setColor(const osg::Vec4& color);
const osg::Vec4& getColor() const { return _color; }
2010-09-29 20:45:35 +08:00
2010-09-29 19:34:55 +08:00
/** Set the Font to use to render the text.
* setFont(0) sets the use of the default font.*/
2011-01-11 19:39:31 +08:00
virtual void setFont(Font* font=0) { setFont(osg::ref_ptr<Font>(font)); };
2010-09-29 19:34:55 +08:00
/** Set the Font to use to render the text.*/
2011-01-11 19:39:31 +08:00
virtual void setFont(osg::ref_ptr<Font> font);
2010-09-29 19:34:55 +08:00
/** Set the font, loaded from the specified front file, to use to render the text,
* setFont("") sets the use of the default font.
* See the osgText::readFontFile function for how the font file will be located. */
2011-01-11 19:39:31 +08:00
virtual void setFont(const std::string& fontfile);
2010-09-29 19:34:55 +08:00
/** Get the font. Return 0 if default is being used.*/
2017-09-21 22:41:21 +08:00
Font* getFont() { return _font.get(); }
/** Get the const font. Return 0 if default is being used.*/
2010-09-29 19:34:55 +08:00
const Font* getFont() const { return _font.get(); }
2010-09-29 20:45:35 +08:00
/** Set the text style.*/
void setStyle(Style* style) { _style = style; }
/** Get the text style.*/
Style* getStyle() { return _style.get(); }
/** Get the const text style.*/
const Style* getStyle() const { return _style.get(); }
/** Get or create the text style.*/
Style* getOrCreateStyle() { if (!_style) _style = new Style; return _style.get(); }
2007-12-10 23:15:56 +08:00
/** Set the Font reference width and height resolution in texels.
2012-03-22 01:36:20 +08:00
* Note, the size may not be supported by current font,
2007-12-10 23:15:56 +08:00
* the closest supported font size will be selected.*/
void setFontResolution(unsigned int width, unsigned int height);
2007-12-23 21:45:37 +08:00
unsigned int getFontWidth() const { return _fontSize.first; }
unsigned int getFontHeight() const { return _fontSize.second; }
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
/** Set the text using a osgText::String.*/
void setText(const String& text);
2012-03-22 01:36:20 +08:00
/** Set the text using a std::string,
2007-12-10 23:15:56 +08:00
* which is converted to an internal TextString.*/
void setText(const std::string& text);
/** Set the text using a Unicode encoded std::string, which is converted to an internal TextString.
* The encoding parameter specificies which Unicode encodeding is used in the std::string. */
void setText(const std::string& text,String::Encoding encoding);
2012-03-22 01:36:20 +08:00
/** Set the text using a wchar_t string,
2007-12-10 23:15:56 +08:00
* which is converted to an internal TextString.*/
void setText(const wchar_t* text);
2012-03-22 01:36:20 +08:00
/** Get the text string.
2007-12-10 23:15:56 +08:00
* Note, if you modify the string you must call Text::update() for
* the internal glyph reprentation to be updated.*/
String& getText() { return _text; }
/** Get the const text string.*/
const String& getText() const { return _text; }
2012-03-22 01:36:20 +08:00
/** update internal glyph respresentation used for rendering,
2007-12-10 23:15:56 +08:00
* and bounding volume.*/
void update() { computeGlyphRepresentation(); }
/** Set the rendered character size in object coordinates.*/
2010-09-29 20:45:35 +08:00
void setCharacterSize(float height);
/** Set the rendered character size in object coordinates.*/
void setCharacterSize(float height, float aspectRatio);
2007-12-10 23:15:56 +08:00
float getCharacterHeight() const { return _characterHeight; }
2010-09-29 20:45:35 +08:00
float getCharacterAspectRatio() const { return _style.valid()? _style->getWidthRatio() : 1.0f; }
2007-12-10 23:15:56 +08:00
enum CharacterSizeMode
{
OBJECT_COORDS, /// default
SCREEN_COORDS, /// internally scale the characters to be constant screen size.
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.
};
/** Set how the CharacterSize value relates to the final rendered character.*/
void setCharacterSizeMode(CharacterSizeMode mode) { _characterSizeMode = mode; }
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
/** Get the CharacterSizeMode.*/
CharacterSizeMode getCharacterSizeMode() const { return _characterSizeMode; }
/** Set the maximum width of the text box.
* With horizontal layouts any characters which do not fit are wrapped around.
2012-03-22 01:36:20 +08:00
* 0 or negative values indicate that no maximum width is set, lines can be as long as
2007-12-10 23:15:56 +08:00
* they need be to fit thre required text*/
void setMaximumWidth(float maximumWidth);
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
/** Get the maximim width of the text box.*/
float getMaximumWidth() const { return _maximumWidth; }
/** Set the maximum height of the text box.
* With horizontal layouts any characters which do not fit are wrapped around.
2012-03-22 01:36:20 +08:00
* 0 or negative values indicate that no maximum height is set, lines can be as long as
2007-12-10 23:15:56 +08:00
* they need be to fit the required text*/
void setMaximumHeight(float maximumHeight);
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
/** Get the maximum height of the text box.*/
float getMaximumHeight() const { return _maximumHeight; }
/** Set the line spacing of the text box, given as a percentage of
2012-03-22 01:36:20 +08:00
* the character height. The default value is 0 for backward
* compatibility. For longer paragraphs of text, a value of at
2007-12-10 23:15:56 +08:00
* least 25% (i.e. set line spacing to 0.25) is recommended. */
void setLineSpacing(float lineSpacing);
/** Get the line spacing of the text box. */
float getLineSpacing() const { return _lineSpacing; }
/** Set the position of text.*/
void setPosition(const osg::Vec3& pos);
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
/** Get the position of text.*/
const osg::Vec3& getPosition() const { return _position; }
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
enum AlignmentType
{
LEFT_TOP,
LEFT_CENTER,
LEFT_BOTTOM,
CENTER_TOP,
CENTER_CENTER,
CENTER_BOTTOM,
RIGHT_TOP,
RIGHT_CENTER,
RIGHT_BOTTOM,
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
LEFT_BASE_LINE,
CENTER_BASE_LINE,
RIGHT_BASE_LINE,
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
LEFT_BOTTOM_BASE_LINE,
CENTER_BOTTOM_BASE_LINE,
RIGHT_BOTTOM_BASE_LINE,
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
BASE_LINE = LEFT_BASE_LINE /// default.
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
};
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
void setAlignment(AlignmentType alignment);
AlignmentType getAlignment() const { return _alignment; }
enum AxisAlignment
{
XY_PLANE,
REVERSED_XY_PLANE,
XZ_PLANE,
REVERSED_XZ_PLANE,
YZ_PLANE,
REVERSED_YZ_PLANE,
SCREEN,
USER_DEFINED_ROTATION
};
void setAxisAlignment(AxisAlignment axis);
AxisAlignment getAxisAlignment() const { return _axisAlignment; }
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
void setRotation(const osg::Quat& quat);
const osg::Quat& getRotation() const { return _rotation; }
void setAutoRotateToScreen(bool autoRotateToScreen);
bool getAutoRotateToScreen() const { return _autoRotateToScreen; }
enum Layout
{
LEFT_TO_RIGHT, /// default
RIGHT_TO_LEFT,
VERTICAL
};
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
void setLayout(Layout layout);
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
Layout getLayout() const { return _layout; }
enum DrawModeMask
{
2009-07-24 22:59:51 +08:00
TEXT = 1, /// default
BOUNDINGBOX = 2,
FILLEDBOUNDINGBOX = 4,
ALIGNMENT = 8
2007-12-10 23:15:56 +08:00
};
void setDrawMode(unsigned int mode);
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
unsigned int getDrawMode() const { return _drawMode; }
2009-07-24 22:59:51 +08:00
void setBoundingBoxMargin(float margin);
2012-03-22 01:36:20 +08:00
2009-07-24 22:59:51 +08:00
float getBoundingBoxMargin() const { return _textBBMargin; }
void setBoundingBoxColor(const osg::Vec4& color){ _textBBColor = color; }
2012-03-22 01:36:20 +08:00
2009-07-24 22:59:51 +08:00
const osg::Vec4& getBoundingBoxColor() const { return _textBBColor; }
2012-03-22 01:36:20 +08:00
2007-12-10 23:15:56 +08:00
void setKerningType(KerningType kerningType) { _kerningType = kerningType; }
KerningType getKerningType() const { return _kerningType; }
/** Get the number of wrapped lines - only valid after computeGlyphRepresentation() has been called, returns 0 otherwise */
unsigned int getLineCount() const { return _lineCount; }
2017-03-04 02:31:51 +08:00
/** Immediately compile this \c Drawable into an OpenGL Display List/VertexBufferObjects.
* @note Operation is ignored if \c _useDisplayList is \c false or VertexBufferObjects are not used.
*/
virtual void compileGLObjects(osg::RenderInfo& renderInfo) const;
2007-12-10 23:15:56 +08:00
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
virtual void releaseGLObjects(osg::State* state=0) const;
2012-03-22 01:36:20 +08:00
2014-05-14 18:19:43 +08:00
virtual osg::BoundingBox computeBoundingBox() const;
2007-12-10 23:15:56 +08:00
2017-03-02 00:43:05 +08:00
typedef osg::ref_ptr<osg::Vec3Array> Coords;
Coords& getCoords() { return _coords; }
const Coords& getCoords() const { return _coords; }
void getCoord(unsigned int i, osg::Vec2& c) const { c.set((*_coords)[i].x(), (*_coords)[i].y()); }
void getCoord(unsigned int i, osg::Vec3& c) const { c = (*_coords)[i]; }
2017-11-21 21:41:57 +08:00
/** Get the cached internal matrix used to provide positioning of text. The cached matrix is originally computed by computeMatrix(..). */
2017-03-28 21:43:07 +08:00
const osg::Matrix& getMatrix() const { return _matrix; }
2017-11-21 21:41:57 +08:00
/** compute the matrix that positions the text in model space for the given viewpoint.*/
bool computeMatrix(osg::Matrix& matrix, osg::State* state=0) const;
2007-12-10 23:15:56 +08:00
protected:
virtual ~TextBase();
2017-09-12 18:50:47 +08:00
virtual osg::StateSet* createStateSet();
virtual void assignStateSet();
2007-12-10 23:15:56 +08:00
2017-03-04 02:31:51 +08:00
void initArraysAndBuffers();
2017-11-29 22:22:31 +08:00
osg::VertexArrayState* createVertexArrayStateImplementation(osg::RenderInfo& renderInfo) const;
2017-03-04 02:31:51 +08:00
2007-12-10 23:15:56 +08:00
void positionCursor(const osg::Vec2 & endOfLine_coords, osg::Vec2 & cursor, unsigned int linelength);
String::iterator computeLastCharacterOnLine(osg::Vec2& cursor, String::iterator first,String::iterator last);
2017-02-23 23:45:43 +08:00
void computePositions();
virtual void computePositionsImplementation();
2007-12-10 23:15:56 +08:00
virtual void computeGlyphRepresentation() = 0;
2012-03-22 01:36:20 +08:00
2017-03-01 19:51:27 +08:00
typedef osg::ref_ptr<osg::Vec2Array> TexCoords;
typedef osg::ref_ptr<osg::Vec4Array> ColorCoords;
2017-05-25 19:38:54 +08:00
typedef std::vector< osg::ref_ptr<osg::DrawElements> > Primitives;
2012-03-22 01:36:20 +08:00
2017-03-02 00:43:05 +08:00
2007-12-10 23:15:56 +08:00
// members which have public access.
2010-11-19 01:59:55 +08:00
osg::Vec4 _color;
2010-09-29 19:34:55 +08:00
osg::ref_ptr<Font> _font;
2019-07-31 21:11:59 +08:00
osg::ref_ptr<Font> _fontFallback;
2010-09-29 20:45:35 +08:00
osg::ref_ptr<Style> _style;
2007-12-24 02:15:54 +08:00
FontResolution _fontSize;
2007-12-10 23:15:56 +08:00
float _characterHeight;
CharacterSizeMode _characterSizeMode;
float _maximumWidth;
float _maximumHeight;
float _lineSpacing;
String _text;
osg::Vec3 _position;
AlignmentType _alignment;
AxisAlignment _axisAlignment;
osg::Quat _rotation;
bool _autoRotateToScreen;
Layout _layout;
unsigned int _drawMode;
2009-07-24 22:59:51 +08:00
float _textBBMargin;
osg::Vec4 _textBBColor;
2007-12-10 23:15:56 +08:00
KerningType _kerningType;
unsigned int _lineCount;
2017-08-16 03:11:15 +08:00
bool _glyphNormalized;
2007-12-10 23:15:56 +08:00
2017-02-23 23:45:43 +08:00
osg::Vec3 _offset;
osg::Vec3 _normal;
osg::BoundingBox _textBB;
2018-04-07 03:05:26 +08:00
osg::BoundingBox _textBBWithMargin;
2012-03-22 01:36:20 +08:00
2017-02-23 23:45:43 +08:00
mutable osg::Matrix _matrix;
2016-09-03 00:01:25 +08:00
2017-03-01 19:51:27 +08:00
Primitives _decorationPrimitives;
2016-09-03 00:01:25 +08:00
void setupDecoration();
2017-03-04 02:31:51 +08:00
osg::ref_ptr<osg::VertexBufferObject> _vbo;
osg::ref_ptr<osg::ElementBufferObject> _ebo;
2017-03-02 00:43:05 +08:00
Coords _coords;
2017-03-02 19:12:55 +08:00
Coords _normals;
2017-03-02 00:43:05 +08:00
ColorCoords _colorCoords;
TexCoords _texcoords;
2017-03-09 02:15:06 +08:00
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; }
unsigned int addCoord(const osg::Vec3& c) { unsigned int s = _coords->size(); _coords->push_back(c); _coords->dirty(); return s; }
2017-03-01 19:51:27 +08:00
2017-03-09 02:15:06 +08:00
void addTexCoord(const osg::Vec2& tc) { _texcoords->push_back(tc); _texcoords->dirty(); }
2016-09-03 00:01:25 +08:00
2007-12-10 23:15:56 +08:00
};
}
#endif
2012-03-22 01:36:20 +08:00