summaryrefslogtreecommitdiff
path: root/src/Graphic2d/Graphic2d_FramedText.cxx
blob: b63dce0600412dcb92116f7f27f8e78ba43ee2ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// S3824        10/06/98 : GG ;
//              1) Method "Fit" includes optional argument 
//                 "Expand" allowing to avoid adjusting text width
//                 when it is less then size of FIT.
//              2) Method "Trunc" allows trunkating the text to the 
//                 exact size.

#define PRO14304	//GG_160698

#define PERF    //GG_200898
//              The MinMax are now computed at the right time and no more
//              soon an attribute has change 
//              (see the redefined method ComputeMinMax()).

#define BUC60583        //GG_300999 Enable to compute correctly a
//                      Mirror transformation

#include <Graphic2d_FramedText.ixx>
#include <Aspect_WindowDriver.hxx>

#include <Graphic2d_Primitive.pxx>

Graphic2d_FramedText::Graphic2d_FramedText
   (const Handle(Graphic2d_GraphicObject)& aGraphicObject,
    const TCollection_ExtendedString& aText,
    const Standard_Real X, const Standard_Real Y,
    const Quantity_PlaneAngle anAngle,
    const Quantity_Ratio aMargin,
    const Aspect_TypeOfText aType,
    const Quantity_Factor aScale)
    : Graphic2d_Text (aGraphicObject,aText,X,Y,anAngle,aType,aScale),
	myMargin(Standard_ShortReal( aMargin )) {

    SetFrameColorIndex();
    SetFrameWidthIndex();

#ifndef PERF
    ComputeMinMax();
#endif
}

void Graphic2d_FramedText::SetFrameColorIndex (const Standard_Integer anIndex) {

        myFrameColorIndex = anIndex;
}

void Graphic2d_FramedText::SetFrameWidthIndex (const Standard_Integer anIndex) { 
        myFrameWidthIndex = anIndex;
 
}

void Graphic2d_FramedText::Draw (const Handle(Graphic2d_Drawer)& aDrawer) {
Standard_Boolean IsIn = Standard_False;
Standard_ShortReal hscale = (myIsZoomable) ? 
           Standard_ShortReal( myHScale * aDrawer->Scale() )
         : Standard_ShortReal( myHScale );
Standard_ShortReal wscale = (myIsZoomable) ? 
           Standard_ShortReal( myWScale * aDrawer->Scale() )
         : Standard_ShortReal( myWScale );
Standard_ShortReal ox = aDrawer->ConvertMapToFrom(myDeltax);
Standard_ShortReal oy = aDrawer->ConvertMapToFrom(myDeltay);

#ifdef PERF
        if( (myMaxX < myMinX) || (myMaxY < myMinY) ) {
	  if( !ComputeMinMax() ) return;
	}
#else
        if( myResetFlag ) {
	  ComputeMinMax();
        }   
#endif

        if (! myGOPtr->IsTransformed ()) {
          IsIn = aDrawer->IsIn (myMinX+ox,myMaxX+ox,myMinY+oy,myMaxY+oy);
          if (IsIn) {
            aDrawer->SetFramedTextAttrib(myColorIndex,myFrameColorIndex,
					myFrameWidthIndex,myFontIndex,
					mySlant,hscale,wscale,myIsUnderlined);
            Standard_ShortReal dx = myDx,dy = myDy;
            if( myAngle != 0. ) {
              Standard_ShortReal cosa = Standard_ShortReal( Cos(myAngle) );
              Standard_ShortReal sina = Standard_ShortReal( Sin(myAngle) );

              dx = XROTATE(myDx,myDy) ;
              dy = YROTATE(myDx,myDy) ;
            }
            aDrawer->MapFramedTextFromTo(myText,myX+dx,myY+dy,myAngle,myMargin,
                                                myDeltax,myDeltay,myType);
          }
        } else {
	  Standard_ShortReal minx,miny,maxx,maxy;
          gp_GTrsf2d aTrsf = myGOPtr->Transform ();
	  MinMax(minx,maxx,miny,maxy);
          IsIn = aDrawer->IsIn (minx+ox,maxx+ox,miny+oy,maxy+oy);
          if (IsIn) {
            Standard_Real A = Standard_Real (myX);
            Standard_Real B = Standard_Real (myY);
            Standard_Real C = Standard_Real (myAngle);
            Standard_Real cosa = Cos (C);
            Standard_Real sina = Sin (C);
            aTrsf.Transforms (A, B);

            Standard_ShortReal a = Standard_ShortReal (A);
            Standard_ShortReal b = Standard_ShortReal (B);
#ifdef BUC60583
            aTrsf.Transforms (cosa, sina);
            A = B = 0.;
            aTrsf.Transforms (A, B);
            cosa -= A; sina -= B;
            if( aTrsf.IsNegative() ) hscale = - hscale;
#else
	    // To calculate new aperture angles 
	    // the calculation is done on the trigonometric circle
	    // and taken into account in case of translation.
	    aTrsf.SetValue (1, 3, 0.0);
	    aTrsf.SetValue (2, 3, 0.0);
            aTrsf.Transforms (cosa, sina);
#endif
            Standard_ShortReal angle = Standard_ShortReal (atan2(sina,cosa));
            if( myIsZoomable ) {
              hscale *= Standard_ShortReal(Sqrt(cosa*cosa + sina*sina));
              wscale *= Standard_ShortReal (Sqrt(cosa*cosa + sina*sina));
            }

            aDrawer->SetFramedTextAttrib(myColorIndex,myFrameColorIndex,
					myFrameWidthIndex,myFontIndex,
					mySlant,hscale,wscale,myIsUnderlined);
            Standard_ShortReal dx = myDx,dy = myDy;
            if( angle != 0. ) {
              dx = Standard_ShortReal( XROTATE(myDx,myDy) );
              dy = Standard_ShortReal( YROTATE(myDx,myDy) );
            }
            a += dx; b += dy;
            aDrawer->MapFramedTextFromTo(myText,a,b,angle,myMargin,
                                                myDeltax,myDeltay,myType);
          }
        }
}


