FreeImage+FreeType生成字体的例子

这个是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();  
}  


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值