NL3D::CFontGenerator Class Reference

#include <font_generator.h>


Detailed Description

Generate bitmap based on a true type font (using freetype2)
Author:
Vianney Lecroart

Nevrax France

Date:
2000

Definition at line 52 of file font_generator.h.

Public Member Functions

 CFontGenerator (const std::string &fontFileName, const std::string &fontExFileName="")
uint8getBitmap (ucchar c, uint32 size, uint32 &width, uint32 &height, uint32 &pitch, sint32 &left, sint32 &top, sint32 &advx, uint32 &glyphIndex)
uint32 getCharIndex (ucchar c)
void getKerning (ucchar left, ucchar right, sint32 &kernx)
void getSizes (ucchar c, uint32 size, uint32 &width, uint32 &height)
uint32 getUID ()
virtual ~CFontGenerator ()

Private Member Functions

const char * getFT2Error (FT_Error fte)

Private Attributes

FT_Face _Face
std::string _FontFileName
uint32 _UID

Static Private Attributes

uint32 _FontGeneratorCounterUID = 1
FT_Library _Library
bool _LibraryInit = false


Constructor & Destructor Documentation

NL3D::CFontGenerator::CFontGenerator const std::string &  fontFileName,
const std::string &  fontExFileName = ""
 

Constructor

Parameters:
fontFileName path+filename (ex: "c:\winnt\fonts\arial.ttf")

Definition at line 93 of file font_generator.cpp.

References _Face, _FontFileName, _FontGeneratorCounterUID, _Library, _LibraryInit, _UID, getFT2Error(), nlerror, and nlwarning.

00094 {
00095         NL_ALLOC_CONTEXT (FreeTyp);
00096 
00097         _UID = _FontGeneratorCounterUID;
00098         _FontGeneratorCounterUID++;
00099         _FontFileName = fontFileName;
00100 
00101         FT_Error error;
00102 
00103         if (!_LibraryInit)
00104         {
00105                 error = FT_Init_FreeType (&_Library);
00106                 if (error)
00107                 {
00108                         nlerror ("FT_Init_FreeType() failed: %s", getFT2Error(error));
00109                 }
00110                 _LibraryInit = true;
00111         }
00112 
00113         error = FT_New_Face (_Library, fontFileName.c_str (), 0, &_Face);
00114         if (error)
00115         {
00116                 nlerror ("FT_New_Face() failed with file '%s': %s", fontFileName.c_str(), getFT2Error(error));
00117         }
00118 
00119         string fontEx = fontExFileName;
00120         if (fontEx == "")
00121         {
00122                 // try to see if the ex filename exists based on the fontExFileName
00123                 fontEx = CPath::lookup(CFile::getFilenameWithoutExtension (fontFileName)+".afm", false, false);
00124         }
00125 
00126         if (fontEx != "")
00127         {
00128                 error = FT_Attach_File (_Face, fontEx.c_str ());
00129                 if (error)
00130                 {
00131                         nlwarning ("FT_Attach_File() failed with file '%s': %s", fontEx.c_str(), getFT2Error(error));
00132                 }
00133         }
00134 
00135         error = FT_Select_Charmap (_Face, ft_encoding_unicode);
00136         if (error)
00137         {
00138                 nlerror ("FT_Select_Charmap() failed with file '%s': %s", fontFileName.c_str(), getFT2Error(error));
00139         }
00140 }

NL3D::CFontGenerator::~CFontGenerator  )  [virtual]
 

Definition at line 142 of file font_generator.cpp.

00143 {
00144 }


Member Function Documentation

uint8 * NL3D::CFontGenerator::getBitmap ucchar  c,
uint32  size,
uint32 width,
uint32 height,
uint32 pitch,
sint32 left,
sint32 top,
sint32 advx,
uint32 glyphIndex
 

generate and return a bitmap

Parameters:
c the unicode char
size size of the generated font in ??? format
width width of the generated bitmap, this value is set by this function
height height of the generated bitmap, this value is set by this function
pitch pitch of the generated bitmap (+ or - the number of bytes per row), this value is set by this function

Definition at line 173 of file font_generator.cpp.

References _Face, getFT2Error(), height, nlerror, sint32, size, ucchar, uint32, uint8, and width.

Referenced by NL3D::CTextureFont::getLetterInfo(), and NL3D::CTextureFont::rebuildLetter().

00174 {
00175         NL_ALLOC_CONTEXT (FreeTyp);
00176 
00177         FT_Error error;
00178 
00179         error = FT_Set_Pixel_Sizes (_Face, size, size);
00180         if (error)
00181         {
00182                 nlerror ("FT_Set_Pixel_Sizes() failed: %s", getFT2Error(error));
00183         }
00184 
00185         // retrieve glyph index from character code
00186         FT_UInt glyph_index = FT_Get_Char_Index (_Face, c);
00187 
00188         // load glyph image into the slot (erase previous one)
00189         error = FT_Load_Glyph (_Face, glyph_index, FT_LOAD_DEFAULT);
00190         if (error)
00191         {
00192                 nlerror ("FT_Load_Glyph() failed: %s", getFT2Error(error));
00193         }
00194 
00195         if (size == 0)
00196         {
00197                 width = 0;
00198                 height = 0;
00199                 pitch = 0;
00200                 left = 0;
00201                 top = 0;
00202                 advx = 0;
00203                 glyphIndex = glyph_index;
00204                 return NULL;
00205         }
00206 
00207         // convert to an anti-aliased bitmap
00208         error = FT_Render_Glyph (_Face->glyph, ft_render_mode_normal);
00209         if (error)
00210         {
00211                 nlerror ("FT_Render_Glyph() failed: %s", getFT2Error(error));
00212         }
00213 
00214         width = _Face->glyph->bitmap.width;
00215         height = _Face->glyph->bitmap.rows;
00216         pitch = _Face->glyph->bitmap.pitch;
00217 
00218         left = _Face->glyph->bitmap_left;
00219         top = _Face->glyph->bitmap_top;
00220 
00221         advx = _Face->glyph->advance.x >> 6;
00222 
00223         glyphIndex = glyph_index;
00224 
00225         return (uint8 *) _Face->glyph->bitmap.buffer;
00226 }

