0022792: Globally defined symbol PI conflicts with VTK definition (Intel compiler)
[occt.git] / src / Graphic2d / Graphic2d_Text.cxx
1
2 #define PERF    //GG_200898
3 //              The MinMax are now computed at the right time and no more
4 //              soon an attribute has change 
5 //              (see the redefined method ComputeMinMax()).
6
7 #define BUC60583        //GG_300999 Enable to compute correctly a
8 //                      Mirror transformation
9
10 #include <Graphic2d_Text.ixx>
11 #include <Aspect_WindowDriver.hxx>
12
13 #include <Graphic2d_Primitive.pxx>
14
15 Graphic2d_Text::Graphic2d_Text
16    (const Handle(Graphic2d_GraphicObject)& aGraphicObject,
17     const TCollection_ExtendedString& aText,
18     const Standard_Real X, const Standard_Real Y,
19     const Quantity_PlaneAngle anAngle,
20     const Aspect_TypeOfText aType,
21     const Quantity_Factor aScale)
22     : Graphic2d_Primitive (aGraphicObject),
23       myFontIndex (0),
24       myX(Standard_ShortReal(X)),
25       myDx(Standard_ShortReal(0.)),
26       myY(Standard_ShortReal(Y)),
27       myDy(Standard_ShortReal(0.)),
28       myAngle(Standard_ShortReal(anAngle)),
29       myType(aType),
30       myText(aText),
31       myAdjustFlag(Standard_False), 
32       myDeltax(Standard_ShortReal(0.)), myDeltay(Standard_ShortReal(0.)),
33       mySlant(Standard_ShortReal(0.)),
34       myIsZoomable(Standard_True),
35       myIsUnderlined(Standard_False),
36       myHScale(Standard_ShortReal(aScale)),
37       myWScale(Standard_ShortReal(aScale)),
38       myAlignment(Graphic2d_TOA_LEFT)
39 #ifndef PERF
40       ,myResetFlag(Standard_True),
41 #endif
42 {
43
44       SetFamily(Graphic2d_TOP_TEXT);
45
46       while (myAngle < 0.) myAngle += Standard_ShortReal(2. * M_PI);
47       while (myAngle >= 2. * M_PI) myAngle -= Standard_ShortReal(2. * M_PI);
48
49 #ifndef PERF
50       this->ComputeMinMax();
51 #endif
52
53 }
54
55 void Graphic2d_Text::SetFontIndex (const Standard_Integer anIndex) {
56
57         myFontIndex = anIndex;
58 #ifdef PERF
59         myMinX = myMinY = ShortRealLast ();
60         myMaxX = myMaxY = ShortRealFirst ();
61 #else
62         myResetFlag = Standard_True;
63         this->ComputeMinMax();
64 #endif
65
66 }
67
68 void Graphic2d_Text::SetSlant (const Quantity_PlaneAngle aSlant) {
69
70         mySlant = Standard_ShortReal(aSlant);
71         while (mySlant < 0.) mySlant += Standard_ShortReal(2. * M_PI);
72         while (mySlant >= 2. * M_PI) mySlant -= Standard_ShortReal(2. * M_PI);
73 #ifdef PERF
74         myMinX = myMinY = ShortRealLast ();
75         myMaxX = myMaxY = ShortRealFirst ();
76 #else
77         myResetFlag = Standard_True;
78         this->ComputeMinMax();
79 #endif
80 }
81
82 Standard_Boolean Graphic2d_Text::IsZoomable () const {
83
84         return myIsZoomable;
85
86 }
87
88 Standard_Boolean Graphic2d_Text::IsUnderlined () const {
89
90         return myIsUnderlined;
91
92 }
93
94 void Graphic2d_Text::SetZoomable (const Standard_Boolean aFlag) {
95
96         myIsZoomable    = aFlag;
97 #ifdef PERF
98         myMinX = myMinY = ShortRealLast ();
99         myMaxX = myMaxY = ShortRealFirst ();
100 #else
101         myResetFlag = Standard_True;
102         this->ComputeMinMax();
103 #endif
104
105 }
106
107 void Graphic2d_Text::SetUnderline (const Standard_Boolean aFlag) {
108
109         myIsUnderlined   = aFlag;
110 #ifdef PERF
111         myMinX = myMinY = ShortRealLast ();
112         myMaxX = myMaxY = ShortRealFirst ();
113 #else
114         myResetFlag = Standard_True;
115         this->ComputeMinMax();
116 #endif
117
118 }
119
120 void Graphic2d_Text::SetOffset (const Standard_Real aDx, const Standard_Real aDy) {
121
122         myDeltax = Standard_ShortReal(aDx);
123         myDeltay = Standard_ShortReal(aDy);
124 #ifdef PERF
125         myMinX = myMinY = ShortRealLast ();
126         myMaxX = myMaxY = ShortRealFirst ();
127 #else
128         myResetFlag = Standard_True;
129         this->ComputeMinMax();
130 #endif
131 }
132
133 void Graphic2d_Text::SetAlignment (const Graphic2d_TypeOfAlignment anAlignment) {
134
135         myAlignment   = anAlignment;
136 #ifdef PERF
137         myMinX = myMinY = ShortRealLast ();
138         myMaxX = myMaxY = ShortRealFirst ();
139 #else
140         myResetFlag = Standard_True;
141         this->ComputeMinMax();
142 #endif
143 }
144
145 Graphic2d_TypeOfAlignment Graphic2d_Text::Alignment () const {
146
147         return myAlignment;
148
149 }
150
151 void Graphic2d_Text::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
152 Standard_Boolean IsIn = Standard_False;
153 Standard_ShortReal hscale = (myIsZoomable) ? Standard_ShortReal(myHScale * aDrawer->Scale()) : Standard_ShortReal(myHScale);
154 Standard_ShortReal wscale = (myIsZoomable) ? Standard_ShortReal(myWScale * aDrawer->Scale()) : Standard_ShortReal(myWScale);
155 Standard_ShortReal ox = aDrawer->ConvertMapToFrom(myDeltax);
156 Standard_ShortReal oy = aDrawer->ConvertMapToFrom(myDeltay);
157
158 #ifdef PERF
159         if( (myMaxX < myMinX) || (myMaxY < myMinY) ) {
160           if( !ComputeMinMax() ) return;
161         }
162 #else
163         if( myResetFlag ) {
164           ComputeMinMax();
165         }
166 #endif
167  
168         if (! myGOPtr->IsTransformed ()) {
169           IsIn = aDrawer->IsIn (myMinX+ox,myMaxX+ox,myMinY+oy,myMaxY+oy);
170           if (IsIn) {
171             aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
172                                         mySlant,hscale,wscale,myIsUnderlined);
173             Standard_ShortReal dx = myDx,dy = myDy;
174             if( myAngle != 0. ) {
175               Standard_ShortReal cosa = Standard_ShortReal(Cos(myAngle));
176               Standard_ShortReal sina = Standard_ShortReal(Sin(myAngle));
177
178               dx = XROTATE(myDx,myDy) ;
179               dy = YROTATE(myDx,myDy) ;
180             }
181             aDrawer->MapTextFromTo(myText,myX+dx,myY+dy,myAngle,
182                                                 myDeltax,myDeltay,myType);
183           }
184         } else {
185           gp_GTrsf2d aTrsf = myGOPtr->Transform ();
186           Standard_ShortReal minx,miny,maxx,maxy;
187           MinMax(minx,maxx,miny,maxy);
188           IsIn = aDrawer->IsIn (minx+ox,maxx+ox,miny+oy,maxy+oy);
189           if (IsIn) {
190             Standard_Real A = Standard_Real (myX); 
191             Standard_Real B = Standard_Real (myY);
192             Standard_Real C = Standard_Real (myAngle);
193             Standard_Real cosa = Cos (C);
194             Standard_Real sina = Sin (C);
195             aTrsf.Transforms (A, B);
196             Standard_ShortReal a = Standard_ShortReal (A);   
197             Standard_ShortReal b = Standard_ShortReal (B);
198 #ifdef BUC60583
199             aTrsf.Transforms (cosa, sina);
200             A = B = 0.;
201             aTrsf.Transforms (A, B);
202             cosa -= A; sina -= B;
203             if( aTrsf.IsNegative() ) hscale = - hscale;
204 #else
205             // To calculate new aperture angles 
206             // calculation is done on the trigonometric circle
207             // and in this case translation is not taken into account.
208             aTrsf.SetValue (1, 3, 0.0);
209             aTrsf.SetValue (2, 3, 0.0);
210             aTrsf.Transforms (cosa, sina);
211 #endif
212             Standard_ShortReal angle = Standard_ShortReal (atan2(sina,cosa));
213             if( myIsZoomable ) {
214               hscale *= Standard_ShortReal (Sqrt(cosa*cosa + sina*sina));
215               wscale *= Standard_ShortReal (Sqrt(cosa*cosa + sina*sina));
216             }
217  
218             aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
219                                         mySlant,hscale,wscale,myIsUnderlined);
220             Standard_ShortReal dx = myDx,dy = myDy;
221             if( angle != 0. ) {
222               dx = Standard_ShortReal(XROTATE(myDx,myDy));
223               dy = Standard_ShortReal(YROTATE(myDx,myDy));
224             }
225             a += dx; b += dy;
226             aDrawer->MapTextFromTo(myText,a,b,angle,myDeltax,myDeltay,myType);
227           }
228         }
229 }
230
231
232 Standard_Boolean Graphic2d_Text::Pick (const Standard_ShortReal X,
233                                        const Standard_ShortReal Y,
234                                        const Standard_ShortReal aPrecision,
235                                        const Handle(Graphic2d_Drawer)& aDrawer) 
236 {
237 Standard_Boolean theStatus = Standard_False;
238 Standard_ShortReal width,height,xoffset,yoffset;
239 Standard_ShortReal hscale = (myIsZoomable) ? Standard_ShortReal(myHScale * aDrawer->Scale()) : Standard_ShortReal(myHScale);
240 Standard_ShortReal wscale = (myIsZoomable) ? Standard_ShortReal(myWScale * aDrawer->Scale()) : Standard_ShortReal(myWScale);
241 Standard_ShortReal TX = X, TY = Y;
242 Standard_ShortReal ox = aDrawer->ConvertMapToFrom(myDeltax);
243 Standard_ShortReal oy = aDrawer->ConvertMapToFrom(myDeltay);
244
245   if (IsInMinMax (X-ox, Y-oy, aPrecision)) {
246     if (myGOPtr->IsTransformed ()) {
247         gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted ();
248         Standard_Real RX = Standard_Real (X), RY = Standard_Real (Y);
249         aTrsf.Transforms (RX, RY); 
250         TX = Standard_ShortReal (RX); TY = Standard_ShortReal (RY);
251     }
252   
253     aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
254                                         mySlant,hscale,wscale,myIsUnderlined);
255     if( !aDrawer->GetTextSize(myText,width,height,xoffset,yoffset) ) {
256       width = height = xoffset = yoffset = 0.;
257     }
258     Standard_ShortReal cosa = Standard_ShortReal(Cos(-myAngle));
259     Standard_ShortReal sina = Standard_ShortReal(Sin(-myAngle));
260     Standard_ShortReal dx = TX-(myX+ox+myDx);
261     Standard_ShortReal dy = TY-(myY+oy+myDy);
262     Standard_ShortReal x = XROTATE(dx,dy);
263     Standard_ShortReal y = YROTATE(dx,dy);
264
265     theStatus =  (x >= -aPrecision + xoffset)            
266             &&   (x <= width + xoffset + aPrecision)
267             &&   (y >= -yoffset - aPrecision) 
268             &&   (y <= height - yoffset + aPrecision);
269   }
270
271   return theStatus;
272 }
273
274 Standard_Boolean Graphic2d_Text::TextSize ( Quantity_Length &aWidth,
275                                             Quantity_Length &aHeight) const {
276 Handle(Graphic2d_Drawer) aDrawer = Drawer(); 
277
278     if( !aDrawer.IsNull() && aDrawer->IsWindowDriver() ) {
279         Standard_ShortReal hscale = (myIsZoomable) ? Standard_ShortReal(myHScale * aDrawer->Scale()) : Standard_ShortReal(myHScale);
280         Standard_ShortReal wscale = (myIsZoomable) ? Standard_ShortReal(myWScale * aDrawer->Scale()) : Standard_ShortReal(myWScale);
281         Standard_ShortReal width,height;
282         aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
283                                         mySlant,hscale,wscale,myIsUnderlined);
284
285         aDrawer->GetTextSize(myText,width,height);
286         aWidth = width;
287         aHeight = height;
288         return Standard_True;
289     } else {
290         aWidth = aHeight = 0.;
291         return Standard_False;
292     }
293
294 }
295
296 Standard_Boolean Graphic2d_Text::TextSize ( Quantity_Length &aWidth,
297                                             Quantity_Length &aHeight,
298                                             Quantity_Length &anXoffset,
299                                             Quantity_Length &anYoffset) const {
300 Handle(Graphic2d_Drawer) aDrawer = Drawer(); 
301
302     if( !aDrawer.IsNull() && aDrawer->IsWindowDriver() ) {
303         Standard_ShortReal hscale = (myIsZoomable) ? Standard_ShortReal(myHScale * aDrawer->Scale()) : Standard_ShortReal(myHScale);
304         Standard_ShortReal wscale = (myIsZoomable) ? Standard_ShortReal(myWScale * aDrawer->Scale()) : Standard_ShortReal(myWScale);
305         Standard_ShortReal width,height,xoffset,yoffset;
306         aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
307                                         mySlant,hscale,wscale,myIsUnderlined);
308
309         aDrawer->GetTextSize(myText,width,height,xoffset,yoffset);
310         aWidth = width;
311         aHeight = height;
312         anXoffset = xoffset;
313         anYoffset = yoffset;
314         return Standard_True;
315     } else {
316         aWidth = aHeight = anXoffset = anYoffset = 0.;
317         return Standard_False;
318     }
319
320 }
321
322 void Graphic2d_Text::Position( Quantity_Length &X,
323                                Quantity_Length &Y) const {
324 Handle(Graphic2d_Drawer) aDrawer = Drawer();
325  
326     X = myX;
327     Y = myY;
328     if( !aDrawer.IsNull() ) {
329         X += aDrawer->ConvertMapToFrom(myDeltax);
330         Y += aDrawer->ConvertMapToFrom(myDeltay);
331     }    
332 }
333
334 void Graphic2d_Text::Offset( Standard_Real &X,
335                              Standard_Real &Y) const {
336
337         X = myDeltax;
338         Y = myDeltay;
339 }
340
341 Quantity_PlaneAngle Graphic2d_Text::Slant() const {
342
343         return mySlant;
344 }
345
346 Quantity_PlaneAngle Graphic2d_Text::Angle() const {
347
348         return myAngle;
349 }
350
351 Standard_Integer Graphic2d_Text::FontIndex() const {
352
353         return myFontIndex;
354 }
355
356 Quantity_Factor Graphic2d_Text::Scale() const {
357
358         return myHScale;
359 }
360
361 Standard_Boolean Graphic2d_Text::Fit(const Quantity_Length aWidth,const Quantity_Length aHeight,const Standard_Boolean Adjust,const Standard_Boolean Expand) {
362 Quantity_Length twidth,theight,txoffset,tyoffset;
363 Standard_ShortReal wscale,hscale;
364 Standard_Boolean status;
365
366     myAdjustFlag = Adjust;
367     if(( status = TextSize(twidth,theight,txoffset,tyoffset) )) {
368       wscale = Standard_ShortReal(aWidth/twidth);
369       if( wscale > 0. ) {
370         if( Expand || (twidth > aWidth) ) myWScale *= wscale;
371       }
372       hscale = Standard_ShortReal(aHeight/theight);
373       if( hscale > 0. ) myHScale *= hscale;
374 #ifdef PERF
375       myMinX = myMinY = ShortRealLast ();
376       myMaxX = myMaxY = ShortRealFirst ();
377 #else
378       myResetFlag = Standard_True;
379       this->ComputeMinMax();
380 #endif
381     }
382
383     return status;
384 }
385
386 Standard_Boolean Graphic2d_Text::Trunc(const Quantity_Length aWidth) {
387 Quantity_Length twidth,theight,txoffset,tyoffset;
388 //Standard_ShortReal wscale,hscale;
389 Standard_Boolean status;
390
391     if(( status = TextSize(twidth,theight,txoffset,tyoffset) )) {
392       Standard_Integer l = myText.Length();
393       while( (l > 1) && (twidth > aWidth) ) {
394         --l;
395         myText.Split(l); 
396         TextSize(twidth,theight,txoffset,tyoffset);
397       } 
398 #ifdef PERF
399       myMinX = myMinY = ShortRealLast ();
400       myMaxX = myMaxY = ShortRealFirst ();
401 #else
402       myResetFlag = Standard_True;
403       this->ComputeMinMax();
404 #endif
405     }
406
407     return status;
408 }
409
410 Standard_Boolean Graphic2d_Text::ComputeMinMax() {
411 Handle(Graphic2d_Drawer) aDrawer = Drawer(); 
412 Standard_Boolean status = Standard_False;
413
414     if( !aDrawer.IsNull() && aDrawer->IsWindowDriver() ) {
415           Standard_ShortReal hscale = 
416                 (myIsZoomable) ? Standard_ShortReal(myHScale * aDrawer->Scale()) : Standard_ShortReal(myHScale);
417           Standard_ShortReal wscale = 
418                 (myIsZoomable) ? Standard_ShortReal(myWScale * aDrawer->Scale()) : Standard_ShortReal(myWScale);
419           Standard_ShortReal width,height,xoffset,yoffset;
420           aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
421                                         mySlant,hscale,wscale,myIsUnderlined);
422           if(( status = 
423                 aDrawer->GetTextSize(myText,width,height,xoffset,yoffset) )) {
424 #ifndef PERF
425             myResetFlag = Standard_False;
426 #endif
427             switch( myAlignment ) {
428               case Graphic2d_TOA_LEFT:
429                 myDx = 0.; myDy = 0.;
430                 break;
431               case Graphic2d_TOA_RIGHT:
432                 myDx = -width; myDy = 0.;
433                 break;
434               case Graphic2d_TOA_CENTER:
435                 myDx = Standard_ShortReal(-width/2.); myDy = Standard_ShortReal(0.);
436                 break;
437               case Graphic2d_TOA_TOPLEFT:
438                 myDx = 0.; myDy = yoffset-height;
439                 break;
440               case Graphic2d_TOA_TOPRIGHT:
441                 myDx = -width; myDy = yoffset-height;
442                 break;
443               case Graphic2d_TOA_TOPCENTER:
444                 myDx = Standard_ShortReal(-width/2.); myDy = yoffset-height;
445                 break;
446               case Graphic2d_TOA_MEDIUMLEFT:
447                 myDx = 0.; myDy = Standard_ShortReal((yoffset-height)/2.);
448                 break;
449               case Graphic2d_TOA_MEDIUMRIGHT:
450                 myDx = -width; myDy = Standard_ShortReal((yoffset-height)/2.);
451                 break;
452               case Graphic2d_TOA_MEDIUMCENTER:
453                 myDx = Standard_ShortReal(-width/2.); myDy = Standard_ShortReal((yoffset-height)/2.);
454                 break;
455               case Graphic2d_TOA_BOTTOMLEFT:
456                 myDx = 0.; myDy = yoffset;
457                 break;
458               case Graphic2d_TOA_BOTTOMRIGHT:
459                 myDx = -width; myDy = yoffset;
460                 break;
461               case Graphic2d_TOA_BOTTOMCENTER:
462                 myDx = Standard_ShortReal(-width/2.); myDy = yoffset;
463                 break;
464             }
465             if( myAdjustFlag ) {
466               myDx -= xoffset; myDy += yoffset;
467             }
468             Standard_ShortReal xmin = myDx + xoffset;
469             Standard_ShortReal ymin = myDy - yoffset;
470             Standard_ShortReal xmax = xmin + width;
471             Standard_ShortReal ymax = ymin + height;
472
473             myMinX = myMinY = ShortRealLast();
474             myMaxX = myMaxY = ShortRealFirst();
475             if( myAngle != 0. ) {
476               Standard_ShortReal cosa = Standard_ShortReal(Cos(myAngle));
477               Standard_ShortReal sina = Standard_ShortReal(Sin(myAngle));
478               Standard_ShortReal dx,dy ;
479
480               dx = XROTATE(xmin,ymin) ;
481               dy = YROTATE(xmin,ymin) ;
482               myMinX = Min(myMinX,myX+dx) ;
483               myMinY = Min(myMinY,myY+dy) ;
484               myMaxX = Max(myMaxX,myX+dx) ;
485               myMaxY = Max(myMaxY,myY+dy) ;
486
487               dx = XROTATE(xmax,ymin) ;
488               dy = YROTATE(xmax,ymin) ;
489               myMinX = Min(myMinX,myX+dx) ;
490               myMinY = Min(myMinY,myY+dy) ;
491               myMaxX = Max(myMaxX,myX+dx) ;
492               myMaxY = Max(myMaxY,myY+dy) ;
493
494               dx = XROTATE(xmax,ymax) ;
495               dy = YROTATE(xmax,ymax) ;
496               myMinX = Min(myMinX,myX+dx) ;
497               myMinY = Min(myMinY,myY+dy) ;
498               myMaxX = Max(myMaxX,myX+dx) ;
499               myMaxY = Max(myMaxY,myY+dy) ;
500
501               dx = XROTATE(xmin,ymax) ;
502               dy = YROTATE(xmin,ymax) ;
503               myMinX = Min(myMinX,myX+dx) ;
504               myMinY = Min(myMinY,myY+dy) ;
505               myMaxX = Max(myMaxX,myX+dx) ;
506               myMaxY = Max(myMaxY,myY+dy) ;
507             } else {
508               myMinX = Min(myMinX,myX+xmin) ;
509               myMinY = Min(myMinY,myY+ymin) ;
510               myMaxX = Max(myMaxX,myX+xmax) ;
511               myMaxY = Max(myMaxY,myY+ymax) ;
512             }
513           }
514         }
515 #ifdef PERF
516          else {
517           cout << "*Graphic2d_Text::ComputeMinMax() returns wrong values*" << endl;
518         }
519 #endif
520
521         return status;
522 }
523
524
525 TCollection_ExtendedString Graphic2d_Text::GetText() const {
526
527     return myText;
528 }
529
530 Aspect_TypeOfText Graphic2d_Text::GetType() const {
531     
532     return myType;
533 }
534
535 void Graphic2d_Text::Save(Aspect_FStream& aFStream) const
536 {
537 }
538  
539