freetype(demo)

来源:互联网 发布:淘宝一分钱壁纸 编辑:程序博客网 时间:2024/05/21 20:30

freetype显示一行文字:

#include <stdio.h>  #include <string.h>  #include <math.h>    #include <ft2build.h>  #include FT_FREETYPE_H      #define WIDTH   80  #define HEIGHT  80      /* origin is the upper left corner */  unsigned char image[HEIGHT][WIDTH];      /* Replace this function with something useful. */    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;                   /* transformation matrix */    FT_Vector     pen;                    /* untransformed origin  */    FT_Error      error;    FT_Matrix     matrix;      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];                           /* first argument     */    text          = argv[2];                           /* second argument    */    num_chars     = strlen( text );    target_height = HEIGHT;      error = FT_Init_FreeType( &library );              /* initialize library */    /* error handling omitted */      error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */    /* error handling omitted */                    FT_Set_Pixel_Sizes(face, 24, 0);               /* set character size */      /* error handling omitted */      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 );            /* the pen position in 26.6 cartesian space coordinates; */    /* start at (0,40) relative to the upper left corner  */    pen.x = 0 * 64;    pen.y = ( target_height - 70 ) * 64;      for ( n = 0; n < num_chars; n++ )    {          /* set transformation */         FT_Set_Transform( face, &matrix, &pen );        /* load glyph image into the slot (erase previous one) */      error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );      if ( error )        continue;                 /* ignore errors */        /* now, draw to our target surface (convert position) */      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;  }  


freetype对换行的支持:

#include <stdio.h>#include <string.h>#include <math.h>#include <ft2build.h>#include FT_FREETYPE_H#include FT_GLYPH_H#define WIDTH   80#define HEIGHT  80/* origin is the upper left corner */unsigned char image[HEIGHT][WIDTH];/* Replace this function with something useful. */voiddraw_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];    }  }}voidshow_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' );  }}intmain( int     argc,      char**  argv ){  FT_Library    library;  FT_Face       face;  FT_Glyph  glyph;  FT_GlyphSlot  slot;  FT_Vector     pen;                    /* untransformed origin  */  FT_BBox bbox;  FT_Error      error;  char*         filename;  unsigned int chinese[]={0x4e2d,0x56fd,0x4eba,0x4e2d};  int           target_height;  int           n, num_chars;  int line_box_yMin=1000;  int line_box_yMax=HEIGHT;  int line_box_xMin=0;  int line_box_xMax=-1000;  if ( argc != 2 )  {    fprintf ( stderr, "usage: %s font \n", argv[0] );    exit( 1 );  }  filename      = argv[1];                           /* first argument     */  target_height = HEIGHT;  error = FT_Init_FreeType( &library );              /* initialize library */  /* error handling omitted */  error = FT_New_Face( library, argv[1], 0, &face ); /* create face object */  /* error handling omitted */#if 0  /* use 50pt at 100dpi */  error = FT_Set_Char_Size( face, 50 * 64, 0,                            100, 0 );                /* set character size *//* pixels = 50 /72 * 100 = 69  */#elseFT_Set_Pixel_Sizes(face, 24, 0);#endif  /* error handling omitted */  slot = face->glyph;  /* the pen position in 26.6 cartesian space coordinates; */  /* start at (0,40) relative to the upper left corner  */  pen.x = 0 * 64;  pen.y = ( target_height - 24 ) * 64;  printf("%ld\n",sizeof(chinese));  for ( n = 0; n < sizeof(chinese)/4; n++ )  {    /* set transformation */    FT_Set_Transform( face, 0 , &pen );    /* load glyph image into the slot (erase previous one) */    error = FT_Load_Char( face, chinese[n], FT_LOAD_RENDER );    if ( error )      continue;                 /* ignore errors */    error = FT_Get_Glyph( slot , &glyph );    if (error){    printf("FT_Get_Glyph error!\n");    return -1;}    FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox );    printf("x.min:%ld x.max:%ld\n",bbox.xMin,bbox.xMax);    printf("y.min:%ld y.max:%ld\n",bbox.yMin,bbox.yMax);    if(line_box_yMin>bbox.yMin){line_box_yMin=bbox.yMin;    }    if(bbox.xMax>WIDTH){pen.x = 0 * 64;pen.y = (target_height-line_box_yMax+line_box_yMin-24) * 64;FT_Set_Transform( face, 0 , &pen );error = FT_Load_Char( face, chinese[n], FT_LOAD_RENDER );        if ( error )            continue;                 /* ignore errors */    }    /* now, draw to our target surface (convert position) */    draw_bitmap( &slot->bitmap,                 slot->bitmap_left,                 target_height - slot->bitmap_top );    /* increment pen position */    pen.x += slot->advance.x;  }  show_image();  FT_Done_Face    ( face );  FT_Done_FreeType( library );  return 0;}/* EOF */


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 魅族mx3开不开机怎么办 魅蓝max3充电慢怎么办? 魅族手机home键失灵怎么办 魅族开关键坏了怎么办 魅族开关键不灵怎么办 魅蓝开机键坏了怎么办 魅族开关机坏了怎么办 小米开机键坏了怎么办 魅族手机关机键失灵怎么办 魅蓝5s发热严重怎么办 华为荣耀v8信号差怎么办 贴膜白边去除液漏进屏幕里面怎么办 魅族pro6s电池休眠了怎么办 京东预约错了怎么办 魅蓝手机声音小怎么办 魅族2手机锁定了怎么办 flyme的密码忘了怎么办 魅族note3忘记开机密码怎么办 小米3s手机死机怎么办 二手小米手机有账号锁怎么办 小米五指纹解锁失灵怎么办 小米note3指纹解锁失灵怎么办 小米4s手机屏幕失灵怎么办 vivo手机没有otg功能怎么办 头戴耳机压头发怎么办 小米4c很卡怎么办 小米4c玩王者怎么办 小米4s屏幕乱跳怎么办 小米4s手机后壳碎了怎么办 小米5spius开不了机怎么办 小米5s无限重启怎么办 小米5s外屏坏了怎么办 小米5s内屏碎了怎么办 小米4充电没反应怎么办 小米5手机变卡了怎么办 小米5变卡了怎么办 小米手机充电无反应怎么办 小米6相机卡顿怎么办 华为手机玩游戏发热怎么办 华为手机变慢了怎么办 华为p10手机变慢怎么办