test-ot-metrics-tt-var.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright © 2019 Adobe 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. * Adobe Author(s): Michiharu Ariza
  25. */
  26. #include "hb-test.h"
  27. #include <hb-ot.h>
  28. /* Unit tests for glyph advance widths and extents of TrueType variable fonts */
  29. static void
  30. test_extents_tt_var (void)
  31. {
  32. hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf");
  33. g_assert (face);
  34. hb_font_t *font = hb_font_create (face);
  35. hb_face_destroy (face);
  36. g_assert (font);
  37. hb_ot_font_set_funcs (font);
  38. hb_glyph_extents_t extents;
  39. hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents);
  40. g_assert (result);
  41. g_assert_cmpint (extents.x_bearing, ==, 10);
  42. g_assert_cmpint (extents.y_bearing, ==, 846);
  43. g_assert_cmpint (extents.width, ==, 500);
  44. g_assert_cmpint (extents.height, ==, -846);
  45. float coords[1] = { 500.0f };
  46. hb_font_set_var_coords_design (font, coords, 1);
  47. result = hb_font_get_glyph_extents (font, 2, &extents);
  48. g_assert (result);
  49. g_assert_cmpint (extents.x_bearing, ==, 0);
  50. g_assert_cmpint (extents.y_bearing, ==, 874);
  51. g_assert_cmpint (extents.width, ==, 551);
  52. g_assert_cmpint (extents.height, ==, -874);
  53. hb_font_destroy (font);
  54. }
  55. static void
  56. test_advance_tt_var_nohvar (void)
  57. {
  58. hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman-nohvar-41,C1.ttf");
  59. g_assert (face);
  60. hb_font_t *font = hb_font_create (face);
  61. hb_face_destroy (face);
  62. g_assert (font);
  63. hb_ot_font_set_funcs (font);
  64. hb_position_t x, y;
  65. hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y);
  66. g_assert_cmpint (x, ==, 520);
  67. g_assert_cmpint (y, ==, 0);
  68. hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
  69. g_assert_cmpint (x, ==, 0);
  70. g_assert_cmpint (y, ==, -1257);
  71. float coords[1] = { 500.0f };
  72. hb_font_set_var_coords_design (font, coords, 1);
  73. hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_LTR, &x, &y);
  74. g_assert_cmpint (x, ==, 551);
  75. g_assert_cmpint (y, ==, 0);
  76. hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
  77. g_assert_cmpint (x, ==, 0);
  78. g_assert_cmpint (y, ==, -1257);
  79. hb_font_destroy (font);
  80. }
  81. static void
  82. test_advance_tt_var_hvarvvar (void)
  83. {
  84. hb_face_t *face = hb_test_open_font_file ("fonts/SourceSerifVariable-Roman-VVAR.abc.ttf");
  85. g_assert (face);
  86. hb_font_t *font = hb_font_create (face);
  87. hb_face_destroy (face);
  88. g_assert (font);
  89. hb_ot_font_set_funcs (font);
  90. hb_position_t x, y;
  91. hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y);
  92. g_assert_cmpint (x, ==, 508);
  93. g_assert_cmpint (y, ==, 0);
  94. hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y);
  95. g_assert_cmpint (x, ==, 0);
  96. g_assert_cmpint (y, ==, -1000);
  97. float coords[1] = { 700.0f };
  98. hb_font_set_var_coords_design (font, coords, 1);
  99. hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_LTR, &x, &y);
  100. g_assert_cmpint (x, ==, 531);
  101. g_assert_cmpint (y, ==, 0);
  102. hb_font_get_glyph_advance_for_direction(font, 1, HB_DIRECTION_TTB, &x, &y);
  103. g_assert_cmpint (x, ==, 0);
  104. g_assert_cmpint (y, ==, -1012);
  105. hb_font_destroy (font);
  106. }
  107. static void
  108. test_advance_tt_var_anchor (void)
  109. {
  110. hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.anchor.ttf");
  111. g_assert (face);
  112. hb_font_t *font = hb_font_create (face);
  113. hb_face_destroy (face);
  114. g_assert (font);
  115. hb_ot_font_set_funcs (font);
  116. hb_glyph_extents_t extents;
  117. hb_bool_t result = hb_font_get_glyph_extents (font, 2, &extents);
  118. g_assert (result);
  119. g_assert_cmpint (extents.x_bearing, ==, 56);
  120. g_assert_cmpint (extents.y_bearing, ==, 672);
  121. g_assert_cmpint (extents.width, ==, 556);
  122. g_assert_cmpint (extents.height, ==, -684);
  123. float coords[1] = { 500.0f };
  124. hb_font_set_var_coords_design (font, coords, 1);
  125. result = hb_font_get_glyph_extents (font, 2, &extents);
  126. g_assert (result);
  127. g_assert_cmpint (extents.x_bearing, ==, 50);
  128. g_assert_cmpint (extents.y_bearing, ==, 667);
  129. g_assert_cmpint (extents.width, ==, 592);
  130. g_assert_cmpint (extents.height, ==, -679);
  131. hb_font_destroy (font);
  132. }
  133. static void
  134. test_extents_tt_var_comp (void)
  135. {
  136. hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf");
  137. g_assert (face);
  138. hb_font_t *font = hb_font_create (face);
  139. hb_face_destroy (face);
  140. g_assert (font);
  141. hb_ot_font_set_funcs (font);
  142. hb_glyph_extents_t extents;
  143. float coords[1] = { 800.0f };
  144. hb_font_set_var_coords_design (font, coords, 1);
  145. hb_bool_t result;
  146. result = hb_font_get_glyph_extents (font, 2, &extents); /* Ccedilla, cedilla y-scaled by 0.8, with unscaled component offset */
  147. g_assert (result);
  148. g_assert_cmpint (extents.x_bearing, ==, 19);
  149. g_assert_cmpint (extents.y_bearing, ==, 663);
  150. g_assert_cmpint (extents.width, ==, 519);
  151. g_assert_cmpint (extents.height, ==, -895);
  152. result = hb_font_get_glyph_extents (font, 3, &extents); /* Cacute, acute y-scaled by 0.8, with unscaled component offset (default) */
  153. g_assert (result);
  154. g_assert_cmpint (extents.x_bearing, ==, 19);
  155. g_assert_cmpint (extents.y_bearing, ==, 909);
  156. g_assert_cmpint (extents.width, ==, 519);
  157. g_assert_cmpint (extents.height, ==, -921);
  158. result = hb_font_get_glyph_extents (font, 4, &extents); /* Ccaron, caron y-scaled by 0.8, with scaled component offset */
  159. g_assert (result);
  160. g_assert_cmpint (extents.x_bearing, ==, 19);
  161. g_assert_cmpint (extents.y_bearing, ==, 866);
  162. g_assert_cmpint (extents.width, ==, 519);
  163. g_assert_cmpint (extents.height, ==, -878);
  164. hb_font_destroy (font);
  165. }
  166. static void
  167. test_advance_tt_var_comp_v (void)
  168. {
  169. hb_face_t *face = hb_test_open_font_file ("fonts/SourceSansVariable-Roman.modcomp.ttf");
  170. g_assert (face);
  171. hb_font_t *font = hb_font_create (face);
  172. hb_face_destroy (face);
  173. g_assert (font);
  174. hb_ot_font_set_funcs (font);
  175. float coords[1] = { 800.0f };
  176. hb_font_set_var_coords_design (font, coords, 1);
  177. hb_position_t x, y;
  178. hb_font_get_glyph_advance_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y); /* No VVAR; 'C' in composite Ccedilla determines metrics */
  179. g_assert_cmpint (x, ==, 0);
  180. g_assert_cmpint (y, ==, -991);
  181. hb_font_get_glyph_origin_for_direction(font, 2, HB_DIRECTION_TTB, &x, &y);
  182. g_assert_cmpint (x, ==, 292);
  183. g_assert_cmpint (y, ==, 1013);
  184. hb_font_destroy (font);
  185. }
  186. static void
  187. test_advance_tt_var_gvar_infer (void)
  188. {
  189. hb_face_t *face = hb_test_open_font_file ("fonts/TestGVAREight.ttf");
  190. hb_font_t *font = hb_font_create (face);
  191. hb_ot_font_set_funcs (font);
  192. hb_face_destroy (face);
  193. int coords[6] = {100};
  194. hb_font_set_var_coords_normalized (font, coords, 6);
  195. hb_glyph_extents_t extents = {0};
  196. g_assert (hb_font_get_glyph_extents (font, 4, &extents));
  197. hb_font_destroy (font);
  198. }
  199. int
  200. main (int argc, char **argv)
  201. {
  202. hb_test_init (&argc, &argv);
  203. hb_test_add (test_extents_tt_var);
  204. hb_test_add (test_advance_tt_var_nohvar);
  205. hb_test_add (test_advance_tt_var_hvarvvar);
  206. hb_test_add (test_advance_tt_var_anchor);
  207. hb_test_add (test_extents_tt_var_comp);
  208. hb_test_add (test_advance_tt_var_comp_v);
  209. hb_test_add (test_advance_tt_var_gvar_infer);
  210. return hb_test_run ();
  211. }