版本freetype2.4.8
./configure --prefix=/root/freetype/install CC=arm-linux-gcc --host=arm-linux
make
make install
编译程序的时候加入连接库选项CFLAGS="-I/usr/local/arm/4.3.2/freetype/include/ -I/usr/local/arm/4.3.2/freetype/include/freetype2 -lfreetype -lz -lm"
并把库文件加入到文件系统中
开始编译的时候只能显示英文字符,不能显示汉字,
测试了好多次,找了好多资料发现,gedit创建的文本编码时UTF-8的编码,而freetype没有这种编码的解码方式,
字体库也没有UTF-8的,
所有在网上下了个GB2312的字体库,
并且使用gb2312的创建文档的时候选择GB2312的编码,然后测试成功。
现在问题是字体大小和颜色。
测试程序
/* 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 <fcntl.h>
#include <unistd.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <ft2build.h>
#include FT_FREETYPE_H
int xoffset=0;
int yoffset=100;
void show_image( FT_Bitmap* bitmap)
{
int fbfd = 0;
int i=0,j=0;
unsigned short t;
struct fb_var_screeninfo vinfo;//屏幕信息
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
long int location = 0;
fbfd = open("/dev/fb0", O_RDWR);//打开字符设备
if (!fbfd)
{
printf("Error: cannot open framebuffer device.\n");
exit(1);
}
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
printf("Error reading fixed information.\n");
exit(2);
}
//FBIOGET_VSCREENINFO 获得可变的屏幕参数
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
printf("Error reading variable information.\n");
exit(3);
}
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;//计算屏幕大小
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,fbfd, 0);//分配内存映射同文件一样操作,返回初始地址
if ((int)fbp == -1) {
printf("Error: failed to map framebuffer device to memory.\n");
exit(4);
}
for (y =yoffset; y < bitmap->rows+yoffset&&y<vinfo.yres; y++)
{
for (x = xoffset; x < vinfo.xres&&x<xoffset+bitmap->width; x++)
{
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +(y+vinfo.yoffset) * finfo.line_length;
t=(short)(bitmap->buffer[i+j*bitmap->width]);
if(t!=0)
{
*((unsigned short *)(fbp + location)) =t;
}
i++;
}
j++;
i=0;
}
xoffset+=bitmap->width;
munmap(fbp, screensize);//删除特定地址区域的对象映射 int munmap(void *start, size_t length);
close(fbfd);
}
int main( int argc, char** argv )
{
FT_Library library;//字体库句柄
FT_Face face;//字体句柄
FT_GlyphSlot slot;//字形槽
FT_Matrix matrix;//转换矩阵
FT_Vector pen; /* untransformed origin */
FT_Error error;
FT_UInt glyph_index ;
char* filename;
char* text;
double angle;//角度
int target_height;//目标高度
int n, num_chars;
int cur_unicode;
filename = argv[1]; /* first argument */
text = "王"; /* second argument */
//text = "xiaohe"; /* second argument */
num_chars = strlen( text );//文本字符长度
angle =0;
target_height = 64;//高度64
error = FT_Init_FreeType( &library );//库时话库
error = FT_New_Face( library, argv[1], 0, &face );//创建字体对象,
/* use 50pt at 100dpi */
error = FT_Set_Char_Size( face, 0, 100*96,96,96);//设置字符大小
slot = face->glyph;//获取字形槽
//set up matrix FT_Matrix位于fttypes.h { FT_Fixed xx, xy;//long FT_Fixed yx, yy;}
matrix.xx = (FT_Fixed)( cos( angle ) * 0x10000L );//cos(0)=1
matrix.xy = (FT_Fixed)(-sin( angle ) * 0x10000L );//sin(0)=0
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;
pen.y = target_height *10;
FT_Select_Charmap(face,FT_ENCODING_GB2312);
int i=0;
for(n=0;n<num_chars;)
{
cur_unicode='王';
glyph_index = FT_Get_Char_Index( face,cur_unicode );
printf("%s %x %x %d %d\n",face->family_name,cur_unicode,glyph_index,(int)face->num_glyphs,i);
if(glyph_index ==0)
{
i++;
continue;
}
n++;
//FT_Set_Transform( face, &matrix, &pen );
FT_Set_Transform( face, &matrix, &pen );
FT_Load_Glyph(face,glyph_index, FT_LOAD_DEFAULT );
error = FT_Render_Glyph( face->glyph,FT_RENDER_MODE_NORMAL ); //转换为位图图像
show_image(&face->glyph->bitmap);
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
printf("%d %d\n",slot->bitmap.width,&slot->bitmap.rows);
FT_Done_Face ( face );
FT_Done_FreeType( library );
return 0;
}
/* EOF */