freetype显示英文字符时对齐问题
之前使用freetype显示一串英文字母时,会出现字符无法对齐现象,比如说“abc”像a和c这样的小字母会从开始坐标直接写,导致abc不直接在一条线上很不好看。
后来看到网友的解释比如矢量图
如上图有个基准线的,origin是原点。
通过终端打印出的abc这几个字字体信息
bitmp convert complete
WIDTH:27,row:31,pitch:27,bearingx:3, bearingy:31,advance:34
width:27, height:31
CutOsdBuf into function
copy into temp
osd cut over
osd write char over
bitmp convert complete
WIDTH:26,row:46,pitch:26,bearingx:3, bearingy:46,advance:34
width:26, height:46
CutOsdBuf into function
copy into temp
osd cut over
osd write char over
bitmp convert complete
WIDTH:27,row:31,pitch:27,bearingx:3, bearingy:31,advance:34
width:27, height:31
中间的是b他的bearigny是46 远比a和c要大,所以在屏幕的坐标上写一串字母的时候要考虑这个bearingy.
比如在坐标300,100这个位置开始写,由于屏幕是从左到右从上到下,所以在写每一个字母的时候要减去bearingy. 那么abc的位置减去bearingy得到269,254,266,会发现a和c的位置比较靠下,这就对了。
berror = FT_New_Face( pFTLib,
FONTLIB,
0,
&pFTFace );
printf("FT_New_Face %d *****\n",berror);
if ( ! berror)
{
FT_Set_Char_Size(pFTFace, fontsize << 6 , fontsize << 6 , 300 , 300 );
FT_Glyph glyph;
FT_BBox bbox;
FT_Vector origin;
// load glyph 'C' 67 66 B
for(i =0;i<length;i++)
{
FT_Load_Glyph(pFTFace, FT_Get_Char_Index(pFTFace, text[i]), FT_LOAD_DEFAULT);
berror = FT_Get_Glyph(pFTFace -> glyph, & glyph);
if ( ! berror)
{
FT_Glyph_Get_CBox(glyph, ft_glyph_bbox_gridfit, &bbox);
origin.x = bbox.xMin;
origin.y = bbox.yMin;
// convert glyph to bitmap with 256 gray
FT_Glyph_To_Bitmap( & glyph, ft_render_mode_normal, &origin , 1 );
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
FT_Bitmap & bitmap = bitmap_glyph -> bitmap;
printf("bitmp convert complete \n");
tmp_t.height =bitmap.rows ;
tmp_t.width = bitmap.width;
tmp_t.advance= pFTFace->glyph->metrics.horiAdvance / 64;
memcpy(tmp_t.buf,bitmap.buffer,bitmap.width*bitmap.rows);
printf(" WIDTH:%d,row:%d,pitch:%d,bearingx:%d, bearingy:%d,advance:%d \n", bitmap.width,bitmap.rows,bitmap.pitch,pFTFace->glyph->metrics.horiBearingX / 64\
,pFTFace->glyph->metrics.horiBearingY / 64,pFTFace->glyph->metrics.horiAdvance / 64);
OsdWriteChar(b_x-(pFTFace->glyph->metrics.horiBearingY / 64),b_y, tmp_t.width, tmp_t.height,color,0,tmp_t,addr);
printf("osd write char over \n");
b_y = b_y + tmp_t.advance;
}
#if 0
for ( int i = 0 ; i < w_height; ++ i)
{
for ( int j = 0 ; j < w_width; ++ j)
{
// if it has gray>0 we set show it as 1, o otherwise
printf( " %d " , tmp_t.buf[i * w_width + j] ? 1 : 0 );
}
printf( " \n " );
}
#endif
}
// free glyph
FT_Done_Glyph(glyph);
glyph = NULL;
// free face
FT_Done_Face(pFTFace);
pFTFace = NULL;
// free FreeType Lib
FT_Done_FreeType(pFTLib);
pFTLib = NULL;
#endif
}
编辑器贴代码 真是烂透了