a174a3c5 |
1 | // Created on: 2013-01-28 |
2 | // Created by: Kirill GAVRILOV |
3 | // Copyright (c) 2013 OPEN CASCADE SAS |
4 | // |
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. |
9 | // |
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. |
12 | // |
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. |
19 | |
20 | #ifndef _Font_FTFont_H__ |
21 | #define _Font_FTFont_H__ |
22 | |
23 | #include <NCollection_Vec2.hxx> |
24 | #include <NCollection_String.hxx> |
25 | #include <Font_FTLibrary.hxx> |
26 | #include <Image_PixMap.hxx> |
b514beda |
27 | #include <Font_FontAspect.hxx> |
a174a3c5 |
28 | |
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 |
33 | { |
34 | |
35 | public: |
36 | |
37 | //! Auxiliary structure - rectangle definition |
38 | struct Rect |
39 | { |
40 | float Left; |
41 | float Right; |
42 | float Top; |
43 | float Bottom; |
44 | |
45 | NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const |
46 | { |
47 | theVec.x() = Left; |
48 | theVec.y() = Top; |
49 | return theVec; |
50 | } |
51 | |
52 | NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const |
53 | { |
54 | theVec.x() = Right; |
55 | theVec.y() = Top; |
56 | return theVec; |
57 | } |
58 | |
59 | NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const |
60 | { |
61 | theVec.x() = Left; |
62 | theVec.y() = Bottom; |
63 | return theVec; |
64 | } |
65 | |
66 | NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const |
67 | { |
68 | theVec.x() = Right; |
69 | theVec.y() = Bottom; |
70 | return theVec; |
71 | } |
72 | |
73 | }; |
74 | |
75 | public: |
76 | |
77 | //! Create uninitialized instance. |
78 | Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL); |
79 | |
80 | //! Destructor. |
81 | Standard_EXPORT virtual ~Font_FTFont(); |
82 | |
83 | //! @return true if font is loaded |
84 | inline bool IsValid() const |
85 | { |
86 | return myFTFace != NULL; |
87 | } |
88 | |
89 | //! @return image plane for currently rendered glyph |
90 | inline const Image_PixMap& GlyphImage() const |
91 | { |
92 | return myGlyphImg; |
93 | } |
94 | |
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); |
103 | |
b514beda |
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); |
114 | |
a174a3c5 |
115 | //! Release currently loaded font. |
b514beda |
116 | Standard_EXPORT virtual void Release(); |
a174a3c5 |
117 | |
118 | //! Render specified glyph into internal buffer (bitmap). |
119 | Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar); |
120 | |
121 | //! @return maximal glyph width in pixels (rendered to bitmap). |
122 | Standard_EXPORT unsigned int GlyphMaxSizeX() const; |
123 | |
124 | //! @return maximal glyph height in pixels (rendered to bitmap). |
125 | Standard_EXPORT unsigned int GlyphMaxSizeY() const; |
126 | |
127 | //! @return vertical distance from the horizontal baseline to the highest character coordinate. |
128 | inline float Ascender() const |
129 | { |
130 | return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM)); |
131 | } |
132 | |
133 | //! @return vertical distance from the horizontal baseline to the lowest character coordinate. |
134 | inline float Descender() const |
135 | { |
136 | return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM)); |
137 | } |
138 | |
139 | //! @return default line spacing (the baseline-to-baseline distance). |
140 | inline float LineSpacing() const |
141 | { |
142 | return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM)); |
143 | } |
144 | |
145 | //! Configured point size |
146 | unsigned int PointSize() const |
147 | { |
148 | return myPointSize; |
149 | } |
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 theUCharNext); |
154 | |
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); |
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 theUCharNext); |
163 | |
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); |
168 | |
169 | //! @return glyphs number in this font. |
170 | inline Standard_Integer GlyphsNumber() const |
171 | { |
172 | return myFTFace->num_glyphs; |
173 | } |
174 | |
175 | //! Retrieve glyph bitmap rectangle |
176 | inline void GlyphRect (Font_FTFont::Rect& theRect) const |
177 | { |
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); |
183 | } |
184 | |
185 | protected: |
186 | |
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 |
190 | { |
191 | return (FT_F26Dot6)thePointSize * 64; |
192 | } |
193 | |
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 |
197 | { |
198 | return (theReturn_t)theFTUnits / 64.0f; |
199 | } |
200 | |
201 | protected: |
202 | |
203 | //! Load glyph without rendering it. |
204 | Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar); |
205 | |
206 | protected: |
207 | |
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 |
212 | |
213 | Image_PixMap myGlyphImg; //!< cached glyph plane |
214 | FT_Vector myKernAdvance; //!< buffer variable |
215 | Standard_Utf32Char myUChar; //!< currently loaded unicode character |
216 | |
217 | public: |
218 | |
219 | DEFINE_STANDARD_RTTI(Font_FTFont) // Type definition |
220 | |
221 | }; |
222 | |
223 | DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient) |
224 | |
225 | #endif // _Font_FTFont_H__ |