diff --git a/test/api/Makefile.am b/test/api/Makefile.am index efeecb4e0..40e9b9d51 100644 --- a/test/api/Makefile.am +++ b/test/api/Makefile.am @@ -43,6 +43,7 @@ TEST_PROGS = \ test-cplusplus \ test-common \ test-draw \ + test-draw-varc \ test-extents \ test-font \ test-font-scale \ diff --git a/test/fuzzing/fonts/varc-6868.ttf b/test/api/fonts/varc-6868.ttf similarity index 100% rename from test/fuzzing/fonts/varc-6868.ttf rename to test/api/fonts/varc-6868.ttf diff --git a/test/fuzzing/fonts/varc-ac00-ac01.ttf b/test/api/fonts/varc-ac00-ac01.ttf similarity index 100% rename from test/fuzzing/fonts/varc-ac00-ac01.ttf rename to test/api/fonts/varc-ac00-ac01.ttf diff --git a/test/fuzzing/fonts/varc-ac01-conditional.ttf b/test/api/fonts/varc-ac01-conditional.ttf similarity index 100% rename from test/fuzzing/fonts/varc-ac01-conditional.ttf rename to test/api/fonts/varc-ac01-conditional.ttf diff --git a/test/api/meson.build b/test/api/meson.build index 64d7797ef..76126a8de 100644 --- a/test/api/meson.build +++ b/test/api/meson.build @@ -16,6 +16,7 @@ tests = [ 'test-cplusplus.cc', 'test-common.c', 'test-draw.c', + 'test-draw-varc.c', 'test-extents.c', 'test-font.c', 'test-font-scale.c', diff --git a/test/api/test-draw-varc.c b/test/api/test-draw-varc.c new file mode 100644 index 000000000..f4e2d79ae --- /dev/null +++ b/test/api/test-draw-varc.c @@ -0,0 +1,268 @@ +/* + * Copyright © 2024 Behdad Esfahbod + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + */ + +#include "hb-test.h" +#include + +#include + +typedef struct draw_data_t +{ + unsigned move_to_count; + unsigned line_to_count; + unsigned quad_to_count; + unsigned cubic_to_count; + unsigned close_path_count; +} draw_data_t; + +/* Our modified itoa, why not using libc's? it is going to be used + in harfbuzzjs where libc isn't available */ +static void _hb_reverse (char *buf, unsigned int len) +{ + unsigned start = 0, end = len - 1; + while (start < end) + { + char c = buf[end]; + buf[end] = buf[start]; + buf[start] = c; + start++; end--; + } +} +static unsigned _hb_itoa (float fnum, char *buf) +{ + int32_t num = (int32_t) floorf (fnum + .5f); + unsigned int i = 0; + hb_bool_t is_negative = num < 0; + if (is_negative) num = -num; + do + { + buf[i++] = '0' + num % 10; + num /= 10; + } while (num); + if (is_negative) buf[i++] = '-'; + _hb_reverse (buf, i); + buf[i] = '\0'; + return i; +} + +#define ITOA_BUF_SIZE 12 // 10 digits in int32, 1 for negative sign, 1 for \0 + +static void +test_itoa (void) +{ + char s[] = "12345"; + _hb_reverse (s, 5); + g_assert_cmpmem (s, 5, "54321", 5); + + { + unsigned num = 12345; + char buf[ITOA_BUF_SIZE]; + unsigned len = _hb_itoa (num, buf); + g_assert_cmpmem (buf, len, "12345", 5); + } + + { + unsigned num = 3152; + char buf[ITOA_BUF_SIZE]; + unsigned len = _hb_itoa (num, buf); + g_assert_cmpmem (buf, len, "3152", 4); + } + + { + int num = -6457; + char buf[ITOA_BUF_SIZE]; + unsigned len = _hb_itoa (num, buf); + g_assert_cmpmem (buf, len, "-6457", 5); + } +} + +static void +move_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data, + hb_draw_state_t *st, + float to_x, float to_y, + void *user_data) +{ + draw_data->move_to_count++; +} + +static void +line_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data, + hb_draw_state_t *st, + float to_x, float to_y, + void *user_data) +{ + draw_data->line_to_count++; +} + +static void +quadratic_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data, + hb_draw_state_t *st, + float control_x, float control_y, + float to_x, float to_y, + void *user_data) +{ + draw_data->quad_to_count++; +} + +static void +cubic_to (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data, + hb_draw_state_t *st, + float control1_x, float control1_y, + float control2_x, float control2_y, + float to_x, float to_y, + void *user_data) +{ + draw_data->cubic_to_count++; +} + +static void +close_path (hb_draw_funcs_t *dfuncs, draw_data_t *draw_data, + hb_draw_state_t *st, + void *user_data) +{ + draw_data->close_path_count++; +} + +static hb_draw_funcs_t *funcs; + +static void +test_hb_draw_varc_simple_hangul (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/varc-ac00-ac01.ttf"); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + + draw_data_t draw_data0 = {0}; + draw_data_t draw_data;; + unsigned gid = 0; + + hb_font_get_nominal_glyph (font, 0xAC00u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 3); + + hb_font_get_nominal_glyph (font, 0xAC01u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 4); + + hb_variation_t var; + var.tag = HB_TAG ('w','g','h','t'); + var.value = 800; + hb_font_set_variations (font, &var, 1); + + hb_font_get_nominal_glyph (font, 0xAC00u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 3); + + hb_font_get_nominal_glyph (font, 0xAC01u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 4); + + hb_font_destroy (font); +} + +static void +test_hb_draw_varc_simple_hanzi (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/varc-6868.ttf"); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + + draw_data_t draw_data0 = {0}; + draw_data_t draw_data;; + unsigned gid = 0; + + hb_font_get_nominal_glyph (font, 0x6868u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 11); + + hb_variation_t var; + var.tag = HB_TAG ('w','g','h','t'); + var.value = 800; + hb_font_set_variations (font, &var, 1); + + hb_font_get_nominal_glyph (font, 0x6868u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 11); + + hb_font_destroy (font); +} + +static void +test_hb_draw_varc_conditional (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/varc-ac01-conditional.ttf"); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + + draw_data_t draw_data0 = {0}; + draw_data_t draw_data;; + unsigned gid = 0; + + hb_font_get_nominal_glyph (font, 0xAC01u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 2); + + hb_variation_t var; + var.tag = HB_TAG ('w','g','h','t'); + var.value = 800; + hb_font_set_variations (font, &var, 1); + + hb_font_get_nominal_glyph (font, 0xAC01u, &gid); + draw_data = draw_data0; + hb_font_draw_glyph (font, gid, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 4); + + hb_font_destroy (font); +} + +int +main (int argc, char **argv) +{ + funcs = hb_draw_funcs_create (); + hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to, NULL, NULL); + hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to, NULL, NULL); + hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) quadratic_to, NULL, NULL); + hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to, NULL, NULL); + hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path, NULL, NULL); + hb_draw_funcs_make_immutable (funcs); + + hb_test_init (&argc, &argv); + hb_test_add (test_itoa); +#ifdef HB_EXPERIMENTAL_API + hb_test_add (test_hb_draw_varc_simple_hangul); + hb_test_add (test_hb_draw_varc_simple_hanzi); + hb_test_add (test_hb_draw_varc_conditional); +#endif + unsigned result = hb_test_run (); + + hb_draw_funcs_destroy (funcs); + return result; +}