awtk/tests/font_gen_test.cc

103 lines
3.1 KiB
C++
Raw Normal View History

2018-06-27 13:46:42 +08:00

2018-02-21 19:36:38 +08:00
#include "base/font.h"
#include "tkc/mem.h"
2018-02-21 19:36:38 +08:00
#include "base/widget.h"
#include "tools/common/utils.h"
#include "tools/font_gen/font_gen.h"
2018-12-26 18:10:38 +08:00
#include "font_loader/font_loader_truetype.h"
2018-10-19 18:10:58 +08:00
#include "font_loader/font_loader_bitmap.h"
#include "gtest/gtest.h"
#include <stdlib.h>
2018-02-21 19:36:38 +08:00
#include <string>
using std::string;
2019-10-19 07:42:18 +08:00
#define TTF_FILE TK_ROOT "/tests/testdata/assets/default/raw/fonts/starthere.ttf"
2018-02-21 19:36:38 +08:00
#define BUFF_SIZE 1024 * 1024
2020-05-29 22:20:24 +08:00
#if defined(WITH_FT_FONT) || defined(WITH_STB_FONT)
2018-02-21 19:36:38 +08:00
TEST(FontGen, basic) {
uint32_t size = 0;
2020-06-28 16:55:48 +08:00
uint16_t font_size = 50;
2018-04-29 16:51:54 +08:00
uint8_t* bmp_buff = (uint8_t*)TKMEM_ALLOC(BUFF_SIZE);
2018-02-21 19:36:38 +08:00
uint8_t* ttf_buff = (uint8_t*)read_file(TTF_FILE, &size);
2018-12-26 18:10:38 +08:00
font_t* ttf_font = font_truetype_create("default", ttf_buff, size);
2020-06-28 16:55:48 +08:00
const char* str = "helloworld HELLOWORLD 1243541 helloworld HELLOWORLD 1243541";
2020-07-09 17:32:41 +08:00
wbuffer_t wbuffer;
wbuffer_init(&wbuffer, bmp_buff, BUFF_SIZE);
2021-12-19 11:36:40 +08:00
uint32_t ret = font_gen_buff(ttf_font, font_size, GLYPH_FMT_ALPHA, str, &wbuffer);
2018-03-18 11:29:31 +08:00
font_t* bmp_font = font_bitmap_create("default", bmp_buff, ret);
2018-02-21 19:36:38 +08:00
for (uint32_t i = 0; str[i]; i++) {
glyph_t g1;
glyph_t g2;
char c = str[i];
2018-12-28 17:43:54 +08:00
ASSERT_EQ(font_get_glyph(ttf_font, c, font_size, &g1), RET_OK);
ASSERT_EQ(font_get_glyph(bmp_font, c, font_size, &g2), RET_OK);
2018-02-21 19:36:38 +08:00
ASSERT_EQ(g1.x, g2.x);
ASSERT_EQ(g1.y, g2.y);
ASSERT_EQ(g1.w, g2.w);
ASSERT_EQ(g1.h, g2.h);
2021-12-19 11:36:40 +08:00
ASSERT_EQ(memcmp(g1.data, g2.data, g1.pitch * g1.h), 0);
2018-02-21 19:36:38 +08:00
}
ASSERT_EQ(ret > 0, true);
font_destroy(ttf_font);
font_destroy(bmp_font);
2020-07-09 17:32:41 +08:00
wbuffer_deinit(&wbuffer);
2018-04-29 16:51:54 +08:00
TKMEM_FREE(bmp_buff);
TKMEM_FREE(ttf_buff);
2018-02-21 19:36:38 +08:00
}
TEST(FontGen, expand) {
str_t str;
str_init(&str, 100);
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[0-9]][[a-z]][[A-Z]][]!@#$", &str),
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[]!@#$");
#ifndef WIN32
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[0x4e2d-0x4e2f]]", &str), "中丮丯");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[0X4e2d-0X4e2f]]", &str), "中丮丯");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[0X4E2D-0X4E2F]]", &str), "中丮丯");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[20013-20015]]", &str), "中丮丯");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("123[[20013-20015]]abc", &str), "123中丮丯abc");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[0-9]][[20013-20015]][[a-c]]", &str), "0123456789中丮丯abc");
#endif
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("[[48-51]]", &str), "0123");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("abc[[d-d]]", &str), "abcd");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("abc[[d-e]]", &str), "abcde");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("abc[[", &str), "abc");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("abc[[]]", &str), "abc");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("abc[[48-51]]", &str), "abc0123");
str_clear(&str);
2021-12-21 10:16:05 +08:00
ASSERT_STREQ(font_gen_expand_text("abc[[48-51]][[d-e]]", &str), "abc0123de");
str_reset(&str);
}
2020-06-05 17:16:19 +08:00
#endif /**/