freetype的简单使用

本文介绍如何使用FreeType2库在C程序中打印旋转的文本。通过下载字体文件,编写并编译示例代码,展示了如何设置字体大小、角度以及在控制台输出带旋转效果的字符串。

安装完毕以后我们先做个简单的实例程序看看效果

1.首先先下载字体

链接:https://pan.baidu.com/s/1FCOh9SYcVkYCkaT6wtXWtA 
提取码:rohm 
2.编写程序

编译测试文件example.c      

/*编译命令*/ -I指定库文件所在位置
            -L指定动态库位置

gcc  example.c  -o  example   -I /usr/local/freetype/include/freetype2    -L/usr/local/freetype/lib  -lfreetype 

example.c  程序

/* example1.c                                                      */
/*                                                                 */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library.                                             */
 
 
#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;
  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 */

运行测试程序    ./example  ./simsun.ttc   fbg    (预先将simsun.ttc字体文件拷贝到当前目录)结果如下:

 

 

 

 

FreeType是一个开源的字体渲染引擎,它提供了一套API,可以帮助开发人员轻松地处理各种字体文件,如TrueType、Type1、OpenType、CID等,同时也支持多种字体效果,如反锯齿、粗体、斜体等。 下面是FreeType使用详解: 1.安装FreeType库 在Linux下,可以通过包管理器安装FreeType库,在终端中执行以下命令: sudo apt-get install libfreetype6-dev 在Windows下,可以从FreeType的官网下载预编译好的库文件,并将其添加到编译器的库路径中。 2.初始化FreeType库 在使用FreeType库之前,需要先对其进行初始化。初始化的过程比较简单,只需要调用如下代码即可: FT_Library library; if (FT_Init_FreeType(&library)) { // 初始化失败 } 3.加载字体文件 在使用FreeType库渲染字体之前,需要先加载字体文件。FreeType支持多种字体文件格式,如TrueType、Type1、OpenType、CID等。以下是加载TrueType字体文件的示例代码: FT_Face face; if (FT_New_Face(library, "font.ttf", 0, &face)) { // 加载字体文件失败 } 其中,"font.ttf"表示字体文件的路径,face为字体对象。 4.设置字体大小 在渲染字体之前,需要先设置字体的大小。以下是设置字体大小的示例代码: if (FT_Set_Char_Size(face, 0, 16*64, 300, 300)) { // 设置字体大小失败 } 其中,第二个参数表示字符宽度,第三个参数表示字符高度,第四个参数和第五个参数表示水平和垂直分辨率。 5.渲染字体 在设置好字体大小之后,就可以开始渲染字体了。以下是渲染字体的示例代码: FT_UInt glyph_index = FT_Get_Char_Index(face, 'A'); if (FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT)) { // 加载字形失败 } if (FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) { // 渲染字形失败 } 其中,第一个参数表示字形对象,第二个参数表示渲染模式。 6.获取字形信息 在渲染字形之后,可以通过字形对象获取字形的信息,如字形的宽度、高度、左边距、上边距等。以下是获取字形信息的示例代码: FT_Glyph_Metrics metrics = face->glyph->metrics; int width = metrics.width >> 6; int height = metrics.height >> 6; int left = face->glyph->bitmap_left; int top = face->glyph->bitmap_top; 其中,metrics表示字形度量对象,width和height表示字形的宽度和高度,left和top表示字形的左边距和上边距。 7.释放资源 在使用FreeType库之后,需要对其进行资源释放。以下是释放资源的示例代码: FT_Done_Face(face); FT_Done_FreeType(library); 以上就是FreeType使用详解。需要注意的是,在实际使用中,还需要进行一些错误处理和异常情况的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值