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> |
317d68c9 |
21 | #include <Graphic3d_HorizontalTextAlignment.hxx> |
22 | #include <Graphic3d_VerticalTextAlignment.hxx> |
a174a3c5 |
23 | #include <Image_PixMap.hxx> |
317d68c9 |
24 | #include <NCollection_String.hxx> |
a174a3c5 |
25 | |
f9801cf9 |
26 | // forward declarations to avoid including of FreeType headers |
27 | typedef struct FT_FaceRec_* FT_Face; |
28 | typedef struct FT_Vector_ FT_Vector; |
29 | class Font_FTLibrary; |
30 | |
a174a3c5 |
31 | //! Wrapper over FreeType font. |
32 | //! Notice that this class uses internal buffers for loaded glyphs |
33 | //! and it is absolutely UNSAFE to load/read glyph from concurrent threads! |
34 | class Font_FTFont : public Standard_Transient |
35 | { |
a174a3c5 |
36 | public: |
37 | |
38 | //! Create uninitialized instance. |
f9801cf9 |
39 | Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = Handle(Font_FTLibrary)()); |
a174a3c5 |
40 | |
41 | //! Destructor. |
42 | Standard_EXPORT virtual ~Font_FTFont(); |
43 | |
44 | //! @return true if font is loaded |
45 | inline bool IsValid() const |
46 | { |
47 | return myFTFace != NULL; |
48 | } |
49 | |
50 | //! @return image plane for currently rendered glyph |
51 | inline const Image_PixMap& GlyphImage() const |
52 | { |
53 | return myGlyphImg; |
54 | } |
55 | |
56 | //! Initialize the font. |
57 | //! @param theFontPath path to the font |
58 | //! @param thePointSize the face size in points (1/72 inch) |
59 | //! @param theResolution the resolution of the target device in dpi |
60 | //! @return true on success |
61 | Standard_EXPORT bool Init (const NCollection_String& theFontPath, |
62 | const unsigned int thePointSize, |
4b1c8733 |
63 | const unsigned int theResolution); |
a174a3c5 |
64 | |
b514beda |
65 | //! Initialize the font. |
66 | //! @param theFontName the font name |
67 | //! @param theFontAspect the font style |
68 | //! @param thePointSize the face size in points (1/72 inch) |
69 | //! @param theResolution the resolution of the target device in dpi |
70 | //! @return true on success |
71 | Standard_EXPORT bool Init (const NCollection_String& theFontName, |
72 | const Font_FontAspect theFontAspect, |
73 | const unsigned int thePointSize, |
74 | const unsigned int theResolution); |
75 | |
e4f0cc46 |
76 | //! Return TRUE if this is single-stroke (one-line) font, FALSE by default. |
77 | //! Such fonts define single-line glyphs instead of closed contours, so that they are rendered incorrectly by normal software. |
78 | bool IsSingleStrokeFont() const { return myIsSingleLine; } |
79 | |
80 | //! Set if this font should be rendered as single-stroke (one-line). |
81 | void SetSingleStrokeFont (bool theIsSingleLine) { myIsSingleLine = theIsSingleLine; } |
82 | |
a174a3c5 |
83 | //! Release currently loaded font. |
b514beda |
84 | Standard_EXPORT virtual void Release(); |
a174a3c5 |
85 | |
86 | //! Render specified glyph into internal buffer (bitmap). |
87 | Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar); |
88 | |
89 | //! @return maximal glyph width in pixels (rendered to bitmap). |
90 | Standard_EXPORT unsigned int GlyphMaxSizeX() const; |
91 | |
92 | //! @return maximal glyph height in pixels (rendered to bitmap). |
93 | Standard_EXPORT unsigned int GlyphMaxSizeY() const; |
94 | |
95 | //! @return vertical distance from the horizontal baseline to the highest character coordinate. |
f9801cf9 |
96 | Standard_EXPORT float Ascender() const; |
a174a3c5 |
97 | |
98 | //! @return vertical distance from the horizontal baseline to the lowest character coordinate. |
f9801cf9 |
99 | Standard_EXPORT float Descender() const; |
a174a3c5 |
100 | |
101 | //! @return default line spacing (the baseline-to-baseline distance). |
f9801cf9 |
102 | Standard_EXPORT float LineSpacing() const; |
a174a3c5 |
103 | |
104 | //! Configured point size |
105 | unsigned int PointSize() const |
106 | { |
107 | return myPointSize; |
108 | } |
109 | |
151da08b |
110 | //! Setup glyph scaling along X-axis. |
111 | //! By default glyphs are not scaled (scaling factor = 1.0) |
112 | void SetWidthScaling (const float theScaleFactor) |
113 | { |
114 | myWidthScaling = theScaleFactor; |
115 | } |
116 | |
82be4141 |
117 | //! Compute horizontal advance to the next character with kerning applied when applicable. |
a174a3c5 |
118 | //! Assuming text rendered horizontally. |
82be4141 |
119 | //! @param theUCharNext the next character to compute advance from current one |
120 | Standard_EXPORT float AdvanceX (Standard_Utf32Char theUCharNext) const; |
a174a3c5 |
121 | |
82be4141 |
122 | //! Compute horizontal advance to the next character with kerning applied when applicable. |
a174a3c5 |
123 | //! Assuming text rendered horizontally. |
82be4141 |
124 | //! @param theUChar the character to be loaded as current one |
125 | //! @param theUCharNext the next character to compute advance from current one |
126 | Standard_EXPORT float AdvanceX (Standard_Utf32Char theUChar, |
127 | Standard_Utf32Char theUCharNext); |
a174a3c5 |
128 | |
82be4141 |
129 | //! Compute vertical advance to the next character with kerning applied when applicable. |
a174a3c5 |
130 | //! Assuming text rendered vertically. |
82be4141 |
131 | //! @param theUCharNext the next character to compute advance from current one |
132 | Standard_EXPORT float AdvanceY (Standard_Utf32Char theUCharNext) const; |
a174a3c5 |
133 | |
82be4141 |
134 | //! Compute vertical advance to the next character with kerning applied when applicable. |
a174a3c5 |
135 | //! Assuming text rendered vertically. |
82be4141 |
136 | //! @param theUChar the character to be loaded as current one |
137 | //! @param theUCharNext the next character to compute advance from current one |
138 | Standard_EXPORT float AdvanceY (Standard_Utf32Char theUChar, |
139 | Standard_Utf32Char theUCharNext); |
a174a3c5 |
140 | |
141 | //! @return glyphs number in this font. |
f9801cf9 |
142 | Standard_EXPORT Standard_Integer GlyphsNumber() const; |
a174a3c5 |
143 | |
144 | //! Retrieve glyph bitmap rectangle |
f9801cf9 |
145 | Standard_EXPORT void GlyphRect (Font_Rect& theRect) const; |
a174a3c5 |
146 | |
317d68c9 |
147 | //! Computes bounding box of the given text using plain-text formatter (Font_TextFormatter). |
148 | //! Note that bounding box takes into account the text alignment options. |
149 | //! Its corners are relative to the text alignment anchor point, their coordinates can be negative. |
d2eddacc |
150 | Standard_EXPORT Font_Rect BoundingBox (const NCollection_String& theString, |
151 | const Graphic3d_HorizontalTextAlignment theAlignX, |
152 | const Graphic3d_VerticalTextAlignment theAlignY); |
317d68c9 |
153 | |
a174a3c5 |
154 | protected: |
155 | |
156 | //! Convert value to 26.6 fixed-point format for FT library API. |
157 | template <typename theInput_t> |
f9801cf9 |
158 | int32_t toFTPoints (const theInput_t thePointSize) const |
a174a3c5 |
159 | { |
f9801cf9 |
160 | return (int32_t)thePointSize * 64; |
a174a3c5 |
161 | } |
162 | |
163 | //! Convert value from 26.6 fixed-point format for FT library API. |
164 | template <typename theReturn_t, typename theFTUnits_t> |
165 | inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const |
166 | { |
167 | return (theReturn_t)theFTUnits / 64.0f; |
168 | } |
169 | |
170 | protected: |
171 | |
172 | //! Load glyph without rendering it. |
173 | Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar); |
174 | |
82be4141 |
175 | //! Wrapper for FT_Get_Kerning - retrieve kerning values. |
176 | Standard_EXPORT bool getKerning (FT_Vector& theKern, |
177 | Standard_Utf32Char theUCharCurr, |
178 | Standard_Utf32Char theUCharNext) const; |
179 | |
a174a3c5 |
180 | protected: |
181 | |
151da08b |
182 | Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object |
183 | FT_Face myFTFace; //!< FT face object |
184 | NCollection_String myFontPath; //!< font path |
185 | unsigned int myPointSize; //!< point size set by FT_Set_Char_Size |
186 | float myWidthScaling; //!< scale glyphs along X-axis |
187 | int32_t myLoadFlags; //!< default load flags |
188 | bool myIsSingleLine; //!< single stroke font flag, FALSE by default |
189 | |
190 | Image_PixMap myGlyphImg; //!< cached glyph plane |
151da08b |
191 | Standard_Utf32Char myUChar; //!< currently loaded unicode character |
a174a3c5 |
192 | |
193 | public: |
194 | |
92efcf78 |
195 | DEFINE_STANDARD_RTTIEXT(Font_FTFont,Standard_Transient) // Type definition |
a174a3c5 |
196 | |
197 | }; |
198 | |
199 | DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient) |
200 | |
201 | #endif // _Font_FTFont_H__ |