使用Qt编写一个字体转换工具
1、界面布局
2、移植矢量字体解码库stb_truetype
#define STB_TRUETYPE_IMPLEMENTATION
#include “stb_truetype.h”
3、编写字体转换的代码
/* 循环加载word中每个字符 */
for (int i = 0; i < num ; i++)
{
int advanceWidth = 0; //字宽
int leftSideBearing = 0; //左侧位置
/* 获取水平方向上的度量 */
stbtt_GetCodepointHMetrics(&info, word[i], &advanceWidth, &leftSideBearing);
/* 获取字符的边框(边界) */
int c_x1, c_y1, c_x2, c_y2;
stbtt_GetCodepointBitmapBox(&info, word[i], scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);
fv_glyph_array[i]->glyph_unicode = word[i];
fv_glyph_array[i]->glyph_width = c_x2 - c_x1;
fv_glyph_array[i]->glyph_height = c_y2 - c_y1;
fv_glyph_array[i]->glyph_map = (uint8_t *)malloc(fv_glyph_array[i]->glyph_width * fv_glyph_array[i]->glyph_height);
fv_glyph_array[i]->glyph_ascent = 0 - c_y1; //基准线上方的尺寸
fv_glyph_array[i]->glyph_descent = c_y2; //基准线下方的尺寸
fv_glyph_array[i]->glyph_advance = roundf(advanceWidth * scale); //水平方向的总尺寸 = glyph数据的宽度 + 左边距 + 右边距
fv_glyph_array[i]->glyph_leading = roundf(leftSideBearing * scale); //左边距
stbtt_MakeCodepointBitmap(&info, fv_glyph_array[i]->glyph_map, c_x2 - c_x1, c_y2 - c_y1, c_x2 - c_x1, scale, scale, word[i]);
}
4、字体转换工具生成的文件如下:
#include "fv_types_def.h"
static const uint8_t font_24_map_unicode_0030[] = {
0x00, 0x06, 0xbe, 0xc6, 0x00, //...35763..
0x00, 0x9f, 0xb8, 0xdf, 0x70, //..4754673.
0x03, 0xfa, 0x00, 0x0d, 0xf1, //.175...67.
0x09, 0xf2, 0x00, 0x06, 0xf6, //.471...373
0x0e, 0xd0, 0x00, 0x02, 0xfa, //.76....175
0x0f, 0xb0, 0x00, 0x00, 0xfb, //.75.....75
0x1f, 0xa0, 0x00, 0x00, 0xfd, //.75.....76
0x2f, 0x90, 0x00, 0x00, 0xed, //174.....76
0x1f, 0xa0, 0x00, 0x00, 0xfb, //.75.....75
0x0f, 0xc0, 0x00, 0x01, 0xfa, //.76.....75
0x0d, 0xf0, 0x00, 0x05, 0xf6, //.67....273
0x07, 0xf6, 0x00, 0x0b, 0xf0, //.373...57.
0x01, 0xef, 0x74, 0x9f, 0x70, //..7732473.
0x00, 0x1c, 0xff, 0xe7, 0x00, //...67773..
0x00, 0x00, 0x12, 0x00, 0x00, //.....1....
};
static const uint8_t font_24_map_unicode_0031[] = {
0x00, 0x00, 0x00, 0x00, 0x00, //.........
0x00, 0x27, 0xc7, 0x00, 0x00, //..1363...
0x4e, 0xff, 0xf7, 0x00, 0x00, //277773...
0x5b, 0x65, 0xf7, 0x00, 0x00, //253273...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x00, 0x03, 0xf7, 0x00, 0x00, //...173...
0x28, 0x89, 0xfb, 0x88, 0x40, //144475442
0x5f, 0xff, 0xff, 0xff, 0x80, //277777774
};
static const uint8_t font_24_map_unicode_0032[] = {
0x00, 0x05, 0xae, 0x94, 0x00, //...25742..
0x04, 0xfd, 0x99, 0xef, 0x80, //.27644774.
0x05, 0x70, 0x00, 0x0c, 0xe0, //.23....67.
0x00, 0x00, 0x00, 0x07, 0xf5, //.......372
0x00, 0x00, 0x00, 0x07, 0xf3, //.......371
0x00, 0x00, 0x00, 0x0b, 0xe0, //.......57.
0x00, 0x00, 0x00, 0x5f, 0x80, //......274.
0x00, 0x00, 0x03, 0xfc, 0x00, //.....176..
0x00, 0x00, 0x2e, 0xd1, 0x00, //....176...
0x00, 0x02, 0xed, 0x10, 0x00, //...176....
0x00, 0x2e, 0xd1, 0x00, 0x00, //..176.....
0x02, 0xed, 0x10, 0x00, 0x00, //.176......
0x0e, 0xfa, 0x88, 0x88, 0x82, //.775444441
0x0f, 0xff, 0xff, 0xff, 0xf4, //.777777772
};
static const uint8_t font_24_map_unicode_0033[] = {
0x06, 0x9d, 0xc7, 0x20, 0x00, //.346631..
0x3f, 0xd8, 0xcf, 0xf1, 0x00, //1764677..
0x13, 0x00, 0x06, 0xf7, 0x00, //.1...373.
0x00, 0x00, 0x01, 0xfb, 0x00, //......75.
0x00, 0x00, 0x04, 0xf7, 0x00, //.....273.
0x00, 0x00, 0x4c, 0xe2, 0x00, //....2671.
0x04, 0xdf, 0xf9, 0x00, 0x00, //.26774...
0x03, 0x9c, 0xfe, 0x81, 0x00, //.146774..
0x00, 0x00, 0x06, 0xfa, 0x00, //.....375.
0x00, 0x00, 0x00, 0xcf, 0x10, //......67.
0x00, 0x00, 0x00, 0x9f, 0x30, //......471
0x20, 0x00, 0x01, 0xff, 0x00, //1.....77.
0xbd, 0x84, 0x8d, 0xf6, 0x00, //56424673.
0x6c, 0xff, 0xfd, 0x50, 0x00, //3677762..
0x00, 0x03, 0x10, 0x00, 0x00, //...1.....
};
static const uint8_t font_24_map_unicode_0034[] = {
0x00, 0x00, 0x00, 0xbc, 0x30, 0x00, //......561..
0x00, 0x00, 0x09, 0xff, 0x40, 0x00, //.....4772..
0x00, 0x00, 0x4f, 0xaf, 0x40, 0x00, //....27572..
0x00, 0x00, 0xea, 0x6f, 0x40, 0x00, //....75372..
0x00, 0x09, 0xe1, 0x6f, 0x40, 0x00, //...47.372..
0x00, 0x4f, 0x50, 0x6f, 0x40, 0x00, //..272.372..
0x01, 0xea, 0x00, 0x6f, 0x40, 0x00, //..75..372..
0x0a, 0xe1, 0x00, 0x6f, 0x40, 0x00, //.57...372..
0x4f, 0x50, 0x00, 0x6f, 0x40, 0x00, //272...372..
0xcf, 0xff, 0xff, 0xff, 0xff, 0x00, //6777777777.
0x56, 0x66, 0x66, 0xaf, 0x96, 0x00, //2333335743.
0x00, 0x00, 0x00, 0x6f, 0x40, 0x00, //......372..
0x00, 0x00, 0x00, 0x6f, 0x40, 0x00, //......372..
0x00, 0x00, 0x00, 0x6f, 0x40, 0x00, //......372..
};
static const uint8_t font_24_map_unicode_0035[] = {
0x0c, 0xcc, 0xcc, 0xc9, 0x00, //.6666664.
0x0f, 0xec, 0xcc, 0xc9, 0x00, //.7766664.
0x0f, 0x90, 0x00, 0x00, 0x00, //.74......
0x0f, 0x90, 0x00, 0x00, 0x00, //.74......
0x0f, 0x90, 0x00, 0x00, 0x00, //.74......
0x0f, 0xb5, 0x41, 0x00, 0x00, //.7522....
0x0f, 0xff, 0xff, 0xa1, 0x00, //.777775..
0x03, 0x23, 0x7c, 0xfb, 0x00, //.1113675.
0x00, 0x00, 0x00, 0xef, 0x20, //......771
0x00, 0x00, 0x00, 0x8f, 0x50, //......472
0x00, 0x00, 0x00, 0xaf, 0x40, //......572
0x00, 0x00, 0x01, 0xff, 0x10, //......77.
0x6d, 0x84, 0x9e, 0xf6, 0x00, //36424773.
0x4d, 0xff, 0xfd, 0x50, 0x00, //2677762..
0x00, 0x03, 0x10, 0x00, 0x00, //...1.....
};
static const uint8_t font_24_map_unicode_0036[] = {
0x00, 0x06, 0xbe, 0xda, 0x00, //...35765.
0x00, 0x9f, 0xc8, 0xae, 0x00, //..476457.
0x08, 0xf7, 0x00, 0x00, 0x00, //.473.....
0x1f, 0xb0, 0x00, 0x00, 0x00, //.75......
0x7f, 0x40, 0x00, 0x00, 0x00, //372......
0xaf, 0x03, 0x86, 0x00, 0x00, //57.143...
0xce, 0x7f, 0xef, 0xf9, 0x00, //67377774.
0xef, 0xc4, 0x03, 0xbf, 0x20, //7762.1571
0xef, 0x50, 0x00, 0x4f, 0x80, //772...274
0xcf, 0x00, 0x00, 0x0f, 0xd0, //67.....76
0xaf, 0x30, 0x00, 0x1f, 0xc0, //571....76
0x4f, 0x90, 0x00, 0x6f, 0x80, //274...374
0x0b, 0xfb, 0x59, 0xfe, 0x00, //.5752477.
0x00, 0xaf, 0xff, 0xb1, 0x00, //..57775..
0x00, 0x00, 0x20, 0x00, 0x00, //....1....
};
static const uint8_t font_24_map_unicode_0037[] = {
0x2c, 0xcc, 0xcc, 0xcc, 0xc9, //1666666664
0x2c, 0xcc, 0xcc, 0xcd, 0xf8, //1666666674
0x00, 0x00, 0x00, 0x09, 0xf1, //.......47.
0x00, 0x00, 0x00, 0x0f, 0xb0, //.......75.
0x00, 0x00, 0x00, 0x6f, 0x40, //......372.
0x00, 0x00, 0x00, 0xdd, 0x00, //......66..
0x00, 0x00, 0x04, 0xf7, 0x00, //.....273..
0x00, 0x00, 0x0a, 0xf1, 0x00, //.....57...
0x00, 0x00, 0x1f, 0xa0, 0x00, //.....75...
0x00, 0x00, 0x8f, 0x30, 0x00, //....471...
0x00, 0x00, 0xed, 0x00, 0x00, //....76....
0x00, 0x05, 0xf6, 0x00, 0x00, //...273....
0x00, 0x0c, 0xf0, 0x00, 0x00, //...67.....
0x00, 0x3f, 0x90, 0x00, 0x00, //..174.....
};
static const uint8_t font_24_map_unicode_0038[] = {
0x00, 0x05, 0xae, 0xa5, 0x00, //...25752..
0x00, 0xaf, 0xc8, 0xdf, 0x80, //..5764674.
0x02, 0xf9, 0x00, 0x0d, 0xe0, //.174...67.
0x07, 0xf4, 0x00, 0x08, 0xf3, //.372...471
0x03, 0xf9, 0x00, 0x0d, 0xe0, //.174...67.
0x00, 0xbf, 0x82, 0xbf, 0x50, //..5741572.
0x00, 0x08, 0xff, 0xf3, 0x00, //...47771..
0x00, 0x3d, 0xfc, 0xfb, 0x10, //..167675..
0x04, 0xfb, 0x10, 0x5e, 0xf1, //.275..277.
0x0b, 0xf1, 0x00, 0x06, 0xf6, //.57....373
0x0f, 0xc0, 0x00, 0x01, 0xfa, //.76.....75
0x0a, 0xf2, 0x00, 0x06, 0xf8, //.571...374
0x03, 0xfd, 0x74, 0x9e, 0xe2, //.176324771
0x00, 0x5b, 0xff, 0xfb, 0x30, //..2577751.
0x00, 0x00, 0x12, 0x00, 0x00, //.....1....
};
static const uint8_t font_24_map_unicode_0039[] = {
0x00, 0x09, 0xde, 0xb4, 0x00, //...46752..
0x01, 0xdf, 0xca, 0xff, 0x50, //..6765772.
0x09, 0xf6, 0x00, 0x2d, 0xe0, //.473..167.
0x0e, 0xe0, 0x00, 0x08, 0xf5, //.77....472
0x0f, 0xb0, 0x00, 0x03, 0xf8, //.75....174
0x0c, 0xf0, 0x00, 0x05, 0xfa, //.67....275
0x06, 0xf7, 0x00, 0x1c, 0xfa, //.373...675
0x01, 0xef, 0xea, 0xfb, 0xf9, //..77757574
0x00, 0x05, 0xcc, 0x82, 0xf7, //...2664173
0x00, 0x00, 0x00, 0x04, 0xf5, //.......272
0x00, 0x00, 0x00, 0x0a, 0xe0, //.......57.
0x00, 0x00, 0x00, 0x4f, 0x80, //......274.
0x04, 0xa5, 0x48, 0xfc, 0x10, //.2522476..
0x04, 0xef, 0xfe, 0x90, 0x00, //.277774...
0x00, 0x01, 0x20, 0x00, 0x00, //....1.....
};
static const fv_glyph_t font_24_glyph_array[] = {
{.glyph_unicode = 0x0030, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 1, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 10, .glyph_height = 15, .glyph_map = font_24_map_unicode_0030},
{.glyph_unicode = 0x0031, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 15, .glyph_descent = 0, .glyph_advance = 11, .glyph_leading = 2, .glyph_width = 9, .glyph_height = 15, .glyph_map = font_24_map_unicode_0031},
{.glyph_unicode = 0x0032, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 0, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 10, .glyph_height = 14, .glyph_map = font_24_map_unicode_0032},
{.glyph_unicode = 0x0033, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 1, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 9, .glyph_height = 15, .glyph_map = font_24_map_unicode_0033},
{.glyph_unicode = 0x0034, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 0, .glyph_advance = 11, .glyph_leading = 0, .glyph_width = 11, .glyph_height = 14, .glyph_map = font_24_map_unicode_0034},
{.glyph_unicode = 0x0035, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 1, .glyph_advance = 11, .glyph_leading = 2, .glyph_width = 9, .glyph_height = 15, .glyph_map = font_24_map_unicode_0035},
{.glyph_unicode = 0x0036, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 1, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 9, .glyph_height = 15, .glyph_map = font_24_map_unicode_0036},
{.glyph_unicode = 0x0037, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 0, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 10, .glyph_height = 14, .glyph_map = font_24_map_unicode_0037},
{.glyph_unicode = 0x0038, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 1, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 10, .glyph_height = 15, .glyph_map = font_24_map_unicode_0038},
{.glyph_unicode = 0x0039, .glyph_format = FV_GLYPH_FORMAT_4BPP, .glyph_ascent = 14, .glyph_descent = 1, .glyph_advance = 11, .glyph_leading = 1, .glyph_width = 10, .glyph_height = 15, .glyph_map = font_24_map_unicode_0039},
};
const fv_font_t font_24 = {
.font_name = "font_24",
.font_location = FV_FONT_LOCATION_ROM_BIN,
.font_line_height = 24,
.font_baseline = 19,
.font_glyphs = font_24_glyph_array,
.font_glyphs_cnt = 10,
};
FV_ADD_FONT(font_24); //将字体的指针添加到自定义数据段