FreeType 矢量字体 测试移植(1)

 以前有作过 ascii 和汉字库的字体点阵在lcd上显示的例子,都是按照指定大小的字库的点阵来显示的,因此一但选定了字体文件后,就固定了大小,不可变化,固然也能够存放各类 大小的字体文件,但这样的话就须要不少的空间,这种方法显然很差使,因此就引入了失量字体,关于字体的特色就不啰嗦了。能够去网上搜到不少说明。下面咱们一步一步的来作实验测试了解失量字体的用法,先在PC机上测试,而后再移植到开发板上用lcd显示。html

  1、首先咱们要去得到失量字体的源码和一些文档,https://www.freetype.org/freetype2/docs/documentation.html 这里就是官网。而后按照他的文档来简单了解一下,同时还提供了一个C例子,分析例子,修改后能够先在PC机上测试。linux

  2、获得源码后,解压配置安装。列出步骤和命令。注意这是在PC机上运行的命令若是要在开发板上运行,要用交叉编译 还有配置也有不一样服务器

    ./configure    配置ide

    make      编译函数

    sudo make install  安装工具

  3、把源码例子拿过来编译
测试

    gcc -o example example.c -I /usr/local/include/freetype2/ -lfreetype -lm 字体

    /* -I /usr/local/include/freetype2/ 指定头文件目录       -lfreetype 指定库类型        -lm 指定数学库 */ui

    /* 这是大i                                                                     这是小L                               小L */this

    若是没加会出错,不信能够先不加试一试而后一个一个加试下,这是方法,为何我也不知道

    ./example ./simsun.ttc abc    执行就会打印出字体文件在终端上,可是看不到,由于源码里的设置太大了,要改小一些才能够

    在执行时须要一个字体文件,能够从C:\Windows\Fonts里找一个复制过去

    下面贴出一段在PC上显示代码 用FreeType官方例子稍改就能够了。

/* 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 /* 这里修改 原来是680 480 太大 */
#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 simplicity, we assume that `bitmap->pixel_mode' */
    /* is `FT_PIXEL_MODE_GRAY' (i.e., not a bitmap font) */

    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 ); /* 角度设为0不旋转 */ 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 */
    /* error handling omitted */
#else error = FT_Set_Pixel_Sizes( face, /* handle to face object */
              0,      /* pixel_width */
              16 );   /* pixel_height */    
#endif
    /* cmap selection omitted; */
    /* for simplicity we assume that the font contains a Unicode cmap */ 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 - 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 */
View Code

 

  上面显示了英文字符,经过执行程序时传进去的 abc 若是咱们想显示中文怎么办呢,咱们在源码里先定义一个字符串,再显示字符串 这里有一个地方要注意,字符串要以宽字符的方式定义保存,不能以 char *str = "矢量字体"; 这样的方式定义,由于中文是用二个字节保存的 英文是一个字节,若是一个字符串里又有中文又有英文就很差处理了。所以又引入了“宽字符” 宽字符怎么用,老办法 搜。wchar_t *str = L"矢量字体"; 这样定义 同时还有一个头文件要包含进去 #include <wchar.h> ,下面列出一个例子,一样是在上面的基础上改的。

  在这个例子里会出现编译时 提示没法转换字符集 error:converting to execution character set: Invalid or incomplete multibyte or wide character 

  在指定字符输入输出字符集编译 gcc  -finput-charset=GBK -fexec-charset=UTF-8 -o example example.c -I /usr/local/include/freetype2/ -lfreetype -lm 

这里列出源码

 

/* 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 <wchar.h> #include <ft2build.h> #include FT_FREETYPE_H /* 这里修改 原来是680 480 太大 */
#define WIDTH   100
#define HEIGHT  100


/* 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 simplicity, we assume that `bitmap->pixel_mode' */
    /* is `FT_PIXEL_MODE_GRAY' (i.e., not a bitmap font) */

    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"矢量字体"; /* 把参数改成2个 */
    if ( argc != 2 ) { fprintf ( stderr, "usage: %s font \n", argv[0] ); exit( 1 ); } /* 注释掉这两行 */ filename = argv[1];                           /* first argument */
// text = argv[2]; /* second argument */ // num_chars = strlen( text );
    /* 角度设为0不旋转 */ 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 */
    /* error handling omitted */
#else
    /* 直接用这个函数设字像素大小 */ error = FT_Set_Pixel_Sizes( face, /* handle to face object */
              0,      /* pixel_width */
              24 );   /* pixel_height */    
#endif
    /* cmap selection omitted; */
    /* for simplicity we assume that the font contains a Unicode cmap */ 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 = 10 * 64; pen.y = ( target_height - 40 ) * 64; /* man wcslen man strlen 去找到用法 获得字符串长度 */
    for ( n = 0; n < wcslen(chinese_str); 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; } /* EOF */
View Code

 

经过上面的测试,下面总结一下freetype字体的用法,和一些注意事项。参考官方文档。后面会慢慢增长对FreeType的测试代码。

 

