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 <Font_FontAspect.hxx>
20 #include <Font_FTLibrary.hxx>
21 #include <Graphic3d_HorizontalTextAlignment.hxx>
22 #include <Graphic3d_VerticalTextAlignment.hxx>
23 #include <Image_PixMap.hxx>
24 #include <NCollection_String.hxx>
25 #include <NCollection_Vec2.hxx>
27 //! Wrapper over FreeType font.
28 //! Notice that this class uses internal buffers for loaded glyphs
29 //! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
30 class Font_FTFont : public Standard_Transient
35 //! Auxiliary structure - rectangle definition
43 NCollection_Vec2<float> TopLeft() const
45 return NCollection_Vec2<float> (Left, Top);
48 NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const
55 NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const
62 NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const
69 NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const
90 //! Create uninitialized instance.
91 Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL);
94 Standard_EXPORT virtual ~Font_FTFont();
96 //! @return true if font is loaded
97 inline bool IsValid() const
99 return myFTFace != NULL;
102 //! @return image plane for currently rendered glyph
103 inline const Image_PixMap& GlyphImage() const
108 //! Initialize the font.
109 //! @param theFontPath path to the font
110 //! @param thePointSize the face size in points (1/72 inch)
111 //! @param theResolution the resolution of the target device in dpi
112 //! @return true on success
113 Standard_EXPORT bool Init (const NCollection_String& theFontPath,
114 const unsigned int thePointSize,
115 const unsigned int theResolution);
117 //! Initialize the font.
118 //! @param theFontName the font name
119 //! @param theFontAspect the font style
120 //! @param thePointSize the face size in points (1/72 inch)
121 //! @param theResolution the resolution of the target device in dpi
122 //! @return true on success
123 Standard_EXPORT bool Init (const NCollection_String& theFontName,
124 const Font_FontAspect theFontAspect,
125 const unsigned int thePointSize,
126 const unsigned int theResolution);
128 //! Release currently loaded font.
129 Standard_EXPORT virtual void Release();
131 //! Render specified glyph into internal buffer (bitmap).
132 Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
134 //! @return maximal glyph width in pixels (rendered to bitmap).
135 Standard_EXPORT unsigned int GlyphMaxSizeX() const;
137 //! @return maximal glyph height in pixels (rendered to bitmap).
138 Standard_EXPORT unsigned int GlyphMaxSizeY() const;
140 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
141 inline float Ascender() const
143 return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
146 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
147 inline float Descender() const
149 return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
152 //! @return default line spacing (the baseline-to-baseline distance).
153 inline float LineSpacing() const
155 return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
158 //! Configured point size
159 unsigned int PointSize() const
164 //! Compute advance to the next character with kerning applied when applicable.
165 //! Assuming text rendered horizontally.
166 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUCharNext);
168 //! Compute advance to the next character with kerning applied when applicable.
169 //! Assuming text rendered horizontally.
170 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUChar,
171 const Standard_Utf32Char theUCharNext);
173 //! Compute advance to the next character with kerning applied when applicable.
174 //! Assuming text rendered vertically.
175 Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUCharNext);
177 //! Compute advance to the next character with kerning applied when applicable.
178 //! Assuming text rendered vertically.
179 Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUChar,
180 const Standard_Utf32Char theUCharNext);
182 //! @return glyphs number in this font.
183 inline Standard_Integer GlyphsNumber() const
185 return myFTFace->num_glyphs;
188 //! Retrieve glyph bitmap rectangle
189 inline void GlyphRect (Font_FTFont::Rect& theRect) const
191 const FT_Bitmap& aBitmap = myFTFace->glyph->bitmap;
192 theRect.Left = float(myFTFace->glyph->bitmap_left);
193 theRect.Top = float(myFTFace->glyph->bitmap_top);
194 theRect.Right = float(myFTFace->glyph->bitmap_left + (int )aBitmap.width);
195 theRect.Bottom = float(myFTFace->glyph->bitmap_top - (int )aBitmap.rows);
198 //! Computes bounding box of the given text using plain-text formatter (Font_TextFormatter).
199 //! Note that bounding box takes into account the text alignment options.
200 //! Its corners are relative to the text alignment anchor point, their coordinates can be negative.
201 Standard_EXPORT Rect BoundingBox (const NCollection_String& theString,
202 const Graphic3d_HorizontalTextAlignment theAlignX,
203 const Graphic3d_VerticalTextAlignment theAlignY);
207 //! Convert value to 26.6 fixed-point format for FT library API.
208 template <typename theInput_t>
209 inline FT_F26Dot6 toFTPoints (const theInput_t thePointSize) const
211 return (FT_F26Dot6)thePointSize * 64;
214 //! Convert value from 26.6 fixed-point format for FT library API.
215 template <typename theReturn_t, typename theFTUnits_t>
216 inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const
218 return (theReturn_t)theFTUnits / 64.0f;
223 //! Load glyph without rendering it.
224 Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
228 Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object
229 FT_Face myFTFace; //!< FT face object
230 NCollection_String myFontPath; //!< font path
231 unsigned int myPointSize; //!< point size set by FT_Set_Char_Size
232 FT_Int32 myLoadFlags; //!< default load flags
234 Image_PixMap myGlyphImg; //!< cached glyph plane
235 FT_Vector myKernAdvance; //!< buffer variable
236 Standard_Utf32Char myUChar; //!< currently loaded unicode character
240 DEFINE_STANDARD_RTTI(Font_FTFont, Standard_Transient) // Type definition
244 DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
246 #endif // _Font_FTFont_H__