#include "stdafx.h"
#include
#include
#include
#include
#include
#include FT_FREETYPE_H
#include
#include
#pragma comment(lib , "lib/freetype2110.lib")
int bmp_write(unsigned char *image, int xsize, int ysize, char *filename)
{
unsigned char header[54] =
{
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
long file_size = (long)xsize * (long)ysize * 3 + 54;
header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
long width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
long height = -ysize; //数据倒着填充的
header[22] = height &0x000000ff;
header[23] = (height >> 24) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 8) &0x000000ff;
char fname_bmp[128];
sprintf(fname_bmp, "%s.bmp", filename);
FILE *fp;
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)xsize * ysize * 3, fp);
fclose(fp);
return 0;
}
//以下为FT2_Obj的代码.
//主要参考了Nehe的Lesson 43
class FT2_Obj
{
FT_Library library;
int h ;
FT_Face face;
public:
void Init(const char * fname, unsigned int h);
void Free();
void DrawAUnicode(wchar_t ch);
};
void FT2_Obj::Init(const char * fname, unsigned int h)
{
this->h=h;
//初始化FreeType库..
if (FT_Init_FreeType( &library ))
throw std::runtime_error("FT_Init_FreeType failed");
//加载一个字体,取默认的Face,一般为Regualer
if (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_Set_Pixel_Sizes(face, 0, 64);
FT_Matrix matrix; /* transformation matrix */
FT_UInt glyph_index;
FT_Vector pen;
//给它设置个旋转矩阵
float angle = 0/360* 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 );
}
int main()
{
FT2_Obj font;
// 改用其它字体,生成的字会变斜,不知为什么,中文也会变斜
font.Init("c:\\windows\\fonts\\arial.ttf",24);
//font.Init("c:\\windows\\fonts\\simhei.ttf",24);
//wchar_t pText[]=L"米";
//wchar_t pText[]=L"\x4E2D";
wchar_t pText[]=L"A";
for(int n = 0 ; n< wcslen(pText);n++)
{
font.DrawAUnicode(pText[n]);
}
font.Free();
return 1;
}
/*
绘制操作.
*/
void FT2_Obj::DrawAUnicode(wchar_t ch)
{
if(FT_Load_Glyph( face, FT_Get_Char_Index( face, ch ), FT_LOAD_DEFAULT ))
throw std::runtime_error("FT_Load_Glyph failed");
//得到字模
FT_Glyph glyph;
if(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;
//把位图数据拷贝自己定义的数据区里.这样旧可以画到需要的东西上面了。
int width = bitmap.width;
int height = bitmap.rows;
// 4字节对齐
int pitch = bitmap.pitch + 4 -1;
pitch -= pitch%4;
unsigned char* expanded_data = new unsigned char[ 3 * pitch * height];
memset(expanded_data, 0x00, 3 * pitch * height);
for(int j=0; j < height ; j++)
{
for(int i=0; i < width; i++)
{
//int n = 3*(i+(height-j-1)*width);
/*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] = bitmap.buffer[i + bitmap.width*j]; */
//(i>=bitmap.width || j>=bitmap.rows) ? 0 : bitmap.buffer[i + bitmap.width*j];
int n = *bitmap.buffer++;
if (n>0)
{
int nPos = j*pitch*3 + 3*i;
expanded_data[ nPos ] =
expanded_data[nPos+1] =
expanded_data[nPos+2] = 0xFF;//bitmap.buffer[i + bitmap.width*j];
}
}
}
bmp_write(expanded_data, width, height, "E:/22");
delete []expanded_data;
}
void FT2_Obj::Free()
{
FT_Done_Face(face);
FT_Done_FreeType(library);
}