FTFont.cpp

Go to the documentation of this file.
00001 #include    "FTFace.h"
00002 #include    "FTFont.h"
00003 #include    "FTGlyphContainer.h"
00004 #include    "FTBBox.h"
00005 
00006 
00007 FTFont::FTFont( const char* fontname)
00008 :   face( fontname),
00009     glyphList(0)
00010 {
00011     err = face.Error();
00012     if( err == 0)
00013     {
00014         glyphList = new FTGlyphContainer( &face);
00015     }
00016 }
00017 
00018 
00019 FTFont::FTFont( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
00020 :   face( pBufferBytes, bufferSizeInBytes),
00021     glyphList(0)
00022 {
00023     err = face.Error();
00024     if( err == 0)
00025     {
00026         glyphList = new FTGlyphContainer( &face);
00027     }
00028 }
00029 
00030 
00031 FTFont::~FTFont()
00032 {
00033     delete glyphList;
00034 }
00035 
00036 
00037 bool FTFont::Attach( const char* filename)
00038 {
00039     if( face.Attach( filename))
00040     {
00041         err = 0;
00042         return true;
00043     }
00044     else
00045     {
00046         err = face.Error();
00047         return false;
00048     }
00049 }
00050 
00051 
00052 bool FTFont::Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
00053 {
00054     if( face.Attach( pBufferBytes, bufferSizeInBytes))
00055     {
00056         err = 0;
00057         return true;
00058     }
00059     else
00060     {
00061         err = face.Error();
00062         return false;
00063     }
00064 }
00065 
00066 
00067 bool FTFont::FaceSize( const unsigned int size, const unsigned int res )
00068 {
00069     charSize = face.Size( size, res);
00070     
00071     if( face.Error())
00072     {
00073         return false;
00074     }
00075     
00076     if( glyphList != NULL)
00077     {
00078         delete glyphList;
00079     }
00080     
00081     glyphList = new FTGlyphContainer( &face);
00082     return true;
00083 }
00084 
00085 
00086 unsigned int FTFont::FaceSize() const
00087 {
00088     return charSize.CharSize();
00089 }
00090 
00091 
00092 bool FTFont::CharMap( FT_Encoding encoding)
00093 {
00094     bool result = glyphList->CharMap( encoding);
00095     err = glyphList->Error();
00096     return result;
00097 }
00098 
00099 
00100 unsigned int FTFont::CharMapCount()
00101 {
00102     return face.CharMapCount();
00103 }
00104 
00105 
00106 FT_Encoding* FTFont::CharMapList()
00107 {
00108     return face.CharMapList();
00109 }
00110 
00111 
00112 float FTFont::Ascender() const
00113 {
00114     return charSize.Ascender();
00115 }
00116 
00117 
00118 float FTFont::Descender() const
00119 {
00120     return charSize.Descender();
00121 }
00122 
00123 
00124 void FTFont::BBox( const char* string,
00125                    float& llx, float& lly, float& llz, float& urx, float& ury, float& urz)
00126 {
00127     FTBBox totalBBox;
00128 
00129     if((NULL != string) && ('\0' != *string))
00130     {
00131         const unsigned char* c = (unsigned char*)string;
00132 
00133         CheckGlyph( *c);
00134 
00135         totalBBox = glyphList->BBox( *c);
00136         float advance = glyphList->Advance( *c, *(c + 1));
00137         ++c;
00138             
00139         while( *c)
00140         {
00141             CheckGlyph( *c);
00142             FTBBox tempBBox = glyphList->BBox( *c);
00143             tempBBox.Move( FTPoint( advance, 0.0f, 0.0f));
00144             totalBBox += tempBBox;
00145             advance += glyphList->Advance( *c, *(c + 1));
00146             ++c;
00147         }
00148     }
00149 
00150     llx = totalBBox.lowerX;
00151     lly = totalBBox.lowerY;
00152     llz = totalBBox.lowerZ;
00153     urx = totalBBox.upperX;
00154     ury = totalBBox.upperY;
00155     urz = totalBBox.upperZ;
00156 }
00157 
00158 
00159 void FTFont::BBox( const wchar_t* string,
00160                    float& llx, float& lly, float& llz, float& urx, float& ury, float& urz)
00161 {
00162     FTBBox totalBBox;
00163 
00164     if((NULL != string) && ('\0' != *string))
00165     {
00166         const wchar_t* c = string;
00167 
00168         CheckGlyph( *c);
00169 
00170         totalBBox = glyphList->BBox( *c);
00171         float advance = glyphList->Advance( *c, *(c + 1));
00172         ++c;
00173 
00174         while( *c)
00175         {
00176             CheckGlyph( *c);
00177             FTBBox tempBBox = glyphList->BBox( *c);
00178             tempBBox.Move( FTPoint( advance, 0.0f, 0.0f));
00179             totalBBox += tempBBox;
00180             advance += glyphList->Advance( *c, *(c + 1));
00181             ++c;
00182         }
00183     }
00184 
00185     llx = totalBBox.lowerX;
00186     lly = totalBBox.lowerY;
00187     llz = totalBBox.lowerZ;
00188     urx = totalBBox.upperX;
00189     ury = totalBBox.upperY;
00190     urz = totalBBox.upperZ;
00191 }
00192 
00193 
00194 float FTFont::Advance( const wchar_t* string)
00195 {
00196     const wchar_t* c = string;
00197     float width = 0.0f;
00198 
00199     while( *c)
00200     {
00201         CheckGlyph( *c);
00202         width += glyphList->Advance( *c, *(c + 1));
00203         ++c;
00204     }
00205 
00206     return width;
00207 }
00208 
00209 
00210 float FTFont::Advance( const char* string)
00211 {
00212     const unsigned char* c = (unsigned char*)string;
00213     float width = 0.0f;
00214 
00215     while( *c)
00216     {
00217         CheckGlyph( *c);
00218         width += glyphList->Advance( *c, *(c + 1));
00219         ++c;
00220     }
00221     
00222     return width;
00223 }
00224 
00225 
00226 void FTFont::Render( const char* string )
00227 {
00228     const unsigned char* c = (unsigned char*)string;
00229     pen.x = 0; pen.y = 0;
00230 
00231     while( *c)
00232     {
00233         DoRender( *c, *(c + 1));
00234         ++c;
00235     }
00236 }
00237 
00238 
00239 void FTFont::Render( const wchar_t* string )
00240 {
00241     const wchar_t* c = string;
00242     pen.x = 0; pen.y = 0;
00243 
00244     while( *c)
00245     {
00246         DoRender( *c, *(c + 1));
00247         ++c;
00248     }
00249 }
00250 
00251 
00252 void FTFont::DoRender( const unsigned int chr, const unsigned int nextChr)
00253 {
00254     CheckGlyph( chr);
00255 
00256     FTPoint kernAdvance = glyphList->Render( chr, nextChr, pen);
00257     
00258     pen.x += kernAdvance.x;
00259     pen.y += kernAdvance.y;
00260 }
00261 
00262 
00263 void FTFont::CheckGlyph( const unsigned int characterCode)
00264 {
00265     if( NULL == glyphList->Glyph( characterCode))
00266     {
00267         unsigned int glyphIndex = glyphList->FontIndex( characterCode);
00268         glyphList->Add( MakeGlyph( glyphIndex), characterCode);
00269     }
00270 }
00271 

Generated on Tue Aug 22 23:12:44 2006 for FTGL by  doxygen 1.4.7