0024911: Avoid using virtual functions in NCollection classes
[occt.git] / src / Font / Font_BRepFont.hxx
1 // Created on: 2013-09-16
2 // Copyright (c) 2013-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 #ifndef _Font_BRepFont_H__
16 #define _Font_BRepFont_H__
17
18 #include <Adaptor3d_CurveOnSurface.hxx>
19 #include <BRep_Builder.hxx>
20 #include <Font_FTFont.hxx>
21 #include <Geom2dAdaptor_HCurve.hxx>
22 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
23 #include <gp_Ax3.hxx>
24 #include <gp_XY.hxx>
25 #include <gp_XYZ.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <NCollection_String.hxx>
28 #include <Standard_Mutex.hxx>
29 #include <ShapeFix_Face.hxx>
30 #include <TColgp_Array1OfPnt2d.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Face.hxx>
33
34 //! This tool provides basic services for rendering of vectorized text glyphs as BRep shapes.
35 //! Single instance initialize single font for sequential glyphs rendering with implicit caching of already rendered glyphs.
36 //! Thus position of each glyph in the text is specified by shape location.
37 //!
38 //! Please notice that this implementation uses mutex for thread-safety access,
39 //! thus may lead to performance penalties in case of concurrent access.
40 //! Although caching should eliminate this issue after rendering of sufficient number of glyphs.
41 class Font_BRepFont : protected Font_FTFont
42 {
43 public:
44
45   //! Empty constructor
46   Standard_EXPORT Font_BRepFont();
47
48   //! Constructor with initialization.
49   //! @param theFontPath FULL path to the font
50   //! @param theSize     the face size in model units
51   Standard_EXPORT Font_BRepFont (const NCollection_String& theFontPath,
52                                  const Standard_Real       theSize);
53
54   //! Constructor with initialization.
55   //! @param theFontName   the font name
56   //! @param theFontAspect the font style
57   //! @param theSize       the face size in model units
58   Standard_EXPORT Font_BRepFont (const NCollection_String& theFontName,
59                                  const Font_FontAspect     theFontAspect,
60                                  const Standard_Real       theSize);
61
62   //! Release currently loaded font.
63   Standard_EXPORT virtual void Release();
64
65   //! Initialize the font.
66   //! @param theFontPath FULL path to the font
67   //! @param theSize     the face size in model units
68   //! @return true on success
69   Standard_EXPORT bool Init (const NCollection_String& theFontPath,
70                              const Standard_Real       theSize);
71
72   //! Initialize the font.
73   //! Please take into account that size is specified NOT in typography points (pt.).
74   //! If you need to specify size in points, value should be converted.
75   //! Formula for pt. -> m conversion:
76   //!   aSizeMeters = 0.0254 * theSizePt / 72.0
77   //! @param theFontName   the font name
78   //! @param theFontAspect the font style
79   //! @param theSize       the face size in model units
80   //! @return true on success
81   Standard_EXPORT bool Init (const NCollection_String& theFontName,
82                              const Font_FontAspect     theFontAspect,
83                              const Standard_Real       theSize);
84
85   //! Render single glyph as TopoDS_Shape.
86   //! @param theChar glyph identifier
87   //! @return rendered glyph within cache, might be NULL shape
88   Standard_EXPORT TopoDS_Shape RenderGlyph (const Standard_Utf32Char& theChar);
89
90   //! Setup glyph geometry construction mode.
91   //! By default algorithm creates independent TopoDS_Edge
92   //! for each original curve in the glyph (line segment or Bezie curve).
93   //! Algorithm might optionally create composite BSpline curve for each contour
94   //! which reduces memory footprint but limits curve class to C0.
95   //! Notice that altering this flag clears currently accumulated cache!
96   Standard_EXPORT void SetCompositeCurveMode (const Standard_Boolean theToConcatenate);
97
98 public:
99
100   //! Render text as BRep shape.
101   //! @param theString text in UTF-8 encoding
102   //! @return result shape within XOY plane and start position (0,0,0) on the baseline
103   Standard_EXPORT TopoDS_Shape RenderText (const NCollection_String& theString);
104
105   //! Render text as BRep shape.
106   //! @param theString text in UTF-8 encoding
107   //! @param thePenLoc start position and orientation on the baseline
108   //! @return result shape with pen transformation applied as shape location
109   TopoDS_Shape RenderText (const NCollection_String& theString,
110                            const gp_Ax3&             thePenLoc)
111   {
112     TopoDS_Shape aResult = RenderText (theString);
113     gp_Trsf aTrsf;
114     aTrsf.SetTransformation (thePenLoc, gp_Ax3 (gp::XOY()));
115     aResult.Move (aTrsf);
116     return aResult;
117   }
118
119 public:
120
121   //! @return vertical distance from the horizontal baseline to the highest character coordinate.
122   Standard_Real Ascender() const
123   {
124     return myScaleUnits * Standard_Real(Font_FTFont::Ascender());
125   }
126
127   //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
128   Standard_Real Descender() const
129   {
130     return myScaleUnits * Standard_Real(Font_FTFont::Descender());
131   }
132
133   //! @return default line spacing (the baseline-to-baseline distance).
134   Standard_Real LineSpacing() const
135   {
136     return myScaleUnits * Standard_Real(Font_FTFont::LineSpacing());
137   }
138
139   //! Configured point size
140   Standard_Real PointSize() const
141   {
142     return myScaleUnits * Standard_Real(Font_FTFont::PointSize());
143   }
144
145   //! Compute advance to the next character with kerning applied when applicable.
146   //! Assuming text rendered horizontally.
147   Standard_Real AdvanceX (const Standard_Utf32Char theUCharNext)
148   {
149     return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUCharNext));
150   }
151
152   //! Compute advance to the next character with kerning applied when applicable.
153   //! Assuming text rendered horizontally.
154   Standard_Real AdvanceX (const Standard_Utf32Char theUChar,
155                           const Standard_Utf32Char theUCharNext)
156   {
157     return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUChar, theUCharNext));
158   }
159
160   //! Compute advance to the next character with kerning applied when applicable.
161   //! Assuming text rendered vertically.
162   Standard_Real AdvanceY (const Standard_Utf32Char theUCharNext)
163   {
164     return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUCharNext));
165   }
166
167   //! Compute advance to the next character with kerning applied when applicable.
168   //! Assuming text rendered vertically.
169   Standard_Real AdvanceY (const Standard_Utf32Char theUChar,
170                           const Standard_Utf32Char theUCharNext)
171   {
172     return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUChar, theUCharNext));
173   }
174
175 protected:
176
177   //! Render single glyph as TopoDS_Shape. This method does not lock the mutex.
178   //! @param theChar  glyph identifier
179   //! @param theShape rendered glyph within cache, might be NULL shape
180   //! @return true if glyph's geometry is available
181   Standard_EXPORT Standard_Boolean renderGlyph (const Standard_Utf32Char theChar,
182                                                 TopoDS_Shape&            theShape);
183
184 private:
185
186   //! Initialize class fields
187   void init();
188
189   //! Auxiliary method to create 3D curve
190   bool to3d (const Handle(Geom2d_Curve) theCurve2d,
191              const GeomAbs_Shape        theContinuity,
192              Handle(Geom_Curve)&        theCurve3d);
193
194   //! Auxiliary method to convert FT_Vector to gp_XY
195   gp_XY readFTVec (const FT_Vector& theVec) const
196   {
197     return gp_XY (myScaleUnits * Standard_Real(theVec.x) / 64.0, myScaleUnits * Standard_Real(theVec.y) / 64.0);
198   }
199
200 protected: //! @name Protected fields
201
202   NCollection_DataMap<Standard_Utf32Char, TopoDS_Shape>
203                        myCache;            //!< glyphs cache
204   Standard_Mutex       myMutex;            //!< lock for thread-safety
205   Handle(Geom_Surface) mySurface;          //!< surface to place glyphs on to
206   Standard_Real        myPrecision;        //!< algorithm precision
207   Standard_Real        myScaleUnits;       //!< scale font rendering units into model units
208   Standard_Boolean     myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, ON by default
209
210 protected: //! @name Shared temporary variables for glyph construction
211
212   Adaptor3d_CurveOnSurface myCurvOnSurf;
213   Handle(Geom2dAdaptor_HCurve) myCurve2dAdaptor;
214   Geom2dConvert_CompCurveToBSplineCurve myConcatMaker;
215   TColgp_Array1OfPnt2d     my3Poles;
216   TColgp_Array1OfPnt2d     my4Poles;
217   BRep_Builder             myBuilder;
218   ShapeFix_Face            myFixer;
219
220 public:
221
222   DEFINE_STANDARD_RTTI(Font_BRepFont)
223
224 };
225
226 // Definition of HANDLE object using Standard_DefineHandle.hxx
227 DEFINE_STANDARD_HANDLE(Font_BRepFont, Font_FTFont)
228
229 #endif // _Font_BRepFont_H__