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