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