Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-27 08:39:25

0001 // Created on: 2013-01-28
0002 // Created by: Kirill GAVRILOV
0003 // Copyright (c) 2013-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #ifndef Font_FTFont_HeaderFile
0017 #define Font_FTFont_HeaderFile
0018 
0019 #include <Font_FontAspect.hxx>
0020 #include <Font_Hinting.hxx>
0021 #include <Font_Rect.hxx>
0022 #include <Font_StrictLevel.hxx>
0023 #include <Font_UnicodeSubset.hxx>
0024 #include <Graphic3d_HorizontalTextAlignment.hxx>
0025 #include <Graphic3d_VerticalTextAlignment.hxx>
0026 #include <Image_PixMap.hxx>
0027 #include <NCollection_String.hxx>
0028 #include <TCollection_AsciiString.hxx>
0029 
0030 // forward declarations to avoid including of FreeType headers
0031 typedef struct FT_FaceRec_* FT_Face;
0032 typedef struct FT_Vector_   FT_Vector;
0033 typedef struct FT_Outline_  FT_Outline;
0034 class Font_FTLibrary;
0035 
0036 //! Font initialization parameters.
0037 struct Font_FTFontParams
0038 {
0039   unsigned int PointSize;  //!< face size in points (1/72 inch)
0040                            // clang-format off
0041   unsigned int Resolution;                 //!< resolution of the target device in dpi for FT_Set_Char_Size()
0042   Font_Hinting FontHinting;                //!< request hinting (exclude FT_LOAD_NO_HINTING flag), Font_Hinting_Off by default;
0043                                            //!  hinting improves readability of thin text on low-resolution screen,
0044                                            //!  but adds distortions to original font depending on font family and font library version
0045   bool         ToSynthesizeItalic;         //!< generate italic style (e.g. for font family having no italic style); FALSE by default
0046                            // clang-format on
0047   bool IsSingleStrokeFont; //!< single-stroke (one-line) font, FALSE by default
0048 
0049   //! Empty constructor.
0050   Font_FTFontParams()
0051       : PointSize(0),
0052         Resolution(72u),
0053         FontHinting(Font_Hinting_Off),
0054         ToSynthesizeItalic(false),
0055         IsSingleStrokeFont(false)
0056   {
0057   }
0058 
0059   //! Constructor.
0060   Font_FTFontParams(unsigned int thePointSize, unsigned int theResolution)
0061       : PointSize(thePointSize),
0062         Resolution(theResolution),
0063         FontHinting(Font_Hinting_Off),
0064         ToSynthesizeItalic(false),
0065         IsSingleStrokeFont(false)
0066   {
0067   }
0068 };
0069 
0070 DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
0071 
0072 //! Wrapper over FreeType font.
0073 //! Notice that this class uses internal buffers for loaded glyphs
0074 //! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
0075 class Font_FTFont : public Standard_Transient
0076 {
0077   DEFINE_STANDARD_RTTIEXT(Font_FTFont, Standard_Transient)
0078 public:
0079   //! Find the font Initialize the font.
0080   //! @param theFontName    the font name
0081   //! @param theFontAspect  the font style
0082   //! @param theParams      initialization parameters
0083   //! @param theStrictLevel search strict level for using aliases and fallback
0084   //! @return true on success
0085   Standard_EXPORT static Handle(Font_FTFont) FindAndCreate(
0086     const TCollection_AsciiString& theFontName,
0087     const Font_FontAspect          theFontAspect,
0088     const Font_FTFontParams&       theParams,
0089     const Font_StrictLevel         theStrictLevel = Font_StrictLevel_Any);
0090 
0091   //! Return TRUE if specified character is within subset of modern CJK characters.
0092   static bool IsCharFromCJK(Standard_Utf32Char theUChar)
0093   {
0094     return (theUChar >= 0x03400 && theUChar <= 0x04DFF)
0095            || (theUChar >= 0x04E00 && theUChar <= 0x09FFF)
0096            || (theUChar >= 0x0F900 && theUChar <= 0x0FAFF)
0097            || (theUChar >= 0x20000 && theUChar <= 0x2A6DF)
0098            || (theUChar >= 0x2F800 && theUChar <= 0x2FA1F)
0099            // Hiragana and Katakana (Japanese) are NOT part of CJK, but CJK fonts usually include
0100            // these symbols
0101            || IsCharFromHiragana(theUChar) || IsCharFromKatakana(theUChar);
0102   }
0103 
0104   //! Return TRUE if specified character is within subset of Hiragana (Japanese).
0105   static bool IsCharFromHiragana(Standard_Utf32Char theUChar)
0106   {
0107     return (theUChar >= 0x03040 && theUChar <= 0x0309F);
0108   }
0109 
0110   //! Return TRUE if specified character is within subset of Katakana (Japanese).
0111   static bool IsCharFromKatakana(Standard_Utf32Char theUChar)
0112   {
0113     return (theUChar >= 0x030A0 && theUChar <= 0x030FF);
0114   }
0115 
0116   //! Return TRUE if specified character is within subset of modern Korean characters (Hangul).
0117   static bool IsCharFromKorean(Standard_Utf32Char theUChar)
0118   {
0119     return (theUChar >= 0x01100 && theUChar <= 0x011FF)
0120            || (theUChar >= 0x03130 && theUChar <= 0x0318F)
0121            || (theUChar >= 0x0AC00 && theUChar <= 0x0D7A3);
0122   }
0123 
0124   //! Return TRUE if specified character is within subset of Arabic characters.
0125   static bool IsCharFromArabic(Standard_Utf32Char theUChar)
0126   {
0127     return (theUChar >= 0x00600 && theUChar <= 0x006FF);
0128   }
0129 
0130   //! Return TRUE if specified character should be displayed in Right-to-Left order.
0131   static bool IsCharRightToLeft(Standard_Utf32Char theUChar) { return IsCharFromArabic(theUChar); }
0132 
0133   //! Determine Unicode subset for specified character
0134   static Font_UnicodeSubset CharSubset(Standard_Utf32Char theUChar)
0135   {
0136     if (IsCharFromCJK(theUChar))
0137     {
0138       return Font_UnicodeSubset_CJK;
0139     }
0140     else if (IsCharFromKorean(theUChar))
0141     {
0142       return Font_UnicodeSubset_Korean;
0143     }
0144     else if (IsCharFromArabic(theUChar))
0145     {
0146       return Font_UnicodeSubset_Arabic;
0147     }
0148     return Font_UnicodeSubset_Western;
0149   }
0150 
0151 public:
0152   //! Create uninitialized instance.
0153   Standard_EXPORT Font_FTFont(const Handle(Font_FTLibrary)& theFTLib = Handle(Font_FTLibrary)());
0154 
0155   //! Destructor.
0156   Standard_EXPORT virtual ~Font_FTFont();
0157 
0158   //! @return true if font is loaded
0159   inline bool IsValid() const { return myFTFace != NULL; }
0160 
0161   //! @return image plane for currently rendered glyph
0162   inline const Image_PixMap& GlyphImage() const { return myGlyphImg; }
0163 
0164   //! Initialize the font from the given file path.
0165   //! @param theFontPath path to the font
0166   //! @param theParams   initialization parameters
0167   //! @param theFaceId   face id within the file (0 by default)
0168   //! @return true on success
0169   bool Init(const TCollection_AsciiString& theFontPath,
0170             const Font_FTFontParams&       theParams,
0171             const Standard_Integer         theFaceId = 0)
0172   {
0173     return Init(Handle(NCollection_Buffer)(), theFontPath, theParams, theFaceId);
0174   }
0175 
0176   //! Initialize the font from the given file path or memory buffer.
0177   //! @param theData     memory to read from, should NOT be freed after initialization!
0178   //!                    when NULL, function will attempt to open theFileName file
0179   //! @param theFileName optional path to the font
0180   //! @param theParams   initialization parameters
0181   //! @param theFaceId   face id within the file (0 by default)
0182   //! @return true on success
0183   Standard_EXPORT bool Init(const Handle(NCollection_Buffer)& theData,
0184                             const TCollection_AsciiString&    theFileName,
0185                             const Font_FTFontParams&          theParams,
0186                             const Standard_Integer            theFaceId = 0);
0187 
0188   //! Find (using Font_FontMgr) and initialize the font from the given name.
0189   //! @param theFontName    the font name
0190   //! @param theFontAspect  the font style
0191   //! @param theParams      initialization parameters
0192   //! @param theStrictLevel search strict level for using aliases and fallback
0193   //! @return true on success
0194   Standard_EXPORT bool FindAndInit(const TCollection_AsciiString& theFontName,
0195                                    Font_FontAspect                theFontAspect,
0196                                    const Font_FTFontParams&       theParams,
0197                                    Font_StrictLevel theStrictLevel = Font_StrictLevel_Any);
0198 
0199   //! Return flag to use fallback fonts in case if used font does not include symbols from specific
0200   //! Unicode subset; TRUE by default.
0201   //! @sa Font_FontMgr::ToUseUnicodeSubsetFallback()
0202   Standard_Boolean ToUseUnicodeSubsetFallback() const { return myToUseUnicodeSubsetFallback; }
0203 
0204   //! Set if fallback fonts should be used in case if used font does not include symbols from
0205   //! specific Unicode subset.
0206   void SetUseUnicodeSubsetFallback(Standard_Boolean theToFallback)
0207   {
0208     myToUseUnicodeSubsetFallback = theToFallback;
0209   }
0210 
0211   //! Return TRUE if this is single-stroke (one-line) font, FALSE by default.
0212   //! Such fonts define single-line glyphs instead of closed contours, so that they are rendered
0213   //! incorrectly by normal software.
0214   bool IsSingleStrokeFont() const { return myFontParams.IsSingleStrokeFont; }
0215 
0216   //! Set if this font should be rendered as single-stroke (one-line).
0217   void SetSingleStrokeFont(bool theIsSingleLine)
0218   {
0219     myFontParams.IsSingleStrokeFont = theIsSingleLine;
0220   }
0221 
0222   //! Return TRUE if italic style should be synthesized; FALSE by default.
0223   bool ToSynthesizeItalic() const { return myFontParams.ToSynthesizeItalic; }
0224 
0225   //! Release currently loaded font.
0226   Standard_EXPORT virtual void Release();
0227 
0228   //! Render specified glyph into internal buffer (bitmap).
0229   Standard_EXPORT bool RenderGlyph(const Standard_Utf32Char theChar);
0230 
0231   //! @return maximal glyph width in pixels (rendered to bitmap).
0232   Standard_EXPORT unsigned int GlyphMaxSizeX(bool theToIncludeFallback = false) const;
0233 
0234   //! @return maximal glyph height in pixels (rendered to bitmap).
0235   Standard_EXPORT unsigned int GlyphMaxSizeY(bool theToIncludeFallback = false) const;
0236 
0237   //! @return vertical distance from the horizontal baseline to the highest character coordinate.
0238   Standard_EXPORT float Ascender() const;
0239 
0240   //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
0241   Standard_EXPORT float Descender() const;
0242 
0243   //! @return default line spacing (the baseline-to-baseline distance).
0244   Standard_EXPORT float LineSpacing() const;
0245 
0246   //! Configured point size
0247   unsigned int PointSize() const { return myFontParams.PointSize; }
0248 
0249   //! Return glyph scaling along X-axis.
0250   float WidthScaling() const { return myWidthScaling; }
0251 
0252   //! Setup glyph scaling along X-axis.
0253   //! By default glyphs are not scaled (scaling factor = 1.0)
0254   void SetWidthScaling(const float theScaleFactor) { myWidthScaling = theScaleFactor; }
0255 
0256   //! Return TRUE if font contains specified symbol (excluding fallback list).
0257   Standard_EXPORT bool HasSymbol(Standard_Utf32Char theUChar) const;
0258 
0259   //! Compute horizontal advance to the next character with kerning applied when applicable.
0260   //! Assuming text rendered horizontally.
0261   //! @param theUCharNext the next character to compute advance from current one
0262   Standard_EXPORT float AdvanceX(Standard_Utf32Char theUCharNext) const;
0263 
0264   //! Compute horizontal advance to the next character with kerning applied when applicable.
0265   //! Assuming text rendered horizontally.
0266   //! @param theUChar     the character to be loaded as current one
0267   //! @param theUCharNext the next character to compute advance from current one
0268   Standard_EXPORT float AdvanceX(Standard_Utf32Char theUChar, Standard_Utf32Char theUCharNext);
0269 
0270   //! Compute vertical advance to the next character with kerning applied when applicable.
0271   //! Assuming text rendered vertically.
0272   //! @param theUCharNext the next character to compute advance from current one
0273   Standard_EXPORT float AdvanceY(Standard_Utf32Char theUCharNext) const;
0274 
0275   //! Compute vertical advance to the next character with kerning applied when applicable.
0276   //! Assuming text rendered vertically.
0277   //! @param theUChar     the character to be loaded as current one
0278   //! @param theUCharNext the next character to compute advance from current one
0279   Standard_EXPORT float AdvanceY(Standard_Utf32Char theUChar, Standard_Utf32Char theUCharNext);
0280 
0281   //! Return glyphs number in this font.
0282   //! @param theToIncludeFallback if TRUE then the number will include fallback list
0283   Standard_EXPORT Standard_Integer GlyphsNumber(bool theToIncludeFallback = false) const;
0284 
0285   //! Retrieve glyph bitmap rectangle
0286   Standard_EXPORT void GlyphRect(Font_Rect& theRect) const;
0287 
0288   //! Computes bounding box of the given text using plain-text formatter (Font_TextFormatter).
0289   //! Note that bounding box takes into account the text alignment options.
0290   //! Its corners are relative to the text alignment anchor point, their coordinates can be
0291   //! negative.
0292   Standard_EXPORT Font_Rect BoundingBox(const NCollection_String&               theString,
0293                                         const Graphic3d_HorizontalTextAlignment theAlignX,
0294                                         const Graphic3d_VerticalTextAlignment   theAlignY);
0295 
0296 public:
0297   //! Computes outline contour for the symbol.
0298   //! @param[in] theUChar     the character to be loaded as current one
0299   //! @param[out] theOutline   outline contour
0300   //! @return true on success
0301   Standard_EXPORT const FT_Outline* renderGlyphOutline(const Standard_Utf32Char theChar);
0302 
0303 public:
0304   //! Initialize the font.
0305   //! @param theFontPath   path to the font
0306   //! @param thePointSize  the face size in points (1/72 inch)
0307   //! @param theResolution the resolution of the target device in dpi
0308   //! @return true on success
0309   Standard_DEPRECATED("Deprecated method, Font_FTFontParams should be used for passing "
0310                       "parameters")
0311 
0312   bool Init(const NCollection_String& theFontPath,
0313             unsigned int              thePointSize,
0314             unsigned int              theResolution)
0315   {
0316     Font_FTFontParams aParams;
0317     aParams.PointSize  = thePointSize;
0318     aParams.Resolution = theResolution;
0319     return Init(theFontPath.ToCString(), aParams, 0);
0320   }
0321 
0322   //! Initialize the font.
0323   //! @param theFontName   the font name
0324   //! @param theFontAspect the font style
0325   //! @param thePointSize  the face size in points (1/72 inch)
0326   //! @param theResolution the resolution of the target device in dpi
0327   //! @return true on success
0328   Standard_DEPRECATED("Deprecated method, Font_FTFontParams should be used for passing "
0329                       "parameters")
0330 
0331   bool Init(const NCollection_String& theFontName,
0332             Font_FontAspect           theFontAspect,
0333             unsigned int              thePointSize,
0334             unsigned int              theResolution)
0335   {
0336     Font_FTFontParams aParams;
0337     aParams.PointSize  = thePointSize;
0338     aParams.Resolution = theResolution;
0339     return FindAndInit(theFontName.ToCString(), theFontAspect, aParams);
0340   }
0341 
0342 protected:
0343   //! Convert value to 26.6 fixed-point format for FT library API.
0344   template <typename theInput_t>
0345   int32_t toFTPoints(const theInput_t thePointSize) const
0346   {
0347     return (int32_t)thePointSize * 64;
0348   }
0349 
0350   //! Convert value from 26.6 fixed-point format for FT library API.
0351   template <typename theReturn_t, typename theFTUnits_t>
0352   inline theReturn_t fromFTPoints(const theFTUnits_t theFTUnits) const
0353   {
0354     return (theReturn_t)theFTUnits / 64.0f;
0355   }
0356 
0357 protected:
0358   //! Load glyph without rendering it.
0359   Standard_EXPORT bool loadGlyph(const Standard_Utf32Char theUChar);
0360 
0361   //! Wrapper for FT_Get_Kerning - retrieve kerning values.
0362   Standard_EXPORT bool getKerning(FT_Vector&         theKern,
0363                                   Standard_Utf32Char theUCharCurr,
0364                                   Standard_Utf32Char theUCharNext) const;
0365 
0366   //! Initialize fallback font.
0367   Standard_EXPORT bool findAndInitFallback(Font_UnicodeSubset theSubset);
0368 
0369   //! Enable/disable load flag.
0370   void setLoadFlag(int32_t theFlag, bool theToEnable)
0371   {
0372     if (theToEnable)
0373     {
0374       myLoadFlags |= theFlag;
0375     }
0376     else
0377     {
0378       myLoadFlags &= ~theFlag;
0379     }
0380   }
0381 
0382 protected:
0383   Handle(Font_FTLibrary)     myFTLib;  //!< handle to the FT library object
0384   Handle(NCollection_Buffer) myBuffer; //!< memory buffer
0385   Handle(Font_FTFont)        myFallbackFaces[Font_UnicodeSubset_NB]; //!< fallback fonts
0386   FT_Face                    myFTFace;                               //!< FT face object
0387   FT_Face                    myActiveFTFace; //!< active FT face object (the main of fallback)
0388   TCollection_AsciiString    myFontPath;     //!< font path
0389   Font_FTFontParams          myFontParams;   //!< font initialization parameters
0390   Font_FontAspect            myFontAspect;   //!< font initialization aspect
0391   float                      myWidthScaling; //!< scale glyphs along X-axis
0392   int32_t                    myLoadFlags;    //!< default load flags
0393 
0394   Image_PixMap       myGlyphImg; //!< cached glyph plane
0395   Standard_Utf32Char myUChar;    //!< currently loaded unicode character
0396   // clang-format off
0397   Standard_Boolean           myToUseUnicodeSubsetFallback; //!< use default fallback fonts for extended Unicode sub-sets (Korean, CJK, etc.)
0398   // clang-format on
0399 };
0400 
0401 #endif // _Font_FTFont_H__