00001 #include "FTFace.h"
00002 #include "FTLibrary.h"
00003
00004 #include FT_TRUETYPE_TABLES_H
00005
00006 FTFace::FTFace( const char* fontFilePath)
00007 : numGlyphs(0),
00008 fontEncodingList(0),
00009 err(0)
00010 {
00011 const FT_Long DEFAULT_FACE_INDEX = 0;
00012 ftFace = new FT_Face;
00013
00014 err = FT_New_Face( *FTLibrary::Instance().GetLibrary(), fontFilePath, DEFAULT_FACE_INDEX, ftFace);
00015 if (err) {
00016 delete ftFace;
00017 ftFace = 0;
00018 } else {
00019 numGlyphs = (*ftFace)->num_glyphs;
00020 hasKerningTable = FT_HAS_KERNING((*ftFace));
00021 }
00022 }
00023
00024
00025 FTFace::FTFace( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
00026 : numGlyphs(0),
00027 err(0)
00028 {
00029 const FT_Long DEFAULT_FACE_INDEX = 0;
00030 ftFace = new FT_Face;
00031
00032 err = FT_New_Memory_Face( *FTLibrary::Instance().GetLibrary(), (FT_Byte *)pBufferBytes, bufferSizeInBytes, DEFAULT_FACE_INDEX, ftFace);
00033
00034 if (err) {
00035 delete ftFace;
00036 ftFace = 0;
00037 } else {
00038 numGlyphs = (*ftFace)->num_glyphs;
00039 }
00040 }
00041
00042
00043 FTFace::~FTFace()
00044 {
00045 if (ftFace) {
00046 FT_Done_Face( *ftFace);
00047 delete ftFace;
00048 ftFace = 0;
00049 }
00050 }
00051
00052
00053 bool FTFace::Attach( const char* fontFilePath)
00054 {
00055 err = FT_Attach_File( *ftFace, fontFilePath);
00056 return !err;
00057 }
00058
00059
00060 bool FTFace::Attach( const unsigned char *pBufferBytes, size_t bufferSizeInBytes)
00061 {
00062 FT_Open_Args open;
00063
00064 open.flags = FT_OPEN_MEMORY;
00065 open.memory_base = (FT_Byte *)pBufferBytes;
00066 open.memory_size = bufferSizeInBytes;
00067
00068 err = FT_Attach_Stream( *ftFace, &open);
00069 return !err;
00070 }
00071
00072
00073 const FTSize& FTFace::Size( const unsigned int size, const unsigned int res)
00074 {
00075 charSize.CharSize( ftFace, size, res, res);
00076 err = charSize.Error();
00077
00078 return charSize;
00079 }
00080
00081
00082 unsigned int FTFace::CharMapCount()
00083 {
00084 return (*ftFace)->num_charmaps;
00085 }
00086
00087
00088 FT_Encoding* FTFace::CharMapList()
00089 {
00090 if (0 == fontEncodingList) {
00091 fontEncodingList = new FT_Encoding[CharMapCount()];
00092 for (size_t encodingIndex = 0; encodingIndex < CharMapCount(); ++encodingIndex) {
00093 fontEncodingList[encodingIndex] = (*ftFace)->charmaps[encodingIndex]->encoding;
00094 }
00095 }
00096
00097 return fontEncodingList;
00098 }
00099
00100
00101 FTPoint FTFace::KernAdvance( unsigned int index1, unsigned int index2)
00102 {
00103 float x, y;
00104 x = y = 0.0f;
00105
00106 if (hasKerningTable && index1 && index2) {
00107 FT_Vector kernAdvance;
00108 kernAdvance.x = kernAdvance.y = 0;
00109
00110 err = FT_Get_Kerning( *ftFace, index1, index2, ft_kerning_unfitted, &kernAdvance);
00111 if (!err) {
00112 x = static_cast<float>( kernAdvance.x) / 64.0f;
00113 y = static_cast<float>( kernAdvance.y) / 64.0f;
00114 }
00115 }
00116
00117 return FTPoint( x, y, 0.0);
00118 }
00119
00120
00121 FT_GlyphSlot FTFace::Glyph( unsigned int index, FT_Int load_flags)
00122 {
00123 err = FT_Load_Glyph( *ftFace, index, load_flags);
00124 if (err) return NULL;
00125 return (*ftFace)->glyph;
00126 }