0030675: Visualization - remove redundant proxy classes in hierarchy of PrsMgr_Presen...
[occt.git] / src / Font / Font_BRepFont.hxx
CommitLineData
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
1bbd7c79 35DEFINE_STANDARD_HANDLE(Font_BRepFont, Font_FTFont)
b514beda 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.
44class Font_BRepFont : protected Font_FTFont
45{
1bbd7c79 46 DEFINE_STANDARD_RTTIEXT(Font_BRepFont, Font_FTFont)
b514beda 47public:
48
49 //! Empty constructor
50 Standard_EXPORT Font_BRepFont();
51
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);
57
58 //! Constructor with initialization.
1bbd7c79 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
b514beda 63 Standard_EXPORT Font_BRepFont (const NCollection_String& theFontName,
64 const Font_FontAspect theFontAspect,
1bbd7c79 65 const Standard_Real theSize,
66 const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
b514beda 67
68 //! Release currently loaded font.
79104795 69 Standard_EXPORT virtual void Release() Standard_OVERRIDE;
b514beda 70
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);
77
1bbd7c79 78 //! Find (using Font_FontMgr) and initialize the font from the given name.
b514beda 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
1bbd7c79 86 //! @param theStrictLevel search strict level for using aliases and fallback
b514beda 87 //! @return true on success
1bbd7c79 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);
b514beda 92
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);
97
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);
105
151da08b 106 //! Setup glyph scaling along X-axis.
107 //! By default glyphs are not scaled (scaling factor = 1.0)
108 void SetWidthScaling (const float theScaleFactor)
109 {
110 myWidthScaling = theScaleFactor;
111 }
112
b514beda 113public:
114
115 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
116 Standard_Real Ascender() const
117 {
118 return myScaleUnits * Standard_Real(Font_FTFont::Ascender());
119 }
120
121 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
122 Standard_Real Descender() const
123 {
124 return myScaleUnits * Standard_Real(Font_FTFont::Descender());
125 }
126
127 //! @return default line spacing (the baseline-to-baseline distance).
128 Standard_Real LineSpacing() const
129 {
130 return myScaleUnits * Standard_Real(Font_FTFont::LineSpacing());
131 }
132
133 //! Configured point size
134 Standard_Real PointSize() const
135 {
136 return myScaleUnits * Standard_Real(Font_FTFont::PointSize());
137 }
138
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)
142 {
143 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUCharNext));
144 }
145
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)
150 {
151 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceX (theUChar, theUCharNext));
152 }
153
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)
157 {
158 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUCharNext));
159 }
160
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)
165 {
166 return myScaleUnits * Standard_Real(Font_FTFont::AdvanceY (theUChar, theUCharNext));
167 }
168
ac84fcf6 169 //! Returns scaling factor for current font size.
170 Standard_Real Scale() const
171 {
172 return myScaleUnits;
173 }
174
175 //! Returns mutex.
176 Standard_Mutex& Mutex()
177 {
178 return myMutex;
179 }
180
1bbd7c79 181public:
182
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)
188 {
189 return FindAndInit (theFontName.ToCString(), theFontAspect, theSize, Font_StrictLevel_Any);
190 }
191
b514beda 192protected:
193
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);
200
201private:
202
203 //! Initialize class fields
204 void init();
205
206 //! Auxiliary method to create 3D curve
543a9964 207 bool to3d (const Handle(Geom2d_Curve)& theCurve2d,
b514beda 208 const GeomAbs_Shape theContinuity,
209 Handle(Geom_Curve)& theCurve3d);
210
3388cf17 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);
215
1bbd7c79 216 //! Hide visibility.
217 using Font_FTFont::FindAndCreate;
218
b514beda 219protected: //! @name Protected fields
220
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
ac84fcf6 227 Standard_Boolean myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default
b514beda 228
229protected: //! @name Shared temporary variables for glyph construction
230
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;
b514beda 237
b514beda 238};
239
b514beda 240#endif // _Font_BRepFont_H__