diff options
Diffstat (limited to 'tests/pke-test-font.cpp')
| -rw-r--r-- | tests/pke-test-font.cpp | 286 |
1 files changed, 285 insertions, 1 deletions
diff --git a/tests/pke-test-font.cpp b/tests/pke-test-font.cpp index 3f299e4..d475fdd 100644 --- a/tests/pke-test-font.cpp +++ b/tests/pke-test-font.cpp @@ -10,6 +10,10 @@ static pk_membucket *bkt; +float FontType_Inner_LookAheadWordLength(const FontType *const ft, const FontRender *const fr, uint32_t index, float font_glyph_spacing, uint32_t *char_count); +float FontType_Inner_LookAheadLineLength(const FontType *const ft, const FontRender *const fr, const uint32_t index, float font_glyph_spacing, uint32_t *char_count); +float FontType_Inner_LookAheadLineCount(const FontType *const ft, const FontRender *const fr, const uint32_t index, float font_glyph_spacing); + void pke_test_font_setup() { pke_test_stub_init_vulkan(); bkt = pk_mem_bucket_create("pke_test_font", PK_MEM_DEFAULT_BUCKET_SIZE, PK_MEMBUCKET_FLAG_NONE); @@ -93,8 +97,278 @@ int pke_test_font_003() { return 0; } +struct pke_test_font_inner_func_params { + uint32_t index; + uint32_t char_count; + float length; +}; + +int pke_test_font_inner_word_length(const FontType *const ft, const FontRender *const fr, float font_glyph_spacing, pke_test_font_inner_func_params *params, uint32_t n_params) { + float len; + uint32_t u, ii; + for (u = 0; u < n_params; ++u) { + len = FontType_Inner_LookAheadWordLength(ft, fr, params[u].index, font_glyph_spacing, &ii); + PK_TEST_ASSERT_EQ_RET(params[u].char_count, ii); + PK_TEST_ASSERT_NEQ_RET(params[u].length, len); + } + return 0; +} + +int pke_test_font_inner_line_length(const FontType *const ft, const FontRender *const fr, float font_glyph_spacing, pke_test_font_inner_func_params *params, uint32_t n_params) { + float len; + uint32_t u, ii; + for (u = 0; u < n_params; ++u) { + len = FontType_Inner_LookAheadLineLength(ft, fr, params[u].index, font_glyph_spacing, &ii); + PK_TEST_ASSERT_EQ_RET(params[u].char_count, ii); + PK_TEST_ASSERT_NEQ_RET(params[u].length, len); + } + return 0; +} + +int pke_test_font_inner_line_count(const FontType *const ft, const FontRender *const fr, float font_glyph_spacing, pke_test_font_inner_func_params *params, uint32_t n_params) { + float len; + uint32_t u; + for (u = 0; u < n_params; ++u) { + len = FontType_Inner_LookAheadLineCount(ft, fr, params[u].index, font_glyph_spacing); + PK_TEST_ASSERT_EQ_RET(params[u].length, len); + } + return 0; +} + +/* Ensure expected results from LookAhead functions + * Simple + * */ +int pke_test_font_004() { + FontTypeHandle fti{}; + FontTypeRender handle_001{}; + FontType *ft; + FontRender *fr; + FontRenderSettings frs{}; + uint32_t u; + float font_glyph_spacing; + const char *str = "string one"; + + frs.surface_area_size = glm::vec2(300, 300); + + handle_001 = FontType_AddStringRender(fti, std::move(cstring_to_pk_cstr(str)), &frs); + + FontType_Tick(0.f); + + ft = FontType_Get(fti); + fr = FontType_GetFontRender(handle_001); + + PK_TEST_ASSERT_EQ_RET(10, fr->n_glyphs); + + font_glyph_spacing = ft->spacing.em_size * fr->settings.char_scale; + + // word length + { + pke_test_font_inner_func_params params_word_len[5]; + params_word_len[0].index = 0; + params_word_len[0].char_count = 6; + params_word_len[0].length = 0.f; + params_word_len[1].index = 6; + params_word_len[1].char_count = 0; + params_word_len[1].length = 1.f; + params_word_len[2].index = 7; + params_word_len[2].char_count = 3; + params_word_len[2].length = 0.f; + params_word_len[3].index = 9; + params_word_len[3].char_count = 1; + params_word_len[3].length = 0.f; + params_word_len[4].index = 10; + params_word_len[4].char_count = 0; + params_word_len[4].length = 1.f; + + if ((u = pke_test_font_inner_word_length(ft, fr, font_glyph_spacing, params_word_len, 5)), u != 0) { + return u; + } + } + + // line length + { + pke_test_font_inner_func_params params_line_len[5]; + params_line_len[0].index = 0; + params_line_len[0].char_count = 10; + params_line_len[0].length = 0.f; + params_line_len[1].index = 6; + params_line_len[1].char_count = 4; + params_line_len[1].length = 0.f; + params_line_len[2].index = 7; + params_line_len[2].char_count = 3; + params_line_len[2].length = 0.f; + params_line_len[3].index = 9; + params_line_len[3].char_count = 1; + params_line_len[3].length = 0.f; + params_line_len[4].index = 10; + params_line_len[4].char_count = 0; + params_line_len[4].length = 1.f; + + if ((u = pke_test_font_inner_line_length(ft, fr, font_glyph_spacing, params_line_len, 5)), u != 0) { + return u; + } + } + + // line count + { + pke_test_font_inner_func_params params_line_count[5]; + params_line_count[0].index = 0; + params_line_count[0].length = 1.f; + params_line_count[1].index = 6; + params_line_count[1].length = 1.f; + params_line_count[2].index = 7; + params_line_count[2].length = 1.f; + params_line_count[3].index = 9; + params_line_count[3].length = 1.f; + params_line_count[4].index = 10; + params_line_count[4].length = 0.f; + + if ((u = pke_test_font_inner_line_count(ft, fr, font_glyph_spacing, params_line_count, 5)), u != 0) { + return u; + } + } + + return 0; +} + +/* Ensure expected results from LookAhead functions + * multi-line + * */ +int pke_test_font_005() { + FontTypeHandle fti{}; + FontTypeRender handle_001{}; + FontType *ft; + FontRender *fr; + FontRenderSettings frs{}; + uint32_t u; + float font_glyph_spacing; + const char *str = "line one\nline_two\r\nline 3"; + + frs.surface_area_size = glm::vec2(300, 300); + + handle_001 = FontType_AddStringRender(fti, std::move(cstring_to_pk_cstr(str)), &frs); + + FontType_Tick(0.f); + + ft = FontType_Get(fti); + fr = FontType_GetFontRender(handle_001); + + PK_TEST_ASSERT_EQ_RET(25, fr->n_glyphs); + + font_glyph_spacing = ft->spacing.em_size * fr->settings.char_scale; + + // word length + { + pke_test_font_inner_func_params params_word_len[10]; + params_word_len[0].index = 0; + params_word_len[0].char_count = 4; + params_word_len[0].length = 0.f; + params_word_len[1].index = 4; + params_word_len[1].char_count = 0; + params_word_len[1].length = 1.f; + params_word_len[2].index = 5; + params_word_len[2].char_count = 3; + params_word_len[2].length = 0.f; + params_word_len[3].index = 8; + params_word_len[3].char_count = 0; + params_word_len[3].length = 1.f; + params_word_len[4].index = 9; + params_word_len[4].char_count = 8; + params_word_len[4].length = 0.f; + params_word_len[5].index = 17; + params_word_len[5].char_count = 0; + params_word_len[5].length = 1.f; + params_word_len[6].index = 18; + params_word_len[6].char_count = 0; + params_word_len[6].length = 1.f; + params_word_len[7].index = 19; + params_word_len[7].char_count = 4; + params_word_len[7].length = 0.f; + params_word_len[8].index = 23; + params_word_len[8].char_count = 0; + params_word_len[8].length = 1.f; + params_word_len[9].index = 24; + params_word_len[9].char_count = 1; + params_word_len[9].length = 0.f; + + if ((u = pke_test_font_inner_word_length(ft, fr, font_glyph_spacing, params_word_len, 10)), u != 0) { + return u; + } + } + + // line length + { + pke_test_font_inner_func_params params_line_len[10]; + params_line_len[0].index = 0; + params_line_len[0].char_count = 8; + params_line_len[0].length = 0.f; + params_line_len[1].index = 4; + params_line_len[1].char_count = 4; + params_line_len[1].length = 0.f; + params_line_len[2].index = 5; + params_line_len[2].char_count = 3; + params_line_len[2].length = 0.f; + params_line_len[3].index = 8; + params_line_len[3].char_count = 0; + params_line_len[3].length = 1.f; + params_line_len[4].index = 9; + params_line_len[4].char_count = 8; + params_line_len[4].length = 0.f; + params_line_len[5].index = 17; + params_line_len[5].char_count = 0; + params_line_len[5].length = 1.f; + params_line_len[6].index = 18; + params_line_len[6].char_count = 0; + params_line_len[6].length = 1.f; + params_line_len[7].index = 19; + params_line_len[7].char_count = 6; + params_line_len[7].length = 0.f; + params_line_len[8].index = 23; + params_line_len[8].char_count = 2; + params_line_len[8].length = 0.f; + params_line_len[9].index = 24; + params_line_len[9].char_count = 1; + params_line_len[9].length = 0.f; + + if ((u = pke_test_font_inner_line_length(ft, fr, font_glyph_spacing, params_line_len, 10)), u != 0) { + return u; + } + } + + // line count + { + pke_test_font_inner_func_params params_line_count[10]; + params_line_count[0].index = 0; + params_line_count[0].length = 3.f; + params_line_count[1].index = 4; + params_line_count[1].length = 3.f; + params_line_count[2].index = 5; + params_line_count[2].length = 3.f; + params_line_count[3].index = 8; + params_line_count[3].length = 3.f; + params_line_count[4].index = 9; + params_line_count[4].length = 2.f; + params_line_count[5].index = 17; + params_line_count[5].length = 2.f; + params_line_count[6].index = 18; + params_line_count[6].length = 2.f; + params_line_count[7].index = 19; + params_line_count[7].length = 1.f; + params_line_count[8].index = 23; + params_line_count[8].length = 1.f; + params_line_count[9].index = 24; + params_line_count[9].length = 1.f; + + if ((u = pke_test_font_inner_line_count(ft, fr, font_glyph_spacing, params_line_count, 10)), u != 0) { + return u; + } + } + + return 0; +} + struct pk_test_group *pke_test_font_get_group() { - static const uint64_t test_count = 3; + static const uint64_t test_count = 5; static struct pk_test tests[test_count] = { { .title = "test 001", @@ -111,6 +385,16 @@ struct pk_test_group *pke_test_font_get_group() { .func = pke_test_font_003, .expected_result = 0, }, + { + .title = "test 004", + .func = pke_test_font_004, + .expected_result = 0, + }, + { + .title = "test 005", + .func = pke_test_font_005, + .expected_result = 0, + }, }; static struct pk_test_group group = {}; group.title = "font test"; |