上面的代码都是在PC机上运行的,如今咱们来移植到ARM开发板上去,首先要进行交叉编译,试一下一大堆错误,咱们连freetype都没有编译配置,下面先来编译

1:解压 tar xjf freetype-2.9.1.tar.bz2  而后进入目录

2:配置 ./configure --host=arm-linux  指定为交叉编译用于ARM平台

3:编译 make

4:安装 make DESTDIR=$PWD/tmp install 先安装到当前目录下的tmp目录里,而后把 lib include里的东西拷贝到咱们编译器工具链里 若是直接make 会安装到PC机根目录下

  cd tmp/usr/local/include/

  sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include -rf

  cd tmp/usr/local/lib/

  sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib -d -rf

5:编译应用程序 arm-linux-gcc example example.c 出现一大堆错误

  5.1:sudo mv freetype2/freetype .   编译时找头文件目录多了一个freetype2 咱们把下面的都移上来。仍是不行,那就指定头文件目录 指定动态库 指定字符集再编译

    arm-linux-gcc -finput-charset=GBK -o freetype freetype.c -I /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include/freetype -lfreetype

    编译成功后 可执行程序freetype 字体文件simsun.ttc 拷贝到根文件系统目录里去  动态库tmp/usr/local/lib/ 拷贝到文件系统根目录lib/目录下 

    而后就能够执行,且能够在终端打印出字体了。

 

下一步,修改应用程序源码,让其在lcd上显示,咱们以前有作过在Lcd上显示英文和中文字体,那些显示函数是能够用的,这里咱们修改的就是把在终端输出改为在lcd上显示

下面列出源码。

  修改后 编译仍是出错  error: failure to convert GBK to UTF-8 用amn gcc 找一下 charset 看一个gcc里怎么转换 看到这里

 

  -fexec-charset=charset
    Set the execution character set, used for string and character constants. The default is UTF-8. charset can be any encoding supported by the system's "iconv" library
    routine.

  咱们看一下 iconv --help 怎么用 

  Input/Output format specification:
  -f, --from-code=NAME encoding of original text
  -t, --to-code=NAME encoding for output

  咱们用执行一下看看 

  iconv -f GBK -t UTF-8 freetype.c 

  能够看到在处理asicc字体文件时有个别字符他没法识别,咱们去掉这些字符就能够了 源码经测试可经过,彻底显示在lcd上,能够修改参数,换字体 换大小,换角度均可以修改测试,最关键的地方就是 坐标 freetype是基于“笛卡尔”坐标,咱们用的是“lcd”坐标 。

 

 

/* example1.c */
/*                                                                 */
/* This small program shows how to print a rotated string with the */
/* FreeType 2 library. */ #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <stdio.h> #include <linux/fb.h> #include <fcntl.h> #include <string.h> #include <math.h> #include <wchar.h> #include <ft2build.h> #include FT_FREETYPE_H static int fd_fb; static struct fb_var_screeninfo var;    /* 定义一个可变信息结构体 用于保存得到的可变信息 */
static struct fb_fix_screeninfo fix;    /* 定义一个固定信息结构体 用于保存得到的固定信息 */
static int screen_size;                /* 定义一个变量 表示 framebuffer 进行内存映射 */
static unsigned char *fbmem;            /* 定认一个指针变量 用于保存内存映射后的地址 */

static int fd_hzk; static struct stat hzk_stat;            /* 定义一个结构体用于保存统计信息 */
static unsigned char *hzkmem;            /* 定义一个指针变量 用于保存央存映射的地址 */

