test-draw-varc.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright © 2024 Behdad Esfahbod
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. */
  24. #include "hb-test.h"
  25. #include <math.h>
  26. #include <hb.h>
  27. typedef struct draw_data_t
  28. {
  29. unsigned move_to_count;
  30. unsigned line_to_count;
  31. unsigned quad_to_count;
  32. unsigned cubic_to_count;
  33. unsigned close_path_count;
  34. } draw_data_t;
  35. /* Our modified itoa, why not using libc's? it is going to be used
  36. in harfbuzzjs where libc isn't available */
  37. static void _hb_reverse (char *buf, unsigned int len)
  38. {
  39. unsigned start = 0, end = len - 1;
  40. while (start < end)
  41. {
  42. char c = buf[end];
  43. buf[end] = buf[start];
  44. buf[start] = c;
  45. start++; end--;
  46. }
  47. }
  48. static unsigned _hb_itoa (float fnum, char *buf)
  49. {
  50. int32_t num = (int32_t) floorf (fnum + .5f);
  51. unsigned int i = 0;
  52. hb_bool_t is_negative = num < 0;
  53. if (is_negative) num = -num;
  54. do
  55. {
  56. buf[i++] = '0' + num % 10;
  57. num /= 10;
  58. } while (num);
  59. if (is_negative) buf[i++] = '-';
  60. _hb_reverse (buf, i);
  61. buf[i] = '\0';
  62. return i;
  63. }
  64. #define ITOA_BUF_SIZE 12 // 10 digits in int32, 1 for negative sign, 1 for \0
  65. static void
  66. test_itoa (void)
  67. {
  68. char s[] = "12345";
  69. _hb_reverse (s, 5);
  70. g_assert_cmpmem (s, 5, "54321", 5);
  71. {
  72. unsigned num = 12345;
  73. char buf[ITOA_BUF_SIZE];
  74. unsigned len = _hb_itoa (num, buf);
  75. g_assert_cmpmem (buf, len, "12345", 5);
  76. }
  77. {
  78. unsigned num = 3152;
  79. char buf[ITOA_BUF_SIZE];
  80. unsigned len = _hb_itoa (num, buf);
  81. g_assert_cmpmem (buf, len, "3152", 4);
  82. }
  83. {
  84. int num = -6457;
  85. char buf[ITOA_BUF_SIZE];
  86. unsigned len = _hb_itoa (num, buf);
  87. g_assert_cmpmem (buf, len, "-6457", 5);
  88. }
  89. }
  90. static void
  91. move_to (HB_UNUSED hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
  92. HB_UNUSED hb_draw_state_t *st,
  93. HB_UNUSED float to_x, HB_UNUSED float to_y,
  94. HB_UNUSED void *user_data)
  95. {
  96. draw_data->move_to_count++;
  97. }
  98. static void
  99. line_to (HB_UNUSED hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
  100. HB_UNUSED hb_draw_state_t *st,
  101. HB_UNUSED float to_x, HB_UNUSED float to_y,
  102. HB_UNUSED void *user_data)
  103. {
  104. draw_data->line_to_count++;
  105. }
  106. static void
  107. quadratic_to (HB_UNUSED hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
  108. HB_UNUSED hb_draw_state_t *st,
  109. HB_UNUSED float control_x, HB_UNUSED float control_y,
  110. HB_UNUSED float to_x, HB_UNUSED float to_y,
  111. HB_UNUSED void *user_data)
  112. {
  113. draw_data->quad_to_count++;
  114. }
  115. static void
  116. cubic_to (HB_UNUSED hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
  117. HB_UNUSED hb_draw_state_t *st,
  118. HB_UNUSED float control1_x, HB_UNUSED float control1_y,
  119. HB_UNUSED float control2_x, HB_UNUSED float control2_y,
  120. HB_UNUSED float to_x, HB_UNUSED float to_y,
  121. HB_UNUSED void *user_data)
  122. {
  123. draw_data->cubic_to_count++;
  124. }
  125. static void
  126. close_path (HB_UNUSED hb_draw_funcs_t *dfuncs, draw_data_t *draw_data,
  127. HB_UNUSED hb_draw_state_t *st,
  128. HB_UNUSED void *user_data)
  129. {
  130. draw_data->close_path_count++;
  131. }
  132. static hb_draw_funcs_t *funcs;
  133. #ifdef HB_EXPERIMENTAL_API
  134. static void
  135. test_hb_draw_varc_simple_hangul (void)
  136. {
  137. hb_face_t *face = hb_test_open_font_file ("fonts/varc-ac00-ac01.ttf");
  138. hb_font_t *font = hb_font_create (face);
  139. hb_face_destroy (face);
  140. draw_data_t draw_data0 = {0};
  141. draw_data_t draw_data;;
  142. unsigned gid = 0;
  143. hb_font_get_nominal_glyph (font, 0xAC00u, &gid);
  144. draw_data = draw_data0;
  145. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  146. g_assert_cmpuint (draw_data.move_to_count, ==, 3);
  147. hb_font_get_nominal_glyph (font, 0xAC01u, &gid);
  148. draw_data = draw_data0;
  149. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  150. g_assert_cmpuint (draw_data.move_to_count, ==, 4);
  151. hb_variation_t var;
  152. var.tag = HB_TAG ('w','g','h','t');
  153. var.value = 800;
  154. hb_font_set_variations (font, &var, 1);
  155. hb_font_get_nominal_glyph (font, 0xAC00u, &gid);
  156. draw_data = draw_data0;
  157. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  158. g_assert_cmpuint (draw_data.move_to_count, ==, 3);
  159. hb_font_get_nominal_glyph (font, 0xAC01u, &gid);
  160. draw_data = draw_data0;
  161. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  162. g_assert_cmpuint (draw_data.move_to_count, ==, 4);
  163. hb_font_destroy (font);
  164. }
  165. static void
  166. test_hb_draw_varc_simple_hanzi (void)
  167. {
  168. hb_face_t *face = hb_test_open_font_file ("fonts/varc-6868.ttf");
  169. hb_font_t *font = hb_font_create (face);
  170. hb_face_destroy (face);
  171. draw_data_t draw_data0 = {0};
  172. draw_data_t draw_data;;
  173. unsigned gid = 0;
  174. hb_font_get_nominal_glyph (font, 0x6868u, &gid);
  175. draw_data = draw_data0;
  176. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  177. g_assert_cmpuint (draw_data.move_to_count, ==, 11);
  178. hb_variation_t var;
  179. var.tag = HB_TAG ('w','g','h','t');
  180. var.value = 800;
  181. hb_font_set_variations (font, &var, 1);
  182. hb_font_get_nominal_glyph (font, 0x6868u, &gid);
  183. draw_data = draw_data0;
  184. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  185. g_assert_cmpuint (draw_data.move_to_count, ==, 11);
  186. hb_font_destroy (font);
  187. }
  188. static void
  189. test_hb_draw_varc_conditional (void)
  190. {
  191. hb_face_t *face = hb_test_open_font_file ("fonts/varc-ac01-conditional.ttf");
  192. hb_font_t *font = hb_font_create (face);
  193. hb_face_destroy (face);
  194. draw_data_t draw_data0 = {0};
  195. draw_data_t draw_data;;
  196. unsigned gid = 0;
  197. hb_font_get_nominal_glyph (font, 0xAC01u, &gid);
  198. draw_data = draw_data0;
  199. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  200. g_assert_cmpuint (draw_data.move_to_count, ==, 2);
  201. hb_variation_t var;
  202. var.tag = HB_TAG ('w','g','h','t');
  203. var.value = 800;
  204. hb_font_set_variations (font, &var, 1);
  205. hb_font_get_nominal_glyph (font, 0xAC01u, &gid);
  206. draw_data = draw_data0;
  207. hb_font_draw_glyph (font, gid, funcs, &draw_data);
  208. g_assert_cmpuint (draw_data.move_to_count, ==, 4);
  209. hb_font_destroy (font);
  210. }
  211. #endif
  212. int
  213. main (int argc, char **argv)
  214. {
  215. funcs = hb_draw_funcs_create ();
  216. hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to, NULL, NULL);
  217. hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to, NULL, NULL);
  218. hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) quadratic_to, NULL, NULL);
  219. hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to, NULL, NULL);
  220. hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path, NULL, NULL);
  221. hb_draw_funcs_make_immutable (funcs);
  222. hb_test_init (&argc, &argv);
  223. hb_test_add (test_itoa);
  224. #ifdef HB_EXPERIMENTAL_API
  225. hb_test_add (test_hb_draw_varc_simple_hangul);
  226. hb_test_add (test_hb_draw_varc_simple_hanzi);
  227. hb_test_add (test_hb_draw_varc_conditional);
  228. #endif
  229. unsigned result = hb_test_run ();
  230. hb_draw_funcs_destroy (funcs);
  231. return result;
  232. }