2018-02-21 19:36:38 +08:00
|
|
|
/**
|
|
|
|
* File: font_gen.c
|
2018-05-15 09:31:58 +08:00
|
|
|
* Author: AWTK Develop Team
|
2018-02-21 19:36:38 +08:00
|
|
|
* Brief: bitmap font generator
|
|
|
|
*
|
2019-01-07 10:58:36 +08:00
|
|
|
* Copyright (c) 2018 - 2019 Guangzhou ZHIYUAN Electronics Co.,Ltd.
|
2018-02-21 19:36:38 +08:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* License file for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* History:
|
|
|
|
* ================================================================
|
|
|
|
* 2018-01-21 Li XianJing <xianjimli@hotmail.com> created
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-04-13 10:25:58 +08:00
|
|
|
#include <wctype.h>
|
2018-12-15 17:22:05 +08:00
|
|
|
#include "tkc/mem.h"
|
|
|
|
#include "tkc/utf8.h"
|
|
|
|
#include "tkc/buffer.h"
|
2019-08-11 16:52:45 +08:00
|
|
|
#include "base/bitmap.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
#include "common/utils.h"
|
2018-03-18 11:29:31 +08:00
|
|
|
#include "font_gen/font_gen.h"
|
2018-08-24 07:45:37 +08:00
|
|
|
#include "base/assets_manager.h"
|
2018-10-19 18:10:58 +08:00
|
|
|
#include "font_loader/font_loader_stb.h"
|
|
|
|
#include "font_loader/font_loader_bitmap.h"
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
#define MAX_CHARS 100 * 1024
|
2018-02-22 11:37:13 +08:00
|
|
|
#define MAX_BUFF_SIZE 1 * 1024 * 1024
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
static int char_cmp(const void* a, const void* b) {
|
|
|
|
wchar_t c1 = *(wchar_t*)a;
|
|
|
|
wchar_t c2 = *(wchar_t*)b;
|
|
|
|
|
|
|
|
return c1 - c2;
|
|
|
|
}
|
|
|
|
|
2018-03-18 11:29:31 +08:00
|
|
|
ret_t font_gen(font_t* font, uint16_t font_size, const char* str, const char* output_filename) {
|
2018-04-29 16:51:54 +08:00
|
|
|
uint8_t* buff = (uint8_t*)TKMEM_ALLOC(MAX_BUFF_SIZE);
|
2018-03-18 11:29:31 +08:00
|
|
|
uint32_t size = font_gen_buff(font, font_size, str, buff, MAX_BUFF_SIZE);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2018-08-24 07:45:37 +08:00
|
|
|
output_res_c_source(output_filename, ASSET_TYPE_FONT, ASSET_TYPE_FONT_BMP, buff, size);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
2018-04-29 16:51:54 +08:00
|
|
|
TKMEM_FREE(buff);
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
return RET_OK;
|
|
|
|
}
|
|
|
|
|
2018-03-18 17:02:10 +08:00
|
|
|
uint32_t font_gen_buff(font_t* font, uint16_t font_size, const char* str, uint8_t* output_buff,
|
|
|
|
uint32_t buff_size) {
|
2018-02-21 19:36:38 +08:00
|
|
|
int i = 0;
|
|
|
|
glyph_t g;
|
|
|
|
int size = 0;
|
|
|
|
uint8_t* p = NULL;
|
|
|
|
wchar_t wstr[MAX_CHARS];
|
2019-11-04 11:44:36 +08:00
|
|
|
font_vmetrics_t vmetrics = font_get_vmetrics(font, font_size);
|
2018-02-21 19:36:38 +08:00
|
|
|
font_bitmap_header_t* header = (font_bitmap_header_t*)output_buff;
|
|
|
|
|
|
|
|
utf8_to_utf16(str, wstr, MAX_CHARS);
|
|
|
|
size = wcslen(wstr);
|
|
|
|
|
|
|
|
qsort(wstr, size, sizeof(wchar_t), char_cmp);
|
|
|
|
size = unique(wstr, size);
|
|
|
|
|
|
|
|
header->char_nr = size;
|
2018-07-29 11:43:31 +08:00
|
|
|
header->font_size = (uint8_t)font_size;
|
2019-11-04 11:44:36 +08:00
|
|
|
header->ascent = vmetrics.ascent;
|
|
|
|
header->descent = vmetrics.descent;
|
|
|
|
header->line_gap = vmetrics.line_gap;
|
2018-02-21 19:36:38 +08:00
|
|
|
|
|
|
|
p = (uint8_t*)(header->index + size);
|
|
|
|
return_value_if_fail(buff_size > 512, 0);
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
wchar_t c = wstr[i];
|
|
|
|
font_bitmap_index_t* iter = header->index + i;
|
|
|
|
|
|
|
|
iter->c = c;
|
|
|
|
iter->offset = p - output_buff;
|
2018-04-13 10:25:58 +08:00
|
|
|
|
2018-04-15 17:58:24 +08:00
|
|
|
if (iswspace(c)) {
|
2018-04-13 10:25:58 +08:00
|
|
|
continue;
|
|
|
|
}
|
2019-08-11 16:52:45 +08:00
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
printf("%d/%d: 0x%04x\n", i, size, c);
|
2018-12-28 17:43:54 +08:00
|
|
|
if (font_get_glyph(font, c, font_size, &g) == RET_OK) {
|
2019-08-11 16:52:45 +08:00
|
|
|
uint32_t data_size = (g.pitch ? g.pitch : g.w) * g.h;
|
2018-02-21 19:36:38 +08:00
|
|
|
return_value_if_fail(buff_size > (iter->offset + data_size + 4), 0);
|
|
|
|
|
2019-10-15 17:54:52 +08:00
|
|
|
save_uint16(p, g.x);
|
|
|
|
save_uint16(p, g.y);
|
|
|
|
save_uint16(p, g.w);
|
|
|
|
save_uint16(p, g.h);
|
2019-08-11 16:52:45 +08:00
|
|
|
save_uint16(p, g.advance);
|
|
|
|
save_uint8(p, g.format);
|
|
|
|
save_uint8(p, g.pitch);
|
2018-12-20 14:05:00 +08:00
|
|
|
|
2018-02-21 19:36:38 +08:00
|
|
|
memcpy(p, g.data, data_size);
|
|
|
|
p += data_size;
|
2019-08-11 16:52:45 +08:00
|
|
|
|
|
|
|
if (g.format == GLYPH_FMT_MONO) {
|
|
|
|
bitmap_mono_dump(g.data, g.w, g.h);
|
|
|
|
}
|
|
|
|
|
2019-09-05 10:05:29 +08:00
|
|
|
} else if (c > 32 && c != 65279) {
|
2018-02-21 19:36:38 +08:00
|
|
|
printf("not found %d\n", c);
|
|
|
|
exit(0);
|
|
|
|
} else {
|
|
|
|
iter->offset = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p - output_buff;
|
|
|
|
}
|