之前有做过 ascii 和汉字库的字体点阵在lcd上显示的例子,都是按照指定大小的字库的点阵来显示的,所以一但选定了字体文件后,就固定了大小,不可变化,当然也可以存放各种 大小的字体文件,但这样的话就需要很多的空间,这种方法显然不好使,所以就引入了失量字体,关于字体的特点就不啰嗦了。可以去网上搜到很多说明。下面我们一步一步的来做实验测试了解失量字体的用法,先在PC机上测试,然后再移植到开发板上用lcd显示。
一、首先我们要去获得失量字体的源码和一些文档,https://www.freetype.org/freetype2/docs/documentation.html 这里就是官网。然后按照他的文档来简单了解一下,同时还提供了一个C例子,分析例子,修改后可以先在PC机上测试。
二、得到源码后,解压配置安装。列出步骤和命令。注意这是在PC机上运行的命令如果要在开发板上运行,要用交叉编译 还有配置也有不同
./configure 配置
make 编译
sudo make install 安装
三、把源码例子拿过来编译
gcc -o example example.c -I /usr/local/include/freetype2/ -lfreetype -lm
/* -I /usr/local/include/freetype2/ 指定头文件目录 -lfreetype 指定库类型 -lm 指定数学库 */
/* 这是大i 这是小L 小L */
如果没加会出错,不信可以先不加试一试然后一个一个加试下,这是方法,为什么我也不知道
./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
#include
#include
#include
#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 */
上面显示了英文字符,通过执行程序时传进去的 abc 如果我们想显示中文怎么办呢,我们在源码里先定义一个字符串,再显示字符串 这里有一个地方要注意,字符串要以宽字符的方式定义保存,不能以 char *str = "矢量字体"; 这样的方式定义,因为中文是用二个字节保存的 英文是一个字节,如果一个字符串里又有中文又有英文就不好处理了。因此又引入了“宽字符” 宽字符怎么用,老办法 搜。wchar_t *str = L"矢量字体"; 这样定义 同时还有一个头文件要包含进去 #include
在这个例子里会出现编译时 提示无法转换字符集 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
#include
#include
#include
#include
#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 */
通过上面的测试,下面总结一下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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章