2554414a625650e271693beafa07055f9f1c6abb
[occt.git] / src / StdPrs / StdPrs_BRepTextBuilder.cxx
1 // Created on: 2015-08-10
2 // Created by: Ilya SEVRIKOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <StdPrs_BRepTextBuilder.hxx>
17
18 #include <Font_TextFormatter.hxx>
19
20 // =======================================================================
21 // Function : Perfrom
22 // Purpose  :
23 // =======================================================================
24 TopoDS_Shape StdPrs_BRepTextBuilder::Perform (StdPrs_BRepFont&          theFont,
25                                               const Handle(Font_TextFormatter)& theFormatter,
26                                               const gp_Ax3&             thePenLoc)
27 {
28   gp_Trsf          aTrsf;
29   gp_XYZ           aPen;
30   TopoDS_Shape     aGlyphShape;
31   TopoDS_Compound  aResult;
32   Standard_Mutex::Sentry aSentry (theFont.Mutex());
33
34   myBuilder.MakeCompound (aResult);
35
36   Standard_Real aScaleUnits    = theFont.Scale();
37   for (Font_TextFormatter::Iterator aFormatterIt (*theFormatter, Font_TextFormatter::IterationFilter_ExcludeInvisible);
38        aFormatterIt.More(); aFormatterIt.Next())
39   {
40     const NCollection_Vec2<Standard_ShortReal>& aCorner = theFormatter->BottomLeft (aFormatterIt.SymbolPosition());
41
42     aPen.SetCoord (aCorner.x() * aScaleUnits, aCorner.y() * aScaleUnits, 0.0);
43     aGlyphShape = theFont.RenderGlyph (aFormatterIt.Symbol());
44     if (!aGlyphShape.IsNull())
45     {
46       aTrsf.SetTranslation (gp_Vec (aPen));
47       aGlyphShape.Move (aTrsf);
48       myBuilder.Add (aResult, aGlyphShape);
49     }
50   }
51
52   aTrsf.SetTransformation (thePenLoc, gp_Ax3 (gp::XOY()));
53   aResult.Move (aTrsf);
54
55   return aResult;
56 }
57
58 // =======================================================================
59 // Function : Perform
60 // Purpose  :
61 // =======================================================================
62 TopoDS_Shape StdPrs_BRepTextBuilder::Perform (StdPrs_BRepFont&                        theFont,
63                                               const NCollection_String&               theString,
64                                               const gp_Ax3&                           thePenLoc,
65                                               const Graphic3d_HorizontalTextAlignment theHAlign,
66                                               const Graphic3d_VerticalTextAlignment   theVAlign)
67 {
68   Handle(Font_TextFormatter) aFormatter = new Font_TextFormatter();
69
70   aFormatter->Reset();
71   aFormatter->SetupAlignment (theHAlign, theVAlign);
72
73   aFormatter->Append (theString, *(reinterpret_cast<Font_FTFont*> (&theFont)));
74   aFormatter->Format();
75
76   return Perform (theFont, aFormatter, thePenLoc);
77 }