awtk/tools/font_gen/font_gen.c

107 lines
2.7 KiB
C
Raw Normal View History

2018-02-21 19:36:38 +08:00
/**
* File: font_gen.c
* Author: Li XianJing <xianjimli@hotmail.com>
* Brief: bitmap font generator
*
2018-05-08 10:22:32 +08:00
* Copyright (c) 2018 - 2018 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-02-21 19:36:38 +08:00
#include "base/mem.h"
#include "base/utf8.h"
2018-02-21 19:36:38 +08:00
#include "common/utils.h"
#include "font/font_stb.h"
2018-03-18 11:29:31 +08:00
#include "font/font_bitmap.h"
#include "font_gen/font_gen.h"
#include "base/resource_manager.h"
2018-02-21 19:36:38 +08:00
#define MAX_CHARS 100 * 1024
#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-02-21 19:36:38 +08:00
char name[NAME_LEN + 1];
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
filename_to_name(output_filename, name, sizeof(name));
2018-03-18 11:29:31 +08:00
output_res_c_source(output_filename, RESOURCE_TYPE_FONT, RESOURCE_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];
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-03-18 11:29:31 +08:00
header->font_size = font_size;
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;
}
2018-02-21 19:36:38 +08:00
printf("%d/%d: 0x%04x\n", i, size, c);
2018-03-18 11:29:31 +08:00
if (font_find_glyph(font, c, &g, font_size) == RET_OK) {
2018-02-21 19:36:38 +08:00
uint32_t data_size = g.w * g.h;
return_value_if_fail(buff_size > (iter->offset + data_size + 4), 0);
save_uint8(p, g.x);
save_uint8(p, g.y);
save_uint8(p, g.w);
save_uint8(p, g.h);
memcpy(p, g.data, data_size);
p += data_size;
} else if (c > 32) {
printf("not found %d\n", c);
exit(0);
} else {
iter->offset = 0;
}
}
return p - output_buff;
}