0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / WNT / WNT_TextManager.cxx
1 // File:        WNT_TextManager.cxx
2 // Created:     Feb 1998
3 // Author:      CHABROVSKY Dmitry
4 // Copyright:   Matra Datavision 1998
5
6 // include windows.h first to have all definitions available
7 #include <windows.h>
8
9 #define  MFT
10
11 #include <WNT_TextManager.ixx>
12 #include <WNT_MFTDraw.hxx>
13 #include <Aspect_Units.hxx>
14
15 #define CHAR_DESC_LEN 30000
16 #define MY_HDC (HDC)myDevContext
17 #define U2P(v) LONG( (v)/myPixelToUnit + 0.5 )
18 #define TRANSFORM(X,Y)                                           \
19         { Standard_Real x = X,y = Y;                             \
20           X = Standard_ShortReal(x*theCosAngle - y*theSinAngle); \
21           Y = Standard_ShortReal(x*theSinAngle + y*theCosAngle); \
22         }
23
24 enum TypeOfPoint {
25   TOP_MOVETO, TOP_LINETO, TOP_CURVETO
26 };
27
28 static Standard_Integer   thePaintType;
29 static Standard_Integer   theTextColor;
30 static Aspect_TypeOfText  theTypeOfText;
31 static Standard_ShortReal theUnderlinePosition;
32 static Standard_ShortReal theXmin,theXmax;
33 static Standard_Integer   theNchar;
34 static Standard_Real      theSinAngle,theCosAngle;
35 static Standard_Real      theOrientation;
36 //////////////// Character description ///////////////
37 static int                myStrLen  = 0;
38 static POINT              myStrDesc [CHAR_DESC_LEN];
39 static TypeOfPoint        myStrInfo [CHAR_DESC_LEN];
40 static double             myBX, myBY;
41 static double             myCX, myCY;
42 static int                myCharWidth;
43 static int                myCharHeight;
44 static BOOL               myCharInside;
45
46 #define ADD_POINT(X,Y,aType)                                                         \
47   myStrDesc[myStrLen].x = U2P( (X)+myBX );                                           \
48   myStrDesc[myStrLen].y = ( myWin95 ? myDevHeight - U2P((Y)+myBY) : U2P((Y)+myBY) ); \
49   myStrInfo[myStrLen]   = aType; myStrLen++;
50
51 #define CHECK_INSIDE(x,y)                                                 \
52   if (!myCharInside)                                                      \
53     if (myWin95) {                                                        \
54       if ((x > -myCharWidth  && x < (myDevWidth  /*+ myCharWidth*/ )) &&  \
55           (y > -myCharHeight && y < (myDevHeight + myCharHeight)))        \
56         myCharInside = TRUE;                                              \
57     } else {                                                              \
58       if ((x > -myCharWidth  && x < (myDevWidth  /*+ myCharWidth */)) &&  \
59           (y > -myCharHeight && y < (myDevHeight /*+ myCharHeight*/)))    \
60         myCharInside = TRUE;                                              \
61     }
62
63 #define CUR_X() myStrDesc[myStrLen-1].x
64 #define CUR_Y() myStrDesc[myStrLen-1].y
65
66 /*==================================================================*/
67 WNT_TextManager::WNT_TextManager(const Standard_Real aPixelToUnit)
68 {
69   myPixelToUnit = aPixelToUnit;
70   myDevContext  = 0;
71   myDevHeight   = 0;
72   // Clear the memory for STRING description
73   ZeroMemory (myStrDesc, sizeof(myStrDesc));
74   ZeroMemory (myStrInfo, sizeof(myStrInfo));
75   myStrLen = 0;
76 }
77
78 /*==================================================================*/
79 void WNT_TextManager::BeginString(const Quantity_Length X,
80                                   const Quantity_Length Y,
81                                   const Quantity_PlaneAngle anOrientation,
82                                   const Quantity_Length aWidth,
83                                   const Quantity_Length aHeight,
84                                   const Quantity_PlaneAngle aSlant,
85                                   const Standard_Integer aPaintType)
86 {
87   myStrLen       = 0;
88   myCharWidth    = U2P(aWidth );
89   myCharHeight   = U2P(aHeight);
90
91   thePaintType   = aPaintType;
92   theOrientation = anOrientation;
93   myBX           = X;
94   myBY           = Y;
95   theNchar       = 0;
96   theXmin        = theXmax = 0.F;
97   if (!thePaintType) {
98     if ((aHeight < (6. MILLIMETER)) || 
99                     (theTypeOfText == Aspect_TOT_OUTLINE))
100       thePaintType = 2;
101   }
102 }
103
104 /*==================================================================*/
105 Standard_Boolean WNT_TextManager::BeginChar(const Standard_Integer aCharCode,
106                                             const Standard_Real X,
107                                             const Standard_Real Y)
108 {
109   myCharInside = FALSE;
110   return Standard_True;
111 }
112
113 /*==================================================================*/
114 Standard_Boolean WNT_TextManager::SetCharBoundingBox(const Quantity_Length X1,
115                                                      const Quantity_Length Y1,
116                                                      const Quantity_Length X2,
117                                                      const Quantity_Length Y2,
118                                                      const Quantity_Length X3,
119                                                      const Quantity_Length Y3,
120                                                      const Quantity_Length X4,
121                                                      const Quantity_Length Y4)
122 {
123   if (theUnderlinePosition > 0.) {
124     if (!theNchar)
125       theXmin = Standard_ShortReal (X1);
126     theXmax   = Standard_ShortReal (Sqrt(X2*X2 + Y2*Y2));
127   }
128   return Standard_True;
129 }
130
131 /*==================================================================*/
132 Standard_Boolean WNT_TextManager::SetCharEncoding(const Standard_CString anEncoding)
133 {
134   return Standard_True;
135 }
136
137 /*==================================================================*/
138 Standard_Boolean WNT_TextManager::Moveto(const Standard_Real X,
139                                          const Standard_Real Y)
140 {
141   ADD_POINT (X,Y,TOP_MOVETO);
142   myCX = X; myCY = Y;
143   CHECK_INSIDE (CUR_X(),CUR_Y());
144   if (!myCharInside)
145     myStrLen--;
146   return myCharInside;
147 }
148
149 /*==================================================================*/
150 Standard_Boolean WNT_TextManager::Lineto(const Standard_Real X,
151                                          const Standard_Real Y)
152 {
153   ADD_POINT (X,Y,TOP_LINETO);
154   CHECK_INSIDE (CUR_X(),CUR_Y());
155   if (!myCharInside)
156     myStrLen--;
157   return myCharInside;
158 }
159
160 /*==================================================================*/
161 Standard_Boolean WNT_TextManager::Curveto(const Quantity_Length X1,
162                                           const Quantity_Length Y1,
163                                           const Quantity_Length X2,
164                                           const Quantity_Length Y2,
165                                           const Quantity_Length X3,
166                                           const Quantity_Length Y3,
167                                           const Quantity_Length X4,
168                                           const Quantity_Length Y4)
169 {
170   ADD_POINT (X2,Y2,TOP_CURVETO);
171   ADD_POINT (X3,Y3,TOP_CURVETO);
172   ADD_POINT (X4,Y4,TOP_CURVETO);
173   return Standard_True;
174 }
175
176 /*==================================================================*/
177 void WNT_TextManager::ClosePath()
178 {
179   ADD_POINT (myCX,myCY,TOP_LINETO);
180   CHECK_INSIDE (CUR_X(),CUR_Y());
181   if (!myCharInside)
182     myStrLen--;
183 }
184
185 /*==================================================================*/
186 Standard_Boolean WNT_TextManager::EndChar(const Standard_Real X,
187                                           const Standard_Real Y)
188 {
189   theNchar++;
190   return Standard_True;
191 }
192
193 /*==================================================================*/
194 void WNT_TextManager::EndString()
195 {
196   static int i, j;
197
198   // Draw text string
199   i = 0;
200   BeginPath (MY_HDC);
201   do {
202     switch (myStrInfo[i]) {
203       case TOP_MOVETO:
204         MoveToEx     (MY_HDC, myStrDesc[i].x, myStrDesc[i].y, NULL);
205         break;
206       case TOP_LINETO:
207         j = i+1;
208         while ((myStrInfo[j] == TOP_LINETO) && (j < myStrLen))
209           j++;
210         PolylineTo   (MY_HDC, &myStrDesc[i], j-i);
211         i = j-1;
212         break;
213       case TOP_CURVETO:
214         PolyBezierTo (MY_HDC, &myStrDesc[i], 3);
215         i += 2;
216         break;
217       default:
218         break;
219     }
220   } while (++i < myStrLen);
221   EndPath (MY_HDC);
222   if (thePaintType == 0 && !myMonoBuf) FillPath   (MY_HDC);
223   else                                 StrokePath (MY_HDC);
224
225   // Draw underline if necessary
226   if (theUnderlinePosition > 0.) {
227     Standard_ShortReal theX1 = theXmin;
228     Standard_ShortReal theY1 = -theUnderlinePosition;
229     Standard_ShortReal theX2 = theXmax;
230     Standard_ShortReal theY2 = theY1;
231     theSinAngle = Sin (theOrientation);
232     theCosAngle = Cos (theOrientation);
233     TRANSFORM (theX1, theY1);
234     TRANSFORM (theX2, theY2);
235     // Draw UNDERLINE
236     if (myWin95) {
237       MoveToEx     (MY_HDC, U2P(theX1 + myBX), myDevHeight - U2P(theY1 + myBY), NULL);
238       LineTo       (MY_HDC, U2P(theX2 + myBX), myDevHeight - U2P(theY2 + myBY)      );
239     } else {
240       MoveToEx     (MY_HDC, U2P(theX1 + myBX), U2P(theY1 + myBY), NULL);
241       LineTo       (MY_HDC, U2P(theX2 + myBX), U2P(theY2 + myBY)      );
242     }
243   }
244 }
245
246 /*==================================================================*/
247 void WNT_TextManager::SetTextAttribs(const Standard_Integer aTextColor,
248                                      const Aspect_TypeOfText aTypeOfText,
249                                      const Quantity_Length anUnderlinePosition)
250 {
251   theTextColor         = aTextColor;
252   theTypeOfText        = aTypeOfText;
253   theUnderlinePosition = (Standard_ShortReal)anUnderlinePosition;
254 }
255
256 /*==================================================================*/
257 void WNT_TextManager::SetDrawAttribs(const Standard_Address aDrawData)
258 {
259   PMFT_TEXTMAN_DATA aData = (PMFT_TEXTMAN_DATA)aDrawData;
260   myDevContext = (int)aData->theHDC;
261   myWin95      =      aData->theWin95;
262   myDevWidth   =      aData->theDevWidth;
263   myDevHeight  =      aData->theDevHeight;
264   myUWidth     =      aData->theUWidth;
265   myMonoBuf    =      aData->theMonoBuffer;
266 }