uint32 NL3D::CFontGenerator::getCharIndex ucchar  c  ) 
 

Definition at line 252 of file font_generator.cpp.

References _Face, ucchar, and uint32.

00253 {
00254         NL_ALLOC_CONTEXT (FreeTyp);
00255 
00256         return FT_Get_Char_Index (_Face, c);
00257 }

const char * NL3D::CFontGenerator::getFT2Error FT_Error  fte  )  [private]
 

Definition at line 73 of file font_generator.cpp.

References ft_errors, NLMISC::smprintf(), and uint32.

Referenced by CFontGenerator(), getBitmap(), getKerning(), and getSizes().

00074 {
00075         NL_ALLOC_CONTEXT (FreeTyp);
00076 
00077         static char ukn[1024];
00078 
00079         for (uint32 i = 0; ft_errors[i].err_code != 0 || ft_errors[i].err_msg != 0; i++)
00080         {
00081                 if (ft_errors[i].err_code == fte)
00082                         return ft_errors[i].err_msg;
00083         }
00084         smprintf (ukn, 1024, "Unknown freetype2 error, errcode: 0x%x", fte);
00085         return ukn;
00086 }

void NL3D::CFontGenerator::getKerning ucchar  left,
ucchar  right,
sint32 kernx
 

Definition at line 230 of file font_generator.cpp.

References _Face, getFT2Error(), nlerror, sint32, and ucchar.

00231 {
00232         NL_ALLOC_CONTEXT (FreeTyp);
00233 
00234         if (!FT_HAS_KERNING(_Face))
00235         {
00236                 kernx = 0;
00237         }
00238         else
00239         {
00240                 FT_Vector  kerning;
00241                 FT_Error error = FT_Get_Kerning (_Face, left, right, ft_kerning_default, &kerning);
00242                 if (error)
00243                 {
00244                         nlerror ("FT_Get_Kerning() failed: %s", getFT2Error(error));
00245                 }
00246                 kernx = kerning.x;
00247         }
00248 }

void NL3D::CFontGenerator::getSizes ucchar  c,
uint32  size,
uint32 width,
uint32 height
 

returns the width and height of a character using a specific size and

Warning:
this function is not very fast (but faster than getBitmap()) because it has to load the char before.

Definition at line 146 of file font_generator.cpp.

References _Face, getFT2Error(), height, nlerror, size, ucchar, uint32, and width.

00147 {
00148         NL_ALLOC_CONTEXT (FreeTyp);
00149 
00150         FT_Error error;
00151 
00152         error = FT_Set_Pixel_Sizes (_Face, size, size);
00153         if (error)
00154         {
00155                 nlerror ("FT_Set_Pixel_Sizes() failed: %s", getFT2Error(error));
00156         }
00157 
00158         // retrieve glyph index from character code
00159         FT_UInt glyph_index = FT_Get_Char_Index (_Face, c);
00160 
00161         // load glyph image into the slot (erase previous one)
00162         error = FT_Load_Glyph (_Face, glyph_index, FT_LOAD_DEFAULT);
00163         if (error)
00164         {
00165                 nlerror ("FT_Load_Glyph() failed: %s", getFT2Error(error));
00166         }
00167 
00168         // convert 24.6 fixed point into integer
00169         width = _Face->glyph->metrics.width >> 6;
00170         height = _Face->glyph->metrics.height >> 6;
00171 }

uint32 NL3D::CFontGenerator::getUID  )  [inline]
 

Definition at line 82 of file font_generator.h.

References _UID, and uint32.

Referenced by NL3D::CTextureFont::SLetterKey::getVal().

00082 { return _UID; }


Field Documentation

FT_Face NL3D::CFontGenerator::_Face [private]
 

Definition at line 96 of file font_generator.h.

Referenced by CFontGenerator(), getBitmap(), getCharIndex(), getKerning(), and getSizes().

std::string NL3D::CFontGenerator::_FontFileName [private]
 

Definition at line 88 of file font_generator.h.

Referenced by CFontGenerator().

uint32 NL3D::CFontGenerator::_FontGeneratorCounterUID = 1 [static, private]
 

Definition at line 71 of file font_generator.cpp.

Referenced by CFontGenerator().

FT_Library NL3D::CFontGenerator::_Library [static, private]
 

Definition at line 69 of file font_generator.cpp.

Referenced by CFontGenerator().

bool NL3D::CFontGenerator::_LibraryInit = false [static, private]
 

Definition at line 70 of file font_generator.cpp.

Referenced by CFontGenerator().

uint32 NL3D::CFontGenerator::_UID [private]
 

Definition at line 87 of file font_generator.h.

Referenced by CFontGenerator(), and getUID().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 06:46:57 2004 for NeL by doxygen 1.3.6