0027860: Visualization - clean up Transformation Persistence API
[occt.git] / src / AIS / AIS_TextLabel.cxx
CommitLineData
29e2c6d2 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
3f1eb0ab 17#include <AIS_InteractiveContext.hxx>
18#include <Font_FTFont.hxx>
19#include <Font_FontMgr.hxx>
20#include <Font_Rect.hxx>
29e2c6d2 21#include <Graphic3d_AspectText3d.hxx>
3f1eb0ab 22#include <Graphic3d_RenderingParams.hxx>
29e2c6d2 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
3f1eb0ab 31#include <V3d_Viewer.hxx>
32
29e2c6d2 33
92efcf78 34IMPLEMENT_STANDARD_RTTIEXT(AIS_TextLabel,AIS_InteractiveObject)
35
29e2c6d2 36//=======================================================================
37//function : AIS_TextLabel
38//purpose :
39//=======================================================================
40AIS_TextLabel::AIS_TextLabel()
ce01ec26 41: myText ("?"),
42 myFont ("Courier"),
43 myFontAspect (Font_FA_Regular),
3f1eb0ab 44 myHasOrientation3D (Standard_False),
45 myHasFlipping (Standard_False)
29e2c6d2 46{
47 myDrawer->SetTextAspect (new Prs3d_TextAspect());
48
49 SetDisplayMode (0);
50}
51
52//=======================================================================
53//function : SetColor
54//purpose :
55//=======================================================================
56void 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//=======================================================================
67void AIS_TextLabel::SetColor (const Quantity_NameOfColor theColor)
68{
69 SetColor (Quantity_Color (theColor));
70}
71
72//=======================================================================
73//function : SetText
74//purpose :
75//=======================================================================
76void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText)
77{
78 myText = theText;
79}
80
81//=======================================================================
82//function : SetPosition
83//purpose :
84//=======================================================================
85void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition)
86{
ce01ec26 87 myOrientation3D.SetLocation (thePosition);
29e2c6d2 88}
89
90//=======================================================================
91//function : SetHJustification
92//purpose :
93//=======================================================================
94void 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//=======================================================================
103void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust)
104{
105 myDrawer->TextAspect()->SetVerticalJustification (theVJust);
106}
107
108//=======================================================================
109//function : SetAngle
110//purpose :
111//=======================================================================
112void 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//=======================================================================
121void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable)
122{
b6472664 123 myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True);
29e2c6d2 124}
125
126//=======================================================================
127//function : SetHeight
128//purpose :
129//=======================================================================
130void AIS_TextLabel::SetHeight (const Standard_Real theHeight)
131{
132 myDrawer->TextAspect()->SetHeight (theHeight);
133}
134
135//=======================================================================
136//function : SetAngle
137//purpose :
138//=======================================================================
139void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect)
140{
141 myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect);
142}
143
144//=======================================================================
145//function : SetFont
146//purpose :
147//=======================================================================
148void AIS_TextLabel::SetFont (Standard_CString theFont)
149{
150 myFont = theFont;
151 myDrawer->TextAspect()->SetFont (myFont.ToCString());
152}
153
ce01ec26 154//=======================================================================
155//function : SetOrientation3D
156//purpose :
157//=======================================================================
158void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation)
159{
160 myHasOrientation3D = Standard_True;
161 myOrientation3D = theOrientation;
162}
163
164//=======================================================================
165//function : UnsetOrientation3D
166//purpose :
167//=======================================================================
168void AIS_TextLabel::UnsetOrientation3D ()
169{
170 myHasOrientation3D = Standard_False;
171}
172
173//=======================================================================
174//function : Position
175//purpose :
176//=======================================================================
177const gp_Pnt& AIS_TextLabel::Position() const
178{
179 return myOrientation3D.Location();
180}
181
182//=======================================================================
183//function : Orientation3D
184//purpose :
185//=======================================================================
186const gp_Ax2& AIS_TextLabel::Orientation3D() const
187{
188 return myOrientation3D;
189}
190
191//=======================================================================
3f1eb0ab 192//function : HasOrientation3D
ce01ec26 193//purpose :
194//=======================================================================
195Standard_Boolean AIS_TextLabel::HasOrientation3D() const
196{
197 return myHasOrientation3D;
198}
199
3f1eb0ab 200//=======================================================================
201//function : SetFlipping
202//purpose :
203//=======================================================================
204void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping)
205{
206 myHasFlipping = theIsFlipping;
207}
208
209//=======================================================================
210//function : HasFlipping
211//purpose :
212//=======================================================================
213Standard_Boolean AIS_TextLabel::HasFlipping() const
214{
215 return myHasFlipping;
216}
217
61b0191c 218//=======================================================================
219//function : SetDisplayType
220//purpose :
221//=======================================================================
222void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType)
223{
224 myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType);
225}
226
227//=======================================================================
228//function : SetColorSubTitle
229//purpose :
230//=======================================================================
231void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor)
232{
233 myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor);
234}
235
3f1eb0ab 236#include <Graphic3d_ArrayOfPoints.hxx>
29e2c6d2 237//=======================================================================
238//function : Compute
239//purpose :
240//=======================================================================
241void 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();
3f1eb0ab 250 gp_Pnt aPosition = Position();
ce01ec26 251
252 if (myHasOrientation3D)
253 {
3f1eb0ab 254 Standard_Boolean isInit = Standard_False;
255 if (myHasFlipping)
256 {
257 // Get width and height of text
258 Font_FTFont aFont;
3f1eb0ab 259 unsigned int aResolution = GetContext()->CurrentViewer()->DefaultRenderingParams().Resolution;
b6472664 260 if (aFont.Init (anAsp->Aspect()->Font().ToCString(),
261 anAsp->Aspect()->GetTextFontAspect(), (unsigned int)anAsp->Height(), aResolution))
3f1eb0ab 262 {
263 isInit = Standard_True;
fb0b0531 264 const NCollection_String aText (myText.ToExtString());
3f1eb0ab 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
778cd667 287 if (!anAsp->Aspect()->GetTextZoomable()
288 && (TransformPersistence().IsNull()
289 || TransformPersistence()->Mode() == Graphic3d_TMF_ZoomPers))
3f1eb0ab 290 {
291 anAsp->Aspect()->SetTextZoomable (Standard_True);
778cd667 292 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, aPosition));
3f1eb0ab 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);
4ad142d9 303 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, myOrientation3D, !myHasFlipping);
3f1eb0ab 304 if (myHasFlipping && isInit)
305 {
306 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2());
307 }
ce01ec26 308 }
309 else
310 {
4ad142d9 311 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, Position());
ce01ec26 312 }
313
29e2c6d2 314 break;
315 }
316 }
317}
318
319//=======================================================================
320//function : ComputeSelection
321//purpose :
322//=======================================================================
323void 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);
ce01ec26 331 Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position());
29e2c6d2 332 theSelection->Add (aSensitivePoint);
333 break;
334 }
335 }
336}