freetype_4_使用freetype显示多行文字
从左往右显示
1、先描绘一行文字
2、取出该行文字的最大高度,以此为基准算出下一行的位置
3、再描绘另一行文字
根据上一篇文章中代码修改主函数部分
int main(int argv, char **argc)
{
FT_Library library;
FT_Face face;
FT_Vector pen;
wchar_t *str[] = {L"第一行",L"第二行"};
FT_GlyphSlot slot;
int err;
int n,l,lines = 2;
FT_BBox bbox;
FT_Glyph glyph;
int y_min = 32*64,y_max = 0;
if(argv != 2)
{
printf("Usage : %s <font_file>\r\n",argc[0]);
return -1;
}
fd_fb = open("/dev/fb0",O_RDWR);
if(fd_fb < 0)
{
printf("can't open /dev/fb0\r\n");
return -1;
}
if(ioctl(fd_fb,FBIOGET_VSCREENINFO,&var))
{
printf("can't get var\r\n");
return -1;
}
if(ioctl(fd_fb,FBIOGET_FSCREENINFO,&fix))
{
printf("can't get fix\r\n");
return -1;
}
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\r\n");
return -1;
}
printf("xres:%d , yres:%d\r\n",var.xres,var.yres);
pixel_width = var.bits_per_pixel / 8;
line_width = var.xres * pixel_width;
memset(fbmem,0x0,screen_size);
err = FT_Init_FreeType(&library);
err = FT_New_Face(library, argc[1], 0, &face );
FT_Set_Pixel_Sizes(face,32,0);
slot = face->glyph;
pen.x = 0 * 64;
pen.y = ( var.yres - 32 ) * 64;
for(l = 0;l < lines;l++)
{
for(n = 0;n < wcslen(str[l]);n++)
{
FT_Set_Transform(face,0, &pen);
err = FT_Load_Char(face, str[l][n],FT_LOAD_RENDER);
if(err)
{
printf("FT load char err\r\n");
return -1;
}
FT_Get_Glyph(slot,&glyph);
if(err)
{
printf("FT get glyph err\r\n");
return -1;
}
FT_Glyph_Get_CBox(glyph,FT_GLYPH_BBOX_TRUNCATE,&bbox);
if(bbox.yMin < y_min)
y_min = bbox.yMin;
if(bbox.yMax > y_max)
y_max = bbox.yMax;
draw_bitmap(&slot->bitmap,slot->bitmap_left,var.yres - slot->bitmap_top);
pen.x += slot->advance.x;
}
/* 显示下一行 */
pen.x = 0 * 64;
pen.y -= ( y_max - y_min ) * 64;
}
return 0;
}
居中显示
1、先算出欲描绘文字串的长度,得到起点
2、从起点开始描绘一行文字
3、取出该行文字的最大高度,以此为基准描绘下一行
注意这里以笛卡尔坐标(0,0)为起点获取位图,描绘时再用pen根据偏移算出实际地址
定义宏
#define MAX_GLYPHS 100
声明结构体
typedef struct TGlyph_
{
FT_UInt index; /* glyph index */
FT_Vector pos; /* glyph origin on the baseline */
FT_Glyph image; /* glyph image */
} TGlyph, *PGlyph;
根据上面代码修改
int main( int argv, char **argc )
{
FT_Library library;
FT_Face face;
FT_Vector pen;
wchar_t *str[] = {L"第一行",L"第二行"};
int err;
int n,l,lines = 2;
FT_BBox bbox;
TGlyph glyphs[MAX_GLYPHS]; /* glyphs table */
PGlyph glyph; /* current glyph in table */
FT_UInt num_glyphs;
int line_box_width,line_box_height;
if(argv != 2)
{
printf("Usage : %s <font_file>\r\n",argc[0]);
return -1;
}
fd_fb = open("/dev/fb0",O_RDWR);
if(fd_fb < 0)
{
printf("can't open /dev/fb0\r\n");
return -1;
}
if(ioctl(fd_fb,FBIOGET_VSCREENINFO,&var))
{
printf("can't get var\r\n");
return -1;
}
if(ioctl(fd_fb,FBIOGET_FSCREENINFO,&fix))
{
printf("can't get fix\r\n");
return -1;
}
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\r\n");
return -1;
}
printf("xres:%d , yres:%d\r\n",var.xres,var.yres);
pixel_width = var.bits_per_pixel / 8;
line_width = var.xres * pixel_width;
memset(fbmem,0x0,screen_size);
err = FT_Init_FreeType(&library);
err = FT_New_Face(library, argc[1], 0, &face );
FT_Set_Pixel_Sizes(face,32,0);
slot = face->glyph;
pen.x = 0;
pen.y = ( var.yres / 2 + 32 ) * 64;
for(l = 0;l < lines;l++)
{
num_glyphs = Get_Glyph(face,str[l],glyphs);
Compute_Str_BBox(glyphs,num_glyphs,&bbox);
line_box_width = bbox.xMax - bbox.xMin;
line_box_height = bbox.yMax - bbox.yMin;
pen.x = ( var.xres - line_box_width ) / 2 *64;
pen.y -= line_box_height * 64;
Draw_Glyphs(glyphs,num_glyphs,pen);
}
return 0;
}
定义一个函数获取glyph
int Get_Glyph(FT_Face face,wchar_t *str,TGlyph glyphs[])
{
int i;
PGlyph glyph = glyphs;
FT_Vector pen;
int error;
pen.x = pen.y = 0;
for(i = 0;i < wcslen(str);i++)
{
glyph->index = FT_Get_Char_Index(face,str[i]);
glyph->pos.x = pen.x;
glyph->pos.y = pen.y;
error = FT_Load_Glyph( face, glyph->index, FT_LOAD_DEFAULT );
if ( error ) continue;
error = FT_Get_Glyph( face->glyph, &glyph->image );
if ( error ) continue;
FT_Glyph_Transform( glyph->image, 0, &glyph->pos );
pen.x += face->glyph->advance.x ;
glyph++;
}
return (glyph - glyphs);
}
定义一个函数计算BBOX参数
void Compute_Str_BBox( TGlyph glyphs[], FT_UInt num_glyphs, FT_BBox *abbox )
{
FT_BBox bbox;
int i;
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
for(i = 0;i < num_glyphs;i++)
{
FT_BBox glyph_bbox;
FT_Glyph_Get_CBox( glyphs[i].image, FT_GLYPH_BBOX_TRUNCATE, &glyph_bbox );
if (glyph_bbox.xMin < bbox.xMin)
bbox.xMin = glyph_bbox.xMin;
if (glyph_bbox.yMin < bbox.yMin)
bbox.yMin = glyph_bbox.yMin;
if (glyph_bbox.xMax > bbox.xMax)
bbox.xMax = glyph_bbox.xMax;
if (glyph_bbox.yMax > bbox.yMax)
bbox.yMax = glyph_bbox.yMax;
}
*abbox = bbox;
}
定义一个函数描绘一行文字
void Draw_Glyphs( TGlyph glyphs[], FT_UInt num_glyphs, FT_Vector pen )
{
int i;
FT_BBox bbox;
int error;
for(i = 0;i < num_glyphs;i++)
{
FT_Glyph_Transform( glyphs[i].image, 0, &pen );
error = FT_Glyph_To_Bitmap( &glyphs[i].image, FT_RENDER_MODE_NORMAL, 0, 1 );
if(error) continue;
FT_BitmapGlyph bit = (FT_BitmapGlyph) glyphs[i].image;
draw_bitmap( &bit->bitmap, bit->left, var.yres - bit->top );
//pen.x += glyphs[i].image.advance.x;
//pen.y += glyphs[i].image.advance.y;
FT_Done_Glyph( glyphs[i].image );
}
}