目录
前言
使用方法
离线字库解析
工具链接
前言
最近通过Qt写了一个Guilite字库工具,相比原始工具,主要有以下几个优点:
(1)支持同时生成多套字库
(2)支持离线字库生成
(3)支持离线字库导入
(4)字库生成速度更快
使用方法
(1)打开工具之后点击“增加字体”
(2)字体选择完成之后输入字库内容
(3)点击生成字库
字库生成完成之后,会生成一个.cpp字库和.bin离线字库
(4)字库导入
有时我们需要在已有字库上进行增删,那么可以选择“字库导入”,选择正确的离线字库之后,工具会自动导入字库。
离线字库解析
(1)头文件
#ifndef FONT_PARSER_H
#define FONT_PARSER_H
#include <string>
#include "GuiLite.h"
class FontParser{
public:
static LATTICE_FONT_INFO* getFontFromBin(std::string fontPath);
static int releaseFont(LATTICE_FONT_INFO* pFont);
};
#endif
(2)cpp文件
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <string>
#include "FontParser.h"
#define FONT_PRINT(info,...)\
printf("[%s][%d]" info "",__FILE__,__LINE__,##__VA_ARGS__);
class AutoCloseFile {
public:
AutoCloseFile(FILE* fp):m_pFp(fp){}
~AutoCloseFile(void)
{
if (m_pFp)
{
fclose(m_pFp);
}
}
private:
FILE* m_pFp = nullptr;
};
inline int32_t getFileRemainLen_(FILE* fp)
{
size_t curPos = ftell(fp);
fseek(fp,0,SEEK_END);
size_t endPos = ftell(fp);
fseek(fp,curPos,SEEK_SET);
return endPos - curPos;
}
LATTICE_FONT_INFO* FontParser::getFontFromBin(std::string path)
{
FILE* fp = fopen(path.c_str(),"rb");
if(NULL == fp)
{
FONT_PRINT("fopen %s fail!\n",path.c_str());
return NULL;
}
AutoCloseFile closeFile(fp);
char startCode[5];
char fontInfo[64];
char version;
uint8_t fixCode[4];
uint32_t utf_8;
uint16_t fontWidth;
uint16_t fontHeight;
uint32_t fontCount;
uint32_t dataCount;
int32_t realCount;
memset((void*)startCode,0,5);
if(getFileRemainLen_(fp) < 4)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
fread((void*)startCode,1,4,fp);
//读取起始码
if(strcmp(startCode,"font") != 0)
{
FONT_PRINT("%s init fail,%s\n",path.c_str(),startCode);
return NULL;
}
//读取版本号
if(getFileRemainLen_(fp) < 1)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
fread((void*)&version,1,1,fp);
//读取字库信息
if(getFileRemainLen_(fp) < 64)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
memset((void*)fontInfo,0,64);
fread((void*)fontInfo,1,64,fp);
//读取固定码
if(getFileRemainLen_(fp) < 4)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
fread((void*)fixCode,1,4,fp);
if(0xff != fixCode[0] || 0xff != fixCode[1] || 0xff != fixCode[2]
|| 0xff != fixCode[3])
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
//读取字高度
if(getFileRemainLen_(fp) < 2)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
fread((void*)&fontHeight,1,2,fp);
//读取字库总个数
if(getFileRemainLen_(fp) < 4)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
fread((void*)&fontCount,1,4,fp);
LATTICE_FONT_INFO* pFont = (LATTICE_FONT_INFO*)malloc(sizeof(LATTICE_FONT_INFO));
if(NULL == pFont)
{
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
pFont->count = fontCount;
pFont->height = fontHeight;
FONT_PRINT("font count:%d\n",fontCount);
pFont->lattice_array = (LATTICE*)calloc(fontCount,sizeof(LATTICE));
if(NULL == pFont->lattice_array)
{
free((void*)pFont);
FONT_PRINT("%s init fail!\n",path.c_str());
return NULL;
}
realCount = 0;
for(int32_t i = 0;i < fontCount;i++)
{
//读取固定码
if(getFileRemainLen_(fp) < 4)
{
break;
}
fread((void*)fixCode,1,4,fp);
if(0xff != fixCode[0] || 0xff != fixCode[1] || 0xff != fixCode[2]
|| 0xff != fixCode[3])
{
break;
}
//读取utf-8码
if(getFileRemainLen_(fp) < 4)
{
break;
}
fread((void*)&utf_8,1,4,fp);
//读取宽度
if(getFileRemainLen_(fp) < 2)
{
break;
}
fread((void*)&fontWidth,1,2,fp);
//读取点数
if(getFileRemainLen_(fp) < 4)
{
break;
}
fread((void*)&dataCount,1,4,fp);
if(getFileRemainLen_(fp) < dataCount)
{
break;
}
pFont->lattice_array[i].utf8_code = utf_8;
pFont->lattice_array[i].width = fontWidth;
pFont->lattice_array[i].pixel_buffer = (uint8_t*)calloc(1,dataCount);
if(NULL == pFont->lattice_array[i].pixel_buffer)
{
break;
}
fread((void*)pFont->lattice_array[i].pixel_buffer,1,dataCount,fp);
realCount++;
}
if(realCount != fontCount)
{
for(int32_t i = 0;i < fontCount;i++)
{
if(NULL != pFont->lattice_array[i].pixel_buffer)
{
free((void*)pFont->lattice_array[i].pixel_buffer);
}
}
free((void*)pFont->lattice_array);
free((void*)pFont);
FONT_PRINT("%s init fail!\n",path.c_str());
FONT_PRINT("info count:%d,real count:%d\n",fontCount,realCount);
return NULL;
}
FONT_PRINT("%s init success!\n",path.c_str());
FONT_PRINT("font info:%s\n",path.c_str());
return pFont;
}
int FontParser::releaseFont(LATTICE_FONT_INFO* pFont)
{
if(NULL == pFont)
{
return -1;
}
if(NULL == pFont->lattice_array)
{
free((void*)pFont);
return 0;
}
for(int32_t i = 0;i < pFont->count;i++)
{
if(pFont->lattice_array[i].pixel_buffer)
{
free((void*)pFont->lattice_array[i].pixel_buffer);
}
}
free((void*)pFont->lattice_array);
free((void*)pFont);
return 0;
}
工具链接
v2.3版本:
(1)百度网盘链接:链接: https://pan.baidu.com/s/1PAX-1qS0RfOuI3wXd3iBGw 提取码: zkq5
(2)CSDN下载链接:https://download.csdn.net/download/qq_37363702/90260230