前言:最近项目上需要在linux上做字体的图像生成,经查找资料选择使用freetype来进行字体的生成
1.bmp相关的定义 BMP_API.h
这一步我们可以先进行bmp相关基础的了解
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef long LONG;
//位图文件头定义;
typedef struct tagBITMAPFILEHEADER{
// WORD bfType;//单独读取,结构体中就不定义了
DWORD bfSize;//文件大小
WORD bfReserved1;//保留字
WORD bfReserved2;//保留字
DWORD bfOffBits;//从文件头到实际位图数据的偏移字节数
}BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
DWORD biSize;//信息头大小
DWORD biWidth;//图像宽度
DWORD biHeight;//图像高度
WORD biPlanes;//位平面数,必须为1
WORD biBitCount;//每像素位数
DWORD biCompression; //压缩类型
DWORD biSizeImage; //压缩图像大小字节数
DWORD biXPelsPerMeter; //水平分辨率
DWORD biYPelsPerMeter; //垂直分辨率
DWORD biClrUsed; //位图实际用到的色彩数
DWORD biClrImportant; //本位图中重要的色彩数
}BITMAPINFOHEADER; //位图信息头定义
//像素信息
typedef struct tagIMAGEDATA
{
BYTE blue;
BYTE green;
BYTE red;
}DATA;
//BMP整体信息
typedef struct tagBMP_BUFFER
{
WORD mbfType;
BITMAPFILEHEADER hand;
BITMAPINFOHEADER info;
DATA* BUFFER;
}BMP_BUFFER;
#include "BMP_API.h"
int main()
{
BMP_BUFFER one_bmp_BUFFER;
one_bmp_BUFFER.mbfType =0X4d42;
one_bmp_BUFFERhand.bfOffBits =54;
one_bmp_BUFFERhand.bfReserved1 =0;
one_bmp_BUFFERhand.bfReserved2 =0;
one_bmp_BUFFERhand.bfSize =1032*672*3+40+14;
one_bmp_BUFFERBUFFER =new DATA[1032*672];
one_bmp_BUFFERinfo.biBitCount =24;
one_bmp_BUFFERinfo.biClrImportant =0;
one_bmp_BUFFERinfo.biClrUsed =0;
one_bmp_BUFFERinfo.biCompression =0;
one_bmp_BUFFERinfo.biHeight =1032;
one_bmp_BUFFERinfo.biPlanes =1;
one_bmp_BUFFERinfo.biSize =40;
one_bmp_BUFFERinfo.biSizeImage =1032*672*3;
one_bmp_BUFFERinfo.biWidth =672;
one_bmp_BUFFERinfo.biXPelsPerMeter =0;
one_bmp_BUFFERinfo.biYPelsPerMeter =0;
int x=0;
int y=0;
for(x=0;x<1032;x++)
{
for(y=0;y<672;y++)
{
one_bmp_BUFFERBUFFER[x*672+y].red=255;
one_bmp_BUFFERBUFFER[x*672+y].blue=255;
one_bmp_BUFFERBUFFER[x*672+y].green=255;
}
}
FILE *fpo1;
fpo1=fopen("./one_bmp_BUFFERbmp","wb");
fwrite(&one_bmp_BUFFER.mbfType,1,sizeof(WORD),fpo1);
fwrite(&one_bmp_BUFFER.hand,1,sizeof(tagBITMAPFILEHEADER),fpo1);
fwrite(&one_bmp_BUFFER.info,1,sizeof(tagBITMAPINFOHEADER),fpo1);
fwrite(one_bmp_BUFFER.BUFFER,1,sizeof(DATA)*(1032*672),fpo1);
fclose(fpo1);
}
#include "BMP_API.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include FT_OUTLINE_H
int main()
{
BMP_BUFFER one_bmp_BUFFER;
//初始化buffer_k
one_bmp_BUFFER.mbfType =0X4d42;
one_bmp_BUFFER.hand.bfOffBits =54;
one_bmp_BUFFER.hand.bfReserved1 =0;
one_bmp_BUFFER.hand.bfReserved2 =0;
one_bmp_BUFFER.hand.bfSize =1032*672*3+40+14;
one_bmp_BUFFER.BUFFER =new DATA[1032*672];
one_bmp_BUFFER.info.biBitCount =24;
one_bmp_BUFFER.info.biClrImportant =0;
one_bmp_BUFFER.info.biClrUsed =0;
one_bmp_BUFFER.info.biCompression =0;
one_bmp_BUFFER.info.biHeight =1032;
one_bmp_BUFFER.info.biPlanes =1;
one_bmp_BUFFER.info.biSize =40;
one_bmp_BUFFER.info.biSizeImage =1032*672*3;
one_bmp_BUFFER.info.biWidth =672;
one_bmp_BUFFER.info.biXPelsPerMeter =0;
one_bmp_BUFFER.info.biYPelsPerMeter =0;
//全部使用白色区域
int x=0;
int y=0;
for(x=0;x<1032;x++)
{
for(y=0;y<672;y++)
{
one_bmp_BUFFER.BUFFER[x*672+y].red=255;
one_bmp_BUFFER.BUFFER[x*672+y].blue=255;
one_bmp_BUFFER.BUFFER[x*672+y].green=255;
}
}
FT_Library pFTLib = NULL;
FT_Face pFTFace = NULL;
FT_Error error = 0;
//1.初始化一个freetype库
error = FT_Init_FreeType(&pFTLib);
if(error){
pFTLib = 0;
printf("There is some error when init library\n");
return -1;
}
//2.加载字体样式
error = FT_New_Face(pFTLib, "/home/zhoubing/mywork/freetype_test/simsun.ttc", 0, &pFTFace);
if(error){
printf("Open font failed\n");
return -1;
}
//3.设置字体的大小
FT_Set_Char_Size(pFTFace, 0, 32*16, 300, 300);//通过设置长宽范围设置
FT_Glyph glyph;
wchar_t *wszString = L"TEST BY TOPSLUO";
WORD word;
int bitmap_width_sum = -672;//字体在图像上X轴起始位置
int bitmap_high_sum = 50;//字体在图像上Y轴起始位置
int wszStringLen = wcslen(wszString)+1;
printf("wszStringLen: %d\n",wszStringLen);
int k=0;
for(k=0; k < wszStringLen; k++)
{
memcpy(&word, wszString+k, 2);
//转化成位图
FT_Load_Glyph(pFTFace, FT_Get_Char_Index(pFTFace,word), FT_LOAD_DEFAULT);
error = FT_Get_Glyph(pFTFace->glyph, &glyph);
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 i=0;
int j=0;
int font_H=bitmap->rows;
int font_W=bitmap->width;
for( i=0;ibuffer[i * font_W +j] != 0)
{
//arrayColor[(i+100)*strInfo.biWidth+j+bitmap_width_sum+100].green = 0;
//arrayColor[(i+100)*strInfo.biWidth+j+bitmap_width_sum+100].blue = 0;
//arrayColor[(strInfo.biHeight-i)*strInfo.biWidth+j+bitmap_width_sum].green = 0;
//arrayColor[(strInfo.biHeight-i)*strInfo.biWidth+j+bitmap_width_sum].blue = 0;
//文字竖体
// one_bmp_BUFFER.BUFFER[(1032-i-bitmap_width_sum)*672+j].red=0;
// one_bmp_BUFFER.BUFFER[(1032-i-bitmap_width_sum)*672+j].blue=0;
// one_bmp_BUFFER.BUFFER[(1032-i-bitmap_width_sum)*672+j].green=0;
//文字横体
one_bmp_BUFFER.BUFFER[(1032-i-bitmap_high_sum)*672+j+bitmap_width_sum].red=0;
one_bmp_BUFFER.BUFFER[(1032-i-bitmap_high_sum)*672+j+bitmap_width_sum].blue=0;
one_bmp_BUFFER.BUFFER[(1032-i-bitmap_high_sum)*672+j+bitmap_width_sum].green=0;
}
else
{
//文字竖体
// one_bmp_BUFFER.BUFFER[(1032-i-bitmap_width_sum)*672+j].red=255;
//one_bmp_BUFFER.BUFFER[(1032-i-bitmap_width_sum)*672+j].blue=255;
// one_bmp_BUFFER.BUFFER[(1032-i-bitmap_width_sum)*672+j].green=255;
//文字横体
one_bmp_BUFFER.BUFFER[(1032-i-bitmap_high_sum)*672+j+bitmap_width_sum].red=255;
one_bmp_BUFFER.BUFFER[(1032-i-bitmap_high_sum)*672+j+bitmap_width_sum].blue=255;
one_bmp_BUFFER.BUFFER[(1032-i-bitmap_high_sum)*672+j+bitmap_width_sum].green=255;
}
}
}
// bitmap_width_sum += bitmap->width + 10;
bitmap_width_sum += 16;
}
FILE *fpo1;
fpo1=fopen("./one_bmp_BUFFER.bmp","wb");
fwrite(&one_bmp_BUFFER.mbfType,1,sizeof(WORD),fpo1);
fwrite(&one_bmp_BUFFER.hand,1,sizeof(tagBITMAPFILEHEADER),fpo1);
fwrite(&one_bmp_BUFFER.info,1,sizeof(tagBITMAPINFOHEADER),fpo1);
fwrite(one_bmp_BUFFER.BUFFER,1,sizeof(DATA)*(1032*672),fpo1);
fclose(fpo1);
return 0;
}