freetype第一步

#include <stdio.h>
#include <string.h>
#include <math.h>

#include <ft2build.h>
#include FT_FREETYPE_H


#define WIDTH   80
#define HEIGHT  80
unsigned char image[HEIGHT][WIDTH];
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];
    }
  }
}


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.12159*2;
 	target_height=HEIGHT;
 	error=FT_Init_FreeType(&library);//加载库 
 	error=FT_New_Face(library,argv[1],0,&face);//打开字体文件 
 	// error = FT_Set_Char_Size( face, 50 * 64, 0,
                          //  100, 0 );//设置字体大小。后面两个表示水平或垂直分辨率 
 	FT_Set_Pixel_Sizes(face,24,0);// 用这代替设置字体大小,表示设置像素大小24*24 
 	slot=face->glyph;//取出字体插槽 
 	
 	  /* set up matrix */
  	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表示字符原点 ,用笛卡尔坐标 
 	pen.y=(target_height-40)*64;//tartget_height表示lcd高度,tartget_height-40 ,表示在40地方 
 	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);
	 	/* increment pen position */
    	pen.x += slot->advance.x;
    	pen.y += slot->advance.y;
	 }
	show_image();//显示位图

  	FT_Done_Face    ( face );
  	FT_Done_FreeType( library );

  	return 0;
 	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值