1 // Created on: 2013-01-28
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
20 #ifndef _Font_FTFont_H__
21 #define _Font_FTFont_H__
23 #include <NCollection_Vec2.hxx>
24 #include <NCollection_String.hxx>
25 #include <Font_FTLibrary.hxx>
26 #include <Image_PixMap.hxx>
27 #include <Font_FontAspect.hxx>
29 //! Wrapper over FreeType font.
30 //! Notice that this class uses internal buffers for loaded glyphs
31 //! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
32 class Font_FTFont : public Standard_Transient
37 //! Auxiliary structure - rectangle definition
45 NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const
52 NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const
59 NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const
66 NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const
77 //! Create uninitialized instance.
78 Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL);
81 Standard_EXPORT virtual ~Font_FTFont();
83 //! @return true if font is loaded
84 inline bool IsValid() const
86 return myFTFace != NULL;
89 //! @return image plane for currently rendered glyph
90 inline const Image_PixMap& GlyphImage() const
95 //! Initialize the font.
96 //! @param theFontPath path to the font
97 //! @param thePointSize the face size in points (1/72 inch)
98 //! @param theResolution the resolution of the target device in dpi
99 //! @return true on success
100 Standard_EXPORT bool Init (const NCollection_String& theFontPath,
101 const unsigned int thePointSize,
102 const unsigned int theResolution = 72);
104 //! Initialize the font.
105 //! @param theFontName the font name
106 //! @param theFontAspect the font style
107 //! @param thePointSize the face size in points (1/72 inch)
108 //! @param theResolution the resolution of the target device in dpi
109 //! @return true on success
110 Standard_EXPORT bool Init (const NCollection_String& theFontName,
111 const Font_FontAspect theFontAspect,
112 const unsigned int thePointSize,
113 const unsigned int theResolution);
115 //! Release currently loaded font.
116 Standard_EXPORT virtual void Release();
118 //! Render specified glyph into internal buffer (bitmap).
119 Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
121 //! @return maximal glyph width in pixels (rendered to bitmap).
122 Standard_EXPORT unsigned int GlyphMaxSizeX() const;
124 //! @return maximal glyph height in pixels (rendered to bitmap).
125 Standard_EXPORT unsigned int GlyphMaxSizeY() const;
127 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
128 inline float Ascender() const
130 return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
133 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
134 inline float Descender() const
136 return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
139 //! @return default line spacing (the baseline-to-baseline distance).
140 inline float LineSpacing() const
142 return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
145 //! Configured point size
146 unsigned int PointSize() const
151 //! Compute advance to the next character with kerning applied when applicable.
152 //! Assuming text rendered horizontally.
153 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUCharNext);
155 //! Compute advance to the next character with kerning applied when applicable.
156 //! Assuming text rendered horizontally.
157 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUChar,
158 const Standard_Utf32Char theUCharNext);
160 //! Compute advance to the next character with kerning applied when applicable.
161 //! Assuming text rendered vertically.
162 Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUCharNext);
164 //! Compute advance to the next character with kerning applied when applicable.
165 //! Assuming text rendered vertically.
166 Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUChar,
167 const Standard_Utf32Char theUCharNext);
169 //! @return glyphs number in this font.
170 inline Standard_Integer GlyphsNumber() const
172 return myFTFace->num_glyphs;
175 //! Retrieve glyph bitmap rectangle
176 inline void GlyphRect (Font_FTFont::Rect& theRect) const
178 FT_Bitmap aBitmap = myFTFace->glyph->bitmap;
179 theRect.Left = float(myFTFace->glyph->bitmap_left);
180 theRect.Top = float(myFTFace->glyph->bitmap_top);
181 theRect.Right = float(myFTFace->glyph->bitmap_left + aBitmap.width);
182 theRect.Bottom = float(myFTFace->glyph->bitmap_top - aBitmap.rows);
187 //! Convert value to 26.6 fixed-point format for FT library API.
188 template <typename theInput_t>
189 inline FT_F26Dot6 toFTPoints (const theInput_t thePointSize) const
191 return (FT_F26Dot6)thePointSize * 64;
194 //! Convert value from 26.6 fixed-point format for FT library API.
195 template <typename theReturn_t, typename theFTUnits_t>
196 inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const
198 return (theReturn_t)theFTUnits / 64.0f;
203 //! Load glyph without rendering it.
204 Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
208 Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object
209 FT_Face myFTFace; //!< FT face object
210 NCollection_String myFontPath; //!< font path
211 unsigned int myPointSize; //!< point size set by FT_Set_Char_Size
213 Image_PixMap myGlyphImg; //!< cached glyph plane
214 FT_Vector myKernAdvance; //!< buffer variable
215 Standard_Utf32Char myUChar; //!< currently loaded unicode character
219 DEFINE_STANDARD_RTTI(Font_FTFont) // Type definition
223 DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
225 #endif // _Font_FTFont_H__