I.MX6ULL之交叉编译Freetype库
本文章所用到的代码可以到
Gitee
下载。
Freetype
库:是一个完全免费(开源)的,高质量的且可以指的字体引擎,它提供统一的接口来访问多种字体格式文件。
我们在上一次的LCD显示中,采用了点阵字库来显示字母、汉字等等,由于对这些字体进行放大或者缩小的话,会使字体在显示时可能会出现模糊或者锯齿,故引用了矢量字体(放大缩小不会失真):
矢量字体的形成:
①:确定关键点
②:使用数学曲线(贝塞尔曲线)连接关键点
③:填充闭合曲线内部空间
交叉编译
首先要知道的一些小知识:头文件–>需要在编译时指定所在目录,库文件–>需要在编译和运行时都指定目录。
目录可以是系统已经包含的目录,也可以手动指定目录。本次采用把编译和运行时要用到的文件都放到系统目录下去,即可以直接编译。如何知道系统已经包含的目录,可以使用以下指令:echo 'main(){}'| arm-buildroot-linux-gnueabihf-gcc -E -v –
(此处的编译链工具需要根据自己的来修改)。
可以从上面找一个自己喜欢的目录,然后把它记录下来,我使用的路径是:
头文件:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/include
库文件:/home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/
然后就可以去先解压freetype-2.10.2.tar.xz
压缩包,解压完成后用解压出来的Makefile进行编译,这里我是用的新的编译链(包含zlib,如果不包含的话需要用下面的命令来编译安装zlib),编译出错是正常的,是因为freetype
是依赖于libpng
的,而libpng
依赖于zlib
,所以我们要先去①:编译安装libpng
:
tar xJf libpng-1.6.37.tar.xz #解压libpng
cd libpng-1.6.37 #进入到libpng目录
./configure --host=arm-buildroot-linux-gnueabihf --prefix=$PWD/tmp #编译当前配置文件
#--host:主机为arm的编译工具链 --prefix:编译出来的文件存放目录--当前文件目录下的/tmp目录
make #编译
make install #安装libpng
cd tmp/include #把编译出来的库文件、头文件都复制到之前记录下来的路径
cp * -rf /home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/include
cd tmp/lib
cp * -rfd /home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/ #-rfd的意思是为了避免内存浪费,复制完了就直接删掉
②:编译安装freetype
:
tar xJf freetype-2.10.2.tar.xz #解压freetype
cd freetype-2.10.2 #进入到freetype目录
./configure --host=arm-buildroot-linux-gnueabihf --prefix=$PWD/tmp #编译当前配置文件
#--host:主机为arm的编译工具链 --prefix:编译出来的文件存放目录--当前文件目录下的/tmp目录
make #编译
make install #安装freetype
cd tmp/include #把编译出来的库文件、头文件都复制到之前记录下来的路径
cp * -rf /home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/include
cd tmp/lib
cp * -rfd /home/book/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/arm-buildroot-linux-gnueabihf/sysroot/usr/lib/ #-rfd的意思是为了避免内存浪费,复制完了就直接删掉
到此为止,准备工作都已经做完了,想要显示一个矢量汉字在屏幕上,首先需要知道:
freetype
:使用freetype
调用对应的API
接口,提供字体文件,就可以让freetype
库帮我们取出关键点、实现闭合曲线,填充颜色,达到显示矢量字体的目的。
simsun.ttc
文件中,开头是一些字符映射表,以适应不同的编码格式。不同的编码通过自己的字符映射表找到当前这个文字对应的点阵数据,称为glyph
。
1,给定一个文字,如“中”,确定他的编码值(
ASCII
,UNICODE
,GB2312
)(根据字符映射表找到对应的点阵数据–>glyph)2.设置字体的大小
3.根据编码值,从文件头部中找到对应的关键带你,它会根据字体大小调整关键点
4.把关键点转换为位图点阵
5.在LCD显示
Freetype
的使用:
1.初始化:
FT_InitFreetype
2.加载(打开)字体Face:
FT_New_Face
3.设置字体大小:
FT_Set_Char_Sizes
或FT_Set_Pixel_Sizes
4.选择Charmp:
FT_Select_Charmap
,字符映射表5:根据编码值
charcode
找到glyph_index
:glyph_index = FT_Get_Char_Index(face,charcode)
6.根据
glyph_index
取出glyph
:FT_Load_Glyph(face,glyph_index)
7:转为位图:
FT_Render_Glyph
8.移动或旋转:
FT_Set_Transform
9.最后显示出来
上面的⑤⑥⑦可以使用一个函数代替:
FT_Load_Char(face, charcode, FT_LOAD_RENDER)
,它就可以得到位图。
wchar_t
:是C/C++的字符类型,是一种扩展的存储方式,一般为16位或32位。
wcslen
:获取字符串长度,用于宽字符集。
unsigned long int strtoul(const char *str, char **endptr, int base)
把参数 str
所指向的字符串根据给定的 base
转换基数–进制方式 转换为一个无符号长整数(类型为 unsigned long int 型),base
必须介于 2 和 36(包含)之间,或者是特殊值 0。endptr
为返回字符串有效数字的结尾地址.
Freetype
的代码实现思路:
1.初始化
Freetype
库:error = FT_Init_FreeType( &library );
2.加载字体文件,保存在&face中
error = FT_New_Face( library, argv[1], 0, &face );
3.从face中获取
FT_GlyphSlot
:slot = face->glyph;
4.设置字体的大小:
FT_Set_Pixel_Sizes(face, font_size, 0);
5.根据编码值得到位图:
error = FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER );
执行FT_Load_Char之后,字符的位图被存在slot->bitmap里,即face->glyph->bitmap。
这一行代码实现了:①:根据编码值获得
glyph_index
:FT_Get_Char_Index
②:根据
glyph_idex
取出glyph
:FT_Load_Glyph
③:渲染出位图:
FT_Render_Glyph
**在屏幕上显示位图:**采用之前LCD画点的函数来显示
编译运行:
arm-buildroot-linux-gnueabihf-gcc -o lcd_freetype_font lcd_freetype_font.c -lfreetype #编译时要连接freetype库
cp lcd_freetype_font ~/nfs_rootfs/ #复制到nfs目录
./lcd_freetype_font simsun.ttc 200 #运行时,第三个参数可选,为字体大小
代码:
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <wchar.h>
#include <sys/ioctl.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
static int fd_fb;
static struct fb_var_screeninfo var; /* Current var */
static int screen_size;
static unsigned char *fb_base;
static unsigned int line_width;
static unsigned int pixel_width;
/*
description:在(x,y)处画颜色可修改的点
int x: x坐标
int y: y坐标
unsigned int color: 颜色(采用RGB)
*/
void lcd_put_pixel(int x, int y, unsigned int color)
{
/* 字号大小 */
unsigned char *pen_8 = fb_base+y*line_width+x*pixel_width;
unsigned short *pen_16;
unsigned int *pen_32;
unsigned int red, green, blue;
pen_16 = (unsigned short *)pen_8;
pen_32 = (unsigned int *)pen_8;
switch(var.bits_per_pixel) /* 选择BPP */
{
case 8:
{
*pen_8 = color;
break;
}
case 16:
{
/* 565格式 */
red = (color >> 16) & 0xff;
green = (color >> 8) & 0xff;
blue = (color >> 0) & 0xff;
color = (((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3));
*pen_16 = color;
break;
}
case 32:
{
*pen_32 = color;
break;
}
default:
{
printf("can't surport %dbpp\n", var.bits_per_pixel);
break;
}
}
}
/**********************************************************************
* 函数名称: draw_bitmap
* 功能描述: 根据bitmap位图,在LCD指定位置显示汉字
* 输入参数: 位图指针,x坐标,y坐标
* 输出参数: 无
* 返 回 值: 无
***********************************************************************/
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;
//printf("x = %d, y = %d\n", x, y);
for ( j = y, q = 0; j < y_max; j++, q++ )
{
for ( i = x, p = 0; i < x_max; i++, p++ )
{
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)
{
wchar_t *chinese_str = L"中";
int error;
int font_size = 24; /* 字体大小 */
/* 声明一些要用到的结构体变量 */
FT_Library library;
FT_Face face;
FT_Vector pen;
FT_GlyphSlot slot;
if(argc < 2)
{
printf("Usage : %s <font_file> [font_size]\n", argv[0]);
return -1;
}
if (argc == 3)
font_size = strtoul(argv[2], NULL, 0); /* 把传进来的argv[2]转换为整数 */
fd_fb = open("/dev/fb0",O_RDWR); /* 以可读可写的方式打开/dev/fb0 */
if(fd_fb < 0)
{
printf("can't open /dev/fb0\n"); /* 返回值为-1,表示打开文件失败! */
return -1;
}
if(ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) /* get var screen info:获取屏幕的可变信息 */
{
printf("can't get var\n");
return -1;
}
line_width = var.xres * var.bits_per_pixel / 8; /* 行宽 = X*BPP/8 */
pixel_width = var.bits_per_pixel / 8; /* 列宽 = BPP/8 */
screen_size = var.xres * var.yres * var.bits_per_pixel / 8; /* FrameBuffer的总大小 */
fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
if(fb_base == (unsigned char *)-1)
{
printf("can't mmap\n'");
return -1;
}
/* 清屏: 全部设为黑色 */
memset(fb_base, 0, screen_size);
error = FT_Init_FreeType( &library ); /* 初始化 */
error = FT_New_Face( library, argv[1], 0, &face ); /* 加载字体文件,保存在&face中 */
slot = face->glyph; /* 从face中获取关键点 */
FT_Set_Pixel_Sizes(face, font_size, 0); /* 设置字体的大小 */
/* 字符的位图存在slot->bitmap里,即face->glyph->bitmap。 */
error = FT_Load_Char( face, chinese_str[0], FT_LOAD_RENDER );
if(error)
{
printf("FT_Load_Char error\n");
return -1;
}
draw_bitmap( &slot->bitmap,
var.xres/2,
var.yres/2);
return 0;
}