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