0024211: Definition of Basic Runtime Check parameter causes regression in debug mode
[occt.git] / src / Font / Font_FTFont.hxx
CommitLineData
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>
27
28//! Wrapper over FreeType font.
29//! Notice that this class uses internal buffers for loaded glyphs
30//! and it is absolutely UNSAFE to load/read glyph from concurrent threads!
31class Font_FTFont : public Standard_Transient
32{
33
34public:
35
36 //! Auxiliary structure - rectangle definition
37 struct Rect
38 {
39 float Left;
40 float Right;
41 float Top;
42 float Bottom;
43
44 NCollection_Vec2<float>& TopLeft (NCollection_Vec2<float>& theVec) const
45 {
46 theVec.x() = Left;
47 theVec.y() = Top;
48 return theVec;
49 }
50
51 NCollection_Vec2<float>& TopRight (NCollection_Vec2<float>& theVec) const
52 {
53 theVec.x() = Right;
54 theVec.y() = Top;
55 return theVec;
56 }
57
58 NCollection_Vec2<float>& BottomLeft (NCollection_Vec2<float>& theVec) const
59 {
60 theVec.x() = Left;
61 theVec.y() = Bottom;
62 return theVec;
63 }
64
65 NCollection_Vec2<float>& BottomRight (NCollection_Vec2<float>& theVec) const
66 {
67 theVec.x() = Right;
68 theVec.y() = Bottom;
69 return theVec;
70 }
71
72 };
73
74public:
75
76 //! Create uninitialized instance.
77 Standard_EXPORT Font_FTFont (const Handle(Font_FTLibrary)& theFTLib = NULL);
78
79 //! Destructor.
80 Standard_EXPORT virtual ~Font_FTFont();
81
82 //! @return true if font is loaded
83 inline bool IsValid() const
84 {
85 return myFTFace != NULL;
86 }
87
88 //! @return image plane for currently rendered glyph
89 inline const Image_PixMap& GlyphImage() const
90 {
91 return myGlyphImg;
92 }
93
94 //! Initialize the font.
95 //! @param theFontPath path to the font
96 //! @param thePointSize the face size in points (1/72 inch)
97 //! @param theResolution the resolution of the target device in dpi
98 //! @return true on success
99 Standard_EXPORT bool Init (const NCollection_String& theFontPath,
100 const unsigned int thePointSize,
101 const unsigned int theResolution = 72);
102
103 //! Release currently loaded font.
104 Standard_EXPORT void Release();
105
106 //! Render specified glyph into internal buffer (bitmap).
107 Standard_EXPORT bool RenderGlyph (const Standard_Utf32Char theChar);
108
109 //! @return maximal glyph width in pixels (rendered to bitmap).
110 Standard_EXPORT unsigned int GlyphMaxSizeX() const;
111
112 //! @return maximal glyph height in pixels (rendered to bitmap).
113 Standard_EXPORT unsigned int GlyphMaxSizeY() const;
114
115 //! @return vertical distance from the horizontal baseline to the highest character coordinate.
116 inline float Ascender() const
117 {
118 return float(myFTFace->ascender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
119 }
120
121 //! @return vertical distance from the horizontal baseline to the lowest character coordinate.
122 inline float Descender() const
123 {
124 return float(myFTFace->descender) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
125 }
126
127 //! @return default line spacing (the baseline-to-baseline distance).
128 inline float LineSpacing() const
129 {
130 return float(myFTFace->height) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM));
131 }
132
133 //! Configured point size
134 unsigned int PointSize() const
135 {
136 return myPointSize;
137 }
138
139 //! Compute advance to the next character with kerning applied when applicable.
140 //! Assuming text rendered horizontally.
141 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUCharNext);
142
143 //! Compute advance to the next character with kerning applied when applicable.
144 //! Assuming text rendered horizontally.
145 Standard_EXPORT float AdvanceX (const Standard_Utf32Char theUChar,
146 const Standard_Utf32Char theUCharNext);
147
148 //! Compute advance to the next character with kerning applied when applicable.
149 //! Assuming text rendered vertically.
150 Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUCharNext);
151
152 //! Compute advance to the next character with kerning applied when applicable.
153 //! Assuming text rendered vertically.
154 Standard_EXPORT float AdvanceY (const Standard_Utf32Char theUChar,
155 const Standard_Utf32Char theUCharNext);
156
157 //! @return glyphs number in this font.
158 inline Standard_Integer GlyphsNumber() const
159 {
160 return myFTFace->num_glyphs;
161 }
162
163 //! Retrieve glyph bitmap rectangle
164 inline void GlyphRect (Font_FTFont::Rect& theRect) const
165 {
166 FT_Bitmap aBitmap = myFTFace->glyph->bitmap;
167 theRect.Left = float(myFTFace->glyph->bitmap_left);
168 theRect.Top = float(myFTFace->glyph->bitmap_top);
169 theRect.Right = float(myFTFace->glyph->bitmap_left + aBitmap.width);
170 theRect.Bottom = float(myFTFace->glyph->bitmap_top - aBitmap.rows);
171 }
172
173protected:
174
175 //! Convert value to 26.6 fixed-point format for FT library API.
176 template <typename theInput_t>
177 inline FT_F26Dot6 toFTPoints (const theInput_t thePointSize) const
178 {
179 return (FT_F26Dot6)thePointSize * 64;
180 }
181
182 //! Convert value from 26.6 fixed-point format for FT library API.
183 template <typename theReturn_t, typename theFTUnits_t>
184 inline theReturn_t fromFTPoints (const theFTUnits_t theFTUnits) const
185 {
186 return (theReturn_t)theFTUnits / 64.0f;
187 }
188
189protected:
190
191 //! Load glyph without rendering it.
192 Standard_EXPORT bool loadGlyph (const Standard_Utf32Char theUChar);
193
194protected:
195
196 Handle(Font_FTLibrary) myFTLib; //!< handle to the FT library object
197 FT_Face myFTFace; //!< FT face object
198 NCollection_String myFontPath; //!< font path
199 unsigned int myPointSize; //!< point size set by FT_Set_Char_Size
200
201 Image_PixMap myGlyphImg; //!< cached glyph plane
202 FT_Vector myKernAdvance; //!< buffer variable
203 Standard_Utf32Char myUChar; //!< currently loaded unicode character
204
205public:
206
207 DEFINE_STANDARD_RTTI(Font_FTFont) // Type definition
208
209};
210
211DEFINE_STANDARD_HANDLE(Font_FTFont, Standard_Transient)
212
213#endif // _Font_FTFont_H__