1 // Created on: 2013-09-16
2 // Copyright (c) 2013-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 #ifndef _Font_BRepFont_H__
16 #define _Font_BRepFont_H__
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>
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>
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.
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
47 Standard_EXPORT Font_BRepFont();
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);
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);
63 //! Release currently loaded font.
64 Standard_EXPORT virtual void Release() Standard_OVERRIDE;
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);
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);
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);
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);
101 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
102 Standard_Real Ascender() const
104 return myScaleUnits * Standard_Real(Font_FTFont::Ascender());
107 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
108 Standard_Real Descender() const
110 return myScaleUnits * Standard_Real(Font_FTFont::Descender());
113 //! @return default line spacing (the baseline-to-baseline distance).
114 Standard_Real LineSpacing() const
116 return myScaleUnits * Standard_Real(Font_FTFont::LineSpacing());
119 //! Configured point size
120 Standard_Real PointSize() const
122 return myScaleUnits * Standard_Real(Font_FTFont::PointSize());
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)
129 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUCharNext));
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)
137 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUChar, theUCharNext));
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)
144 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUCharNext));
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)
152 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUChar, theUCharNext));
155 //! Returns scaling factor for current font size.
156 Standard_Real Scale() const
162 Standard_Mutex& Mutex()
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);
178 //! Initialize class fields
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);
186 //! Auxiliary method to convert FT_Vector to gp_XY
187 gp_XY readFTVec (const FT_Vector& theVec) const
189 return gp_XY (myScaleUnits * Standard_Real(theVec.x) / 64.0, myScaleUnits * Standard_Real(theVec.y) / 64.0);
192 protected: //! @name Protected fields
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
202 protected: //! @name Shared temporary variables for glyph construction
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;
214 DEFINE_STANDARD_RTTIEXT(Font_BRepFont,Font_FTFont)
218 // Definition of HANDLE object using Standard_DefineHandle.hxx
219 DEFINE_STANDARD_HANDLE(Font_BRepFont, Font_FTFont)
221 #endif // _Font_BRepFont_H__