数码相册——使用FreeType在LCD上显示单个字符
- 硬件平台:韦东山嵌入式Linxu开发板(S3C2440.v3)
- 软件平台:运行于VMware Workstation 12 Player下UbuntuLTS16.04_x64 系统
- 参考资料:《嵌入式Linux应用开发手册》、《嵌入式Linux应用开发手册第2版》、【FreeType教程】、【FreeType2使用总结】
- 开发环境:Linux 3.4.2内核、arm-linux-gcc 4.3.2工具链
- 源码仓库:https://gitee.com/d_1254436976/Embedded-Linux-Phase-3
目录
一、代码文件
代码在官方示例examplel.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 <wchar.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_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++ )
{
printf("%02d", 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"韦gif"; //宽字符,每个字用4个字节表示
unsigned int *p = (wchar_t *)chinese_str;
int i;
FT_BBox bbox;
FT_Glyph glyph;
/* 打印"韦gif"的Uniocde值 */
printf("Uniocde: \n");
for (i = 0; i < wcslen(chinese_str); i++)
{
printf("0x%x ", p[i]);
}
printf("\n");
if ( argc != 2 )
{
fprintf ( stderr, "usage: %s font\n", argv[0] );
exit( 1 );
}
filename = argv[1]; /* first argument */
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 */
FT_Set_Pixel_Sizes(face, 24, 0);
/* 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 < 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 */
error = FT_Get_Glyph( face->glyph, &glyph ); //从插槽中取出face->glyph,存到glyph中
if (error)
{
printf("FT_Get_Glyph error!\n");
return -1;
}
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox ); //得到xMin,xMax等字符大小参数信息
/* now, draw to our target surface (convert position) */
draw_bitmap( &slot->bitmap,
slot->bitmap_left,
target_height - slot->bitmap_top );
printf("Unicode: 0x%x\n", chinese_str[n]);
printf("origin.x/64 = %d, origin.y/64 = %d\n", pen.x/64, pen.y/64);
printf("xMin = %d, xMax = %d, yMin = %d, yMax = %d\n", bbox.xMin, bbox.xMax, bbox.yMin, bbox.yMax);
printf("slot->advance.x/64 = %d, slot->advance.y/64 = %d\n", slot->advance.x/64, slot->advance.y/64);
/* 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 */
二、在开发板根文件系统的控制台进行验证
1、前期准备
1.1 在虚拟机中解压、配置、安装
解压命令:tar xjf freetype-2.4.10.tar.bz2
配置:./configure --host=arm-linux
,指定编译工具链
安装:make
、make DESTDIR=$PWD/tmp install
安装到当前目录下的tmp
目录下
1.2 移动头文件和库文件到交叉编译工具链对应文件夹中
安装好的FreeType需要把它的/include和/lib
目录下的所有文件,分别移动到/4.3.2/arm-none-linux-gnueabi/libc/usr/include
和/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib
先进入到/tmp/usr/local
目录下
命令如下:
-
移动
到/lib
目录文件,执行sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib -d -rf
-
移动
/include
目录文件,执行sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include -rf
1.3 移动库文件的.so
文件到开发板根文件系统的/lib
目录下
先进入到/tmp/usr/local
目录下
命令如下:
- 移动到
/lib
目录文件,执行cp *so* /work/nfs_root/fs_mini_mdev_new/lib -d
2、编译并运行
执行arm-linux-gcc -finput-charset=GBK -o example1 example1.c -lfreetype -lm
命令
-lfreetype
:指定头文件-lm
:指定数学库-finput-charset=GBK
:指定输出文件的编码格式为国标码
2.1 编译出现问题并解决
2.1.1 无法找到头文件
- 问题:
可事实上头文件目录在/freetype2/freetype/config
,而寻找的是/freetype/config
目录下
- 解决方法:
最简便的方法:在/include
目录下执行sudo mv freetype2/freetype .
,把/freetype2
目录下的freetype
文件夹移动到当前目录下include
2.1.2 未定义标识符
- 问题:
- 解决方法:
执行arm-linux-gcc -o example1 example1.c -lfreetype
2.1.3 数学三角运算符未定义
- 问题:
- 解决方法:
执行arm-linux-gcc -o example1 example1.c -lfreetype -lm
2.1.4 指定输入的字符集
执行arm-linux-gcc -finput-charset=GBK -o example1 example1.c -lfreetype -lm
2.2 在开发板上运行
2.2.1 把example
可执行文件上传到开发板上
执行cp examplel /work/nfs_root/fs_mini_mdev_new
2.2.2 在开发板上执行
执行./examplel ./simsun.ttc
- 结果:在控制台输出了以下图示
三、在LCD显示单个文字
1、代码文件
是在【1.3 数码相册—英文和汉字的点阵显示】的Show_font.c
和这篇博文的examplel.c
基础上修改,把examplel.c
的关键部分集成到了Show_font.c
中
#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 <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#define FONTDATAMAX 4096
static const unsigned char fontdata_8x16[FONTDATAMAX] = {
/* 方便显示,这里省略了字库编码,可以到内核的font_8x16.c文件中找*/
};
/* 文件操作 */
static int fd_fb;
static unsigned char *fbmem;
/* hzk16字库操作 */
static int fd_hzk16;
static struct stat hzk_stat;
static unsigned char *hzkmem;
/* lcd参数 */
static int screen_size;
static unsigned int line_width;
static unsigned int pixel_width;
static struct fb_var_screeninfo var;
static struct fb_fix_screeninfo fix;
/* lcd描色 color : 0x00RRGGBB */
void lcd_put_pixel(int x, int y, unsigned int color)
{
unsigned char *pen_8;
unsigned short *pen_16;
unsigned int *pen_32;
unsigned int red, green, blue;
/* 该坐标在内存中对应像素的位置 */
pen_8 = fbmem+y*line_width+x*pixel_width;
pen_16 = (unsigned short *)pen_8;
pen_32 = (unsigned int *)pen_8;
switch (var.bits_per_pixel) {
case 8:
*pen_8 = color;
break;
case 16:
/* RGB:565 */
red = ((color >> 16) & 0xffff) >> 3;
green = ((color >> 8) & 0xffff) >> 2;
blue = ((color >> 0) & 0xffff) >> 3;
*pen_16 = (red << 11) | (green << 5) | blue;
break;
case 32:
*pen_32 = color;
break;
default:
printf("can not surport &dbpp\n", var.bits_per_pixel);
break;
}
}
/* lcd显示ASCII码 */
void lcd_put_ascii(int x, int y, unsigned char c)
{
unsigned char *dots = (unsigned char *)&fontdata_8x16[c*16];
unsigned char byte;
int i, b;
/* 每行进行每位检查,为1则亮,否则灭 */
for (i = 0; i < 16; i++) {
byte = dots[i];
for (b = 7; b >= 0; b--) {
if (byte & (1<<b))
lcd_put_pixel(x+7-b, y+i, 0xffffff); //亮
else
lcd_put_pixel(x+7-b, y+i, 0x000000); //灭
}
}
}
/* lcd显示中文 */
void lcd_put_chinese(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;
int i, j, b;
unsigned char byte;
/* 由于汉字需要16*16个点阵,即一行需要2个字节的数据
* 所以在提取字节时需要:i*2跳到对应行,j跳到对应的字节
* 在描点时需要x+7-b跳到该行该字节的每个位,j*8跳到对应的字节
*/
for (i = 0; i < 16; i++) {
for (j = 0; j < 2; j++) {
byte = dots[i*2 + j];
for (b = 7; b >= 0; b--) {
if (byte & 1 << b)
lcd_put_pixel(x+7-b+j*8, y+i, 0xffffff); //亮
else
lcd_put_pixel(x+7-b+j*8, y+i, 0x000000); //灭
}
}
}
}
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 (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)
{
unsigned char str[] = "中";
wchar_t *chinese_str = L"繁";
FT_Library library;
FT_Face face;
FT_Vector pen;
FT_GlyphSlot slot;
FT_Matrix matrix;
double angle;
int error;
/* 提示信息 */
if (argc != 2) {
printf("Usage : %s <font_file> <angle>\n", argv[0]);
return -1;
}
/* 打开设备:支持读写 */
fd_fb = open("/dev/fb0", O_RDWR);
if (fd_fb < 0) {
printf("can not open /dev/fb0 , err code :%d\n", fd_fb);
return -1;
}
/* 获得可变信息 */
if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)) {
printf("can not get var\n");
return -1;
}
/* 获得固定信息 */
if (ioctl(fd_fb, FBIOGET_FSCREENINFO, &fix)) {
printf("can not get var\n");
return -1;
}
/* 直接映射到内存的Framebuffer */
screen_size = var.xres * var.yres * var.bits_per_pixel / 8; // 屏幕总像素所占的字节数
line_width = var.xres * var.bits_per_pixel / 8; // 每行像素所占的字节数
pixel_width = var.bits_per_pixel / 8; // 每个像素所占字节数
fbmem = (unsigned char *)mmap(NULL, screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);
if (fbmem == (unsigned char *)-1) {
printf("can not mmap\n");
return -1;
}
/* 打开汉字库 */
fd_hzk16 = open("HZK16", O_RDONLY);
if (fd_hzk16 < 0) {
printf("can not open /dev/fb0 , err code :%d\n", fd_hzk16);
return -1;
}
/* 计算字库文件大小 */
if (fstat(fd_hzk16, &hzk_stat)) {
printf("can not get fstat\n");
return -1;
}
/* 汉字库直接映射到内存 */
hzkmem = (unsigned char *)mmap(NULL, hzk_stat.st_size, PROT_READ, MAP_SHARED, fd_hzk16, 0);
if (hzkmem == (unsigned char *)-1) {
printf("can not mmap for hzk16err code :%d\n", fd_hzk16);
return -1;
}
/* 清屏 */
memset(fbmem, 0, screen_size);
/* 屏幕中间显示字母 */
lcd_put_ascii(var.xres/2, var.yres/2, 'A');
/* 屏幕中间显示中文 */
printf("chinese code : %02x %02x\n", str[0], str[1]); //打印编码
lcd_put_chinese(var.xres/2 + 8, var.yres/2, str);
/* 显示矢量字 */
/* 初始化库 */
error = FT_Init_FreeType(&library);
/* 装载字体文件 */
error = FT_New_Face(library, argv[1], 0, &face);
slot = face->glyph;
/* 设置大小:24*24 */
FT_Set_Pixel_Sizes(face, 24, 0);
/* 确定座标:
* lcd_x = var.xres/2 + 8 + 16
* lcd_y = var.yres/2 + 16
* 笛卡尔座标系:
* x = lcd_x = var.xres/2 + 8 + 16
* y = var.yres - lcd_y = var.yres/2 - 16
*/
pen.x = (var.xres/2 + 8 + 16) * 64;
pen.y = (var.yres/2 - 16) * 64;
/* 设置旋转角度 */
angle = (1.0 * strtoul(argv[2], NULL, 0) / 360) * 3.14159 * 2; /* use 25 degrees */
/* 设置矩阵 */
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);
/* 设置转换方法 */
FT_Set_Transform(face, &matrix, &pen);
/* 根据给定的文字信息,加载文字 */
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, slot->bitmap_left, var.yres - slot->bitmap_top);
return 0;
}
2、编译与运行
- 编译:
执行arm-linux-gcc -finput-charset=GBK -fexec-charset=GBK -o show_font Show_font.c -lfreetype -lm
命令
-finput-charset=GBK
:指定输入文件的编码格式为国标码
-fexec-charset=GBK
:指定输出文件的编码格式为国标码
-lfreetype
:指定头文件
-lm
:指定数学库
- 运行
把show_font
可执行文件传到开发板的根文件系统中
执行./show_font ./simsun.ttc 0
,让“繁”字旋转0度显示
执行./show_font ./simsun.ttc 90
,让“繁”字旋转90度显示
执行./show_font ./simsun.ttc 180
,让“繁”字旋转180度显示