From 1289c4ee41288299509ef91ede05285a5beae734 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 30 Aug 2017 16:21:03 +0100 Subject: [PATCH] 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. --- include/osgText/Font | 6 ++++++ include/osgText/Glyph | 5 +++++ src/osgText/Font.cpp | 2 ++ src/osgText/Glyph.cpp | 23 ++++++++++++++--------- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/include/osgText/Font b/include/osgText/Font index 952a6fda4..369493891 100644 --- a/include/osgText/Font +++ b/include/osgText/Font @@ -122,6 +122,11 @@ public: void setGlyphImageMarginRatio(float margin); 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. * Note, this doesn't affect already created Texture Glhph's.*/ @@ -197,6 +202,7 @@ protected: FontResolution _fontSize; unsigned int _margin; float _marginRatio; + int _glyphInterval; unsigned int _textureWidthHint; unsigned int _textureHeightHint; diff --git a/include/osgText/Glyph b/include/osgText/Glyph index f08686841..bb7d5192a 100644 --- a/include/osgText/Glyph +++ b/include/osgText/Glyph @@ -260,6 +260,9 @@ public: void setGlyphImageMarginRatio(float margin) { _marginRatio = margin; } float getGlyphImageMarginRatio() const { return _marginRatio; } + void setGlyphInterval(int interval) { _interval = interval; } + int getGlyphInterval() const { return _interval; } + bool getSpaceForGlyph(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. int _margin; float _marginRatio; + int _interval; + int _usedY; int _partUsedX; int _partUsedY; diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index c06392640..155acadff 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -301,6 +301,7 @@ Font::Font(FontImplementation* implementation): osg::Object(true), _margin(1), _marginRatio(0.02), + _glyphInterval(1), _textureWidthHint(1024), _textureHeightHint(1024), _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. glyphTexture->setGlyphImageMargin(_margin); glyphTexture->setGlyphImageMarginRatio(_marginRatio); + glyphTexture->setGlyphInterval(_glyphInterval); glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint); glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint); glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint); diff --git a/src/osgText/Glyph.cpp b/src/osgText/Glyph.cpp index 3bc346c8f..2699b0627 100644 --- a/src/osgText/Glyph.cpp +++ b/src/osgText/Glyph.cpp @@ -31,6 +31,7 @@ using namespace std; GlyphTexture::GlyphTexture(): _margin(1), _marginRatio(0.02f), + _interval(1), _usedY(0), _partUsedX(0), _partUsedY(0) @@ -60,18 +61,22 @@ bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY) int width = glyph->s()+2*margin; int height = glyph->t()+2*margin; - // first check box (_partUsedX,_usedY) to (width,height) - if (width <= (getTextureWidth()-_partUsedX) && - height <= (getTextureHeight()-_usedY)) + int partUsedX = ((_partUsedX % _interval) == 0) ? _partUsedX : (((_partUsedX/_interval)+1)*_interval); + int partUsedY = ((_partUsedY % _interval) == 0) ? _partUsedY : (((_partUsedY/_interval)+1)*_interval); + 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. // record the position in which the texture will be stored. - posX = _partUsedX+margin; - posY = _usedY+margin; + posX = partUsedX+margin; + posY = usedY+margin; // move used markers on. - _partUsedX += width; + _partUsedX = posX+width; if (_usedY+height>_partUsedY) _partUsedY = _usedY+height; return true; @@ -83,14 +88,14 @@ bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY) { // can fit next row. _partUsedX = 0; - _usedY = _partUsedY; + _usedY = partUsedY; posX = _partUsedX+margin; posY = _usedY+margin; // move used markers on. - _partUsedX += width; - if (_usedY+height>_partUsedY) _partUsedY = _usedY+height; + _partUsedX = posX+width; + _partUsedY = _usedY+height; return true; }