xref: /aosp_15_r20/external/harfbuzz_ng/test/api/test-style.c (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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.h>
28 
29 /* Unit tests for hb-style.h */
30 
31 #define assert_cmpfloat(n1, n2) g_assert_cmpint ((int) (n1 * 100.f), ==, (int) (n2 * 100.f))
32 
33 static void
test_empty_face(void)34 test_empty_face (void)
35 {
36   hb_font_t *empty = hb_font_get_empty ();
37 
38   assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_ITALIC), 0);
39   assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_OPTICAL_SIZE), 12);
40   assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_SLANT_ANGLE), 0);
41   assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_WIDTH), 100);
42   assert_cmpfloat (hb_style_get_value (empty, HB_STYLE_TAG_WEIGHT), 400);
43 }
44 
45 static void
test_regular_face(void)46 test_regular_face (void)
47 {
48   hb_face_t *face = hb_test_open_font_file ("fonts/Roboto-Regular.abc.ttf");
49   hb_font_t *font = hb_font_create (face);
50 
51   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
52   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
53   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
54   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
55   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 400);
56 
57   hb_font_destroy (font);
58   hb_face_destroy (face);
59 }
60 
61 static void
test_bold_face(void)62 test_bold_face (void)
63 {
64   hb_face_t *face = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf");
65   hb_font_t *font = hb_font_create (face);
66 
67   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
68   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
69   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
70   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
71   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 700);
72 
73   hb_font_destroy (font);
74   hb_face_destroy (face);
75 }
76 
77 
78 static void
test_face_user_setting(void)79 test_face_user_setting (void)
80 {
81   hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf");
82   hb_font_t *font = hb_font_create (face);
83 
84   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
85   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
86   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
87   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
88   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 389.34f); /* its default weight */
89   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
90 
91   hb_font_set_var_named_instance (font, 0);
92 
93   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
94   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
95   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
96   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
97   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 200);
98   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
99 
100   hb_font_set_var_named_instance (font, 1);
101 
102   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
103   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
104   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
105   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
106   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 300);
107   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
108 
109   hb_font_set_var_named_instance (font, 2);
110 
111   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
112   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
113   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
114   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
115   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 400);
116   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
117 
118   hb_font_set_var_named_instance (font, 3);
119 
120   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
121   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
122   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
123   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
124   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT),600);
125   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
126 
127   hb_font_set_var_named_instance (font, 4);
128 
129   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
130   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
131   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
132   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
133   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 700);
134   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
135 
136   hb_font_set_var_named_instance (font, 5);
137 
138   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
139   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
140   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
141   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
142   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 900);
143   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 0);
144 
145   hb_font_set_var_named_instance (font, 6);
146 
147   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
148   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
149   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
150   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
151   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 900);
152   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 50);
153 
154   hb_font_set_var_named_instance (font, 7);
155 
156   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_ITALIC), 0);
157   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_OPTICAL_SIZE), 12);
158   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), 0);
159   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WIDTH), 100);
160   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_WEIGHT), 900);
161   assert_cmpfloat (hb_style_get_value (font, (hb_style_tag_t) HB_TAG ('C','N','T','R')), 100);
162 
163   hb_font_destroy (font);
164   hb_face_destroy (face);
165 }
166 
167 static void
test_synthetic_slant(void)168 test_synthetic_slant (void)
169 {
170   hb_face_t *face = hb_test_open_font_file ("fonts/AdobeVFPrototype_vsindex.otf");
171   hb_font_t *font = hb_font_create (face);
172 
173   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), 0);
174 
175   hb_font_set_synthetic_slant (font, 0.2);
176 
177   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), 0.2);
178 
179   hb_font_destroy (font);
180   hb_face_destroy (face);
181 
182   face = hb_test_open_font_file ("fonts/notosansitalic.ttf");
183   font = hb_font_create (face);
184 
185   /* We expect a negative angle for a typical italic font,
186    * which should give us a positive ratio
187    */
188   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_ANGLE), -12);
189   assert_cmpfloat (hb_style_get_value (font, HB_STYLE_TAG_SLANT_RATIO), 0.21);
190 
191   hb_font_destroy (font);
192   hb_face_destroy (face);
193 }
194 
195 int
main(int argc,char ** argv)196 main (int argc, char **argv)
197 {
198   hb_test_init (&argc, &argv);
199 
200   hb_test_add (test_empty_face);
201   hb_test_add (test_regular_face);
202   hb_test_add (test_bold_face);
203   hb_test_add (test_face_user_setting);
204   hb_test_add (test_synthetic_slant);
205 
206   return hb_test_run ();
207 }
208