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 <TCollection_AsciiString.hxx>
19 #include <TCollection_HAsciiString.hxx>
22 // =======================================================================
23 // function : Font_FTFont
25 // =======================================================================
26 Font_FTFont::Font_FTFont (const Handle(Font_FTLibrary)& theFTLib)
30 myLoadFlags (FT_LOAD_NO_HINTING | FT_LOAD_TARGET_NORMAL),
35 myFTLib = new Font_FTLibrary();
39 // =======================================================================
40 // function : Font_FTFont
42 // =======================================================================
43 Font_FTFont::~Font_FTFont()
48 // =======================================================================
49 // function : Font_FTFont
51 // =======================================================================
52 void Font_FTFont::Release()
59 FT_Done_Face (myFTFace);
64 // =======================================================================
67 // =======================================================================
68 bool Font_FTFont::Init (const NCollection_String& theFontPath,
69 const unsigned int thePointSize,
70 const unsigned int theResolution)
73 myFontPath = theFontPath;
74 myPointSize = thePointSize;
75 if (!myFTLib->IsValid())
77 //std::cerr << "FreeType library is unavailable!\n";
82 if (FT_New_Face (myFTLib->Instance(), myFontPath.ToCString(), 0, &myFTFace) != 0)
84 //std::cerr << "Font '" << myFontPath << "' fail to load!\n";
88 else if (FT_Select_Charmap (myFTFace, ft_encoding_unicode) != 0)
90 //std::cerr << "Font '" << myFontPath << "' doesn't contains Unicode charmap!\n";
94 else if (FT_Set_Char_Size (myFTFace, 0L, toFTPoints (thePointSize), theResolution, theResolution) != 0)
96 //std::cerr << "Font '" << myFontPath << "' doesn't contains Unicode charmap!\n";
103 // =======================================================================
106 // =======================================================================
107 bool Font_FTFont::Init (const NCollection_String& theFontName,
108 const Font_FontAspect theFontAspect,
109 const unsigned int thePointSize,
110 const unsigned int theResolution)
112 Handle(Font_FontMgr) aFontMgr = Font_FontMgr::GetInstance();
113 const Handle(TCollection_HAsciiString) aFontName = new TCollection_HAsciiString (theFontName.ToCString());
114 Handle(Font_SystemFont) aRequestedFont = aFontMgr->FindFont (aFontName, theFontAspect, thePointSize);
115 return !aRequestedFont.IsNull()
116 && Font_FTFont::Init (aRequestedFont->FontPath()->ToCString(), thePointSize, theResolution);
119 // =======================================================================
120 // function : loadGlyph
122 // =======================================================================
123 bool Font_FTFont::loadGlyph (const Standard_Utf32Char theUChar)
125 if (myUChar == theUChar)
133 || FT_Load_Char (myFTFace, theUChar, myLoadFlags) != 0
134 || myFTFace->glyph == NULL)
143 // =======================================================================
144 // function : RenderGlyph
146 // =======================================================================
147 bool Font_FTFont::RenderGlyph (const Standard_Utf32Char theUChar)
152 || FT_Load_Char (myFTFace, theUChar, myLoadFlags | FT_LOAD_RENDER) != 0
153 || myFTFace->glyph == NULL
154 || myFTFace->glyph->format != FT_GLYPH_FORMAT_BITMAP)
159 FT_Bitmap aBitmap = myFTFace->glyph->bitmap;
160 if (aBitmap.pixel_mode != FT_PIXEL_MODE_GRAY
161 || aBitmap.buffer == NULL || aBitmap.width == 0 || aBitmap.rows == 0)
165 if (!myGlyphImg.InitWrapper (Image_PixMap::ImgAlpha, aBitmap.buffer,
166 aBitmap.width, aBitmap.rows, Abs (aBitmap.pitch)))
170 myGlyphImg.SetTopDown (aBitmap.pitch > 0);
175 // =======================================================================
176 // function : GlyphMaxSizeX
178 // =======================================================================
179 unsigned int Font_FTFont::GlyphMaxSizeX() const
181 float aWidth = (FT_IS_SCALABLE(myFTFace) != 0)
182 ? float(myFTFace->bbox.xMax - myFTFace->bbox.xMin) * (float(myFTFace->size->metrics.x_ppem) / float(myFTFace->units_per_EM))
183 : fromFTPoints<float> (myFTFace->size->metrics.max_advance);
184 return (unsigned int)(aWidth + 0.5f);
187 // =======================================================================
188 // function : GlyphMaxSizeY
190 // =======================================================================
191 unsigned int Font_FTFont::GlyphMaxSizeY() const
193 float aHeight = (FT_IS_SCALABLE(myFTFace) != 0)
194 ? float(myFTFace->bbox.yMax - myFTFace->bbox.yMin) * (float(myFTFace->size->metrics.y_ppem) / float(myFTFace->units_per_EM))
195 : fromFTPoints<float> (myFTFace->size->metrics.height);
196 return (unsigned int)(aHeight + 0.5f);
199 // =======================================================================
200 // function : AdvanceX
202 // =======================================================================
203 float Font_FTFont::AdvanceX (const Standard_Utf32Char theUChar,
204 const Standard_Utf32Char theUCharNext)
206 loadGlyph (theUChar);
207 return AdvanceX (theUCharNext);
210 // =======================================================================
211 // function : AdvanceY
213 // =======================================================================
214 float Font_FTFont::AdvanceY (const Standard_Utf32Char theUChar,
215 const Standard_Utf32Char theUCharNext)
217 loadGlyph (theUChar);
218 return AdvanceY (theUCharNext);
221 // =======================================================================
222 // function : AdvanceX
224 // =======================================================================
225 float Font_FTFont::AdvanceX (const Standard_Utf32Char theUCharNext)
232 if (FT_HAS_KERNING (myFTFace) == 0 || theUCharNext == 0
233 || FT_Get_Kerning (myFTFace, myUChar, theUCharNext, FT_KERNING_UNFITTED, &myKernAdvance) != 0)
235 return fromFTPoints<float> (myFTFace->glyph->advance.x);
237 return fromFTPoints<float> (myKernAdvance.x + myFTFace->glyph->advance.x);
240 // =======================================================================
241 // function : AdvanceY
243 // =======================================================================
244 float Font_FTFont::AdvanceY (const Standard_Utf32Char theUCharNext)
251 if (FT_HAS_KERNING (myFTFace) == 0 || theUCharNext == 0
252 || FT_Get_Kerning (myFTFace, myUChar, theUCharNext, FT_KERNING_UNFITTED, &myKernAdvance) != 0)
254 return fromFTPoints<float> (myFTFace->glyph->advance.y);
256 return fromFTPoints<float> (myKernAdvance.y + myFTFace->glyph->advance.y);