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 | |
317d68c9 |
19 | #include <Font_FontAspect.hxx> |
a174a3c5 |
20 | #include <Font_FTLibrary.hxx> |
317d68c9 |
21 | #include <Graphic3d_HorizontalTextAlignment.hxx> |
22 | #include <Graphic3d_VerticalTextAlignment.hxx> |
a174a3c5 |
23 | #include <Image_PixMap.hxx> |
317d68c9 |
24 | #include <NCollection_String.hxx> |
25 | #include <NCollection_Vec2.hxx> |
a174a3c5 |
26 | |
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 |
31 | { |
32 | |
33 | public: |
34 | |
35 | //! Auxiliary structure - rectangle definition |
36 | struct Rect |
37 | { |
38 | float Left; |
39 | float Right; |
40 | float Top; |
41 | float Bottom; |
42 | |
317d68c9 |
43 | NCollection_Vec2<float> TopLeft() const |
44 | { |
45 | return NCollection_Vec2<float> (Left, Top); |
46 | } |
47 | |
a174a3c5 |
48 | NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const |
49 | { |
50 | theVec.x() = Left; |
51 | theVec.y() = Top; |
52 | return theVec; |
53 | } |
54 | |
55 | NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const |
56 | { |
57 | theVec.x() = Right; |
58 | theVec.y() = Top; |
59 | return theVec; |
60 | } |
61 | |
62 | NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const |
63 | { |
64 | theVec.x() = Left; |
65 | theVec.y() = Bottom; |
66 | return theVec; |
67 | } |
68 | |
69 | NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const |
70 | { |
71 | theVec.x() = Right; |
72 | theVec.y() = Bottom; |
73 | return theVec; |
74 | } |
75 | |
317d68c9 |
76 | float Width () const |
77 | { |
78 | return Right - Left; |
79 | } |
80 | |
81 | float Height () const |
82 | { |
83 | return Top - Bottom; |
84 | } |
85 | |
a174a3c5 |
86 | }; |
87 | |
88 | public: |
89 | |
90 | //! Create uninitialized instance. |
91 | Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL); |
92 | |
93 | //! Destructor. |
94 | Standard_EXPORT virtual ~Font_FTFont(); |
95 | |
96 | //! @return true if font is loaded |
97 | inline bool IsValid() const |
98 | { |
99 | return myFTFace != NULL; |
100 | } |
101 | |
102 | //! @return image plane for currently rendered glyph |
103 | inline const Image_PixMap& GlyphImage() const |
104 | { |
105 | return myGlyphImg; |
106 | } |
107 | |
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 = 72); |
116 | |
b514beda |
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); |
127 | |
a174a3c5 |
128 | //! Release currently loaded font. |
b514beda |
129 | Standard_EXPORT virtual void Release(); |
a174a3c5 |
130 | |
131 | //! Render specified glyph into internal buffer (bitmap). |
132 | Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar); |
133 | |
134 | //! @return maximal glyph width in pixels (rendered to bitmap). |
135 | Standard_EXPORT unsigned int GlyphMaxSizeX() const; |
136 | |
137 | //! @return maximal glyph height in pixels (rendered to bitmap). |
138 | Standard_EXPORT unsigned int GlyphMaxSizeY() const; |
139 | |
140 | //! @return vertical distance from the horizontal baseline to the highest character coordinate. |
141 | inline float Ascender() const |
142 | { |
143 | return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM)); |
144 | } |
145 | |
146 | //! @return vertical distance from the horizontal baseline to the lowest character coordinate. |
147 | inline float Descender() const |
148 | { |
149 | return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM)); |
150 | } |
151 | |
152 | //! @return default line spacing (the baseline-to-baseline distance). |
153 | inline float LineSpacing() const |
154 | { |
155 | return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM)); |
156 | } |
157 | |
158 | //! Configured point size |
159 | unsigned int PointSize() const |
160 | { |
161 | return myPointSize; |
162 | } |
163 | |
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); |
167 | |
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); |
172 | |
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); |
176 | |
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); |
181 | |
182 | //! @return glyphs number in this font. |
183 | inline Standard_Integer GlyphsNumber() const |
184 | { |
185 | return myFTFace->num_glyphs; |
186 | } |
187 | |
188 | //! Retrieve glyph bitmap rectangle |
189 | inline void GlyphRect (Font_FTFont::Rect& theRect) const |
190 | { |
49297cb6 |
191 | const FT_Bitmap& aBitmap = myFTFace->glyph->bitmap; |
a174a3c5 |
192 | theRect.Left = float(myFTFace->glyph->bitmap_left); |
193 | theRect.Top = float(myFTFace->glyph->bitmap_top); |
49297cb6 |
194 | theRect.Right = float(myFTFace->glyph->bitmap_left + (int )aBitmap.width); |
195 | theRect.Bottom = float(myFTFace->glyph->bitmap_top - (int )aBitmap.rows); |
a174a3c5 |
196 | } |
197 | |
317d68c9 |
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); |
204 | |
a174a3c5 |
205 | protected: |
206 | |
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 |
210 | { |
211 | return (FT_F26Dot6)thePointSize * 64; |
212 | } |
213 | |
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 |
217 | { |
218 | return (theReturn_t)theFTUnits / 64.0f; |
219 | } |
220 | |
221 | protected: |
222 | |
223 | //! Load glyph without rendering it. |
224 | Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar); |
225 | |
226 | protected: |
227 | |
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 |
6788f423 |
232 | FT_Int32 myLoadFlags; //!< default load flags |
a174a3c5 |
233 | |
234 | Image_PixMap myGlyphImg; //!< cached glyph plane |
235 | FT_Vector myKernAdvance; //!< buffer variable |
236 | Standard_Utf32Char myUChar; //!< currently loaded unicode character |
237 | |
238 | public: |
239 | |
ec357c5c |
240 | DEFINE_STANDARD_RTTI(Font_FTFont, Standard_Transient) // Type definition |
a174a3c5 |
241 | |
242 | }; |
243 | |
244 | DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient) |
245 | |
246 | #endif // _Font_FTFont_H__ |