1. 获取一个字符的外框
typedef struct FT_BBox_ {
FT_Pos xMin, yMin;
FT_Pos xMax, yMax;
} FT_BBox;
void FT_Glyph_Get_CBox(FT_Glyph glyph, FT_UInt bbox_mode, FT_BBox *acbox);
2. 获取一行文字的左上角坐标
int compute_string_bbox(FT_Face face, wchar_t *wstr, FT_BBox *abbox)
{
int i;
int error;
FT_BBox bbox;
FT_BBox glyph_bbox;
FT_Vector pen;
FT_Glyph glyph;
FT_GlyphSlot slot = face->glyph;
bbox.xMin = bbox.yMin = 32000;
bbox.xMax = bbox.yMax = -32000;
pen.x = 0;
pen.y = 0;
for (i = 0; i < wcslen(wstr); i++)
{
FT_Set_Transform(face, 0, &pen);
error = FT_Load_Char(face, wstr[i], FT_LOAD_RENDER);
if (error)
{
printf("FT_Load_Char error\n");
return -1;
}
error = FT_Get_Glyph(face->glyph, &glyph);
if (error)
{
printf("FT_Get_Glyph error!\n");
return -1;
}
FT_Glyph_Get_CBox(glyph, 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;
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
*abbox = bbox;
}
3. 显示一行文字
int display_string(FT_Face face, wchar_t *wstr, int lcd_x, int lcd_y)
{
int i;
int error;
FT_BBox bbox;
FT_Vector pen;
FT_Matrix matrix;
FT_Glyph glyph;
FT_GlyphSlot slot = face->glyph;
double angle;
int x = lcd_x;
int y = var.yres - lcd_y;
angle = ( 1.0 * 25 / 360 ) * 3.14159 * 2;
compute_string_bbox(face, wstr, &bbox);
pen.x = (x - bbox.xMin) * 64;
pen.y = (y - bbox.yMax) * 64;
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 );
for (i = 0; i < wcslen(wstr); i++)
{
#if 0
FT_Set_Transform(face, 0, &pen);
#else
FT_Set_Transform( face, &matrix, &pen);
#endif
error = FT_Load_Char(face, wstr[i], 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);
pen.x += slot->advance.x;
pen.y += slot->advance.y;
}
return 0;
}