from:https://blog.csdn.net/xiaodingqq/article/details/84930589
1、分析设置字体大小函数 FT_Set_Char_Size():
error = FT_Set_Char_Size(
face, /* handle to face object */
char width, /* char_width in 1/64th of points */
char height, /* char_height in 1/64th of points */
horz_resol, /* horizontal device resolution */
ver_resol ); /* vertical device resolution */
1)单位
char width和char height 的单位是1/64point(point为物理单位,1/72英寸)
分辨率的单位是dpi——dots per inch(每一英寸有多少个像素)
例如:
FT_Set_Char_Size(face, 50 * 64, 0, 100, 0);
char_height = 0表示char_height等于char_width为50*64.
ver_resol = 0 同理.
pixels(像素) = 50 * 64 * (1/64) * (1/72) * 100 = 69
error = FT_Set_Pixel_Sizes(
face, /* handle to face object */
0, /* pixel_width 水平方向有69个像素 */
69 ); /* pixel_height 竖直方向有69个像素 */
2)坐标
LCD的坐标原点在左上角
字体文件和字体函数的坐标远点在左下角(笛卡尔坐标)
LCD坐标转换为笛卡尔坐标:
2、显示英文
1)安装freetype库:
freetype-2.4.10.tar.bz2位于
\源码_图片_文档\数码相框全部源码_图片_文档\第1个项目数码相框全部源码_图片_文档\源码(含讲课过程中即时编写的文档)\03.freetype字符显示_源码_文档_图片\03.freetype
tar -xjf freetype-2.4.10.tar.bz2
mv freetype-2.4.10 freetype-2.4.10_pc
cd freetype-2.4.10_pc/
./configure
//配置
make
//编译
sudo make install
//直接将库安装到根目录/usr/local/里
查看freetype库:ls /usr/local/lib
2)修改example1.c:
example1.c位于freetype-doc-2.4.10.tar.bz2\freetype-2.4.10\docs\tutorial下
把打印范围缩小:
#define WIDTH 80
#define HEIGHT 80
在(0,40) 显示文字:
pen.x = 0 * 64; //64个像素
pen.y = ( target_height - 40 ) * 64;
修改字体大小:
error = FT_Set_Pixel_Sizes( face, 24, 0 ); //24*24像素
把旋转度数改为0:
FT_Set_Transform( face, 0, &pen );
3)编译运行:
flash@DESKTOP-2C424SM:/mnt/d/work$ gcc -o example example1.c ‘
4)编译出错,修改命令:
指定头文件和库:
gcc -o example1 example1.c -I /usr/local/include/freetype2/
example1.c:(.text+0x31c): undefined reference to `FT_Init_FreeType'
example1.c:(.text+0x34c): undefined reference to `FT_New_Face'
example1.c:(.text+0x368): undefined reference to `FT_Set_Pixel_Sizes'
example1.c:(.text+0x4f4): undefined reference to `FT_Set_Transform'
example1.c:(.text+0x524): undefined reference to `FT_Load_Char'
example1.c:(.text+0x5b8): undefined reference to `FT_Done_Face'
example1.c:(.text+0x5c4): undefined reference to `FT_Done_FreeType'
gcc -o example1 example1.c -I /usr/local/include/freetype2/ -freetype
指定数学库:
gcc -o example1 example1.c -I /usr/local/include/freetype2/ -lfreetype -lm
5)运行example1:
./example1 simsun.ttc agf
3、显示中文+英文
int chinese_str[] = {0x97e6, 0x4e1c, 0x5c71, 0x0067};
但是要字母和汉字一起显示的话,就要修改数据的存储类型,
char *str = “韦东山g”
对于汉字2个字节,对于英文1个字节,需要分辨汉字和英文,wchar_t刚好可以放一个unicode字符。
1)Linux 宽字符的引入:
注意:wchar_t在windows占 2byte,在linux占 4bytes.
宽字符: wchar_t
头文件: #include<wchar.h>
通过wcslen()判断wchar_t数组大小
2)修改example1.c:
...
#include<wchar.h> //添加此行
...
int main( int argc,char** argv )
{
... ...
wchar_t *chinese_str=L"韦东山g"; //添加此行
... ...
for ( n = 0; n <wcslen(chinese_str); n++ ) //修改此行
{
FT_Set_Transform( face, 0, &pen ); //字体转换
/* load glyph image into the slot (erase previous one) */
error = FT_Load_Char( face, chinese_str[n], FT_LOAD_RENDER ); //修改此行
... ...
}
return 0;
}
2)运行example1.c:
通过另存为文件,查看文件编码
gcc -o example1 example1.c -I /usr/local/include/freetype2/
-lfreetype -lm -finput-charset=GBK -fexec-charset=utf-8
-finput-charset:告诉编译器,文件里的字符是GBK格式
-fexec-charset:告诉编译器,需要先将里面的内容转换为utf-8格式后,再来编译
4、字符的坐标
在终端显示行号:
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' );
}
}
运行结果:
原点为(0,40),但是文字会超过原点:
分析:
advance.x(字体是水平的)
advance.x advance.y(字体是旋转的)
advance: 位于face->glyph-> advance,用来存放每个文字之间的间隔信息,每当加载一个新的图像时,系统便会更新该数据.
5、位图文字的信息
当我们每次将新的字形图像(face->glyph)转为位图后,而存放的前一个字形图像就会被删除.
我们想提取字形图像的坐标,该怎么做?
1)添加头文件
#include FT_GLYPH_H
2)通过FT_Get_Glyph()将一个字形图像(face->glyph)存到FT_Glyph类型的变量里
FT_Glyph glyph; /* a handle to the glyph image */
...
error = FT_Load_Glyph( face, glyph_index, FT_LOAD_NORMAL );
//通过字符编码,获取字形图像存到face->glyph里,并转为位图存到face->glyph->bitmap->buffer[]里
if ( error ) { ... }
error = FT_Get_Glyph( face->glyph, &glyph ); //将字形图像(face->glyph)存到glyph里
if ( error ) { ... }
3) 通过FT_Glyph_Get_CBox()获取文字的xMin, xMax, yMin, yMax坐标信息
FT_Glyph_Get_CBox( FT_Glyph glyph, //该值通过FT_Get_Glyph()来获取
FT_UInt bbox_mode, //模式,填入FT_GLYPH_BBOX_TRUNCATE即可
FT_BBox *acbox ); //用来存放获取到的xMin, xMax, yMin, yMax信息
其中FT_GLYPH_BBOX_TRUNCATE表示:获取的坐标信息是像素坐标,而不是点坐标