所学来自百问网
目录
1.简介
Freetype 是开源的字体引擎库,它提供统一的接口来访问多种字体格式文件, 从而实现矢量字体显示
2. 程序运行基础知识
2.1 编译程序时去哪找头文件?
系统目录:就是交叉编译工具链里的某个include目录;也可以自己指定:;
编译时用 “ -I dir ”选项指定。
2.2 链接时去哪找库文件?
系统目录:就是交叉编译工具链里的某个lib目录;也可以自己指定;
链接时用 “ -L dir ”选项指定。
2.3 运行时去哪找库文件?
系统目录:就是板子上的/lib、/usr/lib目录;也可以自己指定;
运行程序用环境变量LD_LIBRARY_PATH指定。
3. 使用freetype库的步骤
1.初始化:FT_InitFreetype
2.加载(打开)字体Face:FT_New_Face
3.设置字体大小:FT_Set_Char_Sizes 或 FT_Set_Pixel_Sizes
4.选择charmap: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.最后显示出来。
上面的5 6 7可以使用一个函数代替:FT_Load_Char(face, charcode, FT_LOAD_RENDER),它就可以得到位图。
4. freetype的重要数据结构
4.1 FT_Library
对应freetype库,使用freetype之前要先调用以下代码:
FT_Library library; /* 对应 freetype 库 */
error = FT_Init_FreeType( &library ); /* 初始化freetype库 */
4.2 FT_Face
它对应一个矢量字体文件,在源码中使用 FT_New_Face 函数打开字体文件 后,就可以得到一个face。
error = FT_New_Face(library, font_file, 0, &face ); /* 加载字体文件 */
4.3 FT_GlyphSlot
用来保存字符的处理结果:比如转换后的glyph、位图,如图 6.33:
一个face 中有很多字符,生成一个字符的点阵位图时,位图保存在插槽中:face->glyph。
生成第1个字符位图时,它保存在face->glyph中;生成第2个字符位图时,也会保存在face->glyph中,会覆盖第1个字符的位图。
FT_GlyphSlot slot = face->glyph; /* 插槽: 字体的处理结果保存在这里 */
4.4 FT_Glyph
字体文件中保存有字符的原始关键点信息,使用freetype的函数可以放大、 缩小、旋转,这些新的关键点保存在插槽中(注意:位图也是保存在插槽中)。
新的关键点使用FT_Glyph来表示,可以使用这样的代码从slot中获得 glyph:
error = FT_Get_Glyph(slot,&glyph);
4.5 FT_BBox
FT_BBox结构体定义如下,它表示一个字符的外框,即新glyph的外框:
可以使用以下代码从glyph中获得这些信息:
FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_TRUNCATE, &bbox );
4.6 位图
5. 示例代码(显示“繁”)
#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
int fd_fb;
struct fb_var_screeninfo var; /* Current var */
int screen_size;
unsigned char *fbmem;
unsigned int line_width;
unsigned int pixel_width;
/**********************************************************************
* 函数名称: lcd_put_pixel
* 功能描述: 在LCD指定位置上输出指定颜色(描点)
* 输入参数: x坐标,y坐标,颜色
* 输出参数: 无
* 返 回 值: 会
***********************************************************************/
void lcd_put_pixel(int x, int y, unsigned int color)
{
unsigned char *pen_8 = fbmem+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)
{
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"繁";
FT_Library library;
FT_Face face;
int error;
FT_Vector pen;
FT_GlyphSlot slot;
int font_size = 24;
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);
fd_fb = open("/dev/fb0", O_RDWR);
if (fd_fb < 0)
{
printf("can't open /dev/fb0\n");
return -1;
}
if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var))
{
printf("can't get var\n");
return -1;
}
line_width = var.xres * var.bits_per_pixel / 8;
pixel_width = var.bits_per_pixel / 8;
screen_size = var.xres * var.yres * 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't mmap\n");
return -1;
}
/* 清屏: 全部设为黑色 */
memset(fbmem, 0, screen_size);
/* 显示矢量字体 */
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, font_size, 0);
/* 确定座标:
*/
//pen.x = 0;
//pen.y = 0;
/* 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,
var.xres/2,
var.yres/2);
return 0;
}