Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / test / api / test-subset-glyf.c
1 /*
2  * Copyright © 2018  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): Garret Rieger
25  */
26
27 #include <stdbool.h>
28
29 #include "hb-test.h"
30 #include "hb-subset-test.h"
31
32 /* Unit tests for hb-subset-glyf.h */
33
34 static void check_maxp_field (uint8_t *raw_maxp, unsigned int offset, uint16_t expected_value)
35 {
36   uint16_t actual_value = (raw_maxp[offset] << 8) + raw_maxp[offset + 1];
37   g_assert_cmpuint(expected_value, ==, actual_value);
38 }
39
40 static void check_maxp_num_glyphs (hb_face_t *face, uint16_t expected_num_glyphs, bool hints)
41 {
42   hb_blob_t *maxp_blob = hb_face_reference_table (face, HB_TAG ('m','a','x', 'p'));
43
44   unsigned int maxp_len;
45   uint8_t *raw_maxp = (uint8_t *) hb_blob_get_data(maxp_blob, &maxp_len);
46
47   check_maxp_field (raw_maxp, 4, expected_num_glyphs); // numGlyphs
48   if (!hints)
49   {
50     check_maxp_field (raw_maxp, 14, 1); // maxZones
51     check_maxp_field (raw_maxp, 16, 0); // maxTwilightPoints
52     check_maxp_field (raw_maxp, 18, 0); // maxStorage
53     check_maxp_field (raw_maxp, 20, 0); // maxFunctionDefs
54     check_maxp_field (raw_maxp, 22, 0); // maxInstructionDefs
55     check_maxp_field (raw_maxp, 24, 0); // maxStackElements
56     check_maxp_field (raw_maxp, 26, 0); // maxSizeOfInstructions
57   }
58
59   hb_blob_destroy (maxp_blob);
60 }
61
62 static void
63 test_subset_glyf (void)
64 {
65   hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Roboto-Regular.abc.ttf");
66   hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Roboto-Regular.ac.ttf");
67
68   hb_set_t *codepoints = hb_set_create();
69   hb_set_add (codepoints, 97);
70   hb_set_add (codepoints, 99);
71   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
72   hb_set_destroy (codepoints);
73
74   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
75   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
76   check_maxp_num_glyphs(face_abc_subset, 3, true);
77
78   hb_face_destroy (face_abc_subset);
79   hb_face_destroy (face_abc);
80   hb_face_destroy (face_ac);
81 }
82
83 static void
84 test_subset_glyf_with_components (void)
85 {
86   hb_face_t *face_components = hb_subset_test_open_font ("fonts/Roboto-Regular.components.ttf");
87   hb_face_t *face_subset = hb_subset_test_open_font ("fonts/Roboto-Regular.components.subset.ttf");
88
89   hb_set_t *codepoints = hb_set_create();
90   hb_set_add (codepoints, 0x1fc);
91   hb_face_t *face_generated_subset = hb_subset_test_create_subset (face_components, hb_subset_test_create_input (codepoints));
92   hb_set_destroy (codepoints);
93
94   hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
95   hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
96   check_maxp_num_glyphs(face_generated_subset, 4, true);
97
98   hb_face_destroy (face_generated_subset);
99   hb_face_destroy (face_subset);
100   hb_face_destroy (face_components);
101 }
102
103 static void
104 test_subset_glyf_noop (void)
105 {
106   hb_face_t *face_abc = hb_subset_test_open_font("fonts/Roboto-Regular.abc.ttf");
107
108   hb_set_t *codepoints = hb_set_create();
109   hb_set_add (codepoints, 97);
110   hb_set_add (codepoints, 98);
111   hb_set_add (codepoints, 99);
112   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
113   hb_set_destroy (codepoints);
114
115   hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('g','l','y','f'));
116   hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('l','o','c', 'a'));
117   check_maxp_num_glyphs(face_abc_subset, 4, true);
118
119   hb_face_destroy (face_abc_subset);
120   hb_face_destroy (face_abc);
121 }
122
123 static void
124 test_subset_glyf_strip_hints_simple (void)
125 {
126   hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Roboto-Regular.abc.ttf");
127   hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Roboto-Regular.ac.nohints.ttf");
128
129   hb_set_t *codepoints = hb_set_create();
130   hb_set_add (codepoints, 'a');
131   hb_set_add (codepoints, 'c');
132   hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
133   *hb_subset_input_drop_hints(input) = true;
134   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, input);
135   hb_set_destroy (codepoints);
136
137   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('l','o','c', 'a'));
138   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('g','l','y','f'));
139   check_maxp_num_glyphs(face_abc_subset, 3, false);
140
141   hb_face_destroy (face_abc_subset);
142   hb_face_destroy (face_abc);
143   hb_face_destroy (face_ac);
144 }
145
146 static void
147 test_subset_glyf_strip_hints_composite (void)
148 {
149   hb_face_t *face_components = hb_subset_test_open_font ("fonts/Roboto-Regular.components.ttf");
150   hb_face_t *face_subset = hb_subset_test_open_font ("fonts/Roboto-Regular.components.1fc.nohints.ttf");
151
152   hb_set_t *codepoints = hb_set_create();
153   hb_set_add (codepoints, 0x1fc);
154   hb_subset_input_t *input = hb_subset_test_create_input (codepoints);
155   *hb_subset_input_drop_hints(input) = true;
156
157   hb_face_t *face_generated_subset = hb_subset_test_create_subset (face_components, input);
158   hb_set_destroy (codepoints);
159
160   hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('g','l','y','f'));
161   hb_subset_test_check (face_subset, face_generated_subset, HB_TAG ('l','o','c', 'a'));
162   check_maxp_num_glyphs(face_generated_subset, 4, false);
163
164   hb_face_destroy (face_generated_subset);
165   hb_face_destroy (face_subset);
166   hb_face_destroy (face_components);
167 }
168
169 // TODO(grieger): test for long loca generation.
170
171 int
172 main (int argc, char **argv)
173 {
174   hb_test_init (&argc, &argv);
175
176   hb_test_add (test_subset_glyf_noop);
177   hb_test_add (test_subset_glyf);
178   hb_test_add (test_subset_glyf_strip_hints_simple);
179   hb_test_add (test_subset_glyf_strip_hints_composite);
180   hb_test_add (test_subset_glyf_with_components);
181
182   return hb_test_run();
183 }