Corrected integration for issue 26393
[occt.git] / src / Font / Font_FTFont.hxx
CommitLineData
a174a3c5 1// Created on: 2013-01-28
2// Created by: Kirill GAVRILOV
d5f74e42 3// Copyright (c) 2013-2014 OPEN CASCADE SAS
a174a3c5 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
a174a3c5 6//
d5f74e42 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
973c2be1 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.
a174a3c5 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
a174a3c5 15
16#ifndef _Font_FTFont_H__
17#define _Font_FTFont_H__
18
19#include <NCollection_Vec2.hxx>
20#include <NCollection_String.hxx>
21#include <Font_FTLibrary.hxx>
22#include <Image_PixMap.hxx>
b514beda 23#include <Font_FontAspect.hxx>
a174a3c5 24
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!
28class Font_FTFont : public Standard_Transient
29{
30
31public:
32
33 //! Auxiliary structure - rectangle definition
34 struct Rect
35 {
36 float Left;
37 float Right;
38 float Top;
39 float Bottom;
40
41 NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const
42 {
43 theVec.x() = Left;
44 theVec.y() = Top;
45 return theVec;
46 }
47
48 NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const
49 {
50 theVec.x() = Right;
51 theVec.y() = Top;
52 return theVec;
53 }
54
55 NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const
56 {
57 theVec.x() = Left;
58 theVec.y() = Bottom;
59 return theVec;
60 }
61
62 NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const
63 {
64 theVec.x() = Right;
65 theVec.y() = Bottom;
66 return theVec;
67 }
68
69 };
70
71public:
72
73 //! Create uninitialized instance.
74 Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL);
75
76 //! Destructor.
77 Standard_EXPORT virtual ~Font_FTFont();
78
79 //! @return true if font is loaded
80 inline bool IsValid() const
81 {
82 return myFTFace != NULL;
83 }
84
85 //! @return image plane for currently rendered glyph
86 inline const Image_PixMap& GlyphImage() const
87 {
88 return myGlyphImg;
89 }
90
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);
99
b514beda 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);
110
a174a3c5 111 //! Release currently loaded font.
b514beda 112 Standard_EXPORT virtual void Release();
a174a3c5 113
114 //! Render specified glyph into internal buffer (bitmap).
115 Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
116
117 //! @return maximal glyph width in pixels (rendered to bitmap).
118 Standard_EXPORT unsigned int GlyphMaxSizeX() const;
119
120 //! @return maximal glyph height in pixels (rendered to bitmap).
121 Standard_EXPORT unsigned int GlyphMaxSizeY() const;
122
123 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
124 inline float Ascender() const
125 {
126 return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
127 }
128
129 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
130 inline float Descender() const
131 {
132 return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
133 }
134
135 //! @return default line spacing (the baseline-to-baseline distance).
136 inline float LineSpacing() const
137 {
138 return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
139 }
140
141 //! Configured point size
142 unsigned int PointSize() const
143 {
144 return myPointSize;
145 }
146
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);
150
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);
155
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);
159
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);
164
165 //! @return glyphs number in this font.
166 inline Standard_Integer GlyphsNumber() const
167 {
168 return myFTFace->num_glyphs;
169 }
170
171 //! Retrieve glyph bitmap rectangle
172 inline void GlyphRect (Font_FTFont::Rect& theRect) const
173 {
49297cb6 174 const FT_Bitmap& aBitmap = myFTFace->glyph->bitmap;
a174a3c5 175 theRect.Left = float(myFTFace->glyph->bitmap_left);
176 theRect.Top = float(myFTFace->glyph->bitmap_top);
49297cb6 177 theRect.Right = float(myFTFace->glyph->bitmap_left + (int )aBitmap.width);
178 theRect.Bottom = float(myFTFace->glyph->bitmap_top - (int )aBitmap.rows);
a174a3c5 179 }
180
181protected:
182
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
186 {
187 return (FT_F26Dot6)thePointSize * 64;
188 }
189
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
193 {
194 return (theReturn_t)theFTUnits / 64.0f;
195 }
196
197protected:
198
199 //! Load glyph without rendering it.
200 Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
201
202protected:
203
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
6788f423 208 FT_Int32 myLoadFlags; //!< default load flags
a174a3c5 209
210 Image_PixMap myGlyphImg; //!< cached glyph plane
211 FT_Vector myKernAdvance; //!< buffer variable
212 Standard_Utf32Char myUChar; //!< currently loaded unicode character
213
214public:
215
ec357c5c 216 DEFINE_STANDARD_RTTI(Font_FTFont, Standard_Transient) // Type definition
a174a3c5 217
218};
219
220DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
221
222#endif // _Font_FTFont_H__