1 /*
2 * Copyright © 2011 Google, Inc.
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 * Google Author(s): Behdad Esfahbod
25 */
26
27 #include "hb-test.h"
28
29 /* Unit tests for hb-shape.h */
30
31 /*
32 * This test provides a framework to test aspects of hb_shape() that are
33 * font-independent. Please add tests for any feature that fits that
34 * description.
35 */
36
37 /* TODO Make this test data-driven and add some real test data */
38 /* TODO Test positions too. And test non-native direction. Test commit 2e18c6dbdfb */
39
40
41 static const char test_data[] = "test\0data";
42
43 static hb_position_t
glyph_h_advance_func(hb_font_t * font HB_UNUSED,void * font_data HB_UNUSED,hb_codepoint_t glyph,void * user_data HB_UNUSED)44 glyph_h_advance_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
45 hb_codepoint_t glyph,
46 void *user_data HB_UNUSED)
47 {
48 switch (glyph) {
49 case 1: return 10;
50 case 2: return 6;
51 case 3: return 5;
52 }
53 return 0;
54 }
55
56 static hb_bool_t
glyph_func(hb_font_t * font HB_UNUSED,void * font_data HB_UNUSED,hb_codepoint_t unicode,hb_codepoint_t * glyph,void * user_data HB_UNUSED)57 glyph_func (hb_font_t *font HB_UNUSED, void *font_data HB_UNUSED,
58 hb_codepoint_t unicode,
59 hb_codepoint_t *glyph,
60 void *user_data HB_UNUSED)
61 {
62 switch (unicode) {
63 case 'T': *glyph = 1; return TRUE;
64 case 'e': *glyph = 2; return TRUE;
65 case 's': *glyph = 3; return TRUE;
66 }
67 return FALSE;
68 }
69
70 static const char TesT[] = "TesT";
71
72 static void
test_font(hb_font_t * font)73 test_font (hb_font_t *font)
74 {
75 hb_buffer_t *buffer;
76 unsigned int len;
77 hb_glyph_info_t *glyphs;
78 hb_glyph_position_t *positions;
79
80 buffer = hb_buffer_create ();
81 hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
82 hb_buffer_add_utf8 (buffer, TesT, 4, 0, 4);
83
84 hb_shape (font, buffer, NULL, 0);
85
86 len = hb_buffer_get_length (buffer);
87 glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
88 positions = hb_buffer_get_glyph_positions (buffer, NULL);
89
90 {
91 const hb_codepoint_t output_glyphs[] = {1, 2, 3, 1};
92 const hb_position_t output_x_advances[] = {10, 6, 5, 10};
93 const hb_position_t output_x_offsets[] = {0, 0, 0, 0};
94 unsigned int i;
95 g_assert_cmpint (len, ==, 4);
96 for (i = 0; i < len; i++) {
97 g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
98 g_assert_cmphex (glyphs[i].cluster, ==, i);
99 }
100 for (i = 0; i < len; i++) {
101 g_assert_cmpint (output_x_advances[i], ==, positions[i].x_advance);
102 g_assert_cmpint (output_x_offsets [i], ==, positions[i].x_offset);
103 g_assert_cmpint (0, ==, positions[i].y_advance);
104 g_assert_cmpint (0, ==, positions[i].y_offset);
105 }
106 }
107
108 hb_buffer_destroy (buffer);
109 }
110
111 static void
test_shape(void)112 test_shape (void)
113 {
114 hb_blob_t *blob;
115 hb_face_t *face;
116 hb_font_funcs_t *ffuncs;
117 hb_font_t *font, *sub_font;
118
119 blob = hb_blob_create (test_data, sizeof (test_data), HB_MEMORY_MODE_READONLY, NULL, NULL);
120 face = hb_face_create (blob, 0);
121 hb_blob_destroy (blob);
122 font = hb_font_create (face);
123 hb_face_destroy (face);
124 hb_font_set_scale (font, 10, 10);
125
126 ffuncs = hb_font_funcs_create ();
127 hb_font_funcs_set_glyph_h_advance_func (ffuncs, glyph_h_advance_func, NULL, NULL);
128 hb_font_funcs_set_nominal_glyph_func (ffuncs, glyph_func, malloc (10), free);
129 hb_font_set_funcs (font, ffuncs, NULL, NULL);
130 hb_font_funcs_destroy (ffuncs);
131
132 test_font (font);
133
134 sub_font = hb_font_create_sub_font (font);
135 test_font (sub_font);
136
137 hb_font_destroy (sub_font);
138 hb_font_destroy (font);
139 }
140
141 static void
test_shape_clusters(void)142 test_shape_clusters (void)
143 {
144 hb_face_t *face;
145 hb_font_t *font;
146 hb_buffer_t *buffer;
147 unsigned int len;
148 hb_glyph_info_t *glyphs;
149
150 face = hb_face_create (NULL, 0);
151 font = hb_font_create (face);
152 hb_face_destroy (face);
153
154 buffer = hb_buffer_create ();
155 hb_buffer_set_direction (buffer, HB_DIRECTION_LTR);
156 {
157 /* https://crbug.com/497578 */
158 hb_codepoint_t test[] = {0xFFF1, 0xF0B6};
159 hb_buffer_add_utf32 (buffer, test, 2, 0, 2);
160 }
161
162 hb_shape (font, buffer, NULL, 0);
163
164 len = hb_buffer_get_length (buffer);
165 glyphs = hb_buffer_get_glyph_infos (buffer, NULL);
166
167 {
168 const hb_codepoint_t output_glyphs[] = {0};
169 const hb_position_t output_clusters[] = {0};
170 unsigned int i;
171 g_assert_cmpint (len, ==, 1);
172 for (i = 0; i < len; i++) {
173 g_assert_cmphex (glyphs[i].codepoint, ==, output_glyphs[i]);
174 g_assert_cmphex (glyphs[i].cluster, ==, output_clusters[i]);
175 }
176 }
177
178 hb_buffer_destroy (buffer);
179 hb_font_destroy (font);
180 }
181
182
183 static void
test_shape_list(void)184 test_shape_list (void)
185 {
186 const char **shapers = hb_shape_list_shapers ();
187
188 unsigned int i;
189 for (i = 0; shapers[i]; i++)
190 ;
191
192 g_assert_cmpint (i, >, 1);
193 g_assert (!strcmp (shapers[i - 1], "fallback"));
194 }
195
196 int
main(int argc,char ** argv)197 main (int argc, char **argv)
198 {
199 hb_test_init (&argc, &argv);
200
201 hb_test_add (test_shape);
202 hb_test_add (test_shape_clusters);
203 /* TODO test fallback shaper */
204 /* TODO test shaper_full */
205 hb_test_add (test_shape_list);
206
207 return hb_test_run();
208 }
209