Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:03:30

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