test-var-coords.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright © 2019 Ebrahim Byagowi
  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 <hb-ot.h>
  26. /* Unit tests for hb_font_[gs]et_var_coords_ */
  27. static void
  28. test_get_var_coords (void)
  29. {
  30. #ifndef G_APPROX_VALUE
  31. #define G_APPROX_VALUE(a, b, epsilon) \
  32. (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon))
  33. #endif
  34. #define EPSILON 0.05f
  35. hb_face_t *face = hb_test_open_font_file ("fonts/TestCFF2VF.otf");
  36. hb_font_t *font = hb_font_create (face);
  37. /* Normalized coords as input */
  38. int normalized_coords[] = {100, 0};
  39. hb_font_set_var_coords_normalized (font, normalized_coords, 2);
  40. g_assert_cmpint ((int) hb_font_get_var_coords_design (font, NULL)[0], ==, 403);
  41. g_assert_cmpint ((int) hb_font_get_var_coords_normalized (font, NULL)[0], ==, 100);
  42. /* Design coords as input */
  43. float design_coords[] = {206.f, 0};
  44. hb_font_set_var_coords_design (font, design_coords, 2);
  45. g_assert_cmpint ((int) hb_font_get_var_coords_normalized (font, NULL)[0], ==, -16117);
  46. g_assert_cmpint ((int) hb_font_get_var_coords_design (font, NULL)[0], ==, 206);
  47. for (float weight = 200; weight < 901; ++weight)
  48. {
  49. int normalized;
  50. hb_ot_var_normalize_coords (face, 1, &weight, &normalized);
  51. hb_font_set_var_coords_normalized (font, &normalized, 1);
  52. float converted_back = hb_font_get_var_coords_design (font, NULL)[0];
  53. // fprintf (stderr, "%f: %d => %f\n", weight, normalized, converted_back);
  54. g_assert_true (G_APPROX_VALUE (converted_back, weight, EPSILON));
  55. }
  56. hb_font_destroy (font);
  57. hb_face_destroy (face);
  58. }
  59. static void
  60. test_get_var_get_axis_infos (void)
  61. {
  62. hb_face_t *face = hb_test_open_font_file ("fonts/Estedad-VF.ttf");
  63. g_assert_cmpint (hb_ot_var_get_axis_count (face), ==, 2);
  64. hb_ot_var_axis_info_t info;
  65. unsigned c = 1;
  66. g_assert_cmpint (hb_ot_var_get_axis_infos (face, 0, &c, &info), ==, 2);
  67. g_assert (info.tag == HB_TAG ('w','g','h','t'));
  68. g_assert_cmpint (c, ==, 1);
  69. hb_ot_var_get_axis_infos (face, 1, &c, &info);
  70. g_assert (info.tag == HB_TAG ('w','d','t','h'));
  71. g_assert_cmpint (c, ==, 1);
  72. hb_ot_var_get_axis_infos (face, 2, &c, &info);
  73. g_assert_cmpint (c, ==, 0);
  74. hb_face_destroy (face);
  75. }
  76. int
  77. main (int argc, char **argv)
  78. {
  79. hb_test_init (&argc, &argv);
  80. hb_test_add (test_get_var_coords);
  81. hb_test_add (test_get_var_get_axis_infos);
  82. return hb_test_run ();
  83. }