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> |
d2eddacc |
20 | #include <Font_Rect.hxx> |
1bbd7c79 |
21 | #include <Font_StrictLevel.hxx> |
912761ea |
22 | #include <Font_UnicodeSubset.hxx> |
317d68c9 |
23 | #include <Graphic3d_HorizontalTextAlignment.hxx> |
24 | #include <Graphic3d_VerticalTextAlignment.hxx> |
a174a3c5 |
25 | #include <Image_PixMap.hxx> |
317d68c9 |
26 | #include <NCollection_String.hxx> |
1bbd7c79 |
27 | #include <TCollection_AsciiString.hxx> |
a174a3c5 |
28 | |
f9801cf9 |
29 | // forward declarations to avoid including of FreeType headers |
30 | typedef struct FT_FaceRec_* FT_Face; |
31 | typedef struct FT_Vector_ FT_Vector; |
32 | class Font_FTLibrary; |
33 | |
1bbd7c79 |
34 | //! Font initialization parameters. |
35 | struct Font_FTFontParams |
36 | { |
912761ea |
37 | unsigned int PointSize; //!< face size in points (1/72 inch) |
38 | unsigned int Resolution; //!< resolution of the target device in dpi for FT_Set_Char_Size() |
39 | bool ToSynthesizeItalic; //!< generate italic style (e.g. for font family having no italic style); FALSE by default |
40 | bool IsSingleStrokeFont; //!< single-stroke (one-line) font, FALSE by default |
1bbd7c79 |
41 | |
42 | //! Empty constructor. |
43 | Font_FTFontParams() : PointSize (0), Resolution (72u), ToSynthesizeItalic (false), IsSingleStrokeFont (false) {} |
44 | |
45 | //! Constructor. |
46 | Font_FTFontParams (unsigned int thePointSize, |
47 | unsigned int theResolution) |
48 | : PointSize (thePointSize), Resolution (theResolution), ToSynthesizeItalic (false), IsSingleStrokeFont (false) {} |
49 | }; |
50 | |
51 | DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient) |
52 | |
a174a3c5 |
53 | //! Wrapper over FreeType font. |
54 | //! Notice that this class uses internal buffers for loaded glyphs |
55 | //! and it is absolutely UNSAFE to load/read glyph from concurrent threads! |
56 | class Font_FTFont : public Standard_Transient |
57 | { |
1bbd7c79 |
58 | DEFINE_STANDARD_RTTIEXT(Font_FTFont, Standard_Transient) |
59 | public: |
60 | |
61 | //! Find the font Initialize the font. |
62 | //! @param theFontName the font name |
63 | //! @param theFontAspect the font style |
64 | //! @param theParams initialization parameters |
65 | //! @param theStrictLevel search strict level for using aliases and fallback |
66 | //! @return true on success |
67 | Standard_EXPORT static Handle(Font_FTFont) FindAndCreate (const TCollection_AsciiString& theFontName, |
68 | const Font_FontAspect theFontAspect, |
69 | const Font_FTFontParams& theParams, |
70 | const Font_StrictLevel theStrictLevel = Font_StrictLevel_Any); |
71 | |
912761ea |
72 | //! Return TRUE if specified character is within subset of modern CJK characters. |
73 | static bool IsCharFromCJK (Standard_Utf32Char theUChar) |
74 | { |
75 | return (theUChar >= 0x03400 && theUChar <= 0x04DFF) |
76 | || (theUChar >= 0x04E00 && theUChar <= 0x09FFF) |
77 | || (theUChar >= 0x0F900 && theUChar <= 0x0FAFF) |
78 | || (theUChar >= 0x20000 && theUChar <= 0x2A6DF) |
79 | || (theUChar >= 0x2F800 && theUChar <= 0x2FA1F) |
80 | // Hiragana and Katakana (Japanese) are NOT part of CJK, but CJK fonts usually include these symbols |
81 | || IsCharFromHiragana (theUChar) |
82 | || IsCharFromKatakana (theUChar); |
83 | } |
84 | |
85 | //! Return TRUE if specified character is within subset of Hiragana (Japanese). |
86 | static bool IsCharFromHiragana (Standard_Utf32Char theUChar) |
87 | { |
88 | return (theUChar >= 0x03040 && theUChar <= 0x0309F); |
89 | } |
90 | |
91 | //! Return TRUE if specified character is within subset of Katakana (Japanese). |
92 | static bool IsCharFromKatakana (Standard_Utf32Char theUChar) |
93 | { |
94 | return (theUChar >= 0x030A0 && theUChar <= 0x030FF); |
95 | } |
96 | |
97 | //! Return TRUE if specified character is within subset of modern Korean characters (Hangul). |
98 | static bool IsCharFromKorean (Standard_Utf32Char theUChar) |
99 | { |
100 | return (theUChar >= 0x01100 && theUChar <= 0x011FF) |
101 | || (theUChar >= 0x03130 && theUChar <= 0x0318F) |
102 | || (theUChar >= 0x0AC00 && theUChar <= 0x0D7A3); |
103 | } |
104 | |
105 | //! Return TRUE if specified character is within subset of Arabic characters. |
106 | static bool IsCharFromArabic (Standard_Utf32Char theUChar) |
107 | { |
108 | return (theUChar >= 0x00600 && theUChar <= 0x006FF); |
109 | } |
110 | |
111 | //! Return TRUE if specified character should be displayed in Right-to-Left order. |
112 | static bool IsCharRightToLeft (Standard_Utf32Char theUChar) |
113 | { |
114 | return IsCharFromArabic(theUChar); |
115 | } |
116 | |
117 | //! Determine Unicode subset for specified character |
118 | static Font_UnicodeSubset CharSubset (Standard_Utf32Char theUChar) |
119 | { |
120 | if (IsCharFromCJK (theUChar)) |
121 | { |
122 | return Font_UnicodeSubset_CJK; |
123 | } |
124 | else if (IsCharFromKorean (theUChar)) |
125 | { |
126 | return Font_UnicodeSubset_Korean; |
127 | } |
128 | else if (IsCharFromArabic (theUChar)) |
129 | { |
130 | return Font_UnicodeSubset_Arabic; |
131 | } |
132 | return Font_UnicodeSubset_Western; |
133 | } |
134 | |
a174a3c5 |
135 | public: |
136 | |
137 | //! Create uninitialized instance. |
f9801cf9 |
138 | Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = Handle(Font_FTLibrary)()); |
a174a3c5 |
139 | |
140 | //! Destructor. |
141 | Standard_EXPORT virtual ~Font_FTFont(); |
142 | |
143 | //! @return true if font is loaded |
144 | inline bool IsValid() const |
145 | { |
146 | return myFTFace != NULL; |
147 | } |
148 | |
149 | //! @return image plane for currently rendered glyph |
150 | inline const Image_PixMap& GlyphImage() const |
151 | { |
152 | return myGlyphImg; |
153 | } |
154 | |
1bbd7c79 |
155 | //! Initialize the font from the given file path. |
156 | //! @param theFontPath path to the font |
157 | //! @param theParams initialization parameters |
a174a3c5 |
158 | //! @return true on success |
1bbd7c79 |
159 | bool Init (const TCollection_AsciiString& theFontPath, |
160 | const Font_FTFontParams& theParams) |
161 | { |
162 | return Init (Handle(NCollection_Buffer)(), theFontPath, theParams); |
163 | } |
a174a3c5 |
164 | |
1bbd7c79 |
165 | //! Initialize the font from the given file path or memory buffer. |
166 | //! @param theData memory to read from, should NOT be freed after initialization! |
167 | //! when NULL, function will attempt to open theFileName file |
168 | //! @param theFileName optional path to the font |
169 | //! @param theParams initialization parameters |
170 | //! @return true on success |
171 | Standard_EXPORT bool Init (const Handle(NCollection_Buffer)& theData, |
172 | const TCollection_AsciiString& theFileName, |
173 | const Font_FTFontParams& theParams); |
174 | |
175 | //! Find (using Font_FontMgr) and initialize the font from the given name. |
176 | //! @param theFontName the font name |
177 | //! @param theFontAspect the font style |
178 | //! @param theParams initialization parameters |
179 | //! @param theStrictLevel search strict level for using aliases and fallback |
b514beda |
180 | //! @return true on success |
1bbd7c79 |
181 | Standard_EXPORT bool FindAndInit (const TCollection_AsciiString& theFontName, |
182 | Font_FontAspect theFontAspect, |
183 | const Font_FTFontParams& theParams, |
184 | Font_StrictLevel theStrictLevel = Font_StrictLevel_Any); |
b514beda |
185 | |
912761ea |
186 | //! Return flag to use fallback fonts in case if used font does not include symbols from specific Unicode subset; TRUE by default. |
187 | //! @sa Font_FontMgr::ToUseUnicodeSubsetFallback() |
188 | Standard_Boolean ToUseUnicodeSubsetFallback() const { return myToUseUnicodeSubsetFallback; } |
189 | |
190 | //! Set if fallback fonts should be used in case if used font does not include symbols from specific Unicode subset. |
191 | void SetUseUnicodeSubsetFallback (Standard_Boolean theToFallback) { myToUseUnicodeSubsetFallback = theToFallback; } |
192 | |
e4f0cc46 |
193 | //! Return TRUE if this is single-stroke (one-line) font, FALSE by default. |
194 | //! Such fonts define single-line glyphs instead of closed contours, so that they are rendered incorrectly by normal software. |
1bbd7c79 |
195 | bool IsSingleStrokeFont() const { return myFontParams.IsSingleStrokeFont; } |
e4f0cc46 |
196 | |
197 | //! Set if this font should be rendered as single-stroke (one-line). |
1bbd7c79 |
198 | void SetSingleStrokeFont (bool theIsSingleLine) { myFontParams.IsSingleStrokeFont = theIsSingleLine; } |
199 | |
200 | //! Return TRUE if italic style should be synthesized; FALSE by default. |
201 | bool ToSynthesizeItalic() const { return myFontParams.ToSynthesizeItalic; } |
e4f0cc46 |
202 | |
a174a3c5 |
203 | //! Release currently loaded font. |
b514beda |
204 | Standard_EXPORT virtual void Release(); |
a174a3c5 |
205 | |
206 | //! Render specified glyph into internal buffer (bitmap). |
207 | Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar); |
208 | |
209 | //! @return maximal glyph width in pixels (rendered to bitmap). |
912761ea |
210 | Standard_EXPORT unsigned int GlyphMaxSizeX (bool theToIncludeFallback = false) const; |
a174a3c5 |
211 | |
212 | //! @return maximal glyph height in pixels (rendered to bitmap). |
912761ea |
213 | Standard_EXPORT unsigned int GlyphMaxSizeY (bool theToIncludeFallback = false) const; |
a174a3c5 |
214 | |
215 | //! @return vertical distance from the horizontal baseline to the highest character coordinate. |
f9801cf9 |
216 | Standard_EXPORT float Ascender() const; |
a174a3c5 |
217 | |
218 | //! @return vertical distance from the horizontal baseline to the lowest character coordinate. |
f9801cf9 |
219 | Standard_EXPORT float Descender() const; |
a174a3c5 |
220 | |
221 | //! @return default line spacing (the baseline-to-baseline distance). |
f9801cf9 |
222 | Standard_EXPORT float LineSpacing() const; |
a174a3c5 |
223 | |
224 | //! Configured point size |
225 | unsigned int PointSize() const |
226 | { |
1bbd7c79 |
227 | return myFontParams.PointSize; |
a174a3c5 |
228 | } |
229 | |
151da08b |
230 | //! Setup glyph scaling along X-axis. |
231 | //! By default glyphs are not scaled (scaling factor = 1.0) |
232 | void SetWidthScaling (const float theScaleFactor) |
233 | { |
234 | myWidthScaling = theScaleFactor; |
235 | } |
236 | |
912761ea |
237 | //! Return TRUE if font contains specified symbol (excluding fallback list). |
238 | Standard_EXPORT bool HasSymbol (Standard_Utf32Char theUChar) const; |
239 | |
82be4141 |
240 | //! Compute horizontal advance to the next character with kerning applied when applicable. |
a174a3c5 |
241 | //! Assuming text rendered horizontally. |
82be4141 |
242 | //! @param theUCharNext the next character to compute advance from current one |
243 | Standard_EXPORT float AdvanceX (Standard_Utf32Char theUCharNext) const; |
a174a3c5 |
244 | |
82be4141 |
245 | //! Compute horizontal advance to the next character with kerning applied when applicable. |
a174a3c5 |
246 | //! Assuming text rendered horizontally. |
82be4141 |
247 | //! @param theUChar the character to be loaded as current one |
248 | //! @param theUCharNext the next character to compute advance from current one |
249 | Standard_EXPORT float AdvanceX (Standard_Utf32Char theUChar, |
250 | Standard_Utf32Char theUCharNext); |
a174a3c5 |
251 | |
82be4141 |
252 | //! Compute vertical advance to the next character with kerning applied when applicable. |
a174a3c5 |
253 | //! Assuming text rendered vertically. |
82be4141 |
254 | //! @param theUCharNext the next character to compute advance from current one |
255 | Standard_EXPORT float AdvanceY (Standard_Utf32Char theUCharNext) const; |
a174a3c5 |
256 | |
82be4141 |
257 | //! Compute vertical advance to the next character with kerning applied when applicable. |
a174a3c5 |
258 | //! Assuming text rendered vertically. |
82be4141 |
259 | //! @param theUChar the character to be loaded as current one |
260 | //! @param theUCharNext the next character to compute advance from current one |
261 | Standard_EXPORT float AdvanceY (Standard_Utf32Char theUChar, |
262 | Standard_Utf32Char theUCharNext); |
a174a3c5 |
263 | |
912761ea |
264 | //! Return glyphs number in this font. |
265 | //! @param theToIncludeFallback if TRUE then the number will include fallback list |
266 | Standard_EXPORT Standard_Integer GlyphsNumber (bool theToIncludeFallback = false) const; |
a174a3c5 |
267 | |
268 | //! Retrieve glyph bitmap rectangle |
f9801cf9 |
269 | Standard_EXPORT void GlyphRect (Font_Rect& theRect) const; |
a174a3c5 |
270 | |
317d68c9 |
271 | //! Computes bounding box of the given text using plain-text formatter (Font_TextFormatter). |
272 | //! Note that bounding box takes into account the text alignment options. |
273 | //! Its corners are relative to the text alignment anchor point, their coordinates can be negative. |
d2eddacc |
274 | Standard_EXPORT Font_Rect BoundingBox (const NCollection_String& theString, |
275 | const Graphic3d_HorizontalTextAlignment theAlignX, |
276 | const Graphic3d_VerticalTextAlignment theAlignY); |
317d68c9 |
277 | |
1bbd7c79 |
278 | public: |
279 | |
280 | //! Initialize the font. |
281 | //! @param theFontPath path to the font |
282 | //! @param thePointSize the face size in points (1/72 inch) |
283 | //! @param theResolution the resolution of the target device in dpi |
284 | //! @return true on success |
285 | Standard_DEPRECATED ("Deprecated method, Font_FTFontParams should be used for passing parameters") |
286 | bool Init (const NCollection_String& theFontPath, |
287 | unsigned int thePointSize, |
288 | unsigned int theResolution) |
289 | { |
290 | Font_FTFontParams aParams; |
291 | aParams.PointSize = thePointSize; |
292 | aParams.Resolution = theResolution; |
293 | return Init (theFontPath.ToCString(), aParams); |
294 | } |
295 | |
296 | //! Initialize the font. |
297 | //! @param theFontName the font name |
298 | //! @param theFontAspect the font style |
299 | //! @param thePointSize the face size in points (1/72 inch) |
300 | //! @param theResolution the resolution of the target device in dpi |
301 | //! @return true on success |
302 | Standard_DEPRECATED ("Deprecated method, Font_FTFontParams should be used for passing parameters") |
303 | bool Init (const NCollection_String& theFontName, |
304 | Font_FontAspect theFontAspect, |
305 | unsigned int thePointSize, |
306 | unsigned int theResolution) |
307 | { |
308 | Font_FTFontParams aParams; |
309 | aParams.PointSize = thePointSize; |
310 | aParams.Resolution = theResolution; |
311 | return FindAndInit (theFontName.ToCString(), theFontAspect, aParams); |
312 | } |
313 | |
a174a3c5 |
314 | protected: |
315 | |
316 | //! Convert value to 26.6 fixed-point format for FT library API. |
317 | template <typename theInput_t> |
f9801cf9 |
318 | int32_t toFTPoints (const theInput_t thePointSize) const |
a174a3c5 |
319 | { |
f9801cf9 |
320 | return (int32_t)thePointSize * 64; |
a174a3c5 |
321 | } |
322 | |
323 | //! Convert value from 26.6 fixed-point format for FT library API. |
324 | template <typename theReturn_t, typename theFTUnits_t> |
325 | inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const |
326 | { |
327 | return (theReturn_t)theFTUnits / 64.0f; |
328 | } |
329 | |
330 | protected: |
331 | |
332 | //! Load glyph without rendering it. |
333 | Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar); |
334 | |
82be4141 |
335 | //! Wrapper for FT_Get_Kerning - retrieve kerning values. |
336 | Standard_EXPORT bool getKerning (FT_Vector& theKern, |
337 | Standard_Utf32Char theUCharCurr, |
338 | Standard_Utf32Char theUCharNext) const; |
339 | |
912761ea |
340 | //! Initialize fallback font. |
341 | Standard_EXPORT bool findAndInitFallback (Font_UnicodeSubset theSubset); |
342 | |
a174a3c5 |
343 | protected: |
344 | |
1bbd7c79 |
345 | Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object |
346 | Handle(NCollection_Buffer) myBuffer; //!< memory buffer |
912761ea |
347 | Handle(Font_FTFont) myFallbackFaces[Font_UnicodeSubset_NB]; //!< fallback fonts |
1bbd7c79 |
348 | FT_Face myFTFace; //!< FT face object |
912761ea |
349 | FT_Face myActiveFTFace; //!< active FT face object (the main of fallback) |
1bbd7c79 |
350 | TCollection_AsciiString myFontPath; //!< font path |
351 | Font_FTFontParams myFontParams; //!< font initialization parameters |
912761ea |
352 | Font_FontAspect myFontAspect; //!< font initialization aspect |
1bbd7c79 |
353 | float myWidthScaling; //!< scale glyphs along X-axis |
354 | int32_t myLoadFlags; //!< default load flags |
151da08b |
355 | |
1bbd7c79 |
356 | Image_PixMap myGlyphImg; //!< cached glyph plane |
357 | Standard_Utf32Char myUChar; //!< currently loaded unicode character |
912761ea |
358 | Standard_Boolean myToUseUnicodeSubsetFallback; //!< use default fallback fonts for extended Unicode sub-sets (Korean, CJK, etc.) |
a174a3c5 |
359 | |
360 | }; |
361 | |
a174a3c5 |
362 | #endif // _Font_FTFont_H__ |