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()
41 myHasOrientation3D (Standard_False),
42 myHasFlipping (Standard_False)
44 myDrawer->SetTextAspect (new Prs3d_TextAspect());
45 myDrawer->SetDisplayMode (0);
48 //=======================================================================
51 //=======================================================================
52 void AIS_TextLabel::SetColor (const Quantity_Color& theColor)
54 hasOwnColor = Standard_True;
55 myDrawer->SetColor (theColor);
56 myDrawer->TextAspect()->SetColor (theColor);
60 //=======================================================================
61 //function : SetTransparency
63 //=======================================================================
64 void AIS_TextLabel::SetTransparency (const Standard_Real theValue)
66 Quantity_ColorRGBA aTextColor (myDrawer->TextAspect()->Aspect()->Color());
67 aTextColor.SetAlpha (Standard_ShortReal(1.0 - theValue));
69 Quantity_ColorRGBA aSubColor (myDrawer->TextAspect()->Aspect()->ColorSubTitle());
70 aSubColor.SetAlpha (aTextColor.Alpha());
72 myDrawer->TextAspect()->Aspect()->SetColor (aTextColor);
73 myDrawer->TextAspect()->Aspect()->SetColorSubTitle (aSubColor);
74 myDrawer->SetTransparency (Standard_ShortReal(theValue));
78 //=======================================================================
81 //=======================================================================
82 void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText)
87 //=======================================================================
88 //function : SetPosition
90 //=======================================================================
91 void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition)
93 myOrientation3D.SetLocation (thePosition);
96 //=======================================================================
97 //function : SetHJustification
99 //=======================================================================
100 void AIS_TextLabel::SetHJustification (const Graphic3d_HorizontalTextAlignment theHJust)
102 myDrawer->TextAspect()->SetHorizontalJustification (theHJust);
105 //=======================================================================
106 //function : SetVJustification
107 //purpose : Setup vertical justification.
108 //=======================================================================
109 void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust)
111 myDrawer->TextAspect()->SetVerticalJustification (theVJust);
114 //=======================================================================
115 //function : SetAngle
117 //=======================================================================
118 void AIS_TextLabel::SetAngle (const Standard_Real theAngle)
120 myDrawer->TextAspect()->Aspect()->SetTextAngle (theAngle * 180.0 / M_PI);
123 //=======================================================================
126 //=======================================================================
127 void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable)
129 myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True);
132 //=======================================================================
133 //function : SetHeight
135 //=======================================================================
136 void AIS_TextLabel::SetHeight (const Standard_Real theHeight)
138 myDrawer->TextAspect()->SetHeight (theHeight);
141 //=======================================================================
142 //function : SetAngle
144 //=======================================================================
145 void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect)
147 myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect);
150 //=======================================================================
153 //=======================================================================
154 void AIS_TextLabel::SetFont (Standard_CString theFont)
156 myDrawer->TextAspect()->SetFont (theFont);
159 //=======================================================================
160 //function : SetOrientation3D
162 //=======================================================================
163 void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation)
165 myHasOrientation3D = Standard_True;
166 myOrientation3D = theOrientation;
169 //=======================================================================
170 //function : UnsetOrientation3D
172 //=======================================================================
173 void AIS_TextLabel::UnsetOrientation3D ()
175 myHasOrientation3D = Standard_False;
178 //=======================================================================
179 //function : Position
181 //=======================================================================
182 const gp_Pnt& AIS_TextLabel::Position() const
184 return myOrientation3D.Location();
187 //=======================================================================
188 //function : FontName
190 //=======================================================================
191 const TCollection_AsciiString& AIS_TextLabel::FontName() const
193 return myDrawer->TextAspect()->Aspect()->Font();
196 //=======================================================================
197 //function : FontAspect
199 //=======================================================================
200 Font_FontAspect AIS_TextLabel::FontAspect() const
202 return myDrawer->TextAspect()->Aspect()->GetTextFontAspect();
205 //=======================================================================
206 //function : Orientation3D
208 //=======================================================================
209 const gp_Ax2& AIS_TextLabel::Orientation3D() const
211 return myOrientation3D;
214 //=======================================================================
215 //function : HasOrientation3D
217 //=======================================================================
218 Standard_Boolean AIS_TextLabel::HasOrientation3D() const
220 return myHasOrientation3D;
223 //=======================================================================
224 //function : SetFlipping
226 //=======================================================================
227 void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping)
229 myHasFlipping = theIsFlipping;
232 //=======================================================================
233 //function : HasFlipping
235 //=======================================================================
236 Standard_Boolean AIS_TextLabel::HasFlipping() const
238 return myHasFlipping;
241 //=======================================================================
242 //function : SetDisplayType
244 //=======================================================================
245 void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType)
247 myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType);
250 //=======================================================================
251 //function : SetColorSubTitle
253 //=======================================================================
254 void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor)
256 myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor);
259 //=======================================================================
262 //=======================================================================
263 void AIS_TextLabel::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
264 const Handle(Prs3d_Presentation)& thePrs,
265 const Standard_Integer theMode)
271 Handle(Prs3d_TextAspect) anAsp = myDrawer->TextAspect();
272 gp_Pnt aPosition = Position();
274 if (myHasOrientation3D)
276 Standard_Boolean isInit = Standard_False;
279 // Get width and height of text
280 Font_FTFontParams aFontParams;
281 aFontParams.PointSize = (unsigned int )anAsp->Height();
282 aFontParams.Resolution = GetContext()->CurrentViewer()->DefaultRenderingParams().Resolution;
283 if (Handle(Font_FTFont) aFont = Font_FTFont::FindAndCreate (anAsp->Aspect()->Font(), anAsp->Aspect()->GetTextFontAspect(), aFontParams))
285 isInit = Standard_True;
286 const NCollection_String aText (myText.ToExtString());
287 Font_Rect aBndBox = aFont->BoundingBox (aText, anAsp->HorizontalJustification(), anAsp->VerticalJustification());
288 Standard_Real aWidth = Abs (aBndBox.Width());
289 Standard_Real aHeight = Abs (aBndBox.Height());
290 gp_Pnt aCenterOfLabel = aPosition;
292 if (anAsp->VerticalJustification() == Graphic3d_VTA_BOTTOM)
294 aCenterOfLabel.ChangeCoord() += myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
296 else if (anAsp->VerticalJustification() == Graphic3d_VTA_TOP)
298 aCenterOfLabel.ChangeCoord() -= myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
300 if (anAsp->HorizontalJustification() == Graphic3d_HTA_LEFT)
302 aCenterOfLabel.ChangeCoord() += myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
304 else if (anAsp->HorizontalJustification() == Graphic3d_HTA_RIGHT)
306 aCenterOfLabel.ChangeCoord() -= myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
309 if (!anAsp->Aspect()->GetTextZoomable()
310 && (TransformPersistence().IsNull()
311 || TransformPersistence()->Mode() == Graphic3d_TMF_ZoomPers))
313 anAsp->Aspect()->SetTextZoomable (Standard_True);
314 SetTransformPersistence (new Graphic3d_TransformPers (Graphic3d_TMF_ZoomPers, aPosition));
315 aPosition = gp::Origin();
318 gp_Ax2 aFlippingAxes (aCenterOfLabel, myOrientation3D.Direction(), myOrientation3D.XDirection());
319 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_True, aFlippingAxes);
323 gp_Ax2 anOrientation = myOrientation3D;
324 anOrientation.SetLocation (aPosition);
325 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, myOrientation3D, !myHasFlipping);
326 if (myHasFlipping && isInit)
328 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2());
333 Prs3d_Text::Draw (Prs3d_Root::CurrentGroup (thePrs), anAsp, myText, Position());
341 //=======================================================================
342 //function : ComputeSelection
344 //=======================================================================
345 void AIS_TextLabel::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
346 const Standard_Integer theMode)
352 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner (this, 10);
353 Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position());
354 theSelection->Add (aSensitivePoint);