From 8d4ab4668ee2cd42577491abd47ec0031c899049 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 26 Jun 2003 16:21:49 +0000 Subject: [PATCH] From Tree, updates to osgText and freetype plugin to support are kerning paramter. --- include/osgText/Font | 16 +++++++++++----- include/osgText/Text | 15 +++++++++------ src/osgPlugins/freetype/FreeTypeFont.cpp | 9 +++++---- src/osgPlugins/freetype/FreeTypeFont.h | 2 +- src/osgText/DefaultFont.cpp | 2 +- src/osgText/DefaultFont.h | 2 +- src/osgText/Font.cpp | 4 ++-- src/osgText/Text.cpp | 12 +++++++----- 8 files changed, 37 insertions(+), 25 deletions(-) diff --git a/include/osgText/Font b/include/osgText/Font index 431894f09..6248c29e8 100644 --- a/include/osgText/Font +++ b/include/osgText/Font @@ -29,6 +29,13 @@ namespace osgText { class Font; class Text; +enum KerningType +{ + KERNING_DEFAULT, //default locked to integer kerning values + KERNING_UNFITTED, //use floating point value for kerning + KERNING_NONE, //no kerning +}; + /** read a font from specified file.*/ extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename); @@ -62,12 +69,12 @@ public: unsigned int getWidth(); unsigned int getHeight(); + /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/ + virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType); + /** Get a Glyph for specified charcode, and the font size nearest to the current font size hint.*/ virtual Glyph* getGlyph(unsigned int charcode); - /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/ - virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode); - /** Return true if this font provides vertical alignments and spacing or glyphs.*/ virtual bool hasVertical() const; @@ -149,7 +156,7 @@ public: virtual Glyph* getGlyph(unsigned int charcode) = 0; /** Get a kerning (adjustment of spacing of two adjacent character) for specified charcodes, w.r.t the current font size hint.*/ - virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode) = 0; + virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType) = 0; /** Return true if this font provides vertical alignments and spacing or glyphs.*/ virtual bool hasVertical() const = 0; @@ -166,7 +173,6 @@ public: Font* _facade; }; - class OSGTEXT_EXPORT GlyphTexture : public osg::Texture2D { public: diff --git a/include/osgText/Text b/include/osgText/Text index 9d7d9abdf..0d95cc0be 100644 --- a/include/osgText/Text +++ b/include/osgText/Text @@ -211,7 +211,10 @@ public: unsigned int getDrawMode() const { return _drawMode; } - + void setKerningType(KerningType kerningType) { _kerningType = kerningType; } + + KerningType getKerningType() const { return _kerningType; } + /** Draw the text.*/ virtual void drawImplementation(osg::State& state) const; @@ -266,13 +269,12 @@ public: typedef std::map,GlyphQuads> TextureGlyphQuadMap; /** Direct Access to GlyphQuads */ - const GlyphQuads* getGlyphQuad(unsigned int index) const + const GlyphQuads* getGlyphQuads(osg::StateSet* stateSet) const { - if (index>=_textureGlyphQuadMap.size()) return NULL; + TextureGlyphQuadMap::iterator itGlyphQuad = _textureGlyphQuadMap.find(stateSet); + if (itGlyphQuad == _textureGlyphQuadMap.end()) return NULL; - TextureGlyphQuadMap::const_iterator itGlyph = _textureGlyphQuadMap.begin(); - while((index--) && (itGlyph!=_textureGlyphQuadMap.end())) itGlyph++; - return &itGlyph->second; + return &itGlyphQuad->second; } protected: @@ -303,6 +305,7 @@ protected: Layout _layout; osg::Vec4 _color; unsigned int _drawMode; + KerningType _kerningType; // iternal map used for rendering. Set up by the computeGlyphRepresentation() method. mutable TextureGlyphQuadMap _textureGlyphQuadMap; diff --git a/src/osgPlugins/freetype/FreeTypeFont.cpp b/src/osgPlugins/freetype/FreeTypeFont.cpp index c38572976..8fed89968 100644 --- a/src/osgPlugins/freetype/FreeTypeFont.cpp +++ b/src/osgPlugins/freetype/FreeTypeFont.cpp @@ -12,7 +12,6 @@ */ #include "FreeTypeFont.h" -//#include FT_GLYPH_H #include #include @@ -126,10 +125,11 @@ osgText::Font::Glyph* FreeTypeFont::getGlyph(unsigned int charcode) } -osg::Vec2 FreeTypeFont::getKerning(unsigned int leftcharcode,unsigned int rightcharcode) +osg::Vec2 FreeTypeFont::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType kerningType) { - if (!FT_HAS_KERNING(_face)) return osg::Vec2(0.0f,0.0f); + if (!FT_HAS_KERNING(_face) || (kerningType == osgText::KERNING_NONE)) return osg::Vec2(0.0f,0.0f); + FT_Kerning_Mode mode = (kerningType==osgText::KERNING_DEFAULT) ? ft_kerning_default : ft_kerning_unfitted; // convert character code to glyph index FT_UInt left = FT_Get_Char_Index( _face, leftcharcode ); @@ -137,10 +137,11 @@ osg::Vec2 FreeTypeFont::getKerning(unsigned int leftcharcode,unsigned int rightc // get the kerning distances. FT_Vector kerning; + FT_Error error = FT_Get_Kerning( _face, // handle to face object left, // left glyph index right, // right glyph index - FT_KERNING_UNFITTED, // kerning mode + mode, // kerning mode &kerning ); // target vector if (error) diff --git a/src/osgPlugins/freetype/FreeTypeFont.h b/src/osgPlugins/freetype/FreeTypeFont.h index 0e7facfd6..73e7c1623 100644 --- a/src/osgPlugins/freetype/FreeTypeFont.h +++ b/src/osgPlugins/freetype/FreeTypeFont.h @@ -34,7 +34,7 @@ public: virtual osgText::Font::Glyph* getGlyph(unsigned int charcode); - virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode); + virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, osgText::KerningType _kerningType); virtual bool hasVertical() const; diff --git a/src/osgText/DefaultFont.cpp b/src/osgText/DefaultFont.cpp index ff01a1544..5616be481 100644 --- a/src/osgText/DefaultFont.cpp +++ b/src/osgText/DefaultFont.cpp @@ -80,7 +80,7 @@ Font::Glyph* DefaultFont::getGlyph(unsigned int charcode) } -osg::Vec2 DefaultFont::getKerning(unsigned int,unsigned int) +osg::Vec2 DefaultFont::getKerning(unsigned int,unsigned int, KerningType) { // no kerning on default font. return osg::Vec2(0.0f,0.0f); diff --git a/src/osgText/DefaultFont.h b/src/osgText/DefaultFont.h index dba3849ee..6616e12d2 100644 --- a/src/osgText/DefaultFont.h +++ b/src/osgText/DefaultFont.h @@ -35,7 +35,7 @@ public: virtual Font::Glyph* getGlyph(unsigned int charcode); - virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode); + virtual osg::Vec2 getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType); virtual bool hasVertical() const; diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index f8c4af284..2d7d27e2d 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -196,9 +196,9 @@ Font::Glyph* Font::getGlyph(unsigned int charcode) else return 0; } -osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode) +osg::Vec2 Font::getKerning(unsigned int leftcharcode,unsigned int rightcharcode, KerningType kerningType) { - if (_implementation.valid()) return _implementation->getKerning(leftcharcode,rightcharcode); + if (_implementation.valid()) return _implementation->getKerning(leftcharcode,rightcharcode,kerningType); else return osg::Vec2(0.0f,0.0f); } diff --git a/src/osgText/Text.cpp b/src/osgText/Text.cpp index 87ee957b2..9812dea84 100644 --- a/src/osgText/Text.cpp +++ b/src/osgText/Text.cpp @@ -35,7 +35,8 @@ Text::Text(): _autoRotateToScreen(false), _layout(LEFT_TO_RIGHT), _color(1.0f,1.0f,1.0f,1.0f), - _drawMode(TEXT) + _drawMode(TEXT), + _kerningType(KERNING_DEFAULT) { setUseDisplayList(false); } @@ -57,7 +58,8 @@ Text::Text(const Text& text,const osg::CopyOp& copyop): _autoRotateToScreen(text._autoRotateToScreen), _layout(text._layout), _color(text._color), - _drawMode(text._drawMode) + _drawMode(text._drawMode), + _kerningType(text._kerningType) { computeGlyphRepresentation(); } @@ -310,14 +312,14 @@ void Text::computeGlyphRepresentation() { case LEFT_TO_RIGHT: { - osg::Vec2 delta(activefont->getKerning(previous_charcode,charcode)); + osg::Vec2 delta(activefont->getKerning(previous_charcode,charcode,_kerningType)); cursor.x() += delta.x() * wr; cursor.y() += delta.y() * hr; break; } case RIGHT_TO_LEFT: { - osg::Vec2 delta(activefont->getKerning(charcode,previous_charcode)); + osg::Vec2 delta(activefont->getKerning(charcode,previous_charcode,_kerningType)); cursor.x() -= delta.x() * wr; cursor.y() -= delta.y() * hr; break; @@ -526,7 +528,7 @@ void Text::computePositions(unsigned int contextID) const float pixelSizeVector_w = M(3,2)*P23 + M(3,3)*P33; - float pixelSize = (_characterHeight*sqrtf(scale_00.length2()+scale_10.length2()))/(pixelSizeVector_w*0.701f); + float pixelSize = (_characterHeight*sqrtf(scale_00.length2()+scale_10.length2()))/(pixelSizeVector_w*0.701f); if (_characterSizeMode==SCREEN_COORDS) {