1 // Created on: 2014-11-10
2 // Copyright (c) 2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
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.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <AIS_TextLabel.hxx>
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>
24 #include <Prs3d_Text.hxx>
25 #include <Prs3d_TextAspect.hxx>
27 #include <Select3D_SensitivePoint.hxx>
28 #include <SelectMgr_Selection.hxx>
29 #include <SelectMgr_EntityOwner.hxx>
31 #include <V3d_Viewer.hxx>
33 IMPLEMENT_STANDARD_RTTIEXT(AIS_TextLabel,AIS_InteractiveObject)
35 //=======================================================================
36 //function : AIS_TextLabel
38 //=======================================================================
39 AIS_TextLabel::AIS_TextLabel()
42 myFontAspect (Font_FA_Regular),
43 myHasOrientation3D (Standard_False),
44 myHasFlipping (Standard_False)
46 myDrawer->SetTextAspect (new Prs3d_TextAspect());
51 //=======================================================================
54 //=======================================================================
55 void AIS_TextLabel::SetColor (const Quantity_Color& theColor)
57 hasOwnColor = Standard_True;
58 myDrawer->SetColor (theColor);
59 myDrawer->TextAspect()->SetColor (theColor);
62 //=======================================================================
63 //function : SetTransparency
65 //=======================================================================
66 void AIS_TextLabel::SetTransparency (const Standard_Real theValue)
68 Quantity_ColorRGBA aTextColor (myDrawer->TextAspect()->Aspect()->Color());
69 aTextColor.SetAlpha (Standard_ShortReal(1.0 - theValue));
71 Quantity_ColorRGBA aSubColor (myDrawer->TextAspect()->Aspect()->ColorSubTitle());
72 aSubColor.SetAlpha (aTextColor.Alpha());
74 myDrawer->TextAspect()->Aspect()->SetColor (aTextColor);
75 myDrawer->TextAspect()->Aspect()->SetColorSubTitle (aSubColor);
76 myDrawer->SetTransparency (Standard_ShortReal(theValue));
79 //=======================================================================
82 //=======================================================================
83 void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText)
88 //=======================================================================
89 //function : SetPosition
91 //=======================================================================
92 void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition)
94 myOrientation3D.SetLocation (thePosition);
97 //=======================================================================
98 //function : SetHJustification
100 //=======================================================================
101 void AIS_TextLabel::SetHJustification (const Graphic3d_HorizontalTextAlignment theHJust)
103 myDrawer->TextAspect()->SetHorizontalJustification (theHJust);
106 //=======================================================================
107 //function : SetVJustification
108 //purpose : Setup vertical justification.
109 //=======================================================================
110 void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust)
112 myDrawer->TextAspect()->SetVerticalJustification (theVJust);
115 //=======================================================================
116 //function : SetAngle
118 //=======================================================================
119 void AIS_TextLabel::SetAngle (const Standard_Real theAngle)
121 myDrawer->TextAspect()->Aspect()->SetTextAngle (theAngle * 180.0 / M_PI);
124 //=======================================================================
127 //=======================================================================
128 void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable)
130 myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True);
133 //=======================================================================
134 //function : SetHeight
136 //=======================================================================
137 void AIS_TextLabel::SetHeight (const Standard_Real theHeight)
139 myDrawer->TextAspect()->SetHeight (theHeight);
142 //=======================================================================
143 //function : SetAngle
145 //=======================================================================
146 void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect)
148 myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect);
151 //=======================================================================
154 //=======================================================================
155 void AIS_TextLabel::SetFont (Standard_CString theFont)
158 myDrawer->TextAspect()->SetFont (myFont.ToCString());
161 //=======================================================================
162 //function : SetOrientation3D
164 //=======================================================================
165 void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation)
167 myHasOrientation3D = Standard_True;
168 myOrientation3D = theOrientation;
171 //=======================================================================
172 //function : UnsetOrientation3D
174 //=======================================================================
175 void AIS_TextLabel::UnsetOrientation3D ()
177 myHasOrientation3D = Standard_False;
180 //=======================================================================
181 //function : Position
183 //=======================================================================
184 const gp_Pnt& AIS_TextLabel::Position() const
186 return myOrientation3D.Location();
189 //=======================================================================
190 //function : Orientation3D
192 //=======================================================================
193 const gp_Ax2& AIS_TextLabel::Orientation3D() const
195 return myOrientation3D;
198 //=======================================================================
199 //function : HasOrientation3D
201 //=======================================================================
202 Standard_Boolean AIS_TextLabel::HasOrientation3D() const
204 return myHasOrientation3D;
207 //=======================================================================
208 //function : SetFlipping
210 //=======================================================================
211 void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping)
213 myHasFlipping = theIsFlipping;
216 //=======================================================================
217 //function : HasFlipping
219 //=======================================================================
220 Standard_Boolean AIS_TextLabel::HasFlipping() const
222 return myHasFlipping;
225 //=======================================================================
226 //function : SetDisplayType
228 //=======================================================================
229 void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType)
231 myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType);
234 //=======================================================================
235 //function : SetColorSubTitle
237 //=======================================================================
238 void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor)
240 myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor);
243 //=======================================================================
246 //=======================================================================
247 void AIS_TextLabel::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
248 const Handle(Prs3d_Presentation)& thePrs,
249 const Standard_Integer theMode)
255 Handle(Prs3d_TextAspect) anAsp = myDrawer->TextAspect();
256 gp_Pnt aPosition = Position();
258 if (myHasOrientation3D)
260 Standard_Boolean isInit = Standard_False;
263 // Get width and height of text
265 unsigned int aResolution = GetContext()->CurrentViewer()->DefaultRenderingParams().Resolution;
266 if (aFont.Init (anAsp->Aspect()->Font().ToCString(),
267 anAsp->Aspect()->GetTextFontAspect(), (unsigned int)anAsp->Height(), aResolution))
269 isInit = Standard_True;
270 const NCollection_String aText (myText.ToExtString());
271 Font_Rect aBndBox = aFont.BoundingBox (aText, anAsp->HorizontalJustification(), anAsp->VerticalJustification());
272 Standard_Real aWidth = Abs (aBndBox.Width());
273 Standard_Real aHeight = Abs (aBndBox.Height());
274 gp_Pnt aCenterOfLabel = aPosition;
276 if (anAsp->VerticalJustification() == Graphic3d_VTA_BOTTOM)
278 aCenterOfLabel.ChangeCoord() += myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
280 else if (anAsp->VerticalJustification() == Graphic3d_VTA_TOP)
282 aCenterOfLabel.ChangeCoord() -= myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
284 if (anAsp->HorizontalJustification() == Graphic3d_HTA_LEFT)
286 aCenterOfLabel.ChangeCoord() += myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
288 else if (anAsp->HorizontalJustification() == Graphic3d_HTA_RIGHT)
290 aCenterOfLabel.ChangeCoord() -= myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
293 if (!anAsp->Aspect()->GetTextZoomable()
294 && (TransformPersistence().IsNull()
295 || TransformPersistence()->Mode() == Graphic3d_TMF_ZoomPers))
297 anAsp->Aspect()->SetTextZoomable (Standard_True);
298 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, aPosition));
299 aPosition = gp::Origin();
302 gp_Ax2 aFlippingAxes (aCenterOfLabel, myOrientation3D.Direction(), myOrientation3D.XDirection());
303 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_True, aFlippingAxes);
307 gp_Ax2 anOrientation = myOrientation3D;
308 anOrientation.SetLocation (aPosition);
309 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, myOrientation3D, !myHasFlipping);
310 if (myHasFlipping && isInit)
312 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2());
317 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, Position());
325 //=======================================================================
326 //function : ComputeSelection
328 //=======================================================================
329 void AIS_TextLabel::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
330 const Standard_Integer theMode)
336 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner (this, 10);
337 Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position());
338 theSelection->Add (aSensitivePoint);