test-shape.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright © 2011 Google, Inc.
  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. * Google Author(s): Behdad Esfahbod
  25. */
  26. #include "hb-test.h"
  27. /* Unit tests for hb-shape.h */
  28. /*
  29. * This test provides a framework to test aspects of hb_shape() that are
  30. * font-independent. Please add tests for any feature that fits that
  31. * description.
  32. */
  33. /* TODO Make this test data-driven and add some real test data */
  34. /* TODO Test positions too. And test non-native direction. Test commit 2e18c6dbdfb */
  35. static const char test_data[] = "test\0data";
  36. static hb_position_t
  37. glyph_h_advance_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
  38. hb_codepoint_t glyph,
  39. void *user_data HB_UNUSED)
  40. {
  41. switch (glyph) {
  42. case 1: return 10;
  43. case 2: return 6;
  44. case 3: return 5;
  45. }
  46. return 0;
  47. }
  48. static hb_bool_t
  49. glyph_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
  50. hb_codepoint_t unicode,
  51. hb_codepoint_t *glyph,
  52. void *user_data HB_UNUSED)
  53. {
  54. switch (unicode) {
  55. case 'T': *glyph = 1; return TRUE;
  56. case 'e': *glyph = 2; return TRUE;
  57. case 's': *glyph = 3; return TRUE;
  58. }
  59. return FALSE;
  60. }
  61. static const char TesT[] = "TesT";
  62. static void
  63. test_font (hb_font_t *font)
  64. {
  65. hb_buffer_t *buffer;
  66. unsigned int len;
  67. hb_glyph_info_t *glyphs;
  68. hb_glyph_position_t *positions;
  69. buffer = hb_buffer_create ();
  70. hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
  71. hb_buffer_add_utf8 (buffer, TesT, 4, 0, 4);
  72. hb_shape (font, buffer, NULL, 0);
  73. len = hb_buffer_get_length (buffer);
  74. glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
  75. positions = hb_buffer_get_glyph_positions (buffer, NULL);
  76. {
  77. const hb_codepoint_t output_glyphs[] = {1, 2, 3, 1};
  78. const hb_position_t output_x_advances[] = {10, 6, 5, 10};
  79. const hb_position_t output_x_offsets[] = {0, 0, 0, 0};
  80. unsigned int i;
  81. g_assert_cmpint (len, ==, 4);
  82. for (i = 0; i < len; i++) {
  83. g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
  84. g_assert_cmphex (glyphs[i].cluster, ==, i);
  85. }
  86. for (i = 0; i < len; i++) {
  87. g_assert_cmpint (output_x_advances[i], ==, positions[i].x_advance);
  88. g_assert_cmpint (output_x_offsets [i], ==, positions[i].x_offset);
  89. g_assert_cmpint (0, ==, positions[i].y_advance);
  90. g_assert_cmpint (0, ==, positions[i].y_offset);
  91. }
  92. }
  93. hb_buffer_destroy (buffer);
  94. }
  95. static void
  96. test_shape (void)
  97. {
  98. hb_blob_t *blob;
  99. hb_face_t *face;
  100. hb_font_funcs_t *ffuncs;
  101. hb_font_t *font, *sub_font;
  102. blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
  103. face = hb_face_create (blob, 0);
  104. hb_blob_destroy (blob);
  105. font = hb_font_create (face);
  106. hb_face_destroy (face);
  107. hb_font_set_scale (font, 10, 10);
  108. ffuncs = hb_font_funcs_create ();
  109. hb_font_funcs_set_glyph_h_advance_func (ffuncs, glyph_h_advance_func, NULL, NULL);
  110. hb_font_funcs_set_nominal_glyph_func (ffuncs, glyph_func, malloc (10), free);
  111. hb_font_set_funcs (font, ffuncs, NULL, NULL);
  112. hb_font_funcs_destroy (ffuncs);
  113. test_font (font);
  114. sub_font = hb_font_create_sub_font (font);
  115. test_font (sub_font);
  116. hb_font_destroy (sub_font);
  117. hb_font_destroy (font);
  118. }
  119. static void
  120. test_shape_clusters (void)
  121. {
  122. hb_face_t *face;
  123. hb_font_t *font;
  124. hb_buffer_t *buffer;
  125. unsigned int len;
  126. hb_glyph_info_t *glyphs;
  127. face = hb_face_create (NULL, 0);
  128. font = hb_font_create (face);
  129. hb_face_destroy (face);
  130. buffer = hb_buffer_create ();
  131. hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
  132. {
  133. /* https://crbug.com/497578 */
  134. hb_codepoint_t test[] = {0xFFF1, 0xF0B6};
  135. hb_buffer_add_utf32 (buffer, test, 2, 0, 2);
  136. }
  137. hb_shape (font, buffer, NULL, 0);
  138. len = hb_buffer_get_length (buffer);
  139. glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
  140. {
  141. const hb_codepoint_t output_glyphs[] = {0};
  142. const hb_position_t output_clusters[] = {0};
  143. unsigned int i;
  144. g_assert_cmpint (len, ==, 1);
  145. for (i = 0; i < len; i++) {
  146. g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
  147. g_assert_cmphex (glyphs[i].cluster, ==, output_clusters[i]);
  148. }
  149. }
  150. hb_buffer_destroy (buffer);
  151. hb_font_destroy (font);
  152. }
  153. static void
  154. test_shape_list (void)
  155. {
  156. const char **shapers = hb_shape_list_shapers ();
  157. unsigned int i;
  158. for (i = 0; shapers[i]; i++)
  159. ;
  160. g_assert_cmpint (i, >, 1);
  161. g_assert (!strcmp (shapers[i - 1], "fallback"));
  162. }
  163. int
  164. main (int argc, char **argv)
  165. {
  166. hb_test_init (&argc, &argv);
  167. hb_test_add (test_shape);
  168. hb_test_add (test_shape_clusters);
  169. /* TODO test fallback shaper */
  170. /* TODO test shaper_full */
  171. hb_test_add (test_shape_list);
  172. return hb_test_run();
  173. }