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 <TColgp_Array1OfPnt2d.hxx>
31 #include <TopoDS_Shape.hxx>
32 #include <TopoDS_Face.hxx>
33 #include <TopTools_SequenceOfShape.hxx>
35 DEFINE_STANDARD_HANDLE(Font_BRepFont, Font_FTFont)
37 //! This tool provides basic services for rendering of vectorized text glyphs as BRep shapes.
38 //! Single instance initialize single font for sequential glyphs rendering with implicit caching of already rendered glyphs.
39 //! Thus position of each glyph in the text is specified by shape location.
41 //! Please notice that this implementation uses mutex for thread-safety access,
42 //! thus may lead to performance penalties in case of concurrent access.
43 //! Although caching should eliminate this issue after rendering of sufficient number of glyphs.
44 class Font_BRepFont : protected Font_FTFont
46 DEFINE_STANDARD_RTTIEXT(Font_BRepFont, Font_FTFont)
50 Standard_EXPORT Font_BRepFont();
52 //! Constructor with initialization.
53 //! @param theFontPath FULL path to the font
54 //! @param theSize the face size in model units
55 Standard_EXPORT Font_BRepFont (const NCollection_String& theFontPath,
56 const Standard_Real theSize);
58 //! Constructor with initialization.
59 //! @param theFontName the font name
60 //! @param theFontAspect the font style
61 //! @param theSize the face size in model units
62 //! @param theStrictLevel search strict level for using aliases and fallback
63 Standard_EXPORT Font_BRepFont (const NCollection_String& theFontName,
64 const Font_FontAspect theFontAspect,
65 const Standard_Real theSize,
66 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
68 //! Release currently loaded font.
69 Standard_EXPORT virtual void Release() Standard_OVERRIDE;
71 //! Initialize the font.
72 //! @param theFontPath FULL path to the font
73 //! @param theSize the face size in model units
74 //! @return true on success
75 Standard_EXPORT bool Init (const NCollection_String& theFontPath,
76 const Standard_Real theSize);
78 //! Find (using Font_FontMgr) and initialize the font from the given name.
79 //! Please take into account that size is specified NOT in typography points (pt.).
80 //! If you need to specify size in points, value should be converted.
81 //! Formula for pt. -> m conversion:
82 //! aSizeMeters = 0.0254 * theSizePt / 72.0
83 //! @param theFontName the font name
84 //! @param theFontAspect the font style
85 //! @param theSize the face size in model units
86 //! @param theStrictLevel search strict level for using aliases and fallback
87 //! @return true on success
88 Standard_EXPORT bool FindAndInit (const TCollection_AsciiString& theFontName,
89 const Font_FontAspect theFontAspect,
90 const Standard_Real theSize,
91 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
93 //! Render single glyph as TopoDS_Shape.
94 //! @param theChar glyph identifier
95 //! @return rendered glyph within cache, might be NULL shape
96 Standard_EXPORT TopoDS_Shape RenderGlyph (const Standard_Utf32Char& theChar);
98 //! Setup glyph geometry construction mode.
99 //! By default algorithm creates independent TopoDS_Edge
100 //! for each original curve in the glyph (line segment or Bezie curve).
101 //! Algorithm might optionally create composite BSpline curve for each contour
102 //! which reduces memory footprint but limits curve class to C0.
103 //! Notice that altering this flag clears currently accumulated cache!
104 Standard_EXPORT void SetCompositeCurveMode (const Standard_Boolean theToConcatenate);
106 //! Setup glyph scaling along X-axis.
107 //! By default glyphs are not scaled (scaling factor = 1.0)
108 void SetWidthScaling (const float theScaleFactor)
110 myWidthScaling = theScaleFactor;
115 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
116 Standard_Real Ascender() const
118 return myScaleUnits * Standard_Real(Font_FTFont::Ascender());
121 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
122 Standard_Real Descender() const
124 return myScaleUnits * Standard_Real(Font_FTFont::Descender());
127 //! @return default line spacing (the baseline-to-baseline distance).
128 Standard_Real LineSpacing() const
130 return myScaleUnits * Standard_Real(Font_FTFont::LineSpacing());
133 //! Configured point size
134 Standard_Real PointSize() const
136 return myScaleUnits * Standard_Real(Font_FTFont::PointSize());
139 //! Compute advance to the next character with kerning applied when applicable.
140 //! Assuming text rendered horizontally.
141 Standard_Real AdvanceX (const Standard_Utf32Char theUCharNext)
143 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUCharNext));
146 //! Compute advance to the next character with kerning applied when applicable.
147 //! Assuming text rendered horizontally.
148 Standard_Real AdvanceX (const Standard_Utf32Char theUChar,
149 const Standard_Utf32Char theUCharNext)
151 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUChar, theUCharNext));
154 //! Compute advance to the next character with kerning applied when applicable.
155 //! Assuming text rendered vertically.
156 Standard_Real AdvanceY (const Standard_Utf32Char theUCharNext)
158 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUCharNext));
161 //! Compute advance to the next character with kerning applied when applicable.
162 //! Assuming text rendered vertically.
163 Standard_Real AdvanceY (const Standard_Utf32Char theUChar,
164 const Standard_Utf32Char theUCharNext)
166 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUChar, theUCharNext));
169 //! Returns scaling factor for current font size.
170 Standard_Real Scale() const
176 Standard_Mutex& Mutex()
183 //! Find (using Font_FontMgr) and initialize the font from the given name.
184 //! Alias for FindAndInit() for backward compatibility.
185 bool Init (const NCollection_String& theFontName,
186 const Font_FontAspect theFontAspect,
187 const Standard_Real theSize)
189 return FindAndInit (theFontName.ToCString(), theFontAspect, theSize, Font_StrictLevel_Any);
194 //! Render single glyph as TopoDS_Shape. This method does not lock the mutex.
195 //! @param theChar glyph identifier
196 //! @param theShape rendered glyph within cache, might be NULL shape
197 //! @return true if glyph's geometry is available
198 Standard_EXPORT Standard_Boolean renderGlyph (const Standard_Utf32Char theChar,
199 TopoDS_Shape& theShape);
203 //! Initialize class fields
206 //! Auxiliary method to create 3D curve
207 bool to3d (const Handle(Geom2d_Curve)& theCurve2d,
208 const GeomAbs_Shape theContinuity,
209 Handle(Geom_Curve)& theCurve3d);
211 //! Auxiliary method for creation faces from sequence of wires.
212 //! Splits to few faces (if it is needed) and updates orientation of wires.
213 Standard_Boolean buildFaces (const NCollection_Sequence<TopoDS_Wire>& theWires,
214 TopoDS_Shape& theRes);
217 using Font_FTFont::FindAndCreate;
219 protected: //! @name Protected fields
221 NCollection_DataMap<Standard_Utf32Char, TopoDS_Shape>
222 myCache; //!< glyphs cache
223 Standard_Mutex myMutex; //!< lock for thread-safety
224 Handle(Geom_Surface) mySurface; //!< surface to place glyphs on to
225 Standard_Real myPrecision; //!< algorithm precision
226 Standard_Real myScaleUnits; //!< scale font rendering units into model units
227 Standard_Boolean myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default
229 protected: //! @name Shared temporary variables for glyph construction
231 Adaptor3d_CurveOnSurface myCurvOnSurf;
232 Handle(Geom2dAdaptor_HCurve) myCurve2dAdaptor;
233 Geom2dConvert_CompCurveToBSplineCurve myConcatMaker;
234 TColgp_Array1OfPnt2d my3Poles;
235 TColgp_Array1OfPnt2d my4Poles;
236 BRep_Builder myBuilder;
240 #endif // _Font_BRepFont_H__