2 * Copyright © 2016 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Sascha Brawer
33 /* Unit tests for hb-ot-color.h */
35 /* Test font with the following CPAL v0 table, as TTX and manual disassembly:
39 <numPaletteEntries value="2"/>
41 <color index="0" value="#000000FF"/>
42 <color index="1" value="#66CCFFFF"/>
45 <color index="0" value="#000000FF"/>
46 <color index="1" value="#800000FF"/>
51 2 | 0002 # numPaletteEntries=2
52 4 | 0002 # numPalettes=2
53 6 | 0004 # numColorRecords=4
54 8 | 00000010 # offsetToFirstColorRecord=16
55 12 | 0000 0002 # colorRecordIndex=[0, 2]
56 16 | 000000ff ffcc66ff # colorRecord #0, #1 (BGRA)
57 24 | 000000ff 000080ff # colorRecord #2, #3 (BGRA)
59 static hb_face_t *cpal_v0 = NULL;
61 /* Test font with the following CPAL v1 table, as TTX and manual disassembly:
65 <numPaletteEntries value="2"/>
66 <palette index="0" label="257" type="2">
67 <color index="0" value="#000000FF"/>
68 <color index="1" value="#66CCFFFF"/>
70 <palette index="1" label="65535" type="1">
71 <color index="0" value="#000000FF"/>
72 <color index="1" value="#FFCC66FF"/>
74 <palette index="2" label="258" type="0">
75 <color index="0" value="#000000FF"/>
76 <color index="1" value="#800000FF"/>
79 <label index="0" value="65535"/>
80 <label index="1" value="256"/>
85 2 | 0002 # numPaletteEntries=2
86 4 | 0003 # numPalettes=3
87 6 | 0006 # numColorRecords=6
88 8 | 0000001e # offsetToFirstColorRecord=30
89 12 | 0000 0002 0004 # colorRecordIndex=[0, 2, 4]
90 18 | 00000036 # offsetToPaletteTypeArray=54
91 22 | 00000042 # offsetToPaletteLabelArray=66
92 26 | 00000048 # offsetToPaletteEntryLabelArray=72
93 30 | 000000ff ffcc66ff 000000ff # colorRecord #0, #1, #2 (BGRA)
94 42 | 66ccffff 000000ff 000080ff # colorRecord #3, #4, #5 (BGRA)
95 54 | 00000002 00000001 00000000 # paletteFlags=[2, 1, 0]
96 66 | 0101 ffff 0102 # paletteName=[257, 0xffff, 258]
97 72 | ffff 0100 # paletteEntryLabel=[0xffff, 256]
99 static hb_face_t *cpal_v1 = NULL;
102 #define assert_color_rgba(colors, i, r, g, b, a) G_STMT_START { \
103 const hb_ot_color_t *_colors = (colors); \
104 const size_t _i = (i); \
105 const uint8_t red = (r), green = (g), blue = (b), alpha = (a); \
106 if (_colors[_i].red != red) { \
107 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
108 "colors[" #i "].red", _colors[_i].red, "==", red, 'x'); \
110 if (_colors[_i].green != green) { \
111 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
112 "colors[" #i "].green", _colors[_i].green, "==", green, 'x'); \
114 if (_colors[_i].blue != blue) { \
115 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
116 "colors[" #i "].blue", colors[i].blue, "==", blue, 'x'); \
118 if (_colors[_i].alpha != alpha) { \
119 g_assertion_message_cmpnum (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
120 "colors[" #i "].alpha", _colors[_i].alpha, "==", alpha, 'x'); \
127 test_hb_ot_color_get_palette_count (void)
129 g_assert_cmpint (hb_ot_color_get_palette_count (hb_face_get_empty()), ==, 0);
130 g_assert_cmpint (hb_ot_color_get_palette_count (cpal_v0), ==, 2);
131 g_assert_cmpint (hb_ot_color_get_palette_count (cpal_v1), ==, 3);
136 test_hb_ot_color_get_palette_name_id_empty (void)
138 /* numPalettes=0, so all calls are for out-of-bounds palette indices */
139 g_assert_cmpint (hb_ot_color_get_palette_name_id (hb_face_get_empty(), 0), ==, 0xffff);
140 g_assert_cmpint (hb_ot_color_get_palette_name_id (hb_face_get_empty(), 1), ==, 0xffff);
145 test_hb_ot_color_get_palette_name_id_v0 (void)
147 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 0), ==, 0xffff);
148 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 1), ==, 0xffff);
150 /* numPalettes=2, so palette #2 is out of bounds */
151 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v0, 2), ==, 0xffff);
156 test_hb_ot_color_get_palette_name_id_v1 (void)
158 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 0), ==, 257);
159 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 1), ==, 0xffff);
160 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 2), ==, 258);
162 /* numPalettes=3, so palette #3 is out of bounds */
163 g_assert_cmpint (hb_ot_color_get_palette_name_id (cpal_v1, 3), ==, 0xffff);
167 test_hb_ot_color_get_palette_flags_empty (void)
169 /* numPalettes=0, so all calls are for out-of-bounds palette indices */
170 g_assert_cmpint (hb_ot_color_get_palette_flags (hb_face_get_empty(), 0), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
171 g_assert_cmpint (hb_ot_color_get_palette_flags (hb_face_get_empty(), 1), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
176 test_hb_ot_color_get_palette_flags_v0 (void)
178 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 0), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
179 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 1), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
181 /* numPalettes=2, so palette #2 is out of bounds */
182 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 2), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
187 test_hb_ot_color_get_palette_flags_v1 (void)
189 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v1, 0), ==, HB_OT_COLOR_PALETTE_FLAG_FOR_DARK_BACKGROUND);
190 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v1, 1), ==, HB_OT_COLOR_PALETTE_FLAG_FOR_LIGHT_BACKGROUND);
191 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 2), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
193 /* numPalettes=3, so palette #3 is out of bounds */
194 g_assert_cmpint (hb_ot_color_get_palette_flags (cpal_v0, 3), ==, HB_OT_COLOR_PALETTE_FLAG_DEFAULT);
199 test_hb_ot_color_get_palette_colors_empty (void)
201 hb_face_t *empty = hb_face_get_empty ();
202 g_assert_cmpint (hb_ot_color_get_palette_colors (empty, 0, 0, NULL, NULL), ==, 0);
207 test_hb_ot_color_get_palette_colors_v0 (void)
209 unsigned int num_colors = hb_ot_color_get_palette_colors (cpal_v0, 0, 0, NULL, NULL);
210 hb_ot_color_t *colors = (hb_ot_color_t*) alloca (num_colors * sizeof (hb_ot_color_t));
211 size_t colors_size = num_colors * sizeof(*colors);
212 g_assert_cmpint (num_colors, ==, 2);
214 /* Palette #0, start_index=0 */
215 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 0, &num_colors, colors), ==, 2);
216 g_assert_cmpint (num_colors, ==, 2);
217 assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff);
218 assert_color_rgba (colors, 1, 0x66, 0xcc, 0xff, 0xff);
220 /* Palette #1, start_index=0 */
221 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 1, 0, &num_colors, colors), ==, 2);
222 g_assert_cmpint (num_colors, ==, 2);
223 assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff);
224 assert_color_rgba (colors, 1, 0x80, 0x00, 0x00, 0xff);
226 /* Palette #2 (there are only #0 and #1 in the font, so this is out of bounds) */
227 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 2, 0, &num_colors, colors), ==, 0);
229 /* Palette #0, start_index=1 */
230 memset(colors, 0x33, colors_size);
232 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 1, &num_colors, colors), ==, 2);
233 g_assert_cmpint (num_colors, ==, 1);
234 assert_color_rgba (colors, 0, 0x66, 0xcc, 0xff, 0xff);
235 assert_color_rgba (colors, 1, 0x33, 0x33, 0x33, 0x33); /* untouched */
237 /* Palette #0, start_index=0, pretend that we have only allocated space for 1 color */
238 memset(colors, 0x44, colors_size);
240 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 0, &num_colors, colors), ==, 2);
241 g_assert_cmpint (num_colors, ==, 1);
242 assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff);
243 assert_color_rgba (colors, 1, 0x44, 0x44, 0x44, 0x44); /* untouched */
245 /* start_index > numPaletteEntries */
246 memset(colors, 0x44, colors_size);
248 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v0, 0, 9876, &num_colors, colors), ==, 2);
249 g_assert_cmpint (num_colors, ==, 0);
250 assert_color_rgba (colors, 0, 0x44, 0x44, 0x44, 0x44); /* untouched */
251 assert_color_rgba (colors, 1, 0x44, 0x44, 0x44, 0x44); /* untouched */
256 test_hb_ot_color_get_palette_colors_v1 (void)
258 hb_ot_color_t colors[3];
259 unsigned int num_colors = hb_ot_color_get_palette_colors (cpal_v1, 0, 0, NULL, NULL);
260 size_t colors_size = 3 * sizeof(*colors);
261 g_assert_cmpint (num_colors, ==, 2);
263 /* Palette #0, start_index=0 */
264 memset(colors, 0x77, colors_size);
265 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 0, 0, &num_colors, colors), ==, 2);
266 g_assert_cmpint (num_colors, ==, 2);
267 assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff);
268 assert_color_rgba (colors, 1, 0x66, 0xcc, 0xff, 0xff);
269 assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */
271 /* Palette #1, start_index=0 */
272 memset(colors, 0x77, colors_size);
273 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 1, 0, &num_colors, colors), ==, 2);
274 g_assert_cmpint (num_colors, ==, 2);
275 assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff);
276 assert_color_rgba (colors, 1, 0xff, 0xcc, 0x66, 0xff);
277 assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */
279 /* Palette #2, start_index=0 */
280 memset(colors, 0x77, colors_size);
281 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 2, 0, &num_colors, colors), ==, 2);
282 g_assert_cmpint (num_colors, ==, 2);
283 assert_color_rgba (colors, 0, 0x00, 0x00, 0x00, 0xff);
284 assert_color_rgba (colors, 1, 0x80, 0x00, 0x00, 0xff);
285 assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */
287 /* Palette #3 (out of bounds), start_index=0 */
288 memset(colors, 0x77, colors_size);
289 g_assert_cmpint (hb_ot_color_get_palette_colors (cpal_v1, 3, 0, &num_colors, colors), ==, 0);
290 g_assert_cmpint (num_colors, ==, 0);
291 assert_color_rgba (colors, 0, 0x77, 0x77, 0x77, 0x77); /* untouched */
292 assert_color_rgba (colors, 1, 0x77, 0x77, 0x77, 0x77); /* untouched */
293 assert_color_rgba (colors, 2, 0x77, 0x77, 0x77, 0x77); /* untouched */
298 main (int argc, char **argv)
302 hb_test_init (&argc, &argv);
303 // cpal_v0 = hb_test_load_face ("../shaping/data/in-house/fonts/e90374e5e439e00725b4fe7a8d73db57c5a97f82.ttf");
304 // cpal_v1 = hb_test_load_face ("../shaping/data/in-house/fonts/319f5d7ebffbefc5c5e6569f8cea73444d7a7268.ttf");
305 // hb_test_add (test_hb_ot_color_get_palette_count);
306 // hb_test_add (test_hb_ot_color_get_palette_name_id_empty);
307 // hb_test_add (test_hb_ot_color_get_palette_name_id_v0);
308 // hb_test_add (test_hb_ot_color_get_palette_name_id_v1);
309 // hb_test_add (test_hb_ot_color_get_palette_flags_empty);
310 // hb_test_add (test_hb_ot_color_get_palette_flags_v0);
311 // hb_test_add (test_hb_ot_color_get_palette_flags_v1);
312 // hb_test_add (test_hb_ot_color_get_palette_colors_empty);
313 // hb_test_add (test_hb_ot_color_get_palette_colors_v0);
314 // hb_test_add (test_hb_ot_color_get_palette_colors_v1);
315 status = hb_test_run();
316 hb_face_destroy (cpal_v0);
317 hb_face_destroy (cpal_v1);