xref: /aosp_15_r20/external/harfbuzz_ng/test/api/test-ot-layout.c (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
1 /*
2  * Copyright © 2021  Khaled Hosny
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 #include <hb-ot.h>
29 
30 #define STATIC_ARRAY_SIZE 255
31 
32 static void
test_ot_layout_table_get_script_tags(void)33 test_ot_layout_table_get_script_tags (void)
34 {
35   hb_face_t *face = hb_test_open_font_file ("fonts/NotoNastaliqUrdu-Regular.ttf");
36 
37   unsigned int total = 0;
38   unsigned int count = STATIC_ARRAY_SIZE;
39   unsigned int offset = 0;
40   hb_tag_t tags[STATIC_ARRAY_SIZE];
41   while (count == STATIC_ARRAY_SIZE)
42   {
43     total = hb_ot_layout_table_get_script_tags (face, HB_OT_TAG_GSUB, offset, &count, tags);
44     g_assert_cmpuint (3, ==, total);
45     offset += count;
46     if (count)
47     {
48       g_assert_cmpuint (3, ==, count);
49       g_assert_cmpuint (HB_TAG ('a','r','a','b'), ==, tags[0]);
50       g_assert_cmpuint (HB_TAG ('d','f','l','t'), ==, tags[1]);
51       g_assert_cmpuint (HB_TAG ('l','a','t','n'), ==, tags[2]);
52     }
53   }
54   count = STATIC_ARRAY_SIZE;
55   offset = 0;
56   while (count == STATIC_ARRAY_SIZE)
57   {
58     total = hb_ot_layout_table_get_script_tags (face, HB_OT_TAG_GPOS, offset, &count, tags);
59     g_assert_cmpuint (1, ==, total);
60     offset += count;
61     if (count)
62     {
63       g_assert_cmpuint (1, ==, count);
64       g_assert_cmpuint (HB_TAG ('a','r','a','b'), ==, tags[0]);
65     }
66   }
67 
68   hb_face_destroy (face);
69 }
70 
71 static void
test_ot_layout_table_find_script(void)72 test_ot_layout_table_find_script (void)
73 {
74   hb_face_t *face = hb_test_open_font_file ("fonts/NotoNastaliqUrdu-Regular.ttf");
75   unsigned int index;
76 
77   g_assert (hb_ot_layout_table_find_script (face, HB_OT_TAG_GSUB, HB_TAG ('a','r','a','b'), &index));
78   g_assert_cmpuint (0, ==, index);
79   g_assert (hb_ot_layout_table_find_script (face, HB_OT_TAG_GSUB, HB_TAG ('d','f','l','t'), &index));
80   g_assert_cmpuint (1, ==, index);
81   g_assert (hb_ot_layout_table_find_script (face, HB_OT_TAG_GSUB, HB_TAG ('l','a','t','n'), &index));
82   g_assert_cmpuint (2, ==, index);
83 
84   g_assert (hb_ot_layout_table_find_script (face, HB_OT_TAG_GPOS, HB_TAG ('a','r','a','b'), &index));
85   g_assert_cmpuint (0, ==, index);
86   g_assert (!hb_ot_layout_table_find_script (face, HB_OT_TAG_GPOS, HB_TAG ('d','f','l','t'), &index));
87   g_assert_cmpuint (0xFFFF, ==, index);
88   g_assert (!hb_ot_layout_table_find_script (face, HB_OT_TAG_GPOS, HB_TAG ('l','a','t','n'), &index));
89   g_assert_cmpuint (0xFFFF, ==, index);
90 
91   hb_face_destroy (face);
92 }
93 
94 static void
test_ot_layout_table_get_feature_tags(void)95 test_ot_layout_table_get_feature_tags (void)
96 {
97   hb_face_t *face = hb_test_open_font_file ("fonts/NotoNastaliqUrdu-Regular.ttf");
98 
99   unsigned int total = 0;
100   unsigned int count = STATIC_ARRAY_SIZE;
101   unsigned int offset = 0;
102   hb_tag_t tags[STATIC_ARRAY_SIZE];
103   while (count == STATIC_ARRAY_SIZE)
104   {
105     total = hb_ot_layout_table_get_feature_tags (face, HB_OT_TAG_GSUB, offset, &count, tags);
106     g_assert_cmpuint (14, ==, total);
107     offset += count;
108     if (count)
109     {
110       g_assert_cmpuint (14, ==, count);
111       g_assert_cmpuint (HB_TAG ('c','c','m','p'), ==, tags[0]);
112       g_assert_cmpuint (HB_TAG ('i','s','o','l'), ==, tags[10]);
113       g_assert_cmpuint (HB_TAG ('r','l','i','g'), ==, tags[13]);
114     }
115   }
116   count = STATIC_ARRAY_SIZE;
117   offset = 0;
118   while (count == STATIC_ARRAY_SIZE)
119   {
120     total = hb_ot_layout_table_get_feature_tags (face, HB_OT_TAG_GPOS, offset, &count, tags);
121     g_assert_cmpuint (3, ==, total);
122     offset += count;
123     if (count)
124     {
125       g_assert_cmpuint (3, ==, count);
126       g_assert_cmpuint (HB_TAG ('c','u','r','s'), ==, tags[0]);
127       g_assert_cmpuint (HB_TAG ('m','a','r','k'), ==, tags[1]);
128       g_assert_cmpuint (HB_TAG ('m','k','m','k'), ==, tags[2]);
129     }
130   }
131 
132   hb_face_destroy (face);
133 }
134 
135 static void
test_ot_layout_script_get_language_tags(void)136 test_ot_layout_script_get_language_tags (void)
137 {
138   hb_face_t *face = hb_test_open_font_file ("fonts/Estedad-VF.ttf");
139 
140   unsigned int total = 0;
141   unsigned int count = STATIC_ARRAY_SIZE;
142   unsigned int offset = 0;
143   hb_tag_t tags[STATIC_ARRAY_SIZE];
144   while (count == STATIC_ARRAY_SIZE)
145   {
146     total = hb_ot_layout_script_get_language_tags (face, HB_OT_TAG_GSUB, 0, offset, &count, tags);
147     g_assert_cmpuint (2, ==, total);
148     offset += count;
149     if (count)
150     {
151       g_assert_cmpuint (2, ==, count);
152       g_assert_cmpuint (HB_TAG ('F','A','R',' '), ==, tags[0]);
153       g_assert_cmpuint (HB_TAG ('K','U','R',' '), ==, tags[1]);
154     }
155   }
156   count = STATIC_ARRAY_SIZE;
157   offset = 0;
158   while (count == STATIC_ARRAY_SIZE)
159   {
160     total = hb_ot_layout_script_get_language_tags (face, HB_OT_TAG_GPOS, 1, offset, &count, tags);
161     g_assert_cmpuint (2, ==, total);
162     offset += count;
163     if (count)
164     {
165       g_assert_cmpuint (2, ==, count);
166       g_assert_cmpuint (HB_TAG ('F','A','R',' '), ==, tags[0]);
167       g_assert_cmpuint (HB_TAG ('K','U','R',' '), ==, tags[1]);
168     }
169   }
170 
171   hb_face_destroy (face);
172 }
173 
174 static void
test_ot_layout_language_get_feature_tags(void)175 test_ot_layout_language_get_feature_tags (void)
176 {
177   hb_face_t *face = hb_test_open_font_file ("fonts/Estedad-VF.ttf");
178 
179   unsigned int total = 0;
180   unsigned int count = STATIC_ARRAY_SIZE;
181   unsigned int offset = 0;
182   hb_tag_t tags[STATIC_ARRAY_SIZE];
183   while (count == STATIC_ARRAY_SIZE)
184   {
185     total = hb_ot_layout_language_get_feature_tags (face, HB_OT_TAG_GSUB, 0, 0, offset, &count, tags);
186     g_assert_cmpuint (6, ==, total);
187     offset += count;
188     if (count)
189     {
190       g_assert_cmpuint (6, ==, count);
191       g_assert_cmpuint (HB_TAG ('c','a','l','t'), ==, tags[0]);
192       g_assert_cmpuint (HB_TAG ('f','i','n','a'), ==, tags[1]);
193       g_assert_cmpuint (HB_TAG ('i','n','i','t'), ==, tags[2]);
194       g_assert_cmpuint (HB_TAG ('l','i','g','a'), ==, tags[3]);
195       g_assert_cmpuint (HB_TAG ('m','e','d','i'), ==, tags[4]);
196       g_assert_cmpuint (HB_TAG ('r','l','i','g'), ==, tags[5]);
197     }
198   }
199   count = STATIC_ARRAY_SIZE;
200   offset = 0;
201   while (count == STATIC_ARRAY_SIZE)
202   {
203     total = hb_ot_layout_language_get_feature_tags (face, HB_OT_TAG_GPOS, 1, 0, offset, &count, tags);
204     g_assert_cmpuint (3, ==, total);
205     offset += count;
206     if (count)
207     {
208       g_assert_cmpuint (3, ==, count);
209       g_assert_cmpuint (HB_TAG ('k','e','r','n'), ==, tags[0]);
210       g_assert_cmpuint (HB_TAG ('m','a','r','k'), ==, tags[1]);
211       g_assert_cmpuint (HB_TAG ('m','k','m','k'), ==, tags[2]);
212     }
213   }
214 
215   hb_face_destroy (face);
216 }
217 
218 int
main(int argc,char ** argv)219 main (int argc, char **argv)
220 {
221   hb_test_init (&argc, &argv);
222   hb_test_add (test_ot_layout_table_get_script_tags);
223   hb_test_add (test_ot_layout_table_find_script);
224   hb_test_add (test_ot_layout_script_get_language_tags);
225   hb_test_add (test_ot_layout_table_get_feature_tags);
226   hb_test_add (test_ot_layout_language_get_feature_tags);
227   return hb_test_run ();
228 }
229