2010-09-24 20:57:55 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield
|
2010-09-08 02:20:03 +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
|
2010-09-08 02:20:03 +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
|
|
|
*
|
2010-09-08 02:20:03 +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
|
2010-09-08 02:20:03 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGTEXT_GLYPH
|
|
|
|
#define OSGTEXT_GLYPH 1
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <istream>
|
|
|
|
|
|
|
|
#include <osg/Vec2>
|
|
|
|
#include <osg/Image>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/StateSet>
|
|
|
|
#include <osg/Geometry>
|
2010-09-26 19:02:03 +08:00
|
|
|
#include <osg/Geode>
|
2010-09-08 02:20:03 +08:00
|
|
|
|
|
|
|
#include <osgText/Export>
|
|
|
|
#include <osgText/KerningType>
|
2010-09-24 20:57:55 +08:00
|
|
|
#include <osgText/Style>
|
2010-09-08 02:20:03 +08:00
|
|
|
|
|
|
|
#include <OpenThreads/Mutex>
|
|
|
|
|
|
|
|
namespace osgText {
|
|
|
|
|
|
|
|
class Font;
|
|
|
|
class Text;
|
|
|
|
class Glyph3D;
|
2010-09-24 20:57:55 +08:00
|
|
|
class GlyphGeometry;
|
2010-09-08 02:20:03 +08:00
|
|
|
class GlyphTexture;
|
|
|
|
|
2017-10-21 00:03:25 +08:00
|
|
|
enum ShaderTechnique
|
|
|
|
{
|
2017-10-23 23:07:24 +08:00
|
|
|
NO_TEXT_SHADER = 0x0,
|
2017-10-21 00:03:25 +08:00
|
|
|
GREYSCALE = 0x1,
|
|
|
|
SIGNED_DISTANCE_FIELD = 0x2,
|
|
|
|
ALL_FEATURES = GREYSCALE | SIGNED_DISTANCE_FIELD
|
|
|
|
};
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
class OSGTEXT_EXPORT Glyph : public osg::Image
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Glyph(Font* font, unsigned int glyphCode);
|
|
|
|
|
2010-09-28 00:18:20 +08:00
|
|
|
Font* getFont() { return _font; }
|
|
|
|
const Font* getFont() const { return _font; }
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
unsigned int getGlyphCode() const { return _glyphCode; }
|
|
|
|
|
2017-09-08 23:59:43 +08:00
|
|
|
void setFontResolution(const FontResolution& fontRes) { _fontResolution = fontRes; }
|
|
|
|
const FontResolution& getFontResolution() const { return _fontResolution; }
|
|
|
|
|
2011-01-11 19:39:31 +08:00
|
|
|
void setWidth(float width) { _width = width; }
|
|
|
|
float getWidth() const { return _width; }
|
|
|
|
|
|
|
|
void setHeight(float height) { _height = height; }
|
|
|
|
float getHeight() const { return _height; }
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
void setHorizontalBearing(const osg::Vec2& bearing);
|
|
|
|
const osg::Vec2& getHorizontalBearing() const;
|
|
|
|
|
|
|
|
void setHorizontalAdvance(float advance);
|
|
|
|
float getHorizontalAdvance() const;
|
|
|
|
|
|
|
|
void setVerticalBearing(const osg::Vec2& bearing);
|
|
|
|
const osg::Vec2& getVerticalBearing() const;
|
|
|
|
|
|
|
|
void setVerticalAdvance(float advance);
|
|
|
|
float getVerticalAdvance() const;
|
|
|
|
|
2017-10-23 21:50:35 +08:00
|
|
|
struct TextureInfo : public osg::Referenced
|
|
|
|
{
|
|
|
|
TextureInfo():
|
|
|
|
texture(0),
|
|
|
|
texelMargin(0.0f) {}
|
|
|
|
|
|
|
|
TextureInfo(GlyphTexture* tex, int x, int y, const osg::Vec2& mintc, const osg::Vec2& maxtc, float margin):
|
|
|
|
texture(tex),
|
|
|
|
texturePositionX(x),
|
|
|
|
texturePositionY(y),
|
|
|
|
minTexCoord(mintc),
|
|
|
|
maxTexCoord(maxtc),
|
|
|
|
texelMargin(margin) {}
|
|
|
|
|
|
|
|
GlyphTexture* texture;
|
|
|
|
int texturePositionX;
|
|
|
|
int texturePositionY;
|
|
|
|
osg::Vec2 minTexCoord;
|
|
|
|
osg::Vec2 maxTexCoord;
|
|
|
|
float texelMargin;
|
|
|
|
};
|
|
|
|
|
2017-10-24 18:36:00 +08:00
|
|
|
void setTextureInfo(ShaderTechnique technique, TextureInfo* info);
|
2017-10-23 21:50:35 +08:00
|
|
|
|
2017-10-24 18:36:00 +08:00
|
|
|
const TextureInfo* getTextureInfo(ShaderTechnique technique) const;
|
2017-10-23 21:50:35 +08:00
|
|
|
|
|
|
|
TextureInfo* getOrCreateTextureInfo(ShaderTechnique technique);
|
2017-09-30 03:21:13 +08:00
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~Glyph();
|
|
|
|
|
|
|
|
Font* _font;
|
|
|
|
unsigned int _glyphCode;
|
|
|
|
|
2017-09-08 23:59:43 +08:00
|
|
|
FontResolution _fontResolution;
|
|
|
|
|
2011-01-11 19:39:31 +08:00
|
|
|
float _width;
|
|
|
|
float _height;
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
osg::Vec2 _horizontalBearing;
|
|
|
|
float _horizontalAdvance;
|
|
|
|
|
|
|
|
osg::Vec2 _verticalBearing;
|
|
|
|
float _verticalAdvance;
|
|
|
|
|
2017-10-23 21:50:35 +08:00
|
|
|
typedef std::vector< osg::ref_ptr<TextureInfo> > TextureInfoList;
|
|
|
|
TextureInfoList _textureInfoList;
|
2018-09-10 20:00:41 +08:00
|
|
|
|
|
|
|
mutable OpenThreads::Mutex _textureInfoListMutex;
|
2010-09-08 02:20:03 +08:00
|
|
|
};
|
|
|
|
|
2010-09-24 20:57:55 +08:00
|
|
|
class OSGTEXT_EXPORT GlyphGeometry : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GlyphGeometry();
|
|
|
|
|
2010-09-28 00:18:20 +08:00
|
|
|
void setup(const Glyph3D* glyph, const Style* style);
|
2010-09-24 20:57:55 +08:00
|
|
|
|
|
|
|
bool match(const Style* style) const;
|
|
|
|
|
|
|
|
osg::Geode* getGeode() const { return _geode.get(); }
|
|
|
|
osg::Geometry* getGeometry() const { return _geometry.get(); }
|
|
|
|
|
|
|
|
/** Set the VertexArray of the glyph. */
|
|
|
|
void setVertexArray(osg::Vec3Array * va) { _vertices = va; }
|
|
|
|
/** Get the VertexArray of the glyph. */
|
|
|
|
osg::Vec3Array * getVertexArray() const { return _vertices.get(); }
|
|
|
|
|
|
|
|
/** Set the VertexArray of the glyph. */
|
|
|
|
void setNormalArray(osg::Vec3Array* na) { _normals = na; }
|
|
|
|
/** Get the NormalArray for the wall face. */
|
|
|
|
osg::Vec3Array* getNormalArray() const { return _normals.get(); }
|
|
|
|
|
|
|
|
/** Get the PrimitiveSetList for the front face. */
|
|
|
|
osg::Geometry::PrimitiveSetList& getFrontPrimitiveSetList() { return _frontPrimitiveSetList; }
|
|
|
|
/** Get the PrimitiveSetList for the wall face. */
|
|
|
|
osg::Geometry::PrimitiveSetList& getWallPrimitiveSetList() { return _wallPrimitiveSetList; }
|
|
|
|
/** Get et the PrimitiveSetList for the back face. */
|
|
|
|
osg::Geometry::PrimitiveSetList& getBackPrimitiveSetList() { return _backPrimitiveSetList; }
|
|
|
|
|
2010-09-28 00:18:20 +08:00
|
|
|
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
|
|
|
|
virtual void setThreadSafeRefUnref(bool threadSafe);
|
|
|
|
|
2010-09-24 20:57:55 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
osg::ref_ptr<Style> _style;
|
|
|
|
osg::ref_ptr<osg::Geode> _geode;
|
|
|
|
osg::ref_ptr<osg::Geometry> _geometry;
|
|
|
|
osg::ref_ptr<osg::Vec3Array> _vertices;
|
|
|
|
osg::ref_ptr<osg::Vec3Array> _normals;
|
|
|
|
|
|
|
|
osg::Geometry::PrimitiveSetList _frontPrimitiveSetList;
|
|
|
|
osg::Geometry::PrimitiveSetList _wallPrimitiveSetList;
|
|
|
|
osg::Geometry::PrimitiveSetList _backPrimitiveSetList;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
class OSGTEXT_EXPORT Glyph3D : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
Glyph3D(Font* font, unsigned int glyphCode);
|
|
|
|
|
2010-09-28 00:18:20 +08:00
|
|
|
Font* getFont() { return _font; }
|
|
|
|
const Font* getFont() const { return _font; }
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
unsigned int getGlyphCode() const { return _glyphCode; }
|
|
|
|
|
2011-01-11 19:39:31 +08:00
|
|
|
void setWidth(float width) { _width = width; }
|
|
|
|
float getWidth() const { return _width; }
|
|
|
|
|
|
|
|
void setHeight(float height) { _height = height; }
|
|
|
|
float getHeight() const { return _height; }
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
void setHorizontalBearing(const osg::Vec2& bearing) { _horizontalBearing=bearing; }
|
|
|
|
const osg::Vec2 & getHorizontalBearing() const { return _horizontalBearing; }
|
|
|
|
|
|
|
|
void setHorizontalAdvance(float advance) { _horizontalAdvance=advance; }
|
|
|
|
float getHorizontalAdvance() const { return _horizontalAdvance; }
|
|
|
|
|
|
|
|
void setVerticalBearing(const osg::Vec2& bearing) { _verticalBearing=bearing; }
|
|
|
|
const osg::Vec2& getVerticalBearing() const { return _verticalBearing; }
|
|
|
|
|
|
|
|
void setVerticalAdvance(float advance) { _verticalAdvance=advance; }
|
|
|
|
float getVerticalAdvance() const { return _verticalAdvance; }
|
|
|
|
|
|
|
|
void setBoundingBox(osg::BoundingBox & bb) { _bb=bb; }
|
|
|
|
const osg::BoundingBox & getBoundingBox() const { return _bb; }
|
|
|
|
|
|
|
|
|
|
|
|
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
|
|
|
|
virtual void setThreadSafeRefUnref(bool threadSafe);
|
|
|
|
|
|
|
|
|
|
|
|
void setRawVertexArray(osg::Vec3Array* vertices) { _rawVertexArray = vertices; }
|
|
|
|
osg::Vec3Array* getRawVertexArray() { return _rawVertexArray.get(); }
|
2010-09-28 00:18:20 +08:00
|
|
|
const osg::Vec3Array* getRawVertexArray() const { return _rawVertexArray.get(); }
|
2010-09-08 02:20:03 +08:00
|
|
|
|
|
|
|
/** Get the PrimitiveSetList for the raw face which hasn't been tessellated. */
|
|
|
|
osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() { return _rawFacePrimitiveSetList; }
|
2010-09-28 00:18:20 +08:00
|
|
|
const osg::Geometry::PrimitiveSetList & getRawFacePrimitiveSetList() const { return _rawFacePrimitiveSetList; }
|
2010-09-08 02:20:03 +08:00
|
|
|
|
2010-09-28 00:18:20 +08:00
|
|
|
GlyphGeometry* getGlyphGeometry(const Style* style);
|
2010-09-08 02:20:03 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~Glyph3D() {}
|
|
|
|
|
|
|
|
Font* _font;
|
|
|
|
unsigned int _glyphCode;
|
|
|
|
|
2011-01-11 19:39:31 +08:00
|
|
|
float _width;
|
|
|
|
float _height;
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
osg::Vec2 _horizontalBearing;
|
|
|
|
float _horizontalAdvance;
|
|
|
|
|
|
|
|
osg::Vec2 _verticalBearing;
|
|
|
|
float _verticalAdvance;
|
|
|
|
|
|
|
|
osg::BoundingBox _bb;
|
|
|
|
// osg::Vec2 _advance;
|
|
|
|
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Vec3Array> _rawVertexArray;
|
|
|
|
osg::Geometry::PrimitiveSetList _rawFacePrimitiveSetList;
|
|
|
|
|
2010-09-24 20:57:55 +08:00
|
|
|
typedef std::list< osg::ref_ptr<GlyphGeometry> > GlyphGeometries;
|
|
|
|
GlyphGeometries _glyphGeometries;
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
GlyphTexture();
|
|
|
|
|
|
|
|
const char* className() const { return "GlyphTexture"; }
|
|
|
|
|
|
|
|
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
|
|
|
|
virtual int compare(const osg::StateAttribute& rhs) const;
|
|
|
|
|
2017-10-21 00:03:25 +08:00
|
|
|
void setShaderTechnique(ShaderTechnique technique) { _shaderTechnique = technique; }
|
2017-09-08 23:59:43 +08:00
|
|
|
|
2017-10-21 00:03:25 +08:00
|
|
|
ShaderTechnique getShaderTechnique() const { return _shaderTechnique; }
|
2017-09-08 23:59:43 +08:00
|
|
|
|
2017-09-30 03:21:13 +08:00
|
|
|
|
|
|
|
int getEffectMargin(const Glyph* glyph);
|
|
|
|
int getTexelMargin(const Glyph* glyph);
|
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
|
|
|
|
|
|
|
|
void addGlyph(Glyph* glyph,int posX, int posY);
|
|
|
|
|
|
|
|
/** Set whether to use a mutex to ensure ref() and unref() are thread safe.*/
|
|
|
|
virtual void setThreadSafeRefUnref(bool threadSafe);
|
|
|
|
|
|
|
|
/** Resize any per context GLObject buffers to specified size. */
|
|
|
|
virtual void resizeGLObjectBuffers(unsigned int maxSize);
|
|
|
|
|
2015-06-01 21:40:20 +08:00
|
|
|
/** 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.*/
|
2011-05-14 03:08:04 +08:00
|
|
|
osg::Image* createImage();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2010-09-08 02:20:03 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~GlyphTexture();
|
|
|
|
|
2017-10-23 21:50:35 +08:00
|
|
|
void copyGlyphImage(Glyph* glyph, Glyph::TextureInfo* info);
|
2017-09-08 23:59:43 +08:00
|
|
|
|
2017-10-21 00:03:25 +08:00
|
|
|
ShaderTechnique _shaderTechnique;
|
2017-09-08 23:59:43 +08:00
|
|
|
|
2017-10-21 00:03:25 +08:00
|
|
|
int _usedY;
|
|
|
|
int _partUsedX;
|
|
|
|
int _partUsedY;
|
2010-09-08 02:20:03 +08:00
|
|
|
|
|
|
|
typedef std::vector< osg::ref_ptr<Glyph> > GlyphRefList;
|
|
|
|
typedef std::vector< const Glyph* > GlyphPtrList;
|
|
|
|
typedef osg::buffered_object< GlyphPtrList > GlyphBuffer;
|
|
|
|
|
|
|
|
GlyphRefList _glyphs;
|
|
|
|
mutable GlyphBuffer _glyphsToSubload;
|
|
|
|
|
|
|
|
mutable OpenThreads::Mutex _mutex;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|