数码相框(一)--LCD上显示矢量字体
https://blog.csdn.net/shtianhai/article/details/5652606
https://blog.csdn.net/shtianhai/article/details/5652617
点阵字体:字体比较简单,但是它有一个缺点就是,一旦选定了字库,每个字符的大小就确定了,无法更改。
矢量字体:弥补点阵字体无法更改字体大小的缺点。
矢量字体文件:
①若干闭合曲线的关键点
②使用数学曲线连接关键点
③填充内部空间
freetppe库:
charmaps |A|…|中|…|国|….
charmaps表示支持哪种编码,使用字库文件(如 simsun.ttc)时根据charmaps找到字的glyph(关键点)。
显示过程:
(1)在字体文件中保存着对应着不同编码类型的文字,当然其保存的方法就是保存关键点以及相对位置
(2)根据文字的编码值从字体文件中找到对应的文字,即glyph,当然找到的是其关键点和相对位置
(3)设置字体大小
(4)用某些函数把glyph里的点缩放为设置的字体大小
(5)转换为位图点阵
(6)在LCD上显示出来
完整的源代码
http://freetype.sourceforge.net/freetype2/docs/tutorial/example1.c
矢量字体实现过程:
(1)初始化库:FT_Init_FreeType
(2)加载字体face:
FT_New_Face:从文件中加载字体
FT_New_Memory_Face:从内存中加载字体
(3)设置大小
FT_Set_Char_Size
FT_Set_Pixel_Sizes
(4)根据字符的编码值加载glyph
a:FT_Select_CharMap( face, FT_ENCODING_BIG5 );
选择编码方式
b:FT_Get_Char_Index( face, charcode )
把字符编码转换为索引值,根据该索引就可以找到glyph
c:FT_Load_Glyph( face,glyph_index, load_flags );
从face中取出glyph
d:FT_Render_Glyph( face->glyph, render_mode );
将glyph转换为位图
(5)变换:移动、旋转
FT_Set_Transform
在串口打印一个矢量字体:
更换交叉编译工具链版本
echo $PATH
当前环境变量:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/work/tools/gcc-3.4.5-glibc-2.3.6/bin
参考:
https://blog.csdn.net/hello__xs/article/details/79547736
更换,环境修改为:
PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/arm/4.3.2/bin”
交叉编译 freetype:
tar xjf freetype-2.4.10.tar.bz2
./configure --host=arm-linux //编译结果在ARM开发板的Linux系统使用
make
make DESTDIR=$PWD/tmp install //安装到了当前目录的tmp目录
- 1
- 2
- 3
- 4
编译好的库文件和头文件放入环境变量:
①
把tmp/usr/local/lib/* 复制到 /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib
sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib -d -rf //-d 表示链接文件 -rf 全部的文件
cp *so* /work/nfs_root/fs_mini_mdev_new/lib -d //库拷贝到文件系统
- 1
- 2
- 3
- 4
②
sudo cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include -rf
cd /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include
mv freetype2/freetype . //移动freetype2/freetype到当前目录
- 1
- 2
- 3
编译文件:example1.c
arm-linux-gcc -finput-charset=GBK -o example1 example1.c -lfreetype -lm
cp example1 /work/nfs_root/fs_mini_mdev_new //拷贝文件到文件件系统
cp simsun.ttc /work/nfs_root/fs_mini_mdev_new //拷贝字库到文件系统
- 1
- 2
- 3
开发板设置:
set bootargs root=/dev/nfs nfsroot=192.168.0.102:/work/nfs_root/fs_mini_mdev_new ip=192.168.0.7:192.168.0.102:192.168.0.1:255.255.255.0::eth0:off init=/linuxrc console=ttySAC0,115200
set bootcmd 'nfs 32000000 192.168.0.102:/work/nfs_root/uImage; bootm 32000000'
- 1
- 2
- 3
串口打印:
./example1 ./simsun.ttc
在LCD上显示矢量字体
在 show_font.c基础上修改,参考example1.c
主函数添加:
wchar_t *chinese_str = L"繁";
FT_Library library;
FT_Face face;
int error;
FT_Vector pen;
FT_GlyphSlot slot;
/*用法*/
if (argc != 2)
{
printf("Usage : %s <font_file>\n", argv[0]);
return -1;
}
/* 显示矢量字体 */
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 */
slot = face->glyph;
/* 设置字体大小 */
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;
/* set transformation */
FT_Set_Transform( face, 0, &pen);
/* load glyph image into the slot (erase previous one) */
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);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
./show_font ./simsun.ttc
显示结果:
旋转角度:
主函数修改添加
T_Matrix matrix; /* transformation matrix */
double angle;
if (argc != 3)
{
printf("Usage : %s <font_file> <angle>\n", argv[0]);
return -1;
}
angle = ( 1.0 * strtoul(argv[2], NULL, 0) / 360 ) * 3.14159 * 2; /* use 25 degrees */
/* 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 );
* set transformation */
FT_Set_Transform( face, &matrix, &pen);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
完整代码show_font.c
编译:arm-linux-gcc -finput-charset=GBK -fexec-charset=GBK -o show_font show_font.c -lfreetype -lm
执行: ./show_font ./simsun.ttc 90