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