main函数
int main( int argc,
char** argv )
{
FT_Library library;
FT_Face face;
FT_GlyphSlot slot;
FT_Matrix matrix; /* transformation matrix */
FT_Vector pen; /* untransformed origin */
FT_Error error;
char* filename;
char* text;
double angle;
int target_height;
int n, num_chars;
if ( argc != 3 )
{
fprintf ( stderr, "usage: %s font sample-text\n", argv[0] );
exit( 1 );
}
//1、字体文件simsun.ttc
filename = argv[1]; /* first argument */
//2、显示字符
text = argv[2]; /* second argument */
num_chars = strlen( text );
//3、旋转角度,FT_Set_Transform,matrix
angle = ( 25.0 / 360 ) * 3.14159 * 2; /* use 25 degrees */
target_height = HEIGHT;
//4、初始化freetype 库
error = FT_Init_FreeType( &library ); /* initialize library */
/* error handling omitted */
//5、打开字体文件simsun.ttc
error = FT_New_Face( library, filename, 0, &face );/* create face object */
/* error handling omitted */
/* use 50pt at 100dpi */
/*6、设置字体大小,一行有50 * 64 /64 / 72英寸,一英寸有 100 像素;
注意,使用FT_Set_Pixel_Sizes(face, 24, 0) 方便直观*/
error = FT_Set_Char_Size( face, 50 * 64, 0,
100, 0 ); /* set character size */
/* error handling omitted */
/* cmap selection omitted; */
/* for simplicity we assume that the font contains a Unicode cmap */
//7、使用slot接收 获取到的glyph
slot = face->glyph;
/* set up matrix */
//8、使用angle构成旋转矩阵
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );
matrix.yx = (FT_Fixed)( sin( angle ) * 0x10000L );
matrix.yy = (FT_Fixed)( cos( angle ) * 0x10000L );
/* the pen position in 26.6 cartesian space coordinates; */
/* start at (300,200) relative to the upper left corner */
//9、使用pen设置字符初始打印位置,FT_Set_Transform
pen.x = 300 * 64;
pen.y = ( target_height - 200 ) * 64;
//10、把num_chars个字符都打印出来
for ( n = 0; n < num_chars; n++ )
{
/* set transformation */
//11、设置字符变换(旋转、偏移)
FT_Set_Transform( face, &matrix, &pen );
/* load glyph image into the slot (erase previous one) */
//12、此函数代替FT_Get_Char_Index、FT_Load_Glyph、FT_Render_Glyph
error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
if ( error )
continue; /* ignore errors */
/* now, draw to our target surface (convert position) */
//13、把获取到的glyph的数据放在images数组中
draw_bitmap( &slot->bitmap,
slot->bitmap_left,
target_height - slot->bitmap_top );
/* increment pen position */
//14、把字符显示位置加上当前显示位置,方便显示下一个字符,不重叠
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
//15、把draw_bitmap里的images全部打印出来
show_image();
FT_Done_Face ( face );
FT_Done_FreeType( library );
return 0;
}
/* EOF */
draw_bitmap函数
void draw_bitmap( FT_Bitmap* bitmap,
FT_Int x,
FT_Int y)
{
FT_Int i, j, p, q;
FT_Int x_max = x + bitmap->width;
FT_Int y_max = y + bitmap->rows;
for ( i = x, p = 0; i < x_max; i++, p++ )
{
for ( j = y, q = 0; j < y_max; j++, q++ )
{
if ( i < 0 || j < 0 ||
i >= WIDTH || j >= HEIGHT )
continue;
image[j][i] |= bitmap->buffer[q * bitmap->width + p];
}
}
}
show_bitmap函数
void show_image( void )
{
int i, j;
for ( i = 0; i < HEIGHT; i++ )
{
for ( j = 0; j < WIDTH; j++ )
putchar( image[i][j] == 0 ? ' '
: image[i][j] < 128 ? '+'
: '*' );
putchar( '\n' );
}
}
扩展
1、显示中文:定义int数组,存放ASCII;然后FT_load_char直接输出chinese_str;这是因为char只有一个字节,而中文字符的ASCII需要两个字节;
2、显示中文:直接使用wchar存放中文字符;然后FT_load_char直接输出chinese_str;wchar是 wide char;
3、获取自型轮廓点:FT_Get_Glyph(face->glyph, &glyph);FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE,&bbox);获取到的bbox.xmin\bbox.xmax\bbox.ymin\bbox.ymax是左上角坐标和右下角坐标;