0027828: Reference documentation of AIS_Trihedron contains invalid information about...
[occt.git] / src / AIS / AIS_TextLabel.cxx
1 // Created on: 2014-11-10
2 // Copyright (c) 2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15 #include <AIS_TextLabel.hxx>
16
17 #include <AIS_InteractiveContext.hxx>
18 #include <Font_FTFont.hxx>
19 #include <Font_FontMgr.hxx>
20 #include <Font_Rect.hxx>
21 #include <Graphic3d_AspectText3d.hxx>
22 #include <Graphic3d_RenderingParams.hxx>
23
24 #include <Prs3d_Text.hxx>
25 #include <Prs3d_TextAspect.hxx>
26
27 #include <Select3D_SensitivePoint.hxx>
28 #include <SelectMgr_Selection.hxx>
29 #include <SelectMgr_EntityOwner.hxx>
30
31 #include <V3d_Viewer.hxx>
32
33
34 IMPLEMENT_STANDARD_RTTIEXT(AIS_TextLabel,AIS_InteractiveObject)
35
36 //=======================================================================
37 //function : AIS_TextLabel
38 //purpose  :
39 //=======================================================================
40 AIS_TextLabel::AIS_TextLabel()
41 : myText             ("?"),
42   myFont             ("Courier"),
43   myFontAspect       (Font_FA_Regular),
44   myHasOrientation3D (Standard_False),
45   myHasFlipping      (Standard_False)
46 {
47   myDrawer->SetTextAspect (new Prs3d_TextAspect());
48
49   SetDisplayMode (0);
50 }
51
52 //=======================================================================
53 //function : SetColor
54 //purpose  :
55 //=======================================================================
56 void AIS_TextLabel::SetColor (const Quantity_Color& theColor)
57 {
58   hasOwnColor = Standard_True;
59   myOwnColor  = theColor;
60   myDrawer->TextAspect()->SetColor (theColor);
61 }
62
63 //=======================================================================
64 //function : SetColor
65 //purpose  :
66 //=======================================================================
67 void AIS_TextLabel::SetColor (const Quantity_NameOfColor theColor)
68 {
69   SetColor (Quantity_Color (theColor));
70 }
71
72 //=======================================================================
73 //function : SetText
74 //purpose  :
75 //=======================================================================
76 void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText)
77 {
78   myText = theText;
79 }
80
81 //=======================================================================
82 //function : SetPosition
83 //purpose  :
84 //=======================================================================
85 void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition)
86 {
87   myOrientation3D.SetLocation (thePosition);
88 }
89
90 //=======================================================================
91 //function : SetHJustification
92 //purpose  :
93 //=======================================================================
94 void AIS_TextLabel::SetHJustification (const Graphic3d_HorizontalTextAlignment theHJust)
95 {
96   myDrawer->TextAspect()->SetHorizontalJustification (theHJust);
97 }
98
99 //=======================================================================
100 //function : SetVJustification
101 //purpose  : Setup vertical justification.
102 //=======================================================================
103 void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust)
104 {
105   myDrawer->TextAspect()->SetVerticalJustification (theVJust);
106 }
107
108 //=======================================================================
109 //function : SetAngle
110 //purpose  :
111 //=======================================================================
112 void AIS_TextLabel::SetAngle (const Standard_Real theAngle)
113 {
114   myDrawer->TextAspect()->Aspect()->SetTextAngle (theAngle * 180.0 / M_PI);
115 }
116
117 //=======================================================================
118 //function : SetZoom
119 //purpose  :
120 //=======================================================================
121 void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable)
122 {
123   myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True);
124 }
125
126 //=======================================================================
127 //function : SetHeight
128 //purpose  :
129 //=======================================================================
130 void AIS_TextLabel::SetHeight (const Standard_Real theHeight)
131 {
132   myDrawer->TextAspect()->SetHeight (theHeight);
133 }
134
135 //=======================================================================
136 //function : SetAngle
137 //purpose  :
138 //=======================================================================
139 void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect)
140 {
141   myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect);
142 }
143
144 //=======================================================================
145 //function : SetFont
146 //purpose  :
147 //=======================================================================
148 void AIS_TextLabel::SetFont (Standard_CString theFont)
149 {
150   myFont = theFont;
151   myDrawer->TextAspect()->SetFont (myFont.ToCString());
152 }
153
154 //=======================================================================
155 //function : SetOrientation3D
156 //purpose  :
157 //=======================================================================
158 void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation)
159 {
160   myHasOrientation3D = Standard_True;
161   myOrientation3D    = theOrientation;
162 }
163
164 //=======================================================================
165 //function : UnsetOrientation3D
166 //purpose  :
167 //=======================================================================
168 void AIS_TextLabel::UnsetOrientation3D ()
169 {
170   myHasOrientation3D = Standard_False;
171 }
172
173 //=======================================================================
174 //function : Position
175 //purpose  :
176 //=======================================================================
177 const gp_Pnt& AIS_TextLabel::Position() const
178 {
179   return myOrientation3D.Location();
180 }
181
182 //=======================================================================
183 //function : Orientation3D
184 //purpose  :
185 //=======================================================================
186 const gp_Ax2& AIS_TextLabel::Orientation3D() const
187 {
188   return myOrientation3D;
189 }
190
191 //=======================================================================
192 //function : HasOrientation3D
193 //purpose  :
194 //=======================================================================
195 Standard_Boolean AIS_TextLabel::HasOrientation3D() const
196 {
197   return myHasOrientation3D;
198 }
199
200 //=======================================================================
201 //function : SetFlipping
202 //purpose  :
203 //=======================================================================
204 void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping)
205 {
206   myHasFlipping = theIsFlipping;
207 }
208
209 //=======================================================================
210 //function : HasFlipping
211 //purpose  :
212 //=======================================================================
213 Standard_Boolean AIS_TextLabel::HasFlipping() const
214 {
215   return myHasFlipping;
216 }
217
218 //=======================================================================
219 //function : SetDisplayType
220 //purpose  :
221 //=======================================================================
222 void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType)
223 {
224   myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType);
225 }
226
227 //=======================================================================
228 //function : SetColorSubTitle
229 //purpose  :
230 //=======================================================================
231 void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor)
232 {
233   myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor);
234 }
235
236 #include <Graphic3d_ArrayOfPoints.hxx>
237 //=======================================================================
238 //function : Compute
239 //purpose  :
240 //=======================================================================
241 void AIS_TextLabel::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
242                              const Handle(Prs3d_Presentation)&             thePrs,
243                              const Standard_Integer                        theMode)
244 {
245   switch (theMode)
246   {
247     case 0:
248     {
249       Handle(Prs3d_TextAspect) anAsp = myDrawer->TextAspect();
250       gp_Pnt aPosition = Position();
251
252       if (myHasOrientation3D)
253       {
254         Standard_Boolean isInit = Standard_False;
255         if (myHasFlipping)
256         {
257           // Get width and height of text
258           Font_FTFont aFont;
259           unsigned int aResolution = GetContext()->CurrentViewer()->DefaultRenderingParams().Resolution;
260           if (aFont.Init (anAsp->Aspect()->Font().ToCString(),
261                           anAsp->Aspect()->GetTextFontAspect(), (unsigned int)anAsp->Height(), aResolution))
262           {
263             isInit = Standard_True;
264             const NCollection_String aText (myText.ToExtString());
265             Font_Rect aBndBox = aFont.BoundingBox (aText, anAsp->HorizontalJustification(), anAsp->VerticalJustification());
266             Standard_Real aWidth = Abs (aBndBox.Width());
267             Standard_Real aHeight = Abs (aBndBox.Height());
268             gp_Pnt aCenterOfLabel = aPosition;
269
270             if (anAsp->VerticalJustification() == Graphic3d_VTA_BOTTOM)
271             {
272               aCenterOfLabel.ChangeCoord() += myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
273             }
274             else if (anAsp->VerticalJustification() == Graphic3d_VTA_TOP)
275             {
276               aCenterOfLabel.ChangeCoord() -= myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
277             }
278             if (anAsp->HorizontalJustification() == Graphic3d_HTA_LEFT)
279             {
280               aCenterOfLabel.ChangeCoord() += myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
281             }
282             else if (anAsp->HorizontalJustification() == Graphic3d_HTA_RIGHT)
283             {
284               aCenterOfLabel.ChangeCoord() -= myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
285             }
286
287             if (!anAsp->Aspect()->GetTextZoomable()
288              && (TransformPersistence().IsNull()
289               || TransformPersistence()->Mode() == Graphic3d_TMF_ZoomPers))
290             {
291               anAsp->Aspect()->SetTextZoomable (Standard_True);
292               SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, aPosition));
293               aPosition = gp::Origin();
294             }
295
296             gp_Ax2 aFlippingAxes (aCenterOfLabel, myOrientation3D.Direction(), myOrientation3D.XDirection());
297             Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_True, aFlippingAxes);
298           }
299         }
300
301         gp_Ax2 anOrientation = myOrientation3D;
302         anOrientation.SetLocation (aPosition);
303         Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, myOrientation3D, !myHasFlipping);
304         if (myHasFlipping && isInit)
305         {
306           Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2());
307         }
308       }
309       else
310       {
311         Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, Position());
312       }
313
314       break;
315     }
316   }
317 }
318
319 //=======================================================================
320 //function : ComputeSelection
321 //purpose  :
322 //=======================================================================
323 void AIS_TextLabel::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
324                                       const Standard_Integer             theMode)
325 {
326   switch (theMode)
327   {
328     case 0:
329     {
330       Handle(SelectMgr_EntityOwner)   anEntityOwner   = new SelectMgr_EntityOwner (this, 10);
331       Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position());
332       theSelection->Add (aSensitivePoint);
333       break;
334     }
335   }
336 }