Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:05:00

0001 // Created on: 2013-09-16
0002 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0003 //
0004 // This file is part of Open CASCADE Technology software library.
0005 //
0006 // This library is free software; you can redistribute it and/or modify it under
0007 // the terms of the GNU Lesser General Public License version 2.1 as published
0008 // by the Free Software Foundation, with special exception defined in the file
0009 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0010 // distribution for complete text of the license and disclaimer of any warranty.
0011 //
0012 // Alternatively, this file may be used under the terms of Open CASCADE
0013 // commercial license or contractual agreement.
0014 
0015 #ifndef _StdPrs_BRepFont_H__
0016 #define _StdPrs_BRepFont_H__
0017 
0018 #include <Adaptor3d_CurveOnSurface.hxx>
0019 #include <BRep_Builder.hxx>
0020 #include <Font_FTFont.hxx>
0021 #include <Font_TextFormatter.hxx>
0022 #include <Geom2dAdaptor_Curve.hxx>
0023 #include <Geom2dConvert_CompCurveToBSplineCurve.hxx>
0024 #include <NCollection_DataMap.hxx>
0025 #include <NCollection_String.hxx>
0026 #include <Standard_Mutex.hxx>
0027 #include <TColgp_Array1OfPnt2d.hxx>
0028 #include <TopoDS_Shape.hxx>
0029 #include <TopTools_SequenceOfShape.hxx>
0030 
0031 DEFINE_STANDARD_HANDLE(StdPrs_BRepFont, Standard_Transient)
0032 
0033 //! This tool provides basic services for rendering of vectorized text glyphs as BRep shapes.
0034 //! Single instance initialize single font for sequential glyphs rendering with implicit caching of already rendered glyphs.
0035 //! Thus position of each glyph in the text is specified by shape location.
0036 //!
0037 //! Please notice that this implementation uses mutex for thread-safety access,
0038 //! thus may lead to performance penalties in case of concurrent access.
0039 //! Although caching should eliminate this issue after rendering of sufficient number of glyphs.
0040 class StdPrs_BRepFont : public Standard_Transient
0041 {
0042   DEFINE_STANDARD_RTTIEXT(StdPrs_BRepFont, Standard_Transient)
0043 public:
0044 
0045   //! Find the font Initialize the font.
0046   //! @param theFontName    the font name
0047   //! @param theFontAspect  the font style
0048   //! @param theSize        the face size in model units
0049   //! @param theStrictLevel search strict level for using aliases and fallback
0050   //! @return true on success
0051   Standard_EXPORT static Handle(StdPrs_BRepFont) FindAndCreate (const TCollection_AsciiString& theFontName,
0052                                                                 const Font_FontAspect     theFontAspect,
0053                                                                 const Standard_Real       theSize,
0054                                                                 const Font_StrictLevel    theStrictLevel = Font_StrictLevel_Any);
0055 
0056   //! Empty constructor
0057   Standard_EXPORT StdPrs_BRepFont();
0058 
0059   //! Constructor with initialization.
0060   //! @param theFontPath FULL path to the font
0061   //! @param theSize     the face size in model units
0062   //! @param theFaceId   face id within the file (0 by default)
0063   Standard_EXPORT StdPrs_BRepFont (const NCollection_String& theFontPath,
0064                                    const Standard_Real       theSize,
0065                                    const Standard_Integer    theFaceId = 0);
0066 
0067   //! Constructor with initialization.
0068   //! @param theFontName    the font name
0069   //! @param theFontAspect  the font style
0070   //! @param theSize        the face size in model units
0071   //! @param theStrictLevel search strict level for using aliases and fallback
0072   Standard_EXPORT StdPrs_BRepFont (const NCollection_String& theFontName,
0073                                    const Font_FontAspect     theFontAspect,
0074                                    const Standard_Real       theSize,
0075                                    const Font_StrictLevel    theStrictLevel = Font_StrictLevel_Any);
0076 
0077   //! Release currently loaded font.
0078   Standard_EXPORT virtual void Release();
0079 
0080   //! Initialize the font.
0081   //! @param theFontPath FULL path to the font
0082   //! @param theSize     the face size in model units
0083   //! @param theFaceId   face id within the file (0 by default)
0084   //! @return true on success
0085   Standard_EXPORT bool Init (const NCollection_String& theFontPath,
0086                              const Standard_Real       theSize,
0087                              const Standard_Integer    theFaceId);
0088 
0089   //! Find (using Font_FontMgr) and initialize the font from the given name.
0090   //! Please take into account that size is specified NOT in typography points (pt.).
0091   //! If you need to specify size in points, value should be converted.
0092   //! Formula for pt. -> m conversion:
0093   //!   aSizeMeters = 0.0254 * theSizePt / 72.0
0094   //! @param theFontName   the font name
0095   //! @param theFontAspect the font style
0096   //! @param theSize       the face size in model units
0097   //! @param theStrictLevel search strict level for using aliases and fallback
0098   //! @return true on success
0099   Standard_EXPORT bool FindAndInit (const TCollection_AsciiString& theFontName,
0100                                     const Font_FontAspect  theFontAspect,
0101                                     const Standard_Real    theSize,
0102                                     const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
0103 
0104   //! Return wrapper over FreeType font.
0105   const Handle(Font_FTFont)& FTFont() const { return myFTFont; }
0106 
0107   //! Render single glyph as TopoDS_Shape.
0108   //! @param theChar glyph identifier
0109   //! @return rendered glyph within cache, might be NULL shape
0110   Standard_EXPORT TopoDS_Shape RenderGlyph (const Standard_Utf32Char& theChar);
0111 
0112   //! Setup glyph geometry construction mode.
0113   //! By default algorithm creates independent TopoDS_Edge
0114   //! for each original curve in the glyph (line segment or Bezie curve).
0115   //! Algorithm might optionally create composite BSpline curve for each contour
0116   //! which reduces memory footprint but limits curve class to C0.
0117   //! Notice that altering this flag clears currently accumulated cache!
0118   Standard_EXPORT void SetCompositeCurveMode (const Standard_Boolean theToConcatenate);
0119 
0120   //! Setup glyph scaling along X-axis.
0121   //! By default glyphs are not scaled (scaling factor = 1.0)
0122   void SetWidthScaling (const float theScaleFactor)
0123   {
0124     myFTFont->SetWidthScaling (theScaleFactor);
0125   }
0126 
0127 public:
0128 
0129   //! @return vertical distance from the horizontal baseline to the highest character coordinate.
0130   Standard_Real Ascender() const
0131   {
0132     return myScaleUnits * Standard_Real(myFTFont->Ascender());
0133   }
0134 
0135   //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
0136   Standard_Real Descender() const
0137   {
0138     return myScaleUnits * Standard_Real(myFTFont->Descender());
0139   }
0140 
0141   //! @return default line spacing (the baseline-to-baseline distance).
0142   Standard_Real LineSpacing() const
0143   {
0144     return myScaleUnits * Standard_Real(myFTFont->LineSpacing());
0145   }
0146 
0147   //! Configured point size
0148   Standard_Real PointSize() const
0149   {
0150     return myScaleUnits * Standard_Real(myFTFont->PointSize());
0151   }
0152 
0153   //! Compute advance to the next character with kerning applied when applicable.
0154   //! Assuming text rendered horizontally.
0155   Standard_Real AdvanceX (const Standard_Utf32Char theUCharNext)
0156   {
0157     return myScaleUnits * Standard_Real(myFTFont->AdvanceX (theUCharNext));
0158   }
0159 
0160   //! Compute advance to the next character with kerning applied when applicable.
0161   //! Assuming text rendered horizontally.
0162   Standard_Real AdvanceX (const Standard_Utf32Char theUChar,
0163                           const Standard_Utf32Char theUCharNext)
0164   {
0165     return myScaleUnits * Standard_Real(myFTFont->AdvanceX (theUChar, theUCharNext));
0166   }
0167 
0168   //! Compute advance to the next character with kerning applied when applicable.
0169   //! Assuming text rendered vertically.
0170   Standard_Real AdvanceY (const Standard_Utf32Char theUCharNext)
0171   {
0172     return myScaleUnits * Standard_Real(myFTFont->AdvanceY (theUCharNext));
0173   }
0174 
0175   //! Compute advance to the next character with kerning applied when applicable.
0176   //! Assuming text rendered vertically.
0177   Standard_Real AdvanceY (const Standard_Utf32Char theUChar,
0178                           const Standard_Utf32Char theUCharNext)
0179   {
0180     return myScaleUnits * Standard_Real(myFTFont->AdvanceY (theUChar, theUCharNext));
0181   }
0182 
0183   //! Returns scaling factor for current font size.
0184   Standard_Real Scale() const
0185   {
0186     return myScaleUnits;
0187   }
0188 
0189   //! Returns mutex.
0190   Standard_Mutex& Mutex()
0191   {
0192     return myMutex;
0193   }
0194 
0195 public:
0196 
0197   //! Find (using Font_FontMgr) and initialize the font from the given name.
0198   //! Alias for FindAndInit() for backward compatibility.
0199   bool Init (const NCollection_String& theFontName,
0200              const Font_FontAspect     theFontAspect,
0201              const Standard_Real       theSize)
0202   {
0203     return FindAndInit (theFontName.ToCString(), theFontAspect, theSize, Font_StrictLevel_Any);
0204   }
0205 
0206 protected:
0207 
0208   //! Render single glyph as TopoDS_Shape. This method does not lock the mutex.
0209   //! @param theChar  glyph identifier
0210   //! @param theShape rendered glyph within cache, might be NULL shape
0211   //! @return true if glyph's geometry is available
0212   Standard_EXPORT Standard_Boolean renderGlyph (const Standard_Utf32Char theChar,
0213                                                 TopoDS_Shape&            theShape);
0214 
0215 private:
0216 
0217   //! Initialize class fields
0218   void init();
0219 
0220   //! Auxiliary method to create 3D curve
0221   bool to3d (const Handle(Geom2d_Curve)& theCurve2d,
0222              const GeomAbs_Shape        theContinuity,
0223              Handle(Geom_Curve)&        theCurve3d);
0224 
0225   //! Auxiliary method for creation faces from sequence of wires.
0226   //! Splits to few faces (if it is needed) and updates orientation of wires.
0227   Standard_Boolean buildFaces (const NCollection_Sequence<TopoDS_Wire>& theWires,
0228                                TopoDS_Shape& theRes);
0229 
0230 protected: //! @name Protected fields
0231 
0232   Handle(Font_FTFont) myFTFont;            //!< wrapper over FreeType font
0233   NCollection_DataMap<Standard_Utf32Char, TopoDS_Shape>
0234                        myCache;            //!< glyphs cache
0235   Standard_Mutex       myMutex;            //!< lock for thread-safety
0236   Handle(Geom_Surface) mySurface;          //!< surface to place glyphs on to
0237   Standard_Real        myPrecision;        //!< algorithm precision
0238   Standard_Real        myScaleUnits;       //!< scale font rendering units into model units
0239   Standard_Boolean     myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default
0240 
0241 protected: //! @name Shared temporary variables for glyph construction
0242 
0243   Adaptor3d_CurveOnSurface myCurvOnSurf;
0244   Handle(Geom2dAdaptor_Curve) myCurve2dAdaptor;
0245   Geom2dConvert_CompCurveToBSplineCurve myConcatMaker;
0246   TColgp_Array1OfPnt2d     my3Poles;
0247   TColgp_Array1OfPnt2d     my4Poles;
0248   BRep_Builder             myBuilder;
0249 
0250 };
0251 
0252 #endif // _StdPrs_BRepFont_H__