PC上测试freetype

 

FreeType 字体引擎分析与指南
http://wenku.baidu.com/view/2d24be10cc7931b765ce155b.html

freetype原说明文档和库文件

链接:https://pan.baidu.com/s/1helfPdCwoQpJkpTQv7LSDQ
提取码:njuw

1、在PC虚拟机上安装库:
tar xjf freetype-2.4.10.tar.bz2
./configure
make
sudo make install

2、编译测试程序

gcc -o example1 example1.c  -I /usr/local/include/freetype2 -lfreetype -lm

(1)在安装了freetype库后,编译测试代码时出错: error: freetype/config/ftheader.h: No such file or directory,需要指定头文件路径

(2)出错:undefined reference to `FT_Init_FreeType',需要指定库文件的名字-lfreetype

(3)出错:undefined reference to `cos',需要添加数学运算-lm
gcc -finput-charset=GBK -fexec-charset=UTF-8 -o example1 example1.c  -I /usr/local/include/freetype2 -lfreetype -lm
./example1 ./simsun.ttc abc

3、运行

./example1 实现
usage: ./example1 font sample-text需要一个字体文件

可以在window系统下的C:\Windows\Fonts找到一个字体文件,我们这里使用新宋字体文件simsun.ttc

然后运行./example1 ./simsun.ttc agfqretet

得到显示结果:

 

例子部分源码:

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];                           /* first argument     */
  text          = argv[2];                           /* second argument    */
  num_chars     = strlen( text );
  angle         = ( 0.0 / 360 ) * 3.14159 * 2;      /* use 25 degrees     */
  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  */
#else
    FT_Set_Pixel_Sizes(face, 24, 0);
#endif
  /* 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 - 40 ) * 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;
}

/* EOF */

 

posted @ 2019-07-24 10:56  也许明天、  阅读(735)  评论(0)    收藏  举报