7f24b768 |
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 _StdPrs_BRepFont_H__ |
16 | #define _StdPrs_BRepFont_H__ |
17 | |
18 | #include <Adaptor3d_CurveOnSurface.hxx> |
19 | #include <BRep_Builder.hxx> |
20 | #include <Font_FTFont.hxx> |
21 | #include <Font_TextFormatter.hxx> |
c22b52d6 |
22 | #include <Geom2dAdaptor_Curve.hxx> |
7f24b768 |
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 <TColgp_Array1OfPnt2d.hxx> |
31 | #include <TopoDS_Shape.hxx> |
32 | #include <TopoDS_Face.hxx> |
33 | #include <TopTools_SequenceOfShape.hxx> |
34 | |
ac5b3cbc |
35 | DEFINE_STANDARD_HANDLE(StdPrs_BRepFont, Standard_Transient) |
7f24b768 |
36 | |
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. |
40 | //! |
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. |
ac5b3cbc |
44 | class StdPrs_BRepFont : public Standard_Transient |
7f24b768 |
45 | { |
ac5b3cbc |
46 | DEFINE_STANDARD_RTTIEXT(StdPrs_BRepFont, Standard_Transient) |
7f24b768 |
47 | public: |
48 | |
ac5b3cbc |
49 | //! Find the font Initialize the font. |
50 | //! @param theFontName the font name |
51 | //! @param theFontAspect the font style |
52 | //! @param theSize the face size in model units |
53 | //! @param theStrictLevel search strict level for using aliases and fallback |
54 | //! @return true on success |
55 | Standard_EXPORT static Handle(StdPrs_BRepFont) FindAndCreate (const TCollection_AsciiString& theFontName, |
56 | const Font_FontAspect theFontAspect, |
57 | const Standard_Real theSize, |
58 | const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any); |
59 | |
7f24b768 |
60 | //! Empty constructor |
61 | Standard_EXPORT StdPrs_BRepFont(); |
62 | |
63 | //! Constructor with initialization. |
64 | //! @param theFontPath FULL path to the font |
65 | //! @param theSize the face size in model units |
66 | //! @param theFaceId face id within the file (0 by default) |
67 | Standard_EXPORT StdPrs_BRepFont (const NCollection_String& theFontPath, |
68 | const Standard_Real theSize, |
69 | const Standard_Integer theFaceId = 0); |
70 | |
71 | //! Constructor with initialization. |
72 | //! @param theFontName the font name |
73 | //! @param theFontAspect the font style |
74 | //! @param theSize the face size in model units |
75 | //! @param theStrictLevel search strict level for using aliases and fallback |
76 | Standard_EXPORT StdPrs_BRepFont (const NCollection_String& theFontName, |
77 | const Font_FontAspect theFontAspect, |
78 | const Standard_Real theSize, |
79 | const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any); |
80 | |
81 | //! Release currently loaded font. |
ac5b3cbc |
82 | Standard_EXPORT virtual void Release(); |
7f24b768 |
83 | |
84 | //! Initialize the font. |
85 | //! @param theFontPath FULL path to the font |
86 | //! @param theSize the face size in model units |
87 | //! @param theFaceId face id within the file (0 by default) |
88 | //! @return true on success |
89 | Standard_EXPORT bool Init (const NCollection_String& theFontPath, |
90 | const Standard_Real theSize, |
91 | const Standard_Integer theFaceId); |
92 | |
93 | //! Find (using Font_FontMgr) and initialize the font from the given name. |
94 | //! Please take into account that size is specified NOT in typography points (pt.). |
95 | //! If you need to specify size in points, value should be converted. |
96 | //! Formula for pt. -> m conversion: |
97 | //! aSizeMeters = 0.0254 * theSizePt / 72.0 |
98 | //! @param theFontName the font name |
99 | //! @param theFontAspect the font style |
100 | //! @param theSize the face size in model units |
101 | //! @param theStrictLevel search strict level for using aliases and fallback |
102 | //! @return true on success |
103 | Standard_EXPORT bool FindAndInit (const TCollection_AsciiString& theFontName, |
104 | const Font_FontAspect theFontAspect, |
105 | const Standard_Real theSize, |
106 | const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any); |
107 | |
ac5b3cbc |
108 | //! Return wrapper over FreeType font. |
109 | const Handle(Font_FTFont)& FTFont() const { return myFTFont; } |
110 | |
7f24b768 |
111 | //! Render single glyph as TopoDS_Shape. |
112 | //! @param theChar glyph identifier |
113 | //! @return rendered glyph within cache, might be NULL shape |
114 | Standard_EXPORT TopoDS_Shape RenderGlyph (const Standard_Utf32Char& theChar); |
115 | |
116 | //! Setup glyph geometry construction mode. |
117 | //! By default algorithm creates independent TopoDS_Edge |
118 | //! for each original curve in the glyph (line segment or Bezie curve). |
119 | //! Algorithm might optionally create composite BSpline curve for each contour |
120 | //! which reduces memory footprint but limits curve class to C0. |
121 | //! Notice that altering this flag clears currently accumulated cache! |
122 | Standard_EXPORT void SetCompositeCurveMode (const Standard_Boolean theToConcatenate); |
123 | |
124 | //! Setup glyph scaling along X-axis. |
125 | //! By default glyphs are not scaled (scaling factor = 1.0) |
126 | void SetWidthScaling (const float theScaleFactor) |
127 | { |
ac5b3cbc |
128 | myFTFont->SetWidthScaling (theScaleFactor); |
7f24b768 |
129 | } |
130 | |
131 | public: |
132 | |
133 | //! @return vertical distance from the horizontal baseline to the highest character coordinate. |
134 | Standard_Real Ascender() const |
135 | { |
ac5b3cbc |
136 | return myScaleUnits * Standard_Real(myFTFont->Ascender()); |
7f24b768 |
137 | } |
138 | |
139 | //! @return vertical distance from the horizontal baseline to the lowest character coordinate. |
140 | Standard_Real Descender() const |
141 | { |
ac5b3cbc |
142 | return myScaleUnits * Standard_Real(myFTFont->Descender()); |
7f24b768 |
143 | } |
144 | |
145 | //! @return default line spacing (the baseline-to-baseline distance). |
146 | Standard_Real LineSpacing() const |
147 | { |
ac5b3cbc |
148 | return myScaleUnits * Standard_Real(myFTFont->LineSpacing()); |
7f24b768 |
149 | } |
150 | |
151 | //! Configured point size |
152 | Standard_Real PointSize() const |
153 | { |
ac5b3cbc |
154 | return myScaleUnits * Standard_Real(myFTFont->PointSize()); |
7f24b768 |
155 | } |
156 | |
157 | //! Compute advance to the next character with kerning applied when applicable. |
158 | //! Assuming text rendered horizontally. |
159 | Standard_Real AdvanceX (const Standard_Utf32Char theUCharNext) |
160 | { |
ac5b3cbc |
161 | return myScaleUnits * Standard_Real(myFTFont->AdvanceX (theUCharNext)); |
7f24b768 |
162 | } |
163 | |
164 | //! Compute advance to the next character with kerning applied when applicable. |
165 | //! Assuming text rendered horizontally. |
166 | Standard_Real AdvanceX (const Standard_Utf32Char theUChar, |
167 | const Standard_Utf32Char theUCharNext) |
168 | { |
ac5b3cbc |
169 | return myScaleUnits * Standard_Real(myFTFont->AdvanceX (theUChar, theUCharNext)); |
7f24b768 |
170 | } |
171 | |
172 | //! Compute advance to the next character with kerning applied when applicable. |
173 | //! Assuming text rendered vertically. |
174 | Standard_Real AdvanceY (const Standard_Utf32Char theUCharNext) |
175 | { |
ac5b3cbc |
176 | return myScaleUnits * Standard_Real(myFTFont->AdvanceY (theUCharNext)); |
7f24b768 |
177 | } |
178 | |
179 | //! Compute advance to the next character with kerning applied when applicable. |
180 | //! Assuming text rendered vertically. |
181 | Standard_Real AdvanceY (const Standard_Utf32Char theUChar, |
182 | const Standard_Utf32Char theUCharNext) |
183 | { |
ac5b3cbc |
184 | return myScaleUnits * Standard_Real(myFTFont->AdvanceY (theUChar, theUCharNext)); |
7f24b768 |
185 | } |
186 | |
187 | //! Returns scaling factor for current font size. |
188 | Standard_Real Scale() const |
189 | { |
190 | return myScaleUnits; |
191 | } |
192 | |
193 | //! Returns mutex. |
194 | Standard_Mutex& Mutex() |
195 | { |
196 | return myMutex; |
197 | } |
198 | |
199 | public: |
200 | |
201 | //! Find (using Font_FontMgr) and initialize the font from the given name. |
202 | //! Alias for FindAndInit() for backward compatibility. |
203 | bool Init (const NCollection_String& theFontName, |
204 | const Font_FontAspect theFontAspect, |
205 | const Standard_Real theSize) |
206 | { |
207 | return FindAndInit (theFontName.ToCString(), theFontAspect, theSize, Font_StrictLevel_Any); |
208 | } |
209 | |
210 | protected: |
211 | |
212 | //! Render single glyph as TopoDS_Shape. This method does not lock the mutex. |
213 | //! @param theChar glyph identifier |
214 | //! @param theShape rendered glyph within cache, might be NULL shape |
215 | //! @return true if glyph's geometry is available |
216 | Standard_EXPORT Standard_Boolean renderGlyph (const Standard_Utf32Char theChar, |
217 | TopoDS_Shape& theShape); |
218 | |
219 | private: |
220 | |
221 | //! Initialize class fields |
222 | void init(); |
223 | |
224 | //! Auxiliary method to create 3D curve |
225 | bool to3d (const Handle(Geom2d_Curve)& theCurve2d, |
226 | const GeomAbs_Shape theContinuity, |
227 | Handle(Geom_Curve)& theCurve3d); |
228 | |
229 | //! Auxiliary method for creation faces from sequence of wires. |
230 | //! Splits to few faces (if it is needed) and updates orientation of wires. |
231 | Standard_Boolean buildFaces (const NCollection_Sequence<TopoDS_Wire>& theWires, |
232 | TopoDS_Shape& theRes); |
233 | |
7f24b768 |
234 | protected: //! @name Protected fields |
235 | |
ac5b3cbc |
236 | Handle(Font_FTFont) myFTFont; //!< wrapper over FreeType font |
7f24b768 |
237 | NCollection_DataMap<Standard_Utf32Char, TopoDS_Shape> |
238 | myCache; //!< glyphs cache |
239 | Standard_Mutex myMutex; //!< lock for thread-safety |
240 | Handle(Geom_Surface) mySurface; //!< surface to place glyphs on to |
241 | Standard_Real myPrecision; //!< algorithm precision |
242 | Standard_Real myScaleUnits; //!< scale font rendering units into model units |
243 | Standard_Boolean myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default |
244 | |
245 | protected: //! @name Shared temporary variables for glyph construction |
246 | |
247 | Adaptor3d_CurveOnSurface myCurvOnSurf; |
c22b52d6 |
248 | Handle(Geom2dAdaptor_Curve) myCurve2dAdaptor; |
7f24b768 |
249 | Geom2dConvert_CompCurveToBSplineCurve myConcatMaker; |
250 | TColgp_Array1OfPnt2d my3Poles; |
251 | TColgp_Array1OfPnt2d my4Poles; |
252 | BRep_Builder myBuilder; |
253 | |
254 | }; |
255 | |
256 | #endif // _StdPrs_BRepFont_H__ |