Changed the osgText::Font implementation so it used a facade us abstract away

the actual implemention. This has been done so that when a freetype font is
created the implementation can unloaded when the freetype plugin is unloaded
without breaking the main font.

Also add image margin around founds to prevent any image boundaries appearing.
This commit is contained in:
Robert Osfield
2003-03-06 17:11:24 +00:00
parent 0d9aaa5ca6
commit fab6f24f4e
10 changed files with 240 additions and 84 deletions

View File

@@ -22,6 +22,8 @@ using namespace osgText;
DefaultFont::DefaultFont()
{
_minFilterHint = osg::Texture::LINEAR_MIPMAP_LINEAR;
_magFilterHint = osg::Texture::NEAREST;
constructGlyphs();
}
@@ -190,15 +192,25 @@ void DefaultFont::constructGlyphs()
{0x00, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x1c, 0x00, 0x00}
};
_width = 8;
_height = 12;
unsigned int sourceWidth = 8;
unsigned int sourceHeight = 12;
_width = sourceWidth+2*_margin;
_height = sourceHeight+2*_margin;
// populate the glyph mp
for(unsigned int i=32;i<127;i++)
{
osg::ref_ptr<Glyph> glyph = new Glyph;
unsigned char* data = new unsigned char[8*12*2];
unsigned int dataSize = _width*_height*2;
unsigned char* data = new unsigned char[dataSize];
// clear the image to zeros.
for(unsigned char* p=data;p!=data+dataSize;++p) *p = 0;
glyph->setImage(_width,_height,1,
GL_LUMINANCE_ALPHA,
GL_LUMINANCE_ALPHA,GL_UNSIGNED_BYTE,
@@ -210,8 +222,13 @@ void DefaultFont::constructGlyphs()
unsigned char* ptr = rasters[i-32];
unsigned char value_on = 255;
unsigned char value_off = 0;
for(unsigned int row=0;row<_height;++row,++ptr)
// skip the top margin
data += (_margin*_width)*2;
for(unsigned int row=0;row<sourceHeight;++row,++ptr)
{
data+=2*_margin; // skip the left margin
(*data++)=((*ptr)&128)?value_on:value_off;
(*data++)=((*ptr)&128)?value_on:value_off;
(*data++)=((*ptr)&64)?value_on:value_off;
@@ -228,6 +245,7 @@ void DefaultFont::constructGlyphs()
(*data++)=((*ptr)&2)?value_on:value_off;
(*data++)=((*ptr)&1)?value_on:value_off;
(*data++)=((*ptr)&1)?value_on:value_off;
data+=2*_margin; // skip the right margin.
}

View File

@@ -75,23 +75,69 @@ osgText::Font* osgText::readFontFile(const std::string& filename)
}
Font::Font():
Font::Font(FontImplementation* implementation):
_width(16),
_height(16)
_height(16),
_margin(2),
_textureWidthHint(256),
_textureHeightHint(256),
_minFilterHint(osg::Texture::LINEAR_MIPMAP_LINEAR),
_magFilterHint(osg::Texture::LINEAR)
{
setImplementation(implementation);
}
Font::~Font()
{
for(TextList::iterator itr=_textList.begin();
itr!=_textList.end();
++itr)
{
Text* text = *itr;
text->_font.release();
}
if (_implementation.valid()) _implementation->_facade = 0;
}
void Font::setImplementation(FontImplementation* implementation)
{
if (_implementation.valid()) _implementation->_facade = 0;
_implementation = implementation;
if (_implementation.valid()) _implementation->_facade = this;
}
std::string Font::getFileName() const
{
if (_implementation.valid()) return _implementation->getFileName();
return "";
}
void Font::setSize(unsigned int width, unsigned int height)
{
if (_implementation.valid()) return _implementation->setSize(width, height);
}
Font::Glyph* Font::getGlyph(unsigned int charcode)
{
SizeGlyphMap::iterator itr = _sizeGlyphMap.find(SizePair(_width,_height));
if (itr!=_sizeGlyphMap.end())
{
GlyphMap& glyphmap = itr->second;
GlyphMap::iterator gitr = glyphmap.find(charcode);
if (gitr!=glyphmap.end()) return gitr->second.get();
}
if (_implementation.valid()) return _implementation->getGlyph(charcode);
else return 0;
}
osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode)
{
if (_implementation.valid()) return _implementation->getKerning(leftcharcode,rightcharcode);
else return osg::Vec2(0.0f,0.0f);
}
bool Font::hasVertical() const
{
if (_implementation.valid()) return _implementation->hasVertical();
else return false;
}
void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charcode, Glyph* glyph)
{
_sizeGlyphMap[SizePair(width,height)][charcode]=glyph;
@@ -115,13 +161,12 @@ void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charco
_stateSetList.push_back(stateset);
glyphTexture = new GlyphTexture;
// reserve enough space for the glyphs.
glyphTexture->setTextureSize(256,256);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR_MIPMAP_LINEAR);
//glyphTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::NEAREST);
glyphTexture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR);
glyphTexture->setGlyphImageMargin(_margin);
glyphTexture->setTextureSize(_textureWidthHint,_textureHeightHint);
glyphTexture->setFilter(osg::Texture::MIN_FILTER,_minFilterHint);
glyphTexture->setFilter(osg::Texture::MAG_FILTER,_magFilterHint);
glyphTexture->setMaxAnisotropy(8);
_glyphTextureList.push_back(glyphTexture);
@@ -147,6 +192,7 @@ void Font::addGlyph(unsigned int width, unsigned int height, unsigned int charco
Font::GlyphTexture::GlyphTexture():
_stateset(0),
_margin(2),
_usedY(0),
_partUsedX(0),
_partUsedY(0)
@@ -159,11 +205,9 @@ Font::GlyphTexture::~GlyphTexture()
bool Font::GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
{
int margin = 2;
int width = glyph->s()+2*margin;
int height = glyph->t()+2*margin;
int width = glyph->s()+2*_margin;
int height = glyph->t()+2*_margin;
// first check box (_partUsedX,_usedY) to (width,height)
if (width <= (getTextureWidth()-_partUsedX) &&
@@ -172,8 +216,8 @@ bool Font::GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
// 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;
@@ -190,8 +234,8 @@ bool Font::GlyphTexture::getSpaceForGlyph(Glyph* glyph, int& posX, int& posY)
_partUsedX = 0;
_usedY = _partUsedY;
posX = _partUsedX+margin;
posY = _usedY+margin;
posX = _partUsedX+_margin;
posY = _usedY+_margin;
// move used markers on.
_partUsedX += width;
@@ -216,8 +260,8 @@ void Font::GlyphTexture::addGlyph(Glyph* glyph, int posX, int posY)
// set up the details of where to place glyph's image in the texture.
glyph->setTexture(this);
glyph->setTexturePosition(posX,posY);
glyph->setMinTexCoord(osg::Vec2(((float)posX-1.0f)/((float)getTextureWidth()-1.0f),((float)posY-1)/((float)getTextureHeight()-1.0f)));
glyph->setMaxTexCoord(osg::Vec2((float)(posX+glyph->s())/((float)getTextureWidth()-1.0f),(float)(posY+glyph->t())/((float)getTextureHeight()-1.0f)));
glyph->setMinTexCoord(osg::Vec2((float)(posX+_margin)/(float)(getTextureWidth()-1),(float)(posY+_margin)/(float)(getTextureHeight()-1)));
glyph->setMaxTexCoord(osg::Vec2((float)(posX+glyph->s()-_margin)/(float)(getTextureWidth()-1),(float)(posY+glyph->t()-_margin)/(float)(getTextureHeight()-1)));
}
void Font::GlyphTexture::apply(osg::State& state) const

View File

@@ -51,25 +51,17 @@ Text::Text(const Text& text,const osg::CopyOp& copyop):
_color(text._color),
_drawMode(text._drawMode)
{
if (_font.valid()) _font->_textList.insert(this);
}
Text::~Text()
{
if (_font.valid()) _font->_textList.erase(this);
}
void Text::setFont(Font* font)
{
if (_font==font) return;
// unregister from the old font.
if (_font.valid()) _font->_textList.erase(this);
_font = font;
// register with the new font.
if (_font.valid()) _font->_textList.insert(this);
computeGlyphRepresentation();
}
@@ -244,8 +236,8 @@ void Text::computeGlyphRepresentation()
if (glyph)
{
float width = (float)glyph->s() * wr;
float height = (float)glyph->t() * hr;
float width = (float)(glyph->s()-2*activefont->getGlyphImageMargin()) * wr;
float height = (float)(glyph->t()-2*activefont->getGlyphImageMargin()) * hr;
if (_layout==RIGHT_TO_LEFT)
{