这个是CEGUI底层生成字体的基本原理,也是研究CEGUI字体优化的起始
使用 Ogre1.7的depends的函数库, 编译时要加 FREEIMAGE_LIB 预编译指令
- // FontTest.cpp : Defines the entry point for the console application.
- //
- //#include "stdafx.h"
- //然后,到CMD里执行:Exer01 "simsun.ttc"
- /**
- 使用FreeImage写FreeType2字体图片
- */
- #include <iostream>
- #include <string>
- #include <FreeImage.h>
- #include <ft2build.h>
- #include FT_FREETYPE_H
- #include FT_GLYPH_H
- //----------------------------------------------------------------------------//
- // 初始化函数
- bool Init(const std::string &fontFileName);
- // 写到图片
- bool DrawToImage(wchar_t uchar,int fw,int fh,const std::string &imgFileName);
- // 清理资源
- void DeInit(void);
- // 全局句柄
- FT_Library g_ftLib;
- FT_Face g_ftFace;
- //----------------------------------------------------------------------------//
- int main(int argc,char **argv)
- {
- using namespace std;
- if ( argc < 2 || !Init(argv[1]) )
- {
- cout<<"初始化失败!"<<endl;
- return -1;
- }
- DrawToImage(L'家',48,48,"jia.jpg");
- DeInit();
- return 0;
- }
- //----------------------------------------------------------------------------//
- bool Init(const std::string &fontFileName)
- {
- // 初始化字体库
- if ( FT_Init_FreeType(&g_ftLib) ) { return false; }
- if ( FT_New_Face(g_ftLib,fontFileName.c_str(),0,&g_ftFace) ) { return false; }
- // FreeImage
- FreeImage_Initialise();
- return true;
- }
- //----------------------------------------------------------------------------//
- bool DrawToImage(wchar_t uchar,int fw,int fh,const std::string &fileName)
- {
- // 设置字体大小
- FT_Set_Char_Size(g_ftFace,fw<<6,fh<<6,96,96);
- // 加载并渲染字体GLYPH
- FT_Load_Glyph(g_ftFace,FT_Get_Char_Index(g_ftFace,uchar),FT_LOAD_DEFAULT);
- FT_Glyph glyph;
- FT_Get_Glyph(g_ftFace->glyph,&glyph);
- FT_Render_Glyph(g_ftFace->glyph,FT_RENDER_MODE_NORMAL);
- // 拷贝到图片写入文件
- FT_Bitmap *pBmp = &g_ftFace->glyph->bitmap;
- int w = pBmp->width;
- int h = pBmp->rows;
- FIBITMAP *fib = FreeImage_Allocate(w,h,24);
- RGBQUAD rgb;
- for ( int i=0;i<h;++i )
- {
- for ( int j=0;j<w;++j )
- {
- rgb.rgbRed = pBmp->buffer[i*w+j];
- rgb.rgbGreen = pBmp->buffer[i*w+j];
- rgb.rgbBlue = pBmp->buffer[i*w+j];
- FreeImage_SetPixelColor(fib,j,h-i,&rgb);
- }
- }
- FreeImage_Save(FIF_JPEG,fib,fileName.c_str());
- FreeImage_Unload(fib);
- return true;
- }
- //----------------------------------------------------------------------------//
- void DeInit()
- {
- FT_Done_Face(g_ftFace);
- FT_Done_FreeType(g_ftLib);
- FreeImage_DeInitialise();
- }