From 614b69432a5b88da70f4611d22d61373cb2f24c9 Mon Sep 17 00:00:00 2001 From: abd Date: Fri, 16 Dec 2005 15:49:23 +0000 Subject: [PATCH] Path of resizable text --- src/GLViewer/GLViewer_Drawer.cxx | 56 +++++++++++++++++++++++++++----- src/GLViewer/GLViewer_Drawer.h | 50 ++++++++++++++++++++++++---- src/GLViewer/GLViewer_Object.h | 2 +- 3 files changed, 92 insertions(+), 16 deletions(-) diff --git a/src/GLViewer/GLViewer_Drawer.cxx b/src/GLViewer/GLViewer_Drawer.cxx index 841fb6b84..59173b3bd 100644 --- a/src/GLViewer/GLViewer_Drawer.cxx +++ b/src/GLViewer/GLViewer_Drawer.cxx @@ -47,6 +47,8 @@ GLint status; GLViewer_TexFont* staticGlFont; +//static float text_scale_factor = 32.; + //================================================================ // Class : GLViewer_TexFont // Description : @@ -139,7 +141,11 @@ void GLViewer_TexFont::generateTexture() QFontMetrics aFM( myQFont ); GLViewer_TexFindId aFindFont; - aFindFont.myFontString = myQFont.toString(); + aFindFont.myFontFamily = myQFont.family();//myQFont.toString(); + aFindFont.myIsBold = myQFont.bold(); + aFindFont.myIsItal = myQFont.italic(); + aFindFont.myIsUndl = myQFont.underline(); + aFindFont.myPointSize = myQFont.pointSize(); aFindFont.myViewPortId = (int)QGLContext::currentContext(); if( TexFontBase.contains( aFindFont ) ) @@ -234,7 +240,7 @@ void GLViewer_TexFont::generateTexture() // Function: drawString // Purpose : //======================================================================= -void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY ) +void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY, GLfloat theScale ) { double aXScale = 1., aYScale = 1.; // store attributes @@ -244,9 +250,9 @@ void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY { glGetDoublev (GL_MODELVIEW_MATRIX, modelMatrix); aXScale = modelMatrix[0]; - aYScale = modelMatrix[5]; - } - + aYScale = modelMatrix[5]; + } + glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); @@ -266,7 +272,14 @@ void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY glBindTexture(GL_TEXTURE_2D, myTexFont); glBegin(GL_QUADS); + if ( myIsResizeable && theScale > 0. ) + { + aXScale = aXScale / theScale; + aYScale = aYScale / theScale; + } + theY = theY - ( myTexFontHeight - QFontMetrics( myQFont ).height() ) / aYScale; + double aLettBegin, aLettEnd, aDY = ( myTexFontHeight - 1 ) / aYScale, aDX; char aLetter; @@ -327,7 +340,11 @@ static GLuint displayListBase( QFont* theFont ) GLuint aList = 0; //static QMap fontCache; GLViewer_TexFindId aFindFont; - aFindFont.myFontString = theFont->toString(); + aFindFont.myFontFamily = theFont->family();//theFont->toString(); + aFindFont.myIsBold = theFont->bold(); + aFindFont.myIsItal = theFont->italic(); + aFindFont.myIsUndl = theFont->underline(); + aFindFont.myPointSize = theFont->pointSize(); #ifdef WIN32 HGLRC ctx = ::wglGetCurrentContext(); @@ -445,6 +462,7 @@ GLViewer_Drawer::GLViewer_Drawer() myObjectType = "GLViewer_Object"; myPriority = 0; myTextFormat = DTF_BITMAP; + myTextScale = 0.125; } //====================================================================== @@ -717,7 +735,10 @@ void GLViewer_Drawer::drawText( const QString& text, GLfloat xPos, GLfloat yPos, { GLViewer_TexFont aTexFont( theFont, theSeparator, theFormat == DTF_TEXTURE_SCALABLE, GL_LINEAR ); aTexFont.generateTexture(); - aTexFont.drawString( text, xPos, yPos ); + if ( theFormat == DTF_TEXTURE_SCALABLE ) + aTexFont.drawString( text, xPos, yPos, textScale() ); + else + aTexFont.drawString( text, xPos, yPos ); } else { @@ -758,9 +779,11 @@ void GLViewer_Drawer::drawGLText( QString text, float x, float y, if( smallFont ) aFont.setPointSize( aFont.pointSize() * 0.8 ); + GLfloat scale = textScale() > 0. ? textScale() : 1.; + QFontMetrics aFontMetrics( aFont ); - float width = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.width( text ) : aFontMetrics.width( text ) / myXScale; - float height = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.height() : aFontMetrics.height() / myYScale; + float width = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.width( text ) * scale : aFontMetrics.width( text ) / myXScale; + float height = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.height() * scale : aFontMetrics.height() / myYScale; float gap = 5 / myXScale; switch( hPosition ) @@ -782,6 +805,21 @@ void GLViewer_Drawer::drawGLText( QString text, float x, float y, drawText( text, x, y, color, &aFont, 2, myTextFormat ); } +//====================================================================== +// Function: textRect +// Purpose : +//======================================================================= +GLViewer_Rect GLViewer_Drawer::textRect( const QString& text ) const +{ + GLfloat scale = textScale() > 0. ? textScale() : 1.; + + QFontMetrics aFontMetrics( myFont ); + float width = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.width( text ) * scale : aFontMetrics.width( text ); + float height = myTextFormat == DTF_TEXTURE_SCALABLE ? aFontMetrics.height() * scale : aFontMetrics.height(); + + return GLViewer_Rect( 0, width, height, 0 ); +} + //====================================================================== // Function: drawRectangle // Purpose : diff --git a/src/GLViewer/GLViewer_Drawer.h b/src/GLViewer/GLViewer_Drawer.h index 56bbdbbec..a5589749a 100644 --- a/src/GLViewer/GLViewer_Drawer.h +++ b/src/GLViewer/GLViewer_Drawer.h @@ -67,15 +67,33 @@ struct GLVIEWER_API GLViewer_TexIdStored */ struct GLVIEWER_API GLViewer_TexFindId { - //! Font description - QString myFontString; + //! Font family description + QString myFontFamily; + //! Bold parameter + bool myIsBold; + //! Italic parameter + bool myIsItal; + //! Underline parameter + bool myIsUndl; + //! Font Size + int myPointSize; //! View POrt ID int myViewPortId; //! Overloaded operator for using struct as MAP key bool operator < (const GLViewer_TexFindId theStruct) const { - if ( myViewPortId != theStruct.myViewPortId ) return myViewPortId < theStruct.myViewPortId; - else return myFontString < theStruct.myFontString; + if ( myViewPortId != theStruct.myViewPortId ) + return myViewPortId < theStruct.myViewPortId; + else if ( myPointSize != theStruct.myPointSize ) + return myPointSize < theStruct.myPointSize; + else if ( myIsBold != theStruct.myIsBold ) + return myIsBold < theStruct.myIsBold; + else if ( myIsItal != theStruct.myIsItal ) + return myIsItal < theStruct.myIsItal; + else if ( myIsUndl != theStruct.myIsUndl ) + return myIsUndl < theStruct.myIsUndl; + else + return myFontFamily < theStruct.myFontFamily; } }; @@ -112,7 +130,10 @@ public: //! Generating font texture void generateTexture(); //! Drawing string theStr in point with coords theX and theY - void drawString( QString theStr, GLdouble theX = 0.0, GLdouble theY = 0.0 ); + void drawString( QString theStr, + GLdouble theX = 0.0, + GLdouble theY = 0.0, + GLfloat theScale = 1.0 ); //! Returns separator between letters int getSeparator(){ return mySeparator; } @@ -359,11 +380,24 @@ public: /*! *\param format - the default text displaying format */ - inline void setTextFormat( DisplayTextFormat format ) { myTextFormat = format; } + inline void setTextFormat( const DisplayTextFormat format ) { myTextFormat = format; } //! Returns a default text displaying format used by drawGLText method inline DisplayTextFormat textFormat() const { return myTextFormat; } + //! Sets a text string displaying scale factor (used only with text format DTF_TEXTURE_SCALABLE) + /*! + *\param factor - scale factor + */ + inline void setTextScale( const GLfloat factor ) { myTextScale = factor; } + + //! Returns a text string displaying scale factor + inline GLfloat textScale() const { return myTextScale; } + + //! Returns a rectangle of text (without viewer scale) + GLViewer_Rect textRect( const QString& ) const; + + //! Draw rectangle with predefined color static void drawRectangle( GLViewer_Rect* theRect, QColor = Qt::black ); @@ -390,6 +424,10 @@ protected: QFont myFont; //! Default text displaying format for drawGLText() method DisplayTextFormat myTextFormat; + + //! Scale factor for text string draw, by default 0.125 + //! (used only with text format DTF_TEXTURE_SCALABLE) + GLfloat myTextScale; }; #ifdef WNT diff --git a/src/GLViewer/GLViewer_Object.h b/src/GLViewer/GLViewer_Object.h index 39c9d9883..6cd21b294 100644 --- a/src/GLViewer/GLViewer_Object.h +++ b/src/GLViewer/GLViewer_Object.h @@ -185,7 +185,7 @@ public: virtual bool finishMove() { return true; } //! Returns visible object status - bool getVisible() const { return myIsVisible; } + virtual bool getVisible() const { return myIsVisible; } //! Installs visible object status virtual void setVisible( bool theStatus ) { myIsVisible = theStatus; } -- 2.39.2