Standard_Boolean Graphic2d_FramedText::Pick (const Standard_ShortReal X,
				       const Standard_ShortReal Y,
				       const Standard_ShortReal aPrecision,
				       const Handle(Graphic2d_Drawer)& aDrawer) 
{
Standard_Boolean theStatus = Standard_False;
Standard_ShortReal width,height,xoffset,yoffset,mwidth,mheight;
Standard_ShortReal hscale = (myIsZoomable) ? 
         Standard_ShortReal( myHScale * aDrawer->Scale() )
       : Standard_ShortReal( myHScale );
Standard_ShortReal wscale = (myIsZoomable) ? 
         Standard_ShortReal( myWScale * aDrawer->Scale() )
       : Standard_ShortReal( myWScale );

Standard_ShortReal TX = X, TY = Y;
Standard_ShortReal ox = aDrawer->ConvertMapToFrom(myDeltax);
Standard_ShortReal oy = aDrawer->ConvertMapToFrom(myDeltay);

  if (IsInMinMax (X-ox, Y-oy, aPrecision)) {
    if (myGOPtr->IsTransformed ()) {
        gp_GTrsf2d aTrsf = (myGOPtr->Transform ()).Inverted ();
        Standard_Real RX = Standard_Real (X), RY = Standard_Real (Y);
        aTrsf.Transforms (RX, RY); 
        TX = Standard_ShortReal (RX); TY = Standard_ShortReal (RY);
    }
    aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
					mySlant,hscale,wscale,myIsUnderlined);
    if( !aDrawer->GetTextSize(myText,width,height,xoffset,yoffset) ) {
      mwidth = mheight = width = height = xoffset = yoffset = 0.;
    } else {
      mwidth = mheight = height*myMargin;   
    }
    Standard_ShortReal cosa = Standard_ShortReal( Cos(-myAngle) );
    Standard_ShortReal sina = Standard_ShortReal( Sin(-myAngle) );
    Standard_ShortReal dx = TX-(myX+ox+myDx);
    Standard_ShortReal dy = TY-(myY+oy+myDy);
    Standard_ShortReal x = XROTATE(dx,dy);
    Standard_ShortReal y = YROTATE(dx,dy);
 
    theStatus =  (x >=  -mwidth + xoffset - aPrecision)
            &&   (x <= width + mwidth  + xoffset + aPrecision)
            &&   (y >= - mheight - yoffset - aPrecision)
            &&   (y <= height + mheight - yoffset + aPrecision);
  }
   
  return theStatus;
}

