Salome HOME
Updated copyright comment
[modules/gui.git] / src / GLViewer / GLViewer_Text.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  Author : OPEN CASCADE
24 //#include <GLViewerAfx.h>
25 //
26 #include "GLViewer_Text.h"
27
28 #include <QApplication>
29 #include <QFontMetrics>
30
31 /*!
32   Constructor
33 */
34 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color )
35 {
36   myText = text;
37   myXPos = xPos;
38   myYPos = yPos;
39   myColor = color;
40   myQFont = QApplication::font();//QFont::defaultFont();
41   mySeparator = 2;
42   myDTF = DTF_BITMAP;
43 }
44
45 /*!
46   Constructor
47 */
48 GLViewer_Text::GLViewer_Text( const QString& text, float xPos, float yPos, const QColor& color, QFont theFont, int theSeparator )
49 {
50   myText = text;
51   myXPos = xPos;
52   myYPos = yPos;
53   myColor = color;
54   myQFont = theFont;
55   mySeparator = theSeparator;
56   myDTF = DTF_BITMAP;
57 }
58
59 /*!
60   Destructor
61 */
62 GLViewer_Text::~GLViewer_Text()
63 {
64 }
65
66 /*!
67   \return width of text
68 */
69 int GLViewer_Text::getWidth()
70 {
71     int aResult = 0;
72     QFontMetrics aFM( myQFont );
73     for( int i = 0; i < myText.length(); i++ )
74         aResult += aFM.width( myText.at(i) ) + mySeparator;
75     return aResult;
76 }
77
78 /*!
79   \return height of text
80 */
81 int GLViewer_Text::getHeight()
82 {
83     QFontMetrics aFM( myQFont );
84     return aFM.height();
85 }
86
87 /*!
88   Codes object as byte copy
89   \return byte array
90 */
91 QByteArray GLViewer_Text::getByteCopy() const
92 {
93     int i;
94     int aSize = 5*sizeof( int ) + myText.length();
95
96     int aR = myColor.red();
97     int aG = myColor.green();
98     int aB = myColor.blue();
99     const char* aStr = myText.toLatin1().constData();
100
101     int anISize = sizeof( int );    
102     QByteArray aResult;
103     aResult.resize( aSize );
104
105     char* aPointer = (char*)&myXPos;
106     for( i = 0; i < anISize; i++, aPointer++ )
107         aResult[i] = *aPointer;
108     aPointer = (char*)&myYPos;
109     for( ; i < 2*anISize; i++, aPointer++ )
110         aResult[i] = *aPointer;
111
112     aPointer = (char*)&aR;
113     for( ; i < 3*anISize; i++, aPointer++ )
114         aResult[i] = *aPointer;
115     aPointer = (char*)&aG;
116     for( ; i < 4*anISize; i++, aPointer++ )
117         aResult[i] = *aPointer;
118     aPointer = (char*)&aB;
119     for( ; i < 5*anISize; i++, aPointer++ )
120         aResult[i] = *aPointer;
121
122     int aTextSize = myText.length();
123     aPointer = (char*)&aTextSize;
124     for( ; i < 6*anISize; i++, aPointer++ )
125         aResult[i] = *aPointer;
126
127     for( i = 0; i < aTextSize; i++ )
128         aResult[6*anISize + i] = aStr[i];
129
130     aPointer = (char*)&mySeparator;
131     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
132         aResult[i] = *aPointer;
133
134     const char* aFontStr = myQFont.toString().toLatin1().constData();
135     int aFontSize = myQFont.toString().length();
136
137     for( i = 0; i < aFontSize; i++ )
138         aResult[7*anISize + aTextSize + i] = aFontStr[i];
139
140     return aResult;
141 }
142
143 /*!
144   Initialize text from binary representation
145   \param theBuf - byte array
146 */
147 GLViewer_Text* GLViewer_Text::fromByteCopy( QByteArray theBuf )
148 {
149     int i = 0;
150     int aSize = (int)theBuf.size();
151     int aR = 0, aG = 0, aB = 0;
152
153     int xPos = 0, yPos = 0;
154
155     int anISize = sizeof( int );
156     char* aPointer = (char*)&xPos;
157     for ( i = 0; i < anISize; i++, aPointer++ )
158         *aPointer = theBuf[i];
159
160     aPointer = (char*)&yPos;
161     for ( ; i < 2*anISize; i++, aPointer++ )
162         *aPointer = theBuf[i];
163
164     aPointer = (char*)&aR;
165     for( ; i < 3*anISize; i++, aPointer++ )
166         *aPointer = theBuf[i];
167     aPointer = (char*)&aG;
168     for( ; i < 4*anISize; i++, aPointer++ )
169         *aPointer = theBuf[i];
170     aPointer = (char*)&aB;
171     for( ; i < 5*anISize; i++, aPointer++ )
172         *aPointer = theBuf[i];
173
174     int aTextSize = 0;
175     aPointer = (char*)&aTextSize;
176     for( ; i < 6*anISize; i++, aPointer++ )
177         *aPointer = theBuf[i];
178
179     QString aText;
180     for( ; i < 6*anISize + aTextSize; i++ )
181     {
182         QChar aChar( theBuf[i] );
183         aText += aChar;
184     }
185
186     int aSeparator = 0;
187     aPointer = (char*)&aSeparator;
188     for( ; i < 7*anISize + aTextSize; i++, aPointer++ )
189         *aPointer = theBuf[i];
190
191     QString aFontStr;
192     for( ; i < aSize; i++ )
193     {
194         QChar aChar( theBuf[i] );
195         aFontStr += aChar;
196     }
197     QFont aFont;
198
199     if( !aFont.fromString( aFontStr ) )
200         return NULL;    
201
202     GLViewer_Text* aGlText = new GLViewer_Text( aText, xPos, yPos, QColor( aR,aG,aB ), aFont, aSeparator  );
203
204     return aGlText;    
205 }