1 /*
2 * Copyright © 2020 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 */
25 #include "hb-ot-var-cvar-table.hh"
26
27 // cvar table data from Multi-ABC.ttf
28 const unsigned char cvar_data[] = "\x0\x1\x0\x0\x0\x2\x0\x14\x0\x51\xa0\x0\xc0\x0\x0\x54\xa0\x0\x40\x0\x2a\x29\x17\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\xd\xff\x0\xfd\x1\x0\xff\x0\xfd\x1\x0\xdb\xdb\xe6\xe6\x82\x0\xfd\x84\x6\xfd\x0\x2\xe3\xe3\xec\xec\x82\x4\x1\xe3\xe3\xec\xec\x82\x0\x1\x2a\x29\x17\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\x1\xd\x1\x0\x5\xfd\x0\x1\x0\x5\xfd\x0\x61\x61\x44\x44\x82\x0\x5\x81\x9\x1\xff\x1\x7\xff\xfb\x49\x49\x35\x35\x82\x4\xff\x49\x49\x35\x35\x82\x0\xff";
29
30 static void
test_decompile_cvar()31 test_decompile_cvar ()
32 {
33 const OT::cvar* cvar_table = reinterpret_cast<const OT::cvar*> (cvar_data);
34 unsigned point_count = 65;
35 unsigned axis_count = 1;
36
37 hb_tag_t axis_tag = HB_TAG ('w', 'g', 'h', 't');
38 hb_map_t axis_idx_tag_map;
39 axis_idx_tag_map.set (0, axis_tag);
40
41 OT::TupleVariationData::tuple_variations_t tuple_variations;
42 hb_vector_t<unsigned> shared_indices;
43 OT::TupleVariationData::tuple_iterator_t iterator;
44
45 const OT::TupleVariationData* tuple_var_data = reinterpret_cast<const OT::TupleVariationData*> (cvar_data + 4);
46
47 unsigned len = sizeof (cvar_data);
48 hb_bytes_t var_data_bytes{(const char* ) cvar_data + 4, len - 4};
49 bool result = OT::TupleVariationData::get_tuple_iterator (var_data_bytes, axis_count, cvar_table,
50 shared_indices, &iterator);
51 assert (result);
52
53 result = tuple_var_data->decompile_tuple_variations (point_count, false, iterator, &axis_idx_tag_map,
54 shared_indices, hb_array<const OT::F2DOT14> (),
55 tuple_variations);
56
57 assert (result);
58 assert (tuple_variations.tuple_vars.length == 2);
59 for (unsigned i = 0; i < 2; i++)
60 {
61 assert (tuple_variations.tuple_vars[i].axis_tuples.get_population () == 1);
62 assert (!tuple_variations.tuple_vars[i].deltas_y);
63 assert (tuple_variations.tuple_vars[i].indices.length == 65);
64 assert (tuple_variations.tuple_vars[i].indices.length == tuple_variations.tuple_vars[i].deltas_x.length);
65 }
66 assert (tuple_variations.tuple_vars[0].axis_tuples.get (axis_tag) == Triple (-1.0, -1.0, 0.0));
67 assert (tuple_variations.tuple_vars[1].axis_tuples.get (axis_tag) == Triple (0.0, 1.0, 1.0));
68
69 hb_vector_t<double> deltas_1 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, -3.0, 1.0, 0.0, -1.0, 0.0, -3.0, 1.0, 0.0, -37.0, -37.0, -26.0, -26.0, 0.0, 0.0, 0.0, -3.0, 0.0, 0.0, 0.0, 0.0, 0.0, -3.0, 0.0, 2.0, -29.0, -29.0, -20.0, -20.0, 0.0, 0.0, 0.0, 1.0, -29.0, -29.0, -20.0, -20.0, 0.0, 0.0, 0.0, 1.0};
70 for (unsigned i = 0; i < 65; i++)
71 {
72 if (i < 23)
73 assert (tuple_variations.tuple_vars[0].indices[i] == 0);
74 else
75 {
76 assert (tuple_variations.tuple_vars[0].indices[i] == 1);
77 assert (tuple_variations.tuple_vars[0].deltas_x[i] == deltas_1[i]);
78 }
79 }
80
81 hb_vector_t<double> deltas_2 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 5.0, -3.0, 0.0, 1.0, 0.0, 5.0, -3.0, 0.0, 97.0, 97.0, 68.0, 68.0, 0.0, 0.0, 0.0, 5.0, 0.0, 0.0, 1.0, -1.0, 1.0, 7.0, -1.0, -5.0, 73.0, 73.0, 53.0, 53.0, 0.0, 0.0, 0.0, -1.0, 73.0, 73.0, 53.0, 53.0, 0.0, 0.0, 0.0, -1.0};
82 for (unsigned i = 0 ; i < 65; i++)
83 {
84 if (i < 23)
85 assert (tuple_variations.tuple_vars[1].indices[i] == 0);
86 else
87 {
88 assert (tuple_variations.tuple_vars[1].indices[i] == 1);
89 assert (tuple_variations.tuple_vars[1].deltas_x[i] == deltas_2[i]);
90 }
91 }
92
93 /* partial instancing wght=300:800 */
94 hb_hashmap_t<hb_tag_t, Triple> normalized_axes_location;
95 normalized_axes_location.set (axis_tag, Triple (-0.512817, 0.0, 0.700012));
96
97 hb_hashmap_t<hb_tag_t, TripleDistances> axes_triple_distances;
98 axes_triple_distances.set (axis_tag, TripleDistances (1.0, 1.0));
99
100 tuple_variations.instantiate (normalized_axes_location, axes_triple_distances);
101
102 assert (tuple_variations.tuple_vars[0].indices.length == 65);
103 assert (tuple_variations.tuple_vars[1].indices.length == 65);
104 assert (!tuple_variations.tuple_vars[0].deltas_y);
105 assert (!tuple_variations.tuple_vars[1].deltas_y);
106 assert (tuple_variations.tuple_vars[0].axis_tuples.get (axis_tag) == Triple (-1.0, -1.0, 0.0));
107 assert (tuple_variations.tuple_vars[1].axis_tuples.get (axis_tag) == Triple (0.0, 1.0, 1.0));
108
109 hb_vector_t<double> rounded_deltas_1 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1, 0.0, -2, 1, 0.0, -1, 0.0, -2, 1, 0.0, -19, -19, -13, -13, 0.0, 0.0, 0.0, -2, 0.0, 0.0, 0.0, 0.0, 0.0, -2, 0.0, 1, -15, -15, -10.0, -10.0, 0.0, 0.0, 0.0, 1, -15, -15, -10.0, -10.0, 0.0, 0.0, 0.0, 1};
110
111 hb_vector_t<double> rounded_deltas_2 {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1, 0.0, 4, -2, 0.0, 1, 0.0, 4, -2, 0.0, 68, 68, 48, 48, 0.0, 0.0, 0.0, 4, 0.0, 0.0, 1, -1, 1, 5, -1, -4, 51, 51, 37, 37, 0.0, 0.0, 0.0, -1, 51, 51, 37, 37, 0.0, 0.0, 0.0, -1};
112
113 for (unsigned i = 0; i < 65; i++)
114 {
115 if (i < 23)
116 {
117 assert (tuple_variations.tuple_vars[0].indices[i] == 0);
118 assert (tuple_variations.tuple_vars[1].indices[i] == 0);
119 }
120 else
121 {
122 assert (tuple_variations.tuple_vars[0].indices[i] == 1);
123 assert (tuple_variations.tuple_vars[1].indices[i] == 1);
124 assert (roundf (tuple_variations.tuple_vars[0].deltas_x[i]) == rounded_deltas_1[i]);
125 assert (roundf (tuple_variations.tuple_vars[1].deltas_x[i]) == rounded_deltas_2[i]);
126 }
127 }
128
129 hb_map_t axes_index_map;
130 axes_index_map.set (0, 0);
131 bool res = tuple_variations.compile_bytes (axes_index_map, axis_idx_tag_map, false);
132 assert (res);
133 assert (tuple_variations.tuple_vars[0].compiled_tuple_header.length == 6);
134 const unsigned char tuple_var_header_1[] = "\x0\x51\xa0\x0\xc0\x0";
135 for (unsigned i = 0; i < 6; i++)
136 assert(tuple_variations.tuple_vars[0].compiled_tuple_header.arrayZ[i] == tuple_var_header_1[i]);
137
138 assert (tuple_variations.tuple_vars[1].compiled_tuple_header.length == 6);
139 const unsigned char tuple_var_header_2[] = "\x0\x54\xa0\x0\x40\x0";
140 for (unsigned i = 0; i < 6; i++)
141 assert(tuple_variations.tuple_vars[1].compiled_tuple_header.arrayZ[i] == tuple_var_header_2[i]);
142
143 assert (tuple_variations.tuple_vars[0].compiled_deltas.length == 37);
144 assert (tuple_variations.tuple_vars[1].compiled_deltas.length == 40);
145 const unsigned char compiled_deltas_1[] = "\x0d\xff\x00\xfe\x01\x00\xff\x00\xfe\x01\x00\xed\xed\xf3\xf3\x82\x00\xfe\x84\x06\xfe\x00\x01\xf1\xf1\xf6\xf6\x82\x04\x01\xf1\xf1\xf6\xf6\x82\x00\x01";
146 for (unsigned i = 0; i < 37; i++)
147 assert (tuple_variations.tuple_vars[0].compiled_deltas.arrayZ[i] == compiled_deltas_1[i]);
148
149 const unsigned char compiled_deltas_2[] = "\x0d\x01\x00\x04\xfe\x00\x01\x00\x04\xfe\x00\x44\x44\x30\x30\x82\x00\x04\x81\x09\x01\xff\x01\x05\xff\xfc\x33\x33\x25\x25\x82\x04\xff\x33\x33\x25\x25\x82\x00\xff";
150 for (unsigned i = 0; i < 40; i++)
151 assert (tuple_variations.tuple_vars[1].compiled_deltas.arrayZ[i] == compiled_deltas_2[i]);
152 }
153
154 int
main(int argc,char ** argv)155 main (int argc, char **argv)
156 {
157 test_decompile_cvar ();
158 }
159