Standard_Boolean Graphic2d_FramedText::TextSize ( Quantity_Length &aWidth, 
                                            Quantity_Length &aHeight,
                                            Quantity_Length &anXoffset,
                                            Quantity_Length &anYoffset) const {
Handle(Graphic2d_Drawer) aDrawer = Drawer();
 
    if( !aDrawer.IsNull() && aDrawer->IsWindowDriver() ) {
        Standard_ShortReal hscale = (myIsZoomable) ? 
            Standard_ShortReal( myHScale * aDrawer->Scale() )
          : Standard_ShortReal( myHScale );
        Standard_ShortReal wscale = (myIsZoomable) ? 
            Standard_ShortReal( myWScale * aDrawer->Scale() )
          : Standard_ShortReal( myWScale );
        Standard_ShortReal width,height,xoffset,yoffset;
        aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
                                        mySlant,hscale,wscale,myIsUnderlined);
                                         
        aDrawer->GetTextSize(myText,width,height,xoffset,yoffset);
        aWidth = width + 2.*height*myMargin;
        aHeight = height + 2.*height*myMargin;
	anXoffset = xoffset - height*myMargin;
        anYoffset = yoffset + height*myMargin;
        return Standard_True;
    } else {
        aWidth = aHeight = anXoffset = anYoffset = 0.;
        return Standard_False;
    }
 
}

Standard_Integer Graphic2d_FramedText::FrameColorIndex() const {

        return myFrameColorIndex;
}

Standard_Integer Graphic2d_FramedText::FrameWidthIndex() const {

        return myFrameWidthIndex;
}

Quantity_Ratio Graphic2d_FramedText::Margin() const {

        return Quantity_Ratio(myMargin);
}

Standard_Boolean Graphic2d_FramedText::Fit(const Quantity_Length aWidth,const Quantity_Length aHeight,const Standard_Boolean Adjust,const Standard_Boolean Expand) {
Quantity_Length twidth,theight,xoffset,yoffset;
Standard_ShortReal wscale,hscale;
Standard_Boolean status;

    myAdjustFlag = Adjust;
    if(( status = TextSize(twidth,theight,xoffset,yoffset) ) == Standard_True ) {
      wscale = Standard_ShortReal( aWidth/twidth );
      if( wscale > 0. ) {
        if( Expand || (twidth > aWidth) ) myWScale *= wscale;
      }
      hscale = Standard_ShortReal( aHeight/theight );
      if( hscale > 0. ) myHScale *= hscale;
#ifdef PERF
      myMinX = myMinY = ShortRealLast ();
      myMaxX = myMaxY = ShortRealFirst ();
#else
      myResetFlag = Standard_True;
      ComputeMinMax();
#endif
    }

    return status;
}

Standard_Boolean Graphic2d_FramedText::Trunc(const Quantity_Length aWidth) {
Quantity_Length twidth,theight,txoffset,tyoffset;
//Standard_ShortReal wscale,hscale;
Standard_Boolean status;

    if(( status = TextSize(twidth,theight,txoffset,tyoffset) ) == Standard_True ) {
      Standard_Integer l = myText.Length();
      while( (l > 1) && (twidth > aWidth) ) {
        --l;
        myText.Split(l);
        TextSize(twidth,theight,txoffset,tyoffset);
      }
#ifdef PERF
      myMinX = myMinY = ShortRealLast ();
      myMaxX = myMaxY = ShortRealFirst ();
#else
      myResetFlag = Standard_True;
      this->ComputeMinMax();
#endif
    }

    return status;
}

