Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-16 09:18:33

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 <gp_Pnt2d.hxx>
0027 #include <NCollection_Array1.hxx>
0028 #include <TopoDS_Shape.hxx>
0029 #include <NCollection_Sequence.hxx>
0030 
0031 #include <mutex>
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
0035 //! already rendered glyphs. 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   //! Find the font Initialize the font.
0045   //! @param theFontName    the font name
0046   //! @param theFontAspect  the font style
0047   //! @param theSize        the face size in model units
0048   //! @param theStrictLevel search strict level for using aliases and fallback
0049   //! @return true on success
0050   Standard_EXPORT static occ::handle<StdPrs_BRepFont> FindAndCreate(
0051     const TCollection_AsciiString& theFontName,
0052     const Font_FontAspect          theFontAspect,
0053     const double                   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 double              theSize,
0065                                   const int                 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 double              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 double              theSize,
0087                             const int                 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 double                   theSize,
0102                                    const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
0103 
0104   //! Return wrapper over FreeType font.
0105   const occ::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 char32_t& 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 bool 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) { myFTFont->SetWidthScaling(theScaleFactor); }
0123 
0124 public:
0125   //! @return vertical distance from the horizontal baseline to the highest character coordinate.
0126   double Ascender() const { return myScaleUnits * double(myFTFont->Ascender()); }
0127 
0128   //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
0129   double Descender() const { return myScaleUnits * double(myFTFont->Descender()); }
0130 
0131   //! @return default line spacing (the baseline-to-baseline distance).
0132   double LineSpacing() const { return myScaleUnits * double(myFTFont->LineSpacing()); }
0133 
0134   //! Configured point size
0135   double PointSize() const { return myScaleUnits * double(myFTFont->PointSize()); }
0136 
0137   //! Compute advance to the next character with kerning applied when applicable.
0138   //! Assuming text rendered horizontally.
0139   double AdvanceX(const char32_t theUCharNext)
0140   {
0141     return myScaleUnits * double(myFTFont->AdvanceX(theUCharNext));
0142   }
0143 
0144   //! Compute advance to the next character with kerning applied when applicable.
0145   //! Assuming text rendered horizontally.
0146   double AdvanceX(const char32_t theUChar, const char32_t theUCharNext)
0147   {
0148     return myScaleUnits * double(myFTFont->AdvanceX(theUChar, theUCharNext));
0149   }
0150 
0151   //! Compute advance to the next character with kerning applied when applicable.
0152   //! Assuming text rendered vertically.
0153   double AdvanceY(const char32_t theUCharNext)
0154   {
0155     return myScaleUnits * double(myFTFont->AdvanceY(theUCharNext));
0156   }
0157 
0158   //! Compute advance to the next character with kerning applied when applicable.
0159   //! Assuming text rendered vertically.
0160   double AdvanceY(const char32_t theUChar, const char32_t theUCharNext)
0161   {
0162     return myScaleUnits * double(myFTFont->AdvanceY(theUChar, theUCharNext));
0163   }
0164 
0165   //! Returns scaling factor for current font size.
0166   double Scale() const { return myScaleUnits; }
0167 
0168   //! Returns mutex.
0169   std::mutex& Mutex() { return myMutex; }
0170 
0171 public:
0172   //! Find (using Font_FontMgr) and initialize the font from the given name.
0173   //! Alias for FindAndInit() for backward compatibility.
0174   bool Init(const NCollection_String& theFontName,
0175             const Font_FontAspect     theFontAspect,
0176             const double              theSize)
0177   {
0178     return FindAndInit(theFontName.ToCString(), theFontAspect, theSize, Font_StrictLevel_Any);
0179   }
0180 
0181 protected:
0182   //! Render single glyph as TopoDS_Shape. This method does not lock the mutex.
0183   //! @param theChar  glyph identifier
0184   //! @param theShape rendered glyph within cache, might be NULL shape
0185   //! @return true if glyph's geometry is available
0186   Standard_EXPORT bool renderGlyph(const char32_t theChar, TopoDS_Shape& theShape);
0187 
0188 private:
0189   //! Initialize class fields
0190   void init();
0191 
0192   //! Auxiliary method to create 3D curve
0193   bool to3d(const occ::handle<Geom2d_Curve>& theCurve2d,
0194             const GeomAbs_Shape              theContinuity,
0195             occ::handle<Geom_Curve>&         theCurve3d);
0196 
0197   //! Auxiliary method for creation faces from sequence of wires.
0198   //! Splits to few faces (if it is needed) and updates orientation of wires.
0199   bool buildFaces(const NCollection_Sequence<TopoDS_Wire>& theWires, TopoDS_Shape& theRes);
0200 
0201 protected:                                                 //! @name Protected fields
0202   occ::handle<Font_FTFont>                    myFTFont;    //!< wrapper over FreeType font
0203   NCollection_DataMap<char32_t, TopoDS_Shape> myCache;     //!< glyphs cache
0204   std::mutex                                  myMutex;     //!< lock for thread-safety
0205   occ::handle<Geom_Surface>                   mySurface;   //!< surface to place glyphs on to
0206   double                                      myPrecision; //!< algorithm precision
0207   double myScaleUnits; //!< scale font rendering units into model units
0208   // clang-format off
0209   bool     myIsCompositeCurve; //!< flag to merge C1 curves of each contour into single C0 curve, OFF by default
0210   // clang-format on
0211 
0212 protected: //! @name Shared temporary variables for glyph construction
0213   Adaptor3d_CurveOnSurface              myCurvOnSurf;
0214   occ::handle<Geom2dAdaptor_Curve>      myCurve2dAdaptor;
0215   Geom2dConvert_CompCurveToBSplineCurve myConcatMaker;
0216   NCollection_Array1<gp_Pnt2d>          my3Poles;
0217   NCollection_Array1<gp_Pnt2d>          my4Poles;
0218   BRep_Builder                          myBuilder;
0219 };
0220 
0221 #endif // _StdPrs_BRepFont_H__