Simplified and improved the glyph margin computation and usage

This commit is contained in:
Robert Osfield
2017-09-29 20:21:13 +01:00
parent beb5801eee
commit d2fa7c4317
8 changed files with 41 additions and 116 deletions

View File

@@ -278,19 +278,6 @@ void FreeTypeFont::setFontResolution(const osgText::FontResolution& fontSize)
int width = fontSize.first;
int height = fontSize.second;
int maxAxis = std::max(width, height);
int margin = _facade->getGlyphImageMargin() + (int)((float)maxAxis * _facade->getGlyphImageMarginRatio());
if ((unsigned int)(width+2*margin) > _facade->getTextureWidthHint() ||
(unsigned int)(width+2*margin) > _facade->getTextureHeightHint())
{
OSG_WARN<<"Warning: FreeTypeFont::setSize("<<width<<","<<height<<") sizes too large,"<<std::endl;
width = _facade->getTextureWidthHint()-2*margin;
height = _facade->getTextureHeightHint()-2*margin;
OSG_WARN<<" sizes capped ("<<width<<","<<height<<") to fit int current glyph texture size."<<std::endl;
}
FT_Error error = FT_Set_Pixel_Sizes( _face, /* handle to face object */
width, /* pixel_width */

View File

@@ -29,9 +29,6 @@ DefaultFont::DefaultFont()
_minFilterHint = osg::Texture::LINEAR_MIPMAP_LINEAR;
_magFilterHint = osg::Texture::NEAREST;
_margin = 8;
_marginRatio = 0.0;
_glyphInterval = 16;
constructGlyphs();
}

View File

@@ -224,9 +224,6 @@ osg::ref_ptr<Font> osgText::readRefFontStream(std::istream& stream, const osgDB:
Font::Font(FontImplementation* implementation):
osg::Object(true),
_margin(1),
_marginRatio(0.02),
_glyphInterval(1),
#if 0
_glyphTextureFeatures(GlyphTexture::ALL_FEATURES),
#else
@@ -289,32 +286,6 @@ std::string Font::getFileName() const
return std::string();
}
void Font::setGlyphImageMargin(unsigned int margin)
{
_margin = margin;
}
unsigned int Font::getGlyphImageMargin() const
{
return _margin;
}
void Font::setGlyphImageMarginRatio(float ratio)
{
_marginRatio = ratio;
}
float Font::getGlyphImageMarginRatio() const
{
return _marginRatio;
}
void Font::setGlyphInterval(int interval)
{
_glyphInterval = interval;
}
void Font::setTextureSizeHint(unsigned int width,unsigned int height)
{
_textureWidthHint = width;
@@ -514,9 +485,6 @@ void Font::addGlyph(const FontResolution& fontRes, unsigned int charcode, Glyph*
OSG_INFO<< " Font " << this<< ", numberOfTexturesAllocated "<<numberOfTexturesAllocated<<std::endl;
// reserve enough space for the glyphs.
glyphTexture->setGlyphImageMargin(_margin);
glyphTexture->setGlyphImageMarginRatio(_marginRatio);
glyphTexture->setGlyphInterval(_glyphInterval);
glyphTexture->setGlyphTextureFeatures(_glyphTextureFeatures);
glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint);

View File

@@ -37,9 +37,6 @@ using namespace std;
#endif
GlyphTexture::GlyphTexture():
_margin(1),
_marginRatio(0.02f),
_interval(1),
_usedY(0),
_partUsedX(0),
_partUsedY(0)
@@ -60,21 +57,39 @@ int GlyphTexture::compare(const osg::StateAttribute& rhs) const
return 0;
}
int GlyphTexture::getEffectMargin(const Glyph* glyph)
{
if (_glyphTextureFeatures==GREYSCALE) return 0;
else return glyph->getFontResolution().second/4;
}
int GlyphTexture::getTexelMargin(const Glyph* glyph)
{
int width = glyph->s();
int height = glyph->t();
int effect_margin = getEffectMargin(glyph);
int max_dimension = std::max(width, height) + 2 * effect_margin;
int margin = osg::maximum(max_dimension/4, 2) + effect_margin;
return margin;
}
bool GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
{
int maxAxis = osg::maximum(glyph->s(), glyph->t());
int margin_from_ratio = (int)((float)maxAxis * _marginRatio);
int search_distance = glyph->getFontResolution().second/8;
int width = glyph->s();
int height = glyph->t();
int margin = _margin + osg::maximum(margin_from_ratio, search_distance);
int margin = getTexelMargin(glyph);
int width = glyph->s()+2*margin;
int height = glyph->t()+2*margin;
width += 2*margin;
height += 2*margin;
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);
int interval = 4;
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) &&
@@ -132,6 +147,9 @@ void GlyphTexture::addGlyph(Glyph* glyph, int posX, int posY)
glyph->setMaxTexCoord( osg::Vec2( static_cast<float>(posX+glyph->s())/static_cast<float>(getTextureWidth()),
static_cast<float>(posY+glyph->t())/static_cast<float>(getTextureHeight()) ) );
glyph->setTexelMargin(float(getTexelMargin(glyph)));
copyGlyphImage(glyph);
}
@@ -156,7 +174,7 @@ void GlyphTexture::copyGlyphImage(Glyph* glyph)
int dest_rows = _image->t();
unsigned char* dest_data = _image->data(glyph->getTexturePositionX(),glyph->getTexturePositionY());
int search_distance = glyph->getFontResolution().second/4;
int search_distance = getEffectMargin(glyph);
int left = -search_distance;
int right = glyph->s()+search_distance;

View File

@@ -464,8 +464,6 @@ void Text::computeGlyphRepresentation()
float hr = _characterHeight;
float wr = hr/getCharacterAspectRatio();
float texelMargin = 5.0f;
for(String::iterator itr=_text.begin();
itr!=_text.end();
)
@@ -632,6 +630,7 @@ void Text::computeGlyphRepresentation()
osg::Vec2 mintc = glyph->getMinTexCoord();
osg::Vec2 maxtc = glyph->getMaxTexCoord();
osg::Vec2 vDiff = maxtc - mintc;
float texelMargin = glyph->getTexelMargin();
float fHorizTCMargin = texelMargin / glyph->getTexture()->getTextureWidth();
float fVertTCMargin = texelMargin / glyph->getTexture()->getTextureHeight();