FreeType2的简单使用:字体绘制技术



FreeType2简单使用:
FreeType2是个简单跨平台字体绘制引擎.目前支持TrueType Type1 Type2等字体格式.不过目前好象还不支持OpenType.
使用FreeType应用很多.著名FTGL就是使用FreeType.能在OpenGL高效率绘制矢量字体.
FTGL我没用过.不想在没了解该如何用FreeType情况下就去用FTGL.

经过个晚上阅读代码(我代码阅读能力是很差).终于知道了如何使用FreeType2了不过是简单使用还不知道如何设置Bold Itainly等属性.主要是简单演示.以后准备做成个完善字体引擎.
下面简单介绍下.

首先当然是包含头文件了头文件要这样包含:
# [ft2build.h]
# FT_FREETYPE_H
不知道为什么.反正就是要这么包含.
以下为FT2化代码.和绘制以及释放代码>
注意这里绘制代码接受是Unicode.表示你这样旧可以绘制了

FT2_Obj font;
font.Init(\"SimSun.ttf\",32);
wchat_t pText=L\"潘李亮是头野猪\";
for( n = 0 ; n< wcslen(pText);n)
{
font.DrawAUnicode(pText[n];
}
font.Free;



//以下为FT2_Obj代码.
//主要参考了NeheLesson 43
FT2_Obj
{
FT_Library library;
h ;
FT_Face face;

public:
void Init(const char * fname, unsigned h);
void Free;
void DrawAUnicode(wchar_t ch)
};
void FT2_Obj::Init(const char * fname, unsigned h)
{
this->h=h;

//化FreeType库..

(FT_Init_FreeType( &library ))
throw std::runtime_error(\"FT_Init_FreeType failed\");




//加载个字体,取默认Face,般为Regualer
(FT_New_Face( library, fname, 0, &face ))
throw std::runtime_error(\"FT_New_Face failed (there is probably a problem with your font file)\");

//大小要乘64.这是规定照做就可以了
FT_Set_Char_Size( face,h<< 6, h << 6, 96, 96);
FT_Matrix matrix; /* transformation matrix */
FT_UInt glyph_index;
FT_Vector pen;

//给它设置个旋转矩阵
float angle = -20/180.* 3.14;
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 );


FT_Set_Transform( face, &matrix, &pen );
}.

void FT2_Obj::DrawAUnicode(wchar_t ch)
{
(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
throw std::runtime_error(\"FT_Load_Glyph failed\");

//得到字模
FT_Glyph glyph;
(FT_Get_Glyph( face->glyph, &glyph ))
throw std::runtime_error(\"FT_Get_Glyph failed\");

//转化成位图
FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL );
FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;

//取道位图数据
FT_Bitmap& bitmap=bitmap_glyph->bitmap;

//把位图数据拷贝自己定义数据区里.这样旧可以画到需要东西上面了
width = bitmap.width;
height = bitmap.rows;

usigned char* expanded_data = usigned char[ 3 * width * height];

for( j=0; j )
{
for( i=0; i < width; i)
{
expanded_data[3*(i+(height-j-1)*width)]=
expanded_data[3*(i+(height-j-1)*width)+1] =
expanded_data[3*(i+(height-j-1)*width)+2] =
(i>=bitmap.width || j>=bitmap.rows) ?
0 : bitmap.buffer[i + bitmap.width*j];

}
}

/*
绘制操作.
*/
}

void FT2_Obj::Free
{
FT_Done_Face(face);
FT_Done_FreeType(library);
}


附件: sf_200471131039.bmp (19014s)
Tags: 

延伸阅读

最新评论

发表评论