static unsigned int line_width; static unsigned int pixel_whdth; static unsigned char *str = ""; /* 描点函数 根据 x,y,值 肯定对应framebuffer的位置 * 而后依次的在对应的framebuffer上写入颜色值便可 */
void lcd_put_pixel(int x, int y, unsigned int color) { unsigned char *pen = fbmem + y*line_width + x*pixel_whdth; *pen = color; } /* 显示中文 如何显示呢? * 1: 先获得汉字库的文件 而后能够像打开framebuffe同样打开这个文件 * 2: 打开后就能够去读了,咱们也能够把这个文件当成内存同样用 即映射一下 * 3: 而后就能够去里面获得字体点阵数据了,接着和显示字符同样描点便可 * 把文件当成内存去映射先要获得文件大小 能够用 fstat 这个函数来得到大小 * 如何使用这个函数 能够在服务器上输入 man fstat 获得使用说明 * 汉字库的使用方法能够去"度娘"去找,基本上就是下面几个注意的地方 * 1: GBK编码用四个字节表示一个中文 第一个字节表示区码 第二个字节表示位码 * 2: 为了兼容ascii码 编码从a1 开始 例:"中"字 值是 D6 D0 D6=区码 D0=位码 * 3: 因此编码的值是 区码-A1 位码-A1 */
void lcd_put_chinses(int x, int y, unsigned char *str) { unsigned int area   = str[0] - 0xa1; unsigned int where  = str[1] - 0xa1; unsigned char *dots = hzkmem + (area * 94 + where) * 32; unsigned char byte; int i,j,k; for (i = 0;i < 16;i ++) { for (j = 0;j < 2;j ++) { byte = dots[i * 2 + j]; for (k = 7;k >=0;k --) { if (byte & (1 << k)) { /* 传入参数的值是lcd要显示的起点坐标 这里每一个点的坐标每描一个点要移动一次 */ lcd_put_pixel(x + j * 8 + 7 - k, y + i, 0xffffff);    /* 0xffffff 表示白色 */ } else { /* 传入参数的值是lcd要显示的起点坐标 这里每一个点的坐标每描一个点要移动一次 */ lcd_put_pixel(x + j * 8 + 7 - k, y + i, 0);        /* 0 表示黑色 */ } } } } } 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 simplicity, we assume that `bitmap->pixel_mode' */
    /* is `FT_PIXEL_MODE_GRAY' (i.e., not a bitmap font) */

    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 >= var.xres|| j >= var.yres ) continue; //image[j][i] |= bitmap->buffer[q * bitmap->width + p];
          lcd_put_pixel(i, j, bitmap->buffer[q * bitmap->width + p]); } } } 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"翻繁敏酩aAbB"; fd_fb = open("/dev/fb0", O_RDWR);    /* 打开lcd设备 可读可写 */
    if (fd_fb < 0) { printf("cnt't open /dev/fb0 !\n"); return -1; } if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))        /* 得到可变信息 */ { /* 正常得到信息的话 ioctl 会返回0 若是返回值不为0时表示出错 */ printf("can't get var! \n"); return -1; } if (ioctl(fd_fb,FBIOGET_FSCREENINFO, &fix))        /* 得到固定信息 */ { /* 正常得到信息的话 ioctl 会返回0 若是返回值不为0时表示出错 */ printf("can't get fix! \n "); return -1; } /* 计算 framebuffer 的大小 用于内存映射单位字节 用x分辩率*y分辩率*每一个像素获得总大小这时的单位是bit 除以8 转换成字节 */ screen_size = var.xres * var.yres * var.bits_per_pixel / 8; /* 内存映射 mmap 怎么用呢,能够在服务器上输入man mmap 获得说明 * void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset); * 参数说明:void *addr 设置为0 让内核自动给咱们分配地址 * :size_t length 映射内存大小 * :int prot 属性为可读可写 * :int flags 共享 其它进程均可见 * :int fd framebuffer * :off_t offset 偏移值 */ fbmem = (unsigned char *)mmap(NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0); if (fbmem == (unsigned char *)-1) { printf("can't mmap!\n"); return -1; } line_width = var.xres * var.bits_per_pixel / 8; pixel_whdth = var.bits_per_pixel / 8; /* 打开当前目录下的 HZK16 文件 属性设为只读 */ fd_hzk = open("HZK16", O_RDONLY); if (fd_hzk < 0) { printf("can't open HZK16\n"); return -1; } /* 获得这个件的统计信息固然也包含了大小 */
    if (fstat(fd_hzk, &hzk_stat)) { printf("can't get hzk_stat! \n "); return -1; } /* 内存映射 mmap 怎么用呢,能够在服务器上输入man mmap 获得说明 * void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset); * 参数说明:void *addr 设置为0 让内核自动给咱们分配地址 * :size_t length 映射内存大小 * :int prot 属性为可读可写 * :int flags 共享 其它进程均可见 * :int fd 文件 * :off_t offset 偏移值 */ hzkmem = (unsigned char *)mmap(NULL, hzk_stat.st_size, PROT_READ, MAP_SHARED, fd_hzk, 0); if (hzkmem == (unsigned char *)-1) { printf("can't mmap!\n"); return -1; } /* 这里先清屏 所有显黑色 */ memset(fbmem, 0, screen_size); filename = argv[1];                           /* first argument */ angle = ( 10.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 */
    /* error handling omitted */
#else
    /* 直接用这个函数设字像素大小 */ error = FT_Set_Pixel_Sizes( face, /* handle to face object */
              0,      /* pixel_width */
              48 );   /* pixel_height */    
#endif
    /* cmap selection omitted; */
    /* for simplicity we assume that the font contains a Unicode cmap */ 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 = (var.xres/2 + 24) * 64;
    pen.x = (0 + 24) * 64; pen.y = (var.yres / 2 - 16)  * 64; /* man wcslen man strlen 去找到用法 获得字符串长度 */
    for ( n = 0; n < wcslen(chinese_str); 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, var.yres - slot->bitmap_top ); /* increment pen position */ pen.x += slot->advance.x; pen.y += slot->advance.y; } // show_image(); 

    /* 在lcd上显示ascii字符 即然是显示,确定要有位置 在那显示先定在中间 */
// lcd_put_ascii(var.xres / 2, var.yres / 2, 'F');
    /* 在lcd上显示中文 即然是显示,确定要有位置 在那显示 */ lcd_put_chinses(var.xres / 2 + 8, var.yres / 2, str); FT_Done_Face ( face ); FT_Done_FreeType( library ); return 0; }
View Code
相关文章
相关标签/搜索