forked from CURTLab/LVGLBuilder
-
Notifications
You must be signed in to change notification settings - Fork 18
/
LVGLFontData.cpp
342 lines (288 loc) · 11.8 KB
/
LVGLFontData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "LVGLFontData.h"
#include <QDebug>
#include <QFile>
#include <QtMath>
#include "LVGLCore.h"
#include <ft2build.h>
#include FT_FREETYPE_H
LVGLFontData::LVGLFontData(const QString &name, const QString &codeName, uint8_t size, const lv_font_t *font)
: m_font(const_cast<lv_font_t*>(font))
, m_name(name)
, m_codeName(codeName)
, m_size(size)
, m_customFont(false)
{
}
LVGLFontData::~LVGLFontData()
{
if (m_customFont) {
lv_font_fmt_txt_dsc_t *fdsc = reinterpret_cast<lv_font_fmt_txt_dsc_t *>(m_font->dsc);
delete [] fdsc->glyph_bitmap;
delete [] fdsc->glyph_dsc;
delete [] fdsc->cmaps;
delete fdsc;
delete m_font;
m_font = nullptr;
}
}
LVGLFontData *LVGLFontData::parse(const QString &fileName, uint8_t size, uint8_t bpp, uint32_t unicodeFirst, uint32_t unicodeLast)
{
FT_Face face;
int error = FT_New_Face(lvgl.m_ft, qPrintable(fileName), 0, &face);
if (error == FT_Err_Unknown_File_Format) {
qCritical() << "LVGLFontData: Unkown file format";
return nullptr;
} else if (error) {
qCritical() << "LVGLFontData: Error loading font:" << FT_Error_String(error);
return nullptr;
}
//QString unicode_start_str = "U+" + QString::number(unicodeFirst, 16).rightJustified(4, '0');
//QString unicode_last_str = "U+" + QString::number(unicodeLast, 16).rightJustified(4, '0');
FT_Set_Char_Size(face, 0, size * 64, 300, 300);
FT_Set_Pixel_Sizes(face, 0, size);
const int mask = (~(((1 << bpp) - 1) & 0xff)) & 0xff;
std::vector<uint8_t> gylph_bitmap;
const uint32_t glyphs = unicodeLast - unicodeFirst + 1;
lv_font_fmt_txt_glyph_dsc_t *glyph_dsc = new lv_font_fmt_txt_glyph_dsc_t[glyphs + 1];
memset(glyph_dsc, 0, sizeof(lv_font_fmt_txt_glyph_dsc_t));
int ascent = 0;
int descent = 0;
uint32_t counter = 0;
for(uint32_t unicodeAct = unicodeFirst; unicodeAct <= unicodeLast; ++unicodeAct) {
int error = FT_Load_Char(face, unicodeAct, FT_LOAD_RENDER);
if (error) {
qCritical() << "Error" << error << FT_Error_String(error);
continue;
}
const size_t curr_buf_size = gylph_bitmap.size();
uint8_t w = 0, h = 0;
if (face->glyph->bitmap.buffer != nullptr) {
FT_Bitmap bitmap = face->glyph->bitmap;
w = bitmap.width & 0xff;
h = bitmap.rows & 0xff;
const uint size_bitmap = bitmap.rows * bitmap.width;
const size_t buf_size = (size_bitmap + 1) * bpp / 8;
gylph_bitmap.resize(curr_buf_size + buf_size);
//assert(size_bitmap % (8 / bpp) == 0);
uint8_t *buf_in = bitmap.buffer;
uint8_t *buf_out = gylph_bitmap.data() + curr_buf_size;
// convert to bpp format
for (uint j = 0; j < buf_size; ++j) {
*buf_out = 0;
for (int i = 0; i < 8; i += bpp) {
const bool ok = ((buf_in - bitmap.buffer) < size_bitmap);
const uint8_t val = (ok?*buf_in:0);
*buf_out |= ((val & mask) >> i);
if (ok)
++buf_in; // avoid pointing to not allocated memory
}
++buf_out; // avoid pointing to not allocated memory
}
}
lv_font_fmt_txt_glyph_dsc_t &desc = glyph_dsc[unicodeAct - unicodeFirst + 1];
desc.bitmap_index = curr_buf_size & 0xffff;
const double advance = face->glyph->metrics.horiAdvance / 64.0;
desc.adv_w = uint32_t(std::ceil(advance * 16));
desc.box_w = w;
desc.box_h = h;
desc.ofs_x = static_cast<int8_t>(face->glyph->bitmap_left);
desc.ofs_y = static_cast<int8_t>(face->glyph->bitmap_top) - h;
ascent = std::max(ascent, face->glyph->bitmap_top);
descent = std::min(descent, face->glyph->bitmap_top - h);
++counter;
//qDebug() << QString::number(unicodeAct, 16).rightJustified(4, '0') << QChar(unicodeAct);
}
Q_ASSERT(counter == glyphs);
uint8_t *gylph_bitmap_buf = new uint8_t[gylph_bitmap.size()];
memcpy(gylph_bitmap_buf, gylph_bitmap.data(), gylph_bitmap.size());
// allocate cmap
lv_font_fmt_txt_cmap_t *cmap = new lv_font_fmt_txt_cmap_t[1];
cmap[0].range_start = unicodeFirst;
cmap[0].range_length = static_cast<uint16_t>(glyphs & 0xffff);
cmap[0].glyph_id_start = 1;
cmap[0].unicode_list = nullptr;
cmap[0].glyph_id_ofs_list = nullptr;
cmap[0].list_length = 0;
cmap[0].type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY;
// allocate txt description
lv_font_fmt_txt_dsc_t *font_dsc = new lv_font_fmt_txt_dsc_t;
font_dsc->glyph_bitmap = gylph_bitmap_buf;
font_dsc->glyph_dsc = glyph_dsc;
font_dsc->cmaps = cmap;
font_dsc->kern_dsc = nullptr;
font_dsc->kern_scale = 0;
font_dsc->cmap_num = 1;
font_dsc->bpp = bpp;
font_dsc->kern_classes = 0;
font_dsc->bitmap_format = LV_FONT_FMT_TXT_PLAIN;
// allocate font in with default callbacks
lv_font_t *font = new lv_font_t;
font->get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt;
font->get_glyph_bitmap = lv_font_get_bitmap_fmt_txt;
//font->line_height = uint8_t(std::round((face->ascender - face->descender) / 64.0));
//font->base_line = uint8_t(-std::round(face->descender / 64.0));
font->line_height = uint8_t(ascent - descent);
font->base_line = uint8_t(-descent);
font->subpx = LV_FONT_SUBPX_NONE;
font->dsc = font_dsc;
// generate font name
QString baseName(face->family_name);
if (strcmp(face->style_name, "Regular") != 0)
baseName += QString(" %1").arg(face->style_name);
QString name = QString("%1 %2").arg(baseName).arg(size);
LVGLFontData *ret = new LVGLFontData(name, name.toLower().replace(" ", "_").replace("-", "_"), size, font);
ret->m_customFont = true;
ret->m_fileName = fileName;
return ret;
}
LVGLFontData *LVGLFontData::parse(QJsonObject object)
{
if (!object.contains("fileName") || !object.contains("bpp") ||
!object.contains("start") || !object.contains("end"))
return nullptr;
const uint8_t size = static_cast<uint8_t>(object["size"].toInt());
const uint8_t bpp = static_cast<uint8_t>(object["bpp"].toInt());
const uint32_t start = static_cast<uint32_t>(object["start"].toInt());
const uint32_t end = static_cast<uint32_t>(object["end"].toInt());
return parse(object["fileName"].toString(), size, bpp, start, end);
}
const lv_font_t *LVGLFontData::font() const
{
return m_font;
}
QString LVGLFontData::name() const
{
return m_name;
}
QString LVGLFontData::codeName() const
{
return m_codeName;
}
bool LVGLFontData::saveAsCode(const QString &fileName) const
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return false;
lv_font_fmt_txt_dsc_t *fdsc = reinterpret_cast<lv_font_fmt_txt_dsc_t *>(m_font->dsc);
if (fdsc->cmap_num != 1) {
qCritical() << "LVGLFontData: Char map count <==> 1!";
return false;
}
const uint32_t unicodeFirst = fdsc->cmaps[0].range_start;
const uint32_t unicodeSec = unicodeFirst + fdsc->cmaps[0].range_length - 1;
QTextStream stream(&file);
stream << "#include \"lvgl.h\"\n\n";
const QString defName = m_codeName.toUpper();
const QString output_name = m_codeName;
stream << "/*******************************************************************************\n";
stream << " * Size: " << m_size << " px\n";
stream << " * Bpp: " << fdsc->bpp << "\n";
stream << " * Opts: \n";
stream << " ******************************************************************************/\n\n";
stream << "#ifndef " << defName << "\n";
stream << "#define " << defName << " 1\n";
stream << "#endif\n\n";
stream << "#if " << defName << "\n\n";
stream << "/*-----------------\n";
stream << " * BITMAPS\n";
stream << " *----------------*/\n\n";
stream << "/* Store the image of the letters (glyph) */\n";
stream << "static const uint8_t " << output_name << "_glyph_bitmap[] =\n{\n";
for (uint32_t unicodeAct = unicodeFirst; unicodeAct <= unicodeSec; ++unicodeAct) {
stream << "\t/* U+" << QString::number(unicodeAct, 16).rightJustified(4, '0') << " \"" << QChar(unicodeAct) << "\" */";
const lv_font_fmt_txt_glyph_dsc_t &gdsc = fdsc->glyph_dsc[unicodeAct - unicodeFirst + 1];
const uint32_t size = (gdsc.box_w * gdsc.box_h + 1) * fdsc->bpp / 8;
const uint8_t *buf = fdsc->glyph_bitmap + gdsc.bitmap_index;
for (uint32_t i = 0; i < size; ++i, ++buf) {
if (i % 8 == 0) stream << "\n\t";
stream << "0x" << QString::number(*buf, 16).rightJustified(2, '0') << ", ";
}
stream << "\n";
if (unicodeAct != unicodeSec) stream << "\n";
}
stream << "};\n\n";
stream << "/*---------------------\n";
stream << " * GLYPH DESCRIPTION\n";
stream << " *--------------------*/\n\n";
stream << "static const lv_font_fmt_txt_glyph_dsc_t " << output_name << "_glyph_dsc[] =\n{\n";
stream << "\t{.bitmap_index = 0, .adv_w = 0, .box_w = 0, .box_h = 0, .ofs_x = 0, .ofs_y = 0} /* id = 0 reserved */,\n";
const lv_font_fmt_txt_glyph_dsc_t *gdsc = fdsc->glyph_dsc + 1;
for (uint32_t unicodeAct = unicodeFirst; unicodeAct <= unicodeSec; ++unicodeAct) {
stream << "\t{.bitmap_index = " << gdsc->bitmap_index << ", .adv_w = "
<< gdsc->adv_w << ", .box_w = " << gdsc->box_w << ", .box_h = "
<< gdsc->box_h << ", .ofs_x = " << gdsc->ofs_x << ", .ofs_y = "
<< gdsc->ofs_y << "}";
stream << " /* U+" << QString::number(unicodeAct, 16).rightJustified(4, '0') << " \"" << QChar(unicodeAct) << "\" */";
if (unicodeAct != unicodeSec) stream << ",";
stream << "\n";
++gdsc;
}
stream << "};\n\n";
stream << "/*---------------------\n";
stream << " * CHARACTER MAPPING\n";
stream << " *--------------------*/\n\n";
stream << "/* Collect the unicode lists and glyph_id offsets */\n";
stream << "static const lv_font_fmt_txt_cmap_t " << output_name << "_cmaps[] =\n{\n";
stream << "\t{\n";
stream << "\t\t.range_start = " << fdsc->cmaps[0].range_start
<< ", .range_length = " << fdsc->cmaps[0].range_length
<< ", .glyph_id_start = " << fdsc->cmaps[0].glyph_id_start << ",\n";
stream << "\t\t.unicode_list = NULL, .glyph_id_ofs_list = NULL, .list_length = 0, .type = LV_FONT_FMT_TXT_CMAP_FORMAT0_TINY\n";
stream << "\t}\n";
stream << "};\n\n";
stream << "/*--------------------\n";
stream << " * ALL CUSTOM DATA\n";
stream << " *--------------------*/\n\n";
stream << "/* Store all the custom data of the font */\n";
stream << "static lv_font_fmt_txt_dsc_t " << output_name << "_font_dsc = \n{\n";
stream << "\t.glyph_bitmap = " << output_name << "_glyph_bitmap,\n";
stream << "\t.glyph_dsc = " << output_name << "_glyph_dsc,\n";
stream << "\t.cmaps = " << output_name << "_cmaps,\n";
stream << "\t.kern_dsc = NULL,\n\t.kern_scale = 0,\n";
stream << "\t.cmap_num = " << fdsc->cmap_num << ",\n";
stream << "\t.bpp = " << fdsc->bpp << ",\n";
stream << "\t.kern_classes = 0,\n";
stream << "\t.bitmap_format = LV_FONT_FMT_TXT_PLAIN\n};\n\n";
stream << "/*-----------------\n";
stream << " * PUBLIC FONT\n";
stream << " *----------------*/\n\n";
stream << "/* Initialize a public general font descriptor */\n";
stream << "lv_font_t " << output_name << " = \n{\n";
stream << "\t.get_glyph_dsc = lv_font_get_glyph_dsc_fmt_txt, /* Function pointer to get glyph's data */\n";
stream << "\t.get_glyph_bitmap = lv_font_get_bitmap_fmt_txt, /* Function pointer to get glyph's bitmap */\n";
stream << "\t.line_height = " << m_font->line_height << ", /* The maximum line height required by the font */\n";
stream << "\t.base_line = " << m_font->base_line << ", /* Baseline measured from the bottom of the line */\n";
stream << "\t.subpx = LV_FONT_SUBPX_NONE,\n";
stream << "\t.dsc = &" << output_name << "_font_dsc /* The custom font data. Will be accessed by `get_glyph_bitmap/dsc` */\n";
stream << "};\n\n";
stream << "#endif /*#if " << defName << "*/\n";
file.close();
return true;
}
bool LVGLFontData::isCustomFont() const
{
return m_customFont;
}
QString LVGLFontData::fileName() const
{
return m_fileName;
}
QJsonObject LVGLFontData::toJson() const
{
QJsonObject object({{"name", m_name},
{"code", m_codeName},
{"size", m_size}
});
if (m_customFont) {
object.insert("fileName", m_fileName);
lv_font_fmt_txt_dsc_t *fdsc = reinterpret_cast<lv_font_fmt_txt_dsc_t *>(m_font->dsc);
object.insert("bpp", fdsc->bpp);
object.insert("start", int(fdsc->cmaps[0].range_start));
object.insert("end", int(fdsc->cmaps[0].range_start + fdsc->cmaps[0].range_length - 1));
}
return object;
}
uint8_t LVGLFontData::size() const
{
return m_size;
}