Standard_Boolean Graphic2d_FramedText::ComputeMinMax() {
Handle(Graphic2d_Drawer) aDrawer = Drawer();
Standard_Boolean status = Standard_False;

    if( !aDrawer.IsNull() && aDrawer->IsWindowDriver() ) {
	  Standard_ShortReal hscale = 
		(myIsZoomable) ? 
        Standard_ShortReal( myHScale * aDrawer->Scale() )
      : Standard_ShortReal( myHScale );
	  Standard_ShortReal wscale = 
		(myIsZoomable) ? 
        Standard_ShortReal( myWScale * aDrawer->Scale() )
      : Standard_ShortReal( myWScale );
      Standard_ShortReal width,height,xoffset,yoffset;
          aDrawer->SetTextAttrib(myColorIndex,myFontIndex,
					mySlant,hscale,wscale,myIsUnderlined);
          if(( status = aDrawer->GetTextSize(myText,width,height,xoffset,yoffset) ) == Standard_True ) {
            Standard_ShortReal dxm,dym;
#ifndef PERF
	    myResetFlag = Standard_False; 
#endif
            dxm = dym = height*myMargin ;

            switch( myAlignment ) {
              case Graphic2d_TOA_LEFT:
                myDx = myDy = 0.;
                break;
              case Graphic2d_TOA_RIGHT:
                myDx = -width; myDy = 0.;
                break;
              case Graphic2d_TOA_CENTER:
                myDx = Standard_ShortReal( -width/2. ); myDy = 0.;
                break;
              case Graphic2d_TOA_TOPLEFT:
                myDx = 0.; myDy = yoffset-height;
                break;
              case Graphic2d_TOA_TOPRIGHT:
                myDx = -width; myDy = yoffset-height;
                break;
              case Graphic2d_TOA_TOPCENTER:
                myDx = Standard_ShortReal( -width/2.); myDy = yoffset-height;
                break;
              case Graphic2d_TOA_MEDIUMLEFT:
                myDx = 0.; myDy = Standard_ShortReal( (yoffset-height)/2. );
                break;
              case Graphic2d_TOA_MEDIUMRIGHT:
                myDx = -width; myDy = Standard_ShortReal( (yoffset-height)/2. );
                break;
              case Graphic2d_TOA_MEDIUMCENTER:
                myDx = Standard_ShortReal( -width/2.); 
                myDy = Standard_ShortReal( (yoffset-height)/2. );
                break;
              case Graphic2d_TOA_BOTTOMLEFT:
                myDx = 0.; myDy = yoffset;
                break;
              case Graphic2d_TOA_BOTTOMRIGHT:
                myDx = -width; myDy = yoffset;
                break;
              case Graphic2d_TOA_BOTTOMCENTER:
                myDx = Standard_ShortReal( -width/2. ); myDy = yoffset;
                break;
            }
            if( myAdjustFlag ) {
              myDx += dxm - xoffset; myDy += dym + yoffset;
            }
            Standard_ShortReal xmin = myDx - dxm + xoffset;
            Standard_ShortReal ymin = myDy - dym - yoffset;
            Standard_ShortReal xmax = xmin + width + 2*dxm;
            Standard_ShortReal ymax = ymin + height + 2*dym;

            myMinX = myMinY = ShortRealLast();
            myMaxX = myMaxY = ShortRealFirst();
            if( myAngle != 0. ) {
              Standard_ShortReal cosa = Standard_ShortReal( Cos(myAngle) );
              Standard_ShortReal sina = Standard_ShortReal( Sin(myAngle) );
              Standard_ShortReal dx,dy;
 
              dx = XROTATE(xmin,ymin) ;
              dy = YROTATE(xmin,ymin) ;
              myMinX = Min(myMinX,myX+dx) ;
              myMinY = Min(myMinY,myY+dy) ;
              myMaxX = Max(myMaxX,myX+dx) ;
              myMaxY = Max(myMaxY,myY+dy) ;

              dx = XROTATE(xmin,ymax) ;
              dy = YROTATE(xmin,ymax) ;
              myMinX = Min(myMinX,myX+dx) ;
              myMinY = Min(myMinY,myY+dy) ;
              myMaxX = Max(myMaxX,myX+dx) ;
              myMaxY = Max(myMaxY,myY+dy) ;

              dx = XROTATE(xmax,ymax) ;
              dy = YROTATE(xmax,ymax) ;
              myMinX = Min(myMinX,myX+dx) ;
              myMinY = Min(myMinY,myY+dy) ;
              myMaxX = Max(myMaxX,myX+dx) ;
              myMaxY = Max(myMaxY,myY+dy) ;
 
              dx = XROTATE(xmax,ymin) ;
              dy = YROTATE(xmax,ymin) ;
              myMinX = Min(myMinX,myX+dx) ;
              myMinY = Min(myMinY,myY+dy) ;
              myMaxX = Max(myMaxX,myX+dx) ;
              myMaxY = Max(myMaxY,myY+dy) ;
            } else {
#ifdef PRO14304
              myMinX = Min(myMinX,myX+xmin) ;
              myMinY = Min(myMinY,myY+ymin) ;
              myMaxX = Max(myMaxX,myX+xmax) ;
              myMaxY = Max(myMaxY,myY+ymax) ;
#else
              myMinX = Min(myMinX,xmin) ;
              myMinY = Min(myMinY,ymin) ;
              myMaxX = Max(myMaxX,xmax) ;
              myMaxY = Max(myMaxY,ymax) ;
#endif
            }
          }
    }
#ifdef PERF
      else {
          cout << "*Graphic2d_FramedText::ComputeMinMax() returns wrong values*" << endl;
   }
#endif

    return status;
}

void Graphic2d_FramedText::Save(Aspect_FStream& aFStream) const
{
}