Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2644636
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-01-23 14:57:22

#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);
}
 
阅读(6226) | 评论(5) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-09-01 13:49:05

光头小蝌蚪

chinaunix网友2009-04-19 01:54:54

我是tgl10

chinaunix网友2009-04-19 01:54:06

看了你保存文件的代码,打算试试。多谢~ 字体变斜是由于代码用了转换矩阵,把那段注释掉就可以了

chinaunix网友2009-01-23 22:42:00

问题:http://topic.csdn.net/u/20070614/21/be771eaf-ceef-4557-9f24-304abf2947bf.html

chinaunix网友2009-01-23 20:50:07

FreeType 2开发文档: http://www.unixresources.net/linux/clf/kylix/archive/00/00/59/21/592188.html API:http://freetype.sourceforge.net/freetype2/docs/reference/ft2-base_interface.html