文本处理以及freetype的使用

1. 字符编码

编码解释
GBK(国标扩展码)英文字符用一个字节进行表示,值小于等于 127。汉字用两个字节进行表示,对应的区位码由国家标准指定
UTF-8变长编码,中文文字编码开头的 1 的个数是指该文字用多少个字节进行表示,第一个字节从 0 开始后面的位与往后字节后 6 位组合在一起就是该文字的 Unicode 码。英文字符用 0-127 表示
UTF-16LE定长编码,用两个字节表示该字符的 Unicode 码,小端存储
UTF-16BE变长编码,用两个字节表示该字符的 Unicode 码,大端存储
Unicode国际码映射表,指定了文字与数值之间的对照关系,并不指定编码的长度
BIG5台湾繁体字编码

例子:黄

编码     十六进制    二进制
UTF-8    e9bb84    1110 1001 10 111011 10 000100
UTF-16LE c49e      11000100 10011110
UTF-16BE 9ec4      10011110 11000100
GBK      bbc6

GBK 编码与 Unicode 码没有直接的转换关系,只能靠映射表进行一对一的转换

2. freetype 库的使用

freetype 的作用

  • 打印矢量字体,也就是字体的大小可以改变。
  • 对字体进行特殊处理,旋转字体等等

freetype 的移植

  • 下载
    https://www.freetype.org/download.html

  • 编译

     ./configure --host=arm-linux
     make
     mkdir tmp
     make DESTDIR=$PWD/tmp install
    
  • 拷贝库以及头文件到交叉编译工具链里面

    sudo cp /work/testfile/digital_prj/freetype/arm-freetype-2.6.5/tmp/usr/local/lib/* ./arm-none-linux-gnueabi/lib/ -drf
    sudo cp /work/testfile/digital_prj/freetype/arm-freetype-2.6.5/tmp/usr/local/include/* ./arm-none-linux-gnueabi/include/ -drf
    sudo cp /work/testfile/digital_prj/freetype/arm-freetype-2.6.5/tmp/usr/local/bin/* ./arm-none-linux-gnueabi/bin/ -drf
    
  • 编译测试文件

arm-linux-gcc -o example1 example1.c -I /work/tools/opt/FriendlyARM/toolschain/4.4.3/arm-none-linux-gnueabi/include/freetype2 -lfreetype -lm
    
  • 拷贝库文件到开发板上面,运行测试
    cp arm-freetype-2.6.5/tmp/usr/local/lib/so  /lib/ -d 
    ./example1 abc

freetype 的使用:一个字符显示的流程

  • 初始化一个freetype库
FT_Init_FreeType( &library ); //创建一个freetype库实例并且初始化 它的处理库
  • 加载字体样式
error = FT_New_Face( library, filename, 0, &face ); //从外部的文件里面加载一个字体样式,然后将这个库对象返回到face中,以待使用
  • 设置字体的大小
    FT_Set_Pixel_Sizes(face, 24, 0);    //通过像素设置
    error = FT_Set_Char_Size( face, 16 * 64, 0, 300, 0 );   //通过设置长宽范围设置
    error = FT_Set_Char_Size(
          face,    /* 字体对象句柄           */
          0,       /* 字宽单位是 1/64 的点  */
          16*64,   /* 字高单位是 1/64 的点 */
          300,     /* 水平方向分辨率    */
          300 );   /* 竖直方向分辨率    */

一个点指的是物理的 1/72 英寸
常用分辨率是 72或者96 dot-per-inch,也就是每英寸是 72个或者96 个像素
- 设置坐标
- 设置字框
- 设置字体矩阵
- 设置参考原点
- 字形到矩阵

    FT_Set_Transform( face, &matrix, &pen );    //设置转换矩阵
    error = FT_Load_Char( face, text[n], FT_LOAD_RENDER );  //加载每一个字,加载出来之后就会有一个bitmap结构体,里面的buffer里面存储的是八位的像素点数据,按照字宽以行进行排列,要求已经转化为位图了,这里是用1个字节表示一个像素。如果加上 FT_LOAD_MONOCHROME 的话就是1位表示一个像素

    //freetype是按照Unicode码来进行索引的,而utf8是Unicode码的变体,经过变换可以得到Uincode码
    //单色位图节省空间,生成的单色位图是整个字框,但是字不一定有字框那么大,我们在描绘的时候仅仅把能包含字的最小字框给描画出来,并不把整个字框范围给描绘出来,这样更加节省空间
  • 描画数据
    draw_bitmap( &slot->bitmap, slot->bitmap_left, target_height - slot->bitmap_top );  //描点。不断循环
    //这个描点要求要知道这个字的原点位置,知道这个字体的宽度以及其左上角的坐标,上面函数后两个参数就分别是最左边坐标和最上面坐标
  • 打印到显示设备上面并且结束
    show_image();   //打印
    FT_Done_Face    ( face );   //销毁创建的各个变量
    FT_Done_FreeType( library );

实例:

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

#include <ft2build.h>
#include FT_FREETYPE_H


#define WIDTH   50
#define HEIGHT  50


/* 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;

  wchar_t *chinese_str = L"叮当猫";
  unsigned int *chinese_ptr = (wchar_t *)chinese_str;

  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, filename, 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 */
#else
  error = 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 (300,200) relative to the upper left corner  */
  pen.x = 0 * 64;
  pen.y = ( target_height - 25 ) * 64;

  for ( n = 0; n < 12; n++ )
  {
    /* set transformation */
    FT_Set_Transform( face, &matrix, &pen );

    /* load glyph image into the slot (erase previous one) */
    error = FT_Load_Char( face, chinese_str[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;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值