#include <ft2build.h>
#include <stdexcept>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <windows.h>
#include <string.h>

class FT2_Obj{
    FT_Library library;
    int size ;
    FT_Face face;
public:
    void Init(const char * fname, unsigned int h);
    void Free();
    void DrawAUnicode(wchar_t ch);
};
void FT2_Obj::Init(const char * fname, unsigned int h){
    this->size=h;
    if (FT_Init_FreeType( &library )) 
        throw std::runtime_error("FT_Init_FreeType failed");
    if (FT_New_Face( library, fname, 0, &face )) 
        throw std::runtime_error("FT_New_Face failed (there is probably a problem with your font file)");
    FT_Set_Char_Size( face,h<< 6, h << 6, 72, 72);
    FT_Matrix     matrix;              /* transformation matrix */
    FT_UInt       glyph_index;
    FT_Vector     pen;

}
void FT2_Obj::DrawAUnicode(wchar_t ch){
    if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
        throw std::runtime_error("FT_Load_Glyph failed");
    FT_Glyph glyph;
    if(FT_Get_Glyph( face->glyph, &glyph ))
        throw std::runtime_error("FT_Get_Glyph failed");
    FT_Render_Glyph( face->glyph,   FT_RENDER_MODE_MONO  ); 
    FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
    FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
    FT_Bitmap& bitmap=bitmap_glyph->bitmap;
    int width =  bitmap.width;
    int height =  bitmap.rows;
    wchar_t wstr[256];
    wsprintfW(wstr,L"World :%c , width :%d height : %d\n",ch,width , height);
    OutputDebugStringW(wstr);
    for(int j=0; j  < height ; j++){
        for(int i=0; i < width; i++){
            char tmp[5]={0};
            sprintf(tmp,"%2x",bitmap.buffer[i+ bitmap.width*j]);
            OutputDebugString(tmp);
        }
        OutputDebugString("\n");
    }
    OutputDebugString("\n");
}
void FT2_Obj::Free(){
    FT_Done_Face(face);
    FT_Done_FreeType(library);
}
int main(int argc, char* argv[]){
    FT2_Obj text;
    text.Init("msjh.ttc",6);
    text.DrawAUnicode(L'王');
    text.Free();
    return 0;
}