1 // Created on: 2013-01-28
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2013-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <Font_FTFont.hxx>
17 #include <Font_FontMgr.hxx>
18 #include <Font_TextFormatter.hxx>
20 #include <TCollection_AsciiString.hxx>
21 #include <TCollection_HAsciiString.hxx>
24 // =======================================================================
25 // function : Font_FTFont
27 // =======================================================================
28 Font_FTFont::Font_FTFont (const Handle(Font_FTLibrary)& theFTLib)
32 myLoadFlags (FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL),
37 myFTLib = new Font_FTLibrary();
41 // =======================================================================
42 // function : Font_FTFont
44 // =======================================================================
45 Font_FTFont::~Font_FTFont()
50 // =======================================================================
51 // function : Font_FTFont
53 // =======================================================================
54 void Font_FTFont::Release()
61 FT_Done_Face (myFTFace);
66 // =======================================================================
69 // =======================================================================
70 bool Font_FTFont::Init (const NCollection_String& theFontPath,
71 const unsigned int thePointSize,
72 const unsigned int theResolution)
75 myFontPath = theFontPath;
76 myPointSize = thePointSize;
77 if (!myFTLib->IsValid())
79 //std::cerr << "FreeType library is unavailable!\n";
84 if (FT_New_Face (myFTLib->Instance(), myFontPath.ToCString(), 0, &myFTFace) != 0)
86 //std::cerr << "Font '" << myFontPath << "' fail to load!\n";
90 else if (FT_Select_Charmap (myFTFace, ft_encoding_unicode) != 0)
92 //std::cerr << "Font '" << myFontPath << "' doesn't contains Unicode charmap!\n";
96 else if (FT_Set_Char_Size (myFTFace, 0L, toFTPoints (thePointSize), theResolution, theResolution) != 0)
98 //std::cerr << "Font '" << myFontPath << "' doesn't contains Unicode charmap!\n";
105 // =======================================================================
108 // =======================================================================
109 bool Font_FTFont::Init (const NCollection_String& theFontName,
110 const Font_FontAspect theFontAspect,
111 const unsigned int thePointSize,
112 const unsigned int theResolution)
114 Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
115 const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theFontName.ToCString());
116 Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, theFontAspect, thePointSize);
117 return !aRequestedFont.IsNull()
118 && Font_FTFont::Init (aRequestedFont->FontPath()->ToCString(), thePointSize, theResolution);
121 // =======================================================================
122 // function : loadGlyph
124 // =======================================================================
125 bool Font_FTFont::loadGlyph (const Standard_Utf32Char theUChar)
127 if (myUChar == theUChar)
135 || FT_Load_Char (myFTFace, theUChar, myLoadFlags) != 0
136 || myFTFace->glyph == NULL)
145 // =======================================================================
146 // function : RenderGlyph
148 // =======================================================================
149 bool Font_FTFont::RenderGlyph (const Standard_Utf32Char theUChar)
154 || FT_Load_Char (myFTFace, theUChar, myLoadFlags | FT_LOAD_RENDER) != 0
155 || myFTFace->glyph == NULL
156 || myFTFace->glyph->format != FT_GLYPH_FORMAT_BITMAP)
161 FT_Bitmap aBitmap = myFTFace->glyph->bitmap;
162 if (aBitmap.pixel_mode != FT_PIXEL_MODE_GRAY
163 || aBitmap.buffer == NULL || aBitmap.width == 0 || aBitmap.rows == 0)
167 if (!myGlyphImg.InitWrapper (Image_PixMap::ImgAlpha, aBitmap.buffer,
168 aBitmap.width, aBitmap.rows, Abs (aBitmap.pitch)))
172 myGlyphImg.SetTopDown (aBitmap.pitch > 0);
177 // =======================================================================
178 // function : GlyphMaxSizeX
180 // =======================================================================
181 unsigned int Font_FTFont::GlyphMaxSizeX() const
183 float aWidth = (FT_IS_SCALABLE(myFTFace) != 0)
184 ? float(myFTFace->bbox.xMax - myFTFace->bbox.xMin) * (float(myFTFace->size->metrics.x_ppem) / float(myFTFace->units_per_EM))
185 : fromFTPoints<float> (myFTFace->size->metrics.max_advance);
186 return (unsigned int)(aWidth + 0.5f);
189 // =======================================================================
190 // function : GlyphMaxSizeY
192 // =======================================================================
193 unsigned int Font_FTFont::GlyphMaxSizeY() const
195 float aHeight = (FT_IS_SCALABLE(myFTFace) != 0)
196 ? float(myFTFace->bbox.yMax - myFTFace->bbox.yMin) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM))
197 : fromFTPoints<float> (myFTFace->size->metrics.height);
198 return (unsigned int)(aHeight + 0.5f);
201 // =======================================================================
202 // function : AdvanceX
204 // =======================================================================
205 float Font_FTFont::AdvanceX (const Standard_Utf32Char theUChar,
206 const Standard_Utf32Char theUCharNext)
208 loadGlyph (theUChar);
209 return AdvanceX (theUCharNext);
212 // =======================================================================
213 // function : AdvanceY
215 // =======================================================================
216 float Font_FTFont::AdvanceY (const Standard_Utf32Char theUChar,
217 const Standard_Utf32Char theUCharNext)
219 loadGlyph (theUChar);
220 return AdvanceY (theUCharNext);
223 // =======================================================================
224 // function : AdvanceX
226 // =======================================================================
227 float Font_FTFont::AdvanceX (const Standard_Utf32Char theUCharNext)
234 if (FT_HAS_KERNING (myFTFace) == 0 || theUCharNext == 0
235 || FT_Get_Kerning (myFTFace, myUChar, theUCharNext, FT_KERNING_UNFITTED, &myKernAdvance) != 0)
237 return fromFTPoints<float> (myFTFace->glyph->advance.x);
239 return fromFTPoints<float> (myKernAdvance.x + myFTFace->glyph->advance.x);
242 // =======================================================================
243 // function : AdvanceY
245 // =======================================================================
246 float Font_FTFont::AdvanceY (const Standard_Utf32Char theUCharNext)
253 if (FT_HAS_KERNING (myFTFace) == 0 || theUCharNext == 0
254 || FT_Get_Kerning (myFTFace, myUChar, theUCharNext, FT_KERNING_UNFITTED, &myKernAdvance) != 0)
256 return fromFTPoints<float> (myFTFace->glyph->advance.y);
258 return fromFTPoints<float> (myKernAdvance.y + myFTFace->glyph->advance.y);
261 // =======================================================================
262 // function : BoundingBox
264 // =======================================================================
265 Font_FTFont::Rect Font_FTFont::BoundingBox (const NCollection_String& theString,
266 const Graphic3d_HorizontalTextAlignment theAlignX,
267 const Graphic3d_VerticalTextAlignment theAlignY)
269 Font_TextFormatter aFormatter;
270 aFormatter.SetupAlignment (theAlignX, theAlignY);
273 aFormatter.Append (theString, *this);
278 aFormatter.BndBox (aBndBox);