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 _StdPrs_BRepFont_H__
16 #define _StdPrs_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_Curve.hxx>
23 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
24 #include <NCollection_DataMap.hxx>
25 #include <NCollection_String.hxx>
26 #include <Standard_Mutex.hxx>
27 #include <TColgp_Array1OfPnt2d.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_SequenceOfShape.hxx>
31 DEFINE_STANDARD_HANDLE(StdPrs_BRepFont, Standard_Transient)
33 //! This tool provides basic services for rendering of vectorized text glyphs as BRep shapes.
34 //! Single instance initialize single font for sequential glyphs rendering with implicit caching of already rendered glyphs.
35 //! Thus position of each glyph in the text is specified by shape location.
37 //! Please notice that this implementation uses mutex for thread-safety access,
38 //! thus may lead to performance penalties in case of concurrent access.
39 //! Although caching should eliminate this issue after rendering of sufficient number of glyphs.
40 class StdPrs_BRepFont : public Standard_Transient
42 DEFINE_STANDARD_RTTIEXT(StdPrs_BRepFont, Standard_Transient)
45 //! Find the font Initialize the font.
46 //! @param theFontName the font name
47 //! @param theFontAspect the font style
48 //! @param theSize the face size in model units
49 //! @param theStrictLevel search strict level for using aliases and fallback
50 //! @return true on success
51 Standard_EXPORT static Handle(StdPrs_BRepFont) FindAndCreate (const TCollection_AsciiString& theFontName,
52 const Font_FontAspect theFontAspect,
53 const Standard_Real theSize,
54 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
57 Standard_EXPORT StdPrs_BRepFont();
59 //! Constructor with initialization.
60 //! @param theFontPath FULL path to the font
61 //! @param theSize the face size in model units
62 //! @param theFaceId face id within the file (0 by default)
63 Standard_EXPORT StdPrs_BRepFont (const NCollection_String& theFontPath,
64 const Standard_Real theSize,
65 const Standard_Integer theFaceId = 0);
67 //! Constructor with initialization.
68 //! @param theFontName the font name
69 //! @param theFontAspect the font style
70 //! @param theSize the face size in model units
71 //! @param theStrictLevel search strict level for using aliases and fallback
72 Standard_EXPORT StdPrs_BRepFont (const NCollection_String& theFontName,
73 const Font_FontAspect theFontAspect,
74 const Standard_Real theSize,
75 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
77 //! Release currently loaded font.
78 Standard_EXPORT virtual void Release();
80 //! Initialize the font.
81 //! @param theFontPath FULL path to the font
82 //! @param theSize the face size in model units
83 //! @param theFaceId face id within the file (0 by default)
84 //! @return true on success
85 Standard_EXPORT bool Init (const NCollection_String& theFontPath,
86 const Standard_Real theSize,
87 const Standard_Integer theFaceId);
89 //! Find (using Font_FontMgr) and initialize the font from the given name.
90 //! Please take into account that size is specified NOT in typography points (pt.).
91 //! If you need to specify size in points, value should be converted.
92 //! Formula for pt. -> m conversion:
93 //! aSizeMeters = 0.0254 * theSizePt / 72.0
94 //! @param theFontName the font name
95 //! @param theFontAspect the font style
96 //! @param theSize the face size in model units
97 //! @param theStrictLevel search strict level for using aliases and fallback
98 //! @return true on success
99 Standard_EXPORT bool FindAndInit (const TCollection_AsciiString& theFontName,
100 const Font_FontAspect theFontAspect,
101 const Standard_Real theSize,
102 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
104 //! Return wrapper over FreeType font.
105 const Handle(Font_FTFont)& FTFont() const { return myFTFont; }
107 //! Render single glyph as TopoDS_Shape.
108 //! @param theChar glyph identifier
109 //! @return rendered glyph within cache, might be NULL shape
110 Standard_EXPORT TopoDS_Shape RenderGlyph (const Standard_Utf32Char& theChar);
112 //! Setup glyph geometry construction mode.
113 //! By default algorithm creates independent TopoDS_Edge
114 //! for each original curve in the glyph (line segment or Bezie curve).
115 //! Algorithm might optionally create composite BSpline curve for each contour
116 //! which reduces memory footprint but limits curve class to C0.
117 //! Notice that altering this flag clears currently accumulated cache!
118 Standard_EXPORT void SetCompositeCurveMode (const Standard_Boolean theToConcatenate);
120 //! Setup glyph scaling along X-axis.
121 //! By default glyphs are not scaled (scaling factor = 1.0)
122 void SetWidthScaling (const float theScaleFactor)
124 myFTFont->SetWidthScaling (theScaleFactor);
129 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
130 Standard_Real Ascender() const
132 return myScaleUnits * Standard_Real(myFTFont->Ascender());
135 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
136 Standard_Real Descender() const
138 return myScaleUnits * Standard_Real(myFTFont->Descender());
141 //! @return default line spacing (the baseline-to-baseline distance).
142 Standard_Real LineSpacing() const
144 return myScaleUnits * Standard_Real(myFTFont->LineSpacing());
147 //! Configured point size
148 Standard_Real PointSize() const
150 return myScaleUnits * Standard_Real(myFTFont->PointSize());
153 //! Compute advance to the next character with kerning applied when applicable.
154 //! Assuming text rendered horizontally.
155 Standard_Real AdvanceX (const Standard_Utf32Char theUCharNext)
157 return myScaleUnits * Standard_Real(myFTFont->AdvanceX (theUCharNext));
160 //! Compute advance to the next character with kerning applied when applicable.
161 //! Assuming text rendered horizontally.
162 Standard_Real AdvanceX (const Standard_Utf32Char theUChar,
163 const Standard_Utf32Char theUCharNext)
165 return myScaleUnits * Standard_Real(myFTFont->AdvanceX (theUChar, theUCharNext));
168 //! Compute advance to the next character with kerning applied when applicable.
169 //! Assuming text rendered vertically.
170 Standard_Real AdvanceY (const Standard_Utf32Char theUCharNext)
172 return myScaleUnits * Standard_Real(myFTFont->AdvanceY (theUCharNext));
175 //! Compute advance to the next character with kerning applied when applicable.
176 //! Assuming text rendered vertically.
177 Standard_Real AdvanceY (const Standard_Utf32Char theUChar,
178 const Standard_Utf32Char theUCharNext)
180 return myScaleUnits * Standard_Real(myFTFont->AdvanceY (theUChar, theUCharNext));
183 //! Returns scaling factor for current font size.
184 Standard_Real Scale() const
190 Standard_Mutex& Mutex()
197 //! Find (using Font_FontMgr) and initialize the font from the given name.
198 //! Alias for FindAndInit() for backward compatibility.
199 bool Init (const NCollection_String& theFontName,
200 const Font_FontAspect theFontAspect,
201 const Standard_Real theSize)
203 return FindAndInit (theFontName.ToCString(), theFontAspect, theSize, Font_StrictLevel_Any);
208 //! Render single glyph as TopoDS_Shape. This method does not lock the mutex.
209 //! @param theChar glyph identifier
210 //! @param theShape rendered glyph within cache, might be NULL shape
211 //! @return true if glyph's geometry is available
212 Standard_EXPORT Standard_Boolean renderGlyph (const Standard_Utf32Char theChar,
213 TopoDS_Shape& theShape);
217 //! Initialize class fields
220 //! Auxiliary method to create 3D curve
221 bool to3d (const Handle(Geom2d_Curve)& theCurve2d,
222 const GeomAbs_Shape theContinuity,
223 Handle(Geom_Curve)& theCurve3d);
225 //! Auxiliary method for creation faces from sequence of wires.
226 //! Splits to few faces (if it is needed) and updates orientation of wires.
227 Standard_Boolean buildFaces (const NCollection_Sequence<TopoDS_Wire>& theWires,
228 TopoDS_Shape& theRes);
230 protected: //! @name Protected fields
232 Handle(Font_FTFont) myFTFont; //!< wrapper over FreeType font
233 NCollection_DataMap<Standard_Utf32Char, TopoDS_Shape>
234 myCache; //!< glyphs cache
235 Standard_Mutex myMutex; //!< lock for thread-safety
236 Handle(Geom_Surface) mySurface; //!< surface to place glyphs on to
237 Standard_Real myPrecision; //!< algorithm precision
238 Standard_Real myScaleUnits; //!< scale font rendering units into model units
239 Standard_Boolean myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default
241 protected: //! @name Shared temporary variables for glyph construction
243 Adaptor3d_CurveOnSurface myCurvOnSurf;
244 Handle(Geom2dAdaptor_Curve) myCurve2dAdaptor;
245 Geom2dConvert_CompCurveToBSplineCurve myConcatMaker;
246 TColgp_Array1OfPnt2d my3Poles;
247 TColgp_Array1OfPnt2d my4Poles;
248 BRep_Builder myBuilder;
252 #endif // _StdPrs_BRepFont_H__