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