CImg应用-输出中文字符(CentOS 6.5)
2015-01-30 09:11
211 查看
编程工具:Eclipse(C/C++)
CImg版本:1.6.0
CImg本身不支持中文,本文通过FreeType和TTF字体文件实现中文字体输出,其它字体的输出也可借鉴此方式实现。
#include <iostream>
using namespace std;
#define cimg_OS 0
#include </home/hqq/公共的/CImg-1.6.0_rolling150120/CImg.h>
using namespace cimg_library;
#include <ft2build.h>
#include FT_FREETYPE_H
void initFreetype(
FT_Library& ftlib,
FT_Face& face,
const std::string& fontFullName
){
FT_Error fterr;
if((fterr = FT_Init_FreeType( &ftlib ))) {
throw "Error init freetype";
}
if((fterr = FT_New_Face(ftlib, fontFullName.c_str(), 0, &face))){
if(fterr == FT_Err_Unknown_File_Format) {
throw "Error feetype, Unsupported font";
} else {
throw "Error feetype, new face";
}
}
}
void closeFreetype(
FT_Library& ftlib,
FT_Face& face
){
FT_Done_Face(face);
FT_Done_FreeType(ftlib);
}
void drawGlyph(
FT_GlyphSlot& glyphSlot,
CImg<unsigned char>& image,
const int& shiftX,
const int& shiftY,
unsigned char fontColor[] = NULL
){
unsigned char buff[] = {255, 255, 255};
if (fontColor == NULL){
fontColor = buff;
}
float alpha = 0;
for (int y = 0; y < glyphSlot->bitmap.rows; ++y){
for (int x = 0; x < glyphSlot->bitmap.width; ++x){
unsigned char glyphValue = glyphSlot->bitmap.buffer[y * glyphSlot->bitmap.width + x];
alpha = (255.0f - glyphValue) / 255.0f;
cimg_forC(image, c){
unsigned char value = (float) glyphValue*fontColor[c]/(255.0f);
image(x + shiftX, y + shiftY, c) =
alpha * image(x + shiftX, y + shiftY, c) + (1.0 - alpha) * value;
}
}
}
}
void drawText(
FT_Face& face,
CImg<unsigned char>& image,
const int& heightText,
const std::wstring& text,
const int& leftTopX,
const int& leftTopY,
unsigned char fontColor[] = NULL,
const int separeteGlyphWidth = 1
){
FT_Set_Pixel_Sizes(face, 0, heightText);
FT_GlyphSlot glyphSlot = face->glyph;
int shiftX = leftTopX;
int shiftY = 0;
for(int numberSymbol = 0; numberSymbol < text.length(); ++numberSymbol){
shiftY = leftTopY;
bool isSpace = false;
FT_ULong symbol = text.at(numberSymbol);
if (symbol == ' ') {
symbol = 'a';
isSpace = true;
}
if(FT_Load_Char(face, symbol, FT_LOAD_RENDER)){
throw "Error, glyph not load!! \n";
}
float shiftFactor = glyphSlot->bitmap.rows - glyphSlot->bitmap_top;
shiftY += shiftFactor;
shiftY += (heightText > glyphSlot->bitmap.rows) ? heightText - glyphSlot->bitmap.rows : 0;
if(!isSpace){
drawGlyph(glyphSlot, image, shiftX, shiftY, fontColor);
}
shiftX += glyphSlot->bitmap.width + separeteGlyphWidth;
}
}
int main() {
FT_Library ftlib;
FT_Face face;
try{
initFreetype(ftlib, face, "/home/hqq
a615
/公共的/字体/华文新宋.ttf");
CImg<unsigned char> img(640, 400, 1, 3);
img.fill(255);
unsigned char redFontColor[] = {255, 0, 0};
drawText(face, img, 50, L"你好!", 0, 0, redFontColor);
img.save_bmp("/home/hqq/HelloCIMG.bmp");
closeFreetype(ftlib, face);
} catch(char const* e) {
std::cout << "Exception: " << e << std::endl;
}
cout << "!!!恭喜成功!!!" << endl;
return 0;
}
FreeType不在头文件不在包含目录中,所以在使用时,需要包含头文件所在目录:
Project->Properties->C/C++ Build->Settings->Cross G++ Compiler->Includes
在Include path(-l)中添加freetype头文件所在目录。
pthread和freetype库不是 Linux 系统默认的库,所以在使用时,需要链接该库:
Project->Properties->C/C++ Build->Settings->GCC C++ Linker->Libraries
在Libraries(-l)中添加pthread,freetype即可。
CImg版本:1.6.0
CImg本身不支持中文,本文通过FreeType和TTF字体文件实现中文字体输出,其它字体的输出也可借鉴此方式实现。
#include <iostream>
using namespace std;
#define cimg_OS 0
#include </home/hqq/公共的/CImg-1.6.0_rolling150120/CImg.h>
using namespace cimg_library;
#include <ft2build.h>
#include FT_FREETYPE_H
void initFreetype(
FT_Library& ftlib,
FT_Face& face,
const std::string& fontFullName
){
FT_Error fterr;
if((fterr = FT_Init_FreeType( &ftlib ))) {
throw "Error init freetype";
}
if((fterr = FT_New_Face(ftlib, fontFullName.c_str(), 0, &face))){
if(fterr == FT_Err_Unknown_File_Format) {
throw "Error feetype, Unsupported font";
} else {
throw "Error feetype, new face";
}
}
}
void closeFreetype(
FT_Library& ftlib,
FT_Face& face
){
FT_Done_Face(face);
FT_Done_FreeType(ftlib);
}
void drawGlyph(
FT_GlyphSlot& glyphSlot,
CImg<unsigned char>& image,
const int& shiftX,
const int& shiftY,
unsigned char fontColor[] = NULL
){
unsigned char buff[] = {255, 255, 255};
if (fontColor == NULL){
fontColor = buff;
}
float alpha = 0;
for (int y = 0; y < glyphSlot->bitmap.rows; ++y){
for (int x = 0; x < glyphSlot->bitmap.width; ++x){
unsigned char glyphValue = glyphSlot->bitmap.buffer[y * glyphSlot->bitmap.width + x];
alpha = (255.0f - glyphValue) / 255.0f;
cimg_forC(image, c){
unsigned char value = (float) glyphValue*fontColor[c]/(255.0f);
image(x + shiftX, y + shiftY, c) =
alpha * image(x + shiftX, y + shiftY, c) + (1.0 - alpha) * value;
}
}
}
}
void drawText(
FT_Face& face,
CImg<unsigned char>& image,
const int& heightText,
const std::wstring& text,
const int& leftTopX,
const int& leftTopY,
unsigned char fontColor[] = NULL,
const int separeteGlyphWidth = 1
){
FT_Set_Pixel_Sizes(face, 0, heightText);
FT_GlyphSlot glyphSlot = face->glyph;
int shiftX = leftTopX;
int shiftY = 0;
for(int numberSymbol = 0; numberSymbol < text.length(); ++numberSymbol){
shiftY = leftTopY;
bool isSpace = false;
FT_ULong symbol = text.at(numberSymbol);
if (symbol == ' ') {
symbol = 'a';
isSpace = true;
}
if(FT_Load_Char(face, symbol, FT_LOAD_RENDER)){
throw "Error, glyph not load!! \n";
}
float shiftFactor = glyphSlot->bitmap.rows - glyphSlot->bitmap_top;
shiftY += shiftFactor;
shiftY += (heightText > glyphSlot->bitmap.rows) ? heightText - glyphSlot->bitmap.rows : 0;
if(!isSpace){
drawGlyph(glyphSlot, image, shiftX, shiftY, fontColor);
}
shiftX += glyphSlot->bitmap.width + separeteGlyphWidth;
}
}
int main() {
FT_Library ftlib;
FT_Face face;
try{
initFreetype(ftlib, face, "/home/hqq
a615
/公共的/字体/华文新宋.ttf");
CImg<unsigned char> img(640, 400, 1, 3);
img.fill(255);
unsigned char redFontColor[] = {255, 0, 0};
drawText(face, img, 50, L"你好!", 0, 0, redFontColor);
img.save_bmp("/home/hqq/HelloCIMG.bmp");
closeFreetype(ftlib, face);
} catch(char const* e) {
std::cout << "Exception: " << e << std::endl;
}
cout << "!!!恭喜成功!!!" << endl;
return 0;
}
FreeType不在头文件不在包含目录中,所以在使用时,需要包含头文件所在目录:
Project->Properties->C/C++ Build->Settings->Cross G++ Compiler->Includes
在Include path(-l)中添加freetype头文件所在目录。
pthread和freetype库不是 Linux 系统默认的库,所以在使用时,需要链接该库:
Project->Properties->C/C++ Build->Settings->GCC C++ Linker->Libraries
在Libraries(-l)中添加pthread,freetype即可。
相关文章推荐
- CImg应用-FreeType下载与安装(CentOS 6.5)
- CImg应用-HelloWorld(CentOS 6.5)
- [导入]xml+xsl应用,包含中文字符的URL编码问题
- snmp4j 中的中文字符输出问题
- 在场景中输出横向或纵向压缩的中文字符
- C++输出中文字符
- [C++] cout、wcout无法正常输出中文字符问题的深入调查(2):VC2005的crt源码分析
- Linux企业应用--RHAS 2.1 下安装中文 Lotus Domino R 6.5 图解
- 输出字符串中的中文字符
- CentOS下只显示文本中中文字符
- C#--第三周实验--任务2--输入一个由若干字符组成的字符串,输出其中的大写字母、小写字母、数字和其他字符的个数(控制台应用)
- 允许Python文档中显示中文 & 允许SublimeText2输入中文 & 允许Python输出unicode字符
- All About JAVA 关于英文服务器下输出含有中文字符文件乱码的问题
- 关于wchar_t不能输出中文字符。
- 在场景中输出横向或纵向压缩的中文字符
- web应用中文字符问题
- 如何将中文输出为UTF-8字符
- 前几天遇到的一个面试题,关于中文字符截取的问题。IO流的应用
- java在eclipse控制台读取并输出含有中文字符的文件
- Windows 控制台(console)输出宽字符(解决中文输出乱码问题)