#include
#include
#include
#include //使用freetype必须添加
#include FT_FREETYPE_H
#define WIDTH 80
#define HEIGHT 80
//模拟一个高度为HEIGHT宽度为WIDTH的屏幕,其实就是控制台了
unsigned char image[HEIGHT][WIDTH];
/* 用户自定义的显示函数,主要就是操作获取到的点阵 */
void draw_bitmap( FT_Bitmap* bitmap,FT_Int x,FT_Int y)//bitmap里面存放的是点阵,x,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];//将点阵存在显示数组的对应区域
}
}
}
//读取存在显示数组中的点阵信息
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' );
}
}
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 );
}
filename = argv[1]; /*传入的第一个参数*/
text = argv[2]; /*传入的第二个参数*/
num_chars = strlen( text );
angle = ( 0.0 / 360 ) * 3.14159 * 2; /*旋转角度设置 */
target_height = HEIGHT;
error = FT_Init_FreeType( &library ); /*初始化freetype库*/
error = FT_New_Face( library, argv[1], 0, &face ); /*打开字体文件*/
FT_Set_Pixel_Sizes(face, 24, 0); /*设置字体的像素大小*/
slot = face->glyph;
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 );
/*设置文字显示的时候相对于屏幕左上方的坐标*/
pen.x = 0 * 64;
pen.y = ( target_height - 40 ) * 64;
for ( n = 0; n < num_chars; n++ )
{
FT_Set_Transform( face, &matrix, &pen );//设置旋转变换
/* 根据文字编码获得它的点阵 */
error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );
if ( error )
continue;
//将获得文字点阵信息保存到显示数组中
draw_bitmap( &slot->bitmap,slot->bitmap_left,target_height - slot->bitmap_top );
//设置下一个文字显示相对于屏幕左上角的坐标
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
//显示出显示数组中的点阵信息
show_image();
//清除freetype库
FT_Done_Face ( face );
FT_Done_FreeType( library );
return 0;
}