Added osgText::Font::s/getGlyphInterval(int) and GlyphTexture::s/getGlyphInterval(int) and internal support for clmapping positions of glyph images an defined intervals, defaults to 1.

This commit is contained in:
Robert Osfield 2017-08-30 16:21:03 +01:00
parent 7323bb776b
commit 1289c4ee41
4 changed files with 27 additions and 9 deletions

View File

@ -122,6 +122,11 @@ public:
void setGlyphImageMarginRatio(float margin); void setGlyphImageMarginRatio(float margin);
float getGlyphImageMarginRatio() const; float getGlyphImageMarginRatio() const;
/** Set the interval that glyph positions are clamped to.
* Default interval is 1 texels.*/
void setGlyphInterval(int interval);
int getGlyphInterval() const;
/** Set the size of texture to create to store the glyph images when rendering. /** Set the size of texture to create to store the glyph images when rendering.
* Note, this doesn't affect already created Texture Glhph's.*/ * Note, this doesn't affect already created Texture Glhph's.*/
@ -197,6 +202,7 @@ protected:
FontResolution _fontSize; FontResolution _fontSize;
unsigned int _margin; unsigned int _margin;
float _marginRatio; float _marginRatio;
int _glyphInterval;
unsigned int _textureWidthHint; unsigned int _textureWidthHint;
unsigned int _textureHeightHint; unsigned int _textureHeightHint;

View File

@ -260,6 +260,9 @@ public:
void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; } void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; }
float getGlyphImageMarginRatio() const { return _marginRatio; } float getGlyphImageMarginRatio() const { return _marginRatio; }
void setGlyphInterval(int interval) { _interval = interval; }
int getGlyphInterval() const { return _interval; }
bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY); bool getSpaceForGlyph(Glyph* glyph, int& posX, int& posY);
void addGlyph(Glyph* glyph,int posX, int posY); void addGlyph(Glyph* glyph,int posX, int posY);
@ -282,6 +285,8 @@ protected:
// in the texture which could accommodate new glyphs. // in the texture which could accommodate new glyphs.
int _margin; int _margin;
float _marginRatio; float _marginRatio;
int _interval;
int _usedY; int _usedY;
int _partUsedX; int _partUsedX;
int _partUsedY; int _partUsedY;

View File

@ -301,6 +301,7 @@ Font::Font(FontImplementation* implementation):
osg::Object(true), osg::Object(true),
_margin(1), _margin(1),
_marginRatio(0.02), _marginRatio(0.02),
_glyphInterval(1),
_textureWidthHint(1024), _textureWidthHint(1024),
_textureHeightHint(1024), _textureHeightHint(1024),
_minFilterHint(osg::Texture::LINEAR_MIPMAP_LINEAR), _minFilterHint(osg::Texture::LINEAR_MIPMAP_LINEAR),
@ -604,6 +605,7 @@ void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph*
// reserve enough space for the glyphs. // reserve enough space for the glyphs.
glyphTexture->setGlyphImageMargin(_margin); glyphTexture->setGlyphImageMargin(_margin);
glyphTexture->setGlyphImageMarginRatio(_marginRatio); glyphTexture->setGlyphImageMarginRatio(_marginRatio);
glyphTexture->setGlyphInterval(_glyphInterval);
glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint); glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint); glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint);
glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint); glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint);

View File

@ -31,6 +31,7 @@ using namespace std;
GlyphTexture::GlyphTexture(): GlyphTexture::GlyphTexture():
_margin(1), _margin(1),
_marginRatio(0.02f), _marginRatio(0.02f),
_interval(1),
_usedY(0), _usedY(0),
_partUsedX(0), _partUsedX(0),
_partUsedY(0) _partUsedY(0)
@ -60,18 +61,22 @@ bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
int width = glyph->s()+2*margin; int width = glyph->s()+2*margin;
int height = glyph->t()+2*margin; int height = glyph->t()+2*margin;
// first check box (_partUsedX,_usedY) to (width,height) int partUsedX = ((_partUsedX % _interval) == 0) ? _partUsedX : (((_partUsedX/_interval)+1)*_interval);
if (width <= (getTextureWidth()-_partUsedX) && int partUsedY = ((_partUsedY % _interval) == 0) ? _partUsedY : (((_partUsedY/_interval)+1)*_interval);
height <= (getTextureHeight()-_usedY)) int usedY = ((_usedY % _interval) == 0) ? _usedY : (((_usedY/_interval)+1)*_interval);
// first check box (partUsedX, usedY) to (width,height)
if (width <= (getTextureWidth()-partUsedX) &&
height <= (getTextureHeight()-usedY))
{ {
// can fit in existing row. // can fit in existing row.
// record the position in which the texture will be stored. // record the position in which the texture will be stored.
posX = _partUsedX+margin; posX = partUsedX+margin;
posY = _usedY+margin; posY = usedY+margin;
// move used markers on. // move used markers on.
_partUsedX += width; _partUsedX = posX+width;
if (_usedY+height>_partUsedY) _partUsedY = _usedY+height; if (_usedY+height>_partUsedY) _partUsedY = _usedY+height;
return true; return true;
@ -83,14 +88,14 @@ bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
{ {
// can fit next row. // can fit next row.
_partUsedX = 0; _partUsedX = 0;
_usedY = _partUsedY; _usedY = partUsedY;
posX = _partUsedX+margin; posX = _partUsedX+margin;
posY = _usedY+margin; posY = _usedY+margin;
// move used markers on. // move used markers on.
_partUsedX += width; _partUsedX = posX+width;
if (_usedY+height>_partUsedY) _partUsedY = _usedY+height; _partUsedY = _usedY+height;
return true; return true;
} }