GUI - Font , FreeImage+FreeType生成字体的例子

这个是CEGUI底层生成字体的基本原理,也是研究CEGUI字体优化的起始

使用 Ogre1.7的depends的函数库, 编译时要加 FREEIMAGE_LIB 预编译指令

 

  1. // FontTest.cpp : Defines the entry point for the console application.  
  2. //  
  3.   
  4. //#include "stdafx.h"  
  5.   
  6. //然后,到CMD里执行:Exer01 "simsun.ttc"  
  7.   
  8. /**  
  9. 使用FreeImage写FreeType2字体图片  
  10. */    
  11. #include <iostream>     
  12. #include <string>     
  13.   
  14. #include <FreeImage.h>     
  15. #include <ft2build.h>  
  16. #include FT_FREETYPE_H     
  17. #include FT_GLYPH_H     
  18.      
  19. //----------------------------------------------------------------------------//     
  20. // 初始化函数     
  21. bool Init(const std::string &fontFileName);    
  22. // 写到图片     
  23. bool DrawToImage(wchar_t uchar,int fw,int fh,const std::string &imgFileName);    
  24. // 清理资源     
  25. void DeInit(void);    
  26. // 全局句柄     
  27. FT_Library g_ftLib;    
  28. FT_Face g_ftFace;    
  29.      
  30. //----------------------------------------------------------------------------//     
  31. int main(int argc,char **argv)    
  32. {    
  33.     using namespace std;    
  34.     if ( argc < 2 || !Init(argv[1]) )    
  35.     {    
  36.         cout<<"初始化失败!"<<endl;    
  37.         return -1;    
  38.     }    
  39.     DrawToImage(L'家',48,48,"jia.jpg");    
  40.   
  41.     DeInit();    
  42.     return 0;    
  43. }    
  44.      
  45. //----------------------------------------------------------------------------//     
  46. bool Init(const std::string &fontFileName)    
  47. {    
  48.     // 初始化字体库     
  49.     if ( FT_Init_FreeType(&g_ftLib) ) { return false; }    
  50.     if ( FT_New_Face(g_ftLib,fontFileName.c_str(),0,&g_ftFace) ) { return false; }    
  51.     // FreeImage     
  52.     FreeImage_Initialise();    
  53.     return true;    
  54. }    
  55. //----------------------------------------------------------------------------//     
  56. bool DrawToImage(wchar_t uchar,int fw,int fh,const std::string &fileName)    
  57. {    
  58.     // 设置字体大小     
  59.     FT_Set_Char_Size(g_ftFace,fw<<6,fh<<6,96,96);    
  60.     // 加载并渲染字体GLYPH     
  61.     FT_Load_Glyph(g_ftFace,FT_Get_Char_Index(g_ftFace,uchar),FT_LOAD_DEFAULT);    
  62.     FT_Glyph glyph;    
  63.     FT_Get_Glyph(g_ftFace->glyph,&glyph);    
  64.     FT_Render_Glyph(g_ftFace->glyph,FT_RENDER_MODE_NORMAL);    
  65.     // 拷贝到图片写入文件     
  66.     FT_Bitmap *pBmp = &g_ftFace->glyph->bitmap;    
  67.     int w = pBmp->width;    
  68.     int h = pBmp->rows;    
  69.     FIBITMAP *fib = FreeImage_Allocate(w,h,24);    
  70.     RGBQUAD rgb;    
  71.     for ( int i=0;i<h;++i )    
  72.     {    
  73.         for ( int j=0;j<w;++j )    
  74.         {    
  75.             rgb.rgbRed = pBmp->buffer[i*w+j];    
  76.             rgb.rgbGreen = pBmp->buffer[i*w+j];    
  77.             rgb.rgbBlue = pBmp->buffer[i*w+j];    
  78.             FreeImage_SetPixelColor(fib,j,h-i,&rgb);    
  79.         }    
  80.     }    
  81.     FreeImage_Save(FIF_JPEG,fib,fileName.c_str());    
  82.     FreeImage_Unload(fib);    
  83.     return true;    
  84. }    
  85. //----------------------------------------------------------------------------//     
  86. void DeInit()    
  87. {    
  88.     FT_Done_Face(g_ftFace);    
  89.     FT_Done_FreeType(g_ftLib);    
  90.     FreeImage_DeInitialise();    
  91. }    


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值