1 // Created on: 2013-01-28
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _Font_FTFont_H__
17 #define _Font_FTFont_H__
19 #include <NCollection_Vec2.hxx>
20 #include <NCollection_String.hxx>
21 #include <Font_FTLibrary.hxx>
22 #include <Image_PixMap.hxx>
23 #include <Font_FontAspect.hxx>
25 //! Wrapper over FreeType font.
26 //! Notice that this class uses internal buffers for loaded glyphs
27 //! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
28 class Font_FTFont : public Standard_Transient
33 //! Auxiliary structure - rectangle definition
41 NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const
48 NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const
55 NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const
62 NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const
73 //! Create uninitialized instance.
74 Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL);
77 Standard_EXPORT virtual ~Font_FTFont();
79 //! @return true if font is loaded
80 inline bool IsValid() const
82 return myFTFace != NULL;
85 //! @return image plane for currently rendered glyph
86 inline const Image_PixMap& GlyphImage() const
91 //! Initialize the font.
92 //! @param theFontPath path to the font
93 //! @param thePointSize the face size in points (1/72 inch)
94 //! @param theResolution the resolution of the target device in dpi
95 //! @return true on success
96 Standard_EXPORT bool Init (const NCollection_String& theFontPath,
97 const unsigned int thePointSize,
98 const unsigned int theResolution = 72);
100 //! Initialize the font.
101 //! @param theFontName the font name
102 //! @param theFontAspect the font style
103 //! @param thePointSize the face size in points (1/72 inch)
104 //! @param theResolution the resolution of the target device in dpi
105 //! @return true on success
106 Standard_EXPORT bool Init (const NCollection_String& theFontName,
107 const Font_FontAspect theFontAspect,
108 const unsigned int thePointSize,
109 const unsigned int theResolution);
111 //! Release currently loaded font.
112 Standard_EXPORT virtual void Release();
114 //! Render specified glyph into internal buffer (bitmap).
115 Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
117 //! @return maximal glyph width in pixels (rendered to bitmap).
118 Standard_EXPORT unsigned int GlyphMaxSizeX() const;
120 //! @return maximal glyph height in pixels (rendered to bitmap).
121 Standard_EXPORT unsigned int GlyphMaxSizeY() const;
123 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
124 inline float Ascender() const
126 return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
129 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
130 inline float Descender() const
132 return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
135 //! @return default line spacing (the baseline-to-baseline distance).
136 inline float LineSpacing() const
138 return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
141 //! Configured point size
142 unsigned int PointSize() const
147 //! Compute advance to the next character with kerning applied when applicable.
148 //! Assuming text rendered horizontally.
149 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUCharNext);
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 theUChar,
154 const Standard_Utf32Char theUCharNext);
156 //! Compute advance to the next character with kerning applied when applicable.
157 //! Assuming text rendered vertically.
158 Standard_EXPORT float AdvanceY (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 theUChar,
163 const Standard_Utf32Char theUCharNext);
165 //! @return glyphs number in this font.
166 inline Standard_Integer GlyphsNumber() const
168 return myFTFace->num_glyphs;
171 //! Retrieve glyph bitmap rectangle
172 inline void GlyphRect (Font_FTFont::Rect& theRect) const
174 const FT_Bitmap& aBitmap = myFTFace->glyph->bitmap;
175 theRect.Left = float(myFTFace->glyph->bitmap_left);
176 theRect.Top = float(myFTFace->glyph->bitmap_top);
177 theRect.Right = float(myFTFace->glyph->bitmap_left + (int )aBitmap.width);
178 theRect.Bottom = float(myFTFace->glyph->bitmap_top - (int )aBitmap.rows);
183 //! Convert value to 26.6 fixed-point format for FT library API.
184 template <typename theInput_t>
185 inline FT_F26Dot6 toFTPoints (const theInput_t thePointSize) const
187 return (FT_F26Dot6)thePointSize * 64;
190 //! Convert value from 26.6 fixed-point format for FT library API.
191 template <typename theReturn_t, typename theFTUnits_t>
192 inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const
194 return (theReturn_t)theFTUnits / 64.0f;
199 //! Load glyph without rendering it.
200 Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
204 Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object
205 FT_Face myFTFace; //!< FT face object
206 NCollection_String myFontPath; //!< font path
207 unsigned int myPointSize; //!< point size set by FT_Set_Char_Size
208 FT_Int32 myLoadFlags; //!< default load flags
210 Image_PixMap myGlyphImg; //!< cached glyph plane
211 FT_Vector myKernAdvance; //!< buffer variable
212 Standard_Utf32Char myUChar; //!< currently loaded unicode character
216 DEFINE_STANDARD_RTTI(Font_FTFont, Standard_Transient) // Type definition
220 DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
222 #endif // _Font_FTFont_H__