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>
34 IMPLEMENT_STANDARD_RTTIEXT(AIS_TextLabel,AIS_InteractiveObject)
36 //=======================================================================
37 //function : AIS_TextLabel
39 //=======================================================================
40 AIS_TextLabel::AIS_TextLabel()
43 myFontAspect (Font_FA_Regular),
44 myHasOrientation3D (Standard_False),
45 myHasFlipping (Standard_False)
47 myDrawer->SetTextAspect (new Prs3d_TextAspect());
52 //=======================================================================
55 //=======================================================================
56 void AIS_TextLabel::SetColor (const Quantity_Color& theColor)
58 hasOwnColor = Standard_True;
59 myOwnColor = theColor;
60 myDrawer->TextAspect()->SetColor (theColor);
63 //=======================================================================
66 //=======================================================================
67 void AIS_TextLabel::SetColor (const Quantity_NameOfColor theColor)
69 SetColor (Quantity_Color (theColor));
72 //=======================================================================
75 //=======================================================================
76 void AIS_TextLabel::SetText (const TCollection_ExtendedString& theText)
81 //=======================================================================
82 //function : SetPosition
84 //=======================================================================
85 void AIS_TextLabel::SetPosition (const gp_Pnt& thePosition)
87 myOrientation3D.SetLocation (thePosition);
90 //=======================================================================
91 //function : SetHJustification
93 //=======================================================================
94 void AIS_TextLabel::SetHJustification (const Graphic3d_HorizontalTextAlignment theHJust)
96 myDrawer->TextAspect()->SetHorizontalJustification (theHJust);
99 //=======================================================================
100 //function : SetVJustification
101 //purpose : Setup vertical justification.
102 //=======================================================================
103 void AIS_TextLabel::SetVJustification (const Graphic3d_VerticalTextAlignment theVJust)
105 myDrawer->TextAspect()->SetVerticalJustification (theVJust);
108 //=======================================================================
109 //function : SetAngle
111 //=======================================================================
112 void AIS_TextLabel::SetAngle (const Standard_Real theAngle)
114 myDrawer->TextAspect()->Aspect()->SetTextAngle (theAngle * 180.0 / M_PI);
117 //=======================================================================
120 //=======================================================================
121 void AIS_TextLabel::SetZoomable (const Standard_Boolean theIsZoomable)
123 myDrawer->TextAspect()->Aspect()->SetTextZoomable (theIsZoomable == Standard_True);
126 //=======================================================================
127 //function : SetHeight
129 //=======================================================================
130 void AIS_TextLabel::SetHeight (const Standard_Real theHeight)
132 myDrawer->TextAspect()->SetHeight (theHeight);
135 //=======================================================================
136 //function : SetAngle
138 //=======================================================================
139 void AIS_TextLabel::SetFontAspect (const Font_FontAspect theFontAspect)
141 myDrawer->TextAspect()->Aspect()->SetTextFontAspect (theFontAspect);
144 //=======================================================================
147 //=======================================================================
148 void AIS_TextLabel::SetFont (Standard_CString theFont)
151 myDrawer->TextAspect()->SetFont (myFont.ToCString());
154 //=======================================================================
155 //function : SetOrientation3D
157 //=======================================================================
158 void AIS_TextLabel::SetOrientation3D (const gp_Ax2& theOrientation)
160 myHasOrientation3D = Standard_True;
161 myOrientation3D = theOrientation;
164 //=======================================================================
165 //function : UnsetOrientation3D
167 //=======================================================================
168 void AIS_TextLabel::UnsetOrientation3D ()
170 myHasOrientation3D = Standard_False;
173 //=======================================================================
174 //function : Position
176 //=======================================================================
177 const gp_Pnt& AIS_TextLabel::Position() const
179 return myOrientation3D.Location();
182 //=======================================================================
183 //function : Orientation3D
185 //=======================================================================
186 const gp_Ax2& AIS_TextLabel::Orientation3D() const
188 return myOrientation3D;
191 //=======================================================================
192 //function : HasOrientation3D
194 //=======================================================================
195 Standard_Boolean AIS_TextLabel::HasOrientation3D() const
197 return myHasOrientation3D;
200 //=======================================================================
201 //function : SetFlipping
203 //=======================================================================
204 void AIS_TextLabel::SetFlipping (const Standard_Boolean theIsFlipping)
206 myHasFlipping = theIsFlipping;
209 //=======================================================================
210 //function : HasFlipping
212 //=======================================================================
213 Standard_Boolean AIS_TextLabel::HasFlipping() const
215 return myHasFlipping;
218 //=======================================================================
219 //function : SetDisplayType
221 //=======================================================================
222 void AIS_TextLabel::SetDisplayType (const Aspect_TypeOfDisplayText theDisplayType)
224 myDrawer->TextAspect()->Aspect()->SetDisplayType(theDisplayType);
227 //=======================================================================
228 //function : SetColorSubTitle
230 //=======================================================================
231 void AIS_TextLabel::SetColorSubTitle (const Quantity_Color& theColor)
233 myDrawer->TextAspect()->Aspect()->SetColorSubTitle(theColor);
236 #include <Graphic3d_ArrayOfPoints.hxx>
237 //=======================================================================
240 //=======================================================================
241 void AIS_TextLabel::Compute (const Handle(PrsMgr_PresentationManager3d)& /*thePrsMgr*/,
242 const Handle(Prs3d_Presentation)& thePrs,
243 const Standard_Integer theMode)
249 Handle(Prs3d_TextAspect) anAsp = myDrawer->TextAspect();
250 gp_Pnt aPosition = Position();
252 if (myHasOrientation3D)
254 Standard_Boolean isInit = Standard_False;
257 // Get width and height of text
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))
263 isInit = Standard_True;
264 const NCollection_String aText ((Standard_Utf16Char* )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;
270 if (anAsp->VerticalJustification() == Graphic3d_VTA_BOTTOM)
272 aCenterOfLabel.ChangeCoord() += myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
274 else if (anAsp->VerticalJustification() == Graphic3d_VTA_TOP)
276 aCenterOfLabel.ChangeCoord() -= myOrientation3D.YDirection().XYZ() * aHeight * 0.5;
278 if (anAsp->HorizontalJustification() == Graphic3d_HTA_LEFT)
280 aCenterOfLabel.ChangeCoord() += myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
282 else if (anAsp->HorizontalJustification() == Graphic3d_HTA_RIGHT)
284 aCenterOfLabel.ChangeCoord() -= myOrientation3D.XDirection().XYZ() * aWidth * 0.5;
287 if (!anAsp->Aspect()->GetTextZoomable())
289 anAsp->Aspect()->SetTextZoomable (Standard_True);
290 SetTransformPersistence (GetTransformPersistenceMode() | Graphic3d_TMF_ZoomPers, aPosition);
291 aPosition = gp::Origin();
294 gp_Ax2 aFlippingAxes (aCenterOfLabel, myOrientation3D.Direction(), myOrientation3D.XDirection());
295 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_True, aFlippingAxes);
299 gp_Ax2 anOrientation = myOrientation3D;
300 anOrientation.SetLocation (aPosition);
301 Prs3d_Text::Draw (thePrs, anAsp, myText, myOrientation3D, !myHasFlipping);
302 if (myHasFlipping && isInit)
304 Prs3d_Root::CurrentGroup (thePrs)->SetFlippingOptions (Standard_False, gp_Ax2());
309 Prs3d_Text::Draw (thePrs, anAsp, myText, Position());
317 //=======================================================================
318 //function : ComputeSelection
320 //=======================================================================
321 void AIS_TextLabel::ComputeSelection (const Handle(SelectMgr_Selection)& theSelection,
322 const Standard_Integer theMode)
328 Handle(SelectMgr_EntityOwner) anEntityOwner = new SelectMgr_EntityOwner (this, 10);
329 Handle(Select3D_SensitivePoint) aSensitivePoint = new Select3D_SensitivePoint (anEntityOwner, Position());
330 theSelection->Add (aSensitivePoint);