00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <wx/utils.h>
00022 #include <wxdialogs/TrueTypeFont.h>
00023 #include <common/Defines.h>
00024
00025 TrueTypeFont::TrueTypeFont(const std::string &typeFace, unsigned int h)
00026 {
00027
00028 if (FT_Init_FreeType(&library))
00029 {
00030 return;
00031 }
00032
00033
00034
00035
00036 if (FT_New_Face(library, typeFace.c_str(), 0, &face))
00037 {
00038 return;
00039 }
00040
00041
00042
00043
00044
00045 FT_Set_Char_Size(face, h << 6, h << 6, 96, 96);
00046
00047
00048 for(unsigned char i=0;i<128;i++)
00049 {
00050 createCharacter(face, i);
00051 }
00052 }
00053
00054 TrueTypeFont::~TrueTypeFont()
00055 {
00056
00057
00058 FT_Done_Face(face);
00059
00060
00061 FT_Done_FreeType(library);
00062 }
00063
00064 bool TrueTypeFont::createCharacter(FT_Face face, unsigned char ch)
00065 {
00066
00067
00068
00069
00070 if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
00071 {
00072 return false;
00073 }
00074
00075
00076 if(FT_Get_Glyph( face->glyph, &glyphs[ch] ))
00077 {
00078 return false;
00079 }
00080
00081
00082 FT_Glyph_To_Bitmap( &glyphs[ch], ft_render_mode_normal, 0, 1 );
00083 FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph) glyphs[ch];
00084
00085
00086 bitmaps[ch]= &bitmap_glyph->bitmap;
00087
00088 return true;
00089 }
00090
00091 bool TrueTypeFont::getImageForText(const std::string &text, wxImage &image)
00092 {
00093 int width = 0;
00094 int height = 0;
00095 int topheight = 0;
00096
00097
00098 FT_GlyphSlot slot = face->glyph;
00099 for (const char *c=text.c_str(); *c; c++)
00100 {
00101 FT_Load_Char(face, *c, FT_LOAD_RENDER);
00102 FT_Bitmap &bitmap = slot->bitmap;
00103
00104 width += slot->advance.x >> 6;
00105 if (bitmap.rows > height) height = bitmap.rows;
00106 if (bitmap.rows - slot->bitmap_top > topheight)
00107 topheight = bitmap.rows - slot->bitmap_top;
00108 }
00109
00110
00111 image.Create(width, height + topheight);
00112
00113
00114 int posx = 0;
00115 int posy = 0;
00116 for (const char *c=text.c_str(); *c; c++)
00117 {
00118 FT_Load_Char( face, *c, FT_LOAD_RENDER);
00119 FT_Bitmap &bitmap = slot->bitmap;
00120
00121 int x = slot->bitmap_left;
00122 int y = height - slot->bitmap_top;
00123
00124 for (int j=0; j<bitmap.rows; j++)
00125 {
00126 for (int i=0; i<bitmap.width; i++)
00127 {
00128 int rx = (i + posx + x);
00129 int ry = (j + posy + y);
00130
00131 if (rx >= image.GetWidth() || rx < 0) continue;
00132 if (ry >= image.GetHeight() || ry < 0) continue;
00133
00134 unsigned char *dest =
00135 image.GetData() + ((ry * image.GetWidth()) + rx) * 3;
00136 unsigned char src = bitmap.buffer[i + bitmap.width * j];
00137
00138 src = (unsigned int) (float(src) * 0.9f);
00139
00140
00141 dest[0] = dest[1] = dest[2] = src;
00142 }
00143 }
00144
00145 posx += slot->advance.x >> 6;
00146 }
00147
00148 return true;
00149 }