From 8d952f35c6ceec1a3077b8f40ed5b013a1b9b198 Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Tue, 3 May 2016 11:58:48 +0200 Subject: [PATCH] Fixed text vertical alignment for bitmap font (OpenGL). --- common/gal/opengl/opengl_gal.cpp | 21 ++++++++++++++------- include/gal/opengl/opengl_gal.h | 5 +++-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index b19fb14bd2..acd1e2995b 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -697,8 +697,10 @@ void OPENGL_GAL::BitmapText( const wxString& aText, const VECTOR2D& aPosition, // Compute text size, so it can be properly justified - VECTOR2D textSize = computeBitmapTextSize( aText ); - const double SCALE = GetGlyphSize().y / textSize.y * 2.0; + std::pair bbox = computeBitmapTextSize( aText ); + VECTOR2D textSize = bbox.first; + int commonOffset = bbox.second; + const double SCALE = GetGlyphSize().y / textSize.y; int tildas = 0; bool overbar = false; @@ -711,6 +713,7 @@ void OPENGL_GAL::BitmapText( const wxString& aText, const VECTOR2D& aPosition, currentManager->Translate( aPosition.x, aPosition.y, layerDepth ); currentManager->Rotate( aRotationAngle, 0.0f, 0.0f, -1.0f ); currentManager->Scale( SCALE, SCALE, 0 ); + currentManager->Translate( 0, -commonOffset, 0 ); switch( GetHorizontalJustify() ) { @@ -733,12 +736,12 @@ void OPENGL_GAL::BitmapText( const wxString& aText, const VECTOR2D& aPosition, { case GR_TEXT_VJUSTIFY_TOP: Translate( VECTOR2D( 0, -textSize.y ) ); - overbarHeight = 0; + overbarHeight = -textSize.y / 2.0; break; case GR_TEXT_VJUSTIFY_CENTER: Translate( VECTOR2D( 0, -textSize.y / 2.0 ) ); - overbarHeight = 0; //textSize.y; + overbarHeight = 0; break; case GR_TEXT_VJUSTIFY_BOTTOM: @@ -1342,9 +1345,10 @@ void OPENGL_GAL::drawBitmapOverbar( double aLength, double aHeight ) } -VECTOR2D OPENGL_GAL::computeBitmapTextSize( const wxString& aText ) const +std::pair OPENGL_GAL::computeBitmapTextSize( const wxString& aText ) const { VECTOR2D textSize( 0, 0 ); + int commonOffset = std::numeric_limits::max(); bool wasTilda = false; for( unsigned int i = 0; i < aText.length(); ++i ) @@ -1368,10 +1372,13 @@ VECTOR2D OPENGL_GAL::computeBitmapTextSize( const wxString& aText ) const const bitmap_glyph& GLYPH = bitmap_chars[aText[i]]; textSize.x += ( GLYPH.x_off + GLYPH.width ); - textSize.y = std::max( (unsigned int)( textSize.y ), GLYPH.y_off * 2 + GLYPH.height ); + textSize.y = std::max( (unsigned int)( textSize.y ), GLYPH.height + GLYPH.y_off ); + commonOffset= std::min( GLYPH.y_off, commonOffset ); } - return textSize; + textSize.y -= commonOffset; + + return std::make_pair( textSize, commonOffset ); } diff --git a/include/gal/opengl/opengl_gal.h b/include/gal/opengl/opengl_gal.h index ba0b97cd26..7fd63c43be 100644 --- a/include/gal/opengl/opengl_gal.h +++ b/include/gal/opengl/opengl_gal.h @@ -369,9 +369,10 @@ private: * @brief Computes a size of text drawn using bitmap font with current text setting applied. * * @param aText is the text to be drawn. - * @return Text size expressed in world coordinates. + * @return Pair containing text bounding box and common Y axis offset. The values are expressed + * as a number of pixels on the bitmap font texture and need to be scaled before drawing. */ - VECTOR2D computeBitmapTextSize( const wxString& aText ) const; + std::pair computeBitmapTextSize( const wxString& aText ) const; // Event handling /**