Imported Upstream version 1.7.6
[platform/upstream/harfbuzz.git] / test / api / test-subset-hmtx.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): Roderick Sheeter
25  */
26
27 #include <stdbool.h>
28
29 #include "hb-test.h"
30 #include "hb-subset-test.h"
31
32 /* Unit tests for hmtx subsetting */
33
34 static void check_num_hmetrics(hb_face_t *face, uint16_t expected_num_hmetrics)
35 {
36   hb_blob_t *hhea_blob = hb_face_reference_table (face, HB_TAG ('h','h','e','a'));
37   hb_blob_t *hmtx_blob = hb_face_reference_table (face, HB_TAG ('h','m','t','x'));
38
39   // TODO I sure wish I could just use the hmtx table struct!
40   unsigned int hhea_len;
41   uint8_t *raw_hhea = (uint8_t *) hb_blob_get_data(hhea_blob, &hhea_len);
42   uint16_t num_hmetrics = (raw_hhea[hhea_len - 2] << 8) + raw_hhea[hhea_len - 1];
43   g_assert_cmpuint(expected_num_hmetrics, ==, num_hmetrics);
44
45   hb_blob_destroy (hhea_blob);
46   hb_blob_destroy (hmtx_blob);
47 }
48
49 static void
50 test_subset_hmtx_simple_subset (void)
51 {
52   hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Roboto-Regular.abc.ttf");
53   hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Roboto-Regular.ac.ttf");
54
55   hb_set_t *codepoints = hb_set_create ();
56   hb_set_add (codepoints, 'a');
57   hb_set_add (codepoints, 'c');
58   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
59   hb_set_destroy (codepoints);
60
61   check_num_hmetrics(face_abc_subset, 3); /* nothing has same width */
62   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('h','m','t','x'));
63
64   hb_face_destroy (face_abc_subset);
65   hb_face_destroy (face_abc);
66   hb_face_destroy (face_ac);
67 }
68
69
70 static void
71 test_subset_hmtx_monospace (void)
72 {
73   hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Inconsolata-Regular.abc.ttf");
74   hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Inconsolata-Regular.ac.ttf");
75
76   hb_set_t *codepoints = hb_set_create ();
77   hb_set_add (codepoints, 'a');
78   hb_set_add (codepoints, 'c');
79   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
80   hb_set_destroy (codepoints);
81
82   check_num_hmetrics(face_abc_subset, 1); /* everything has same width */
83   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('h','m','t','x'));
84
85   hb_face_destroy (face_abc_subset);
86   hb_face_destroy (face_abc);
87   hb_face_destroy (face_ac);
88 }
89
90
91 static void
92 test_subset_hmtx_keep_num_metrics (void)
93 {
94   hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Inconsolata-Regular.abc.widerc.ttf");
95   hb_face_t *face_ac = hb_subset_test_open_font ("fonts/Inconsolata-Regular.ac.widerc.ttf");
96
97   hb_set_t *codepoints = hb_set_create ();
98   hb_set_add (codepoints, 'a');
99   hb_set_add (codepoints, 'c');
100   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
101   hb_set_destroy (codepoints);
102
103   check_num_hmetrics(face_abc_subset, 3); /* c is wider */
104   hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('h','m','t','x'));
105
106   hb_face_destroy (face_abc_subset);
107   hb_face_destroy (face_abc);
108   hb_face_destroy (face_ac);
109 }
110
111 static void
112 test_subset_hmtx_decrease_num_metrics (void)
113 {
114   hb_face_t *face_abc = hb_subset_test_open_font ("fonts/Inconsolata-Regular.abc.widerc.ttf");
115   hb_face_t *face_ab = hb_subset_test_open_font ("fonts/Inconsolata-Regular.ab.ttf");
116
117   hb_set_t *codepoints = hb_set_create ();
118   hb_set_add (codepoints, 'a');
119   hb_set_add (codepoints, 'b');
120   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
121   hb_set_destroy (codepoints);
122
123   check_num_hmetrics(face_abc_subset, 1); /* everything left has same width */
124   hb_subset_test_check (face_ab, face_abc_subset, HB_TAG ('h','m','t','x'));
125
126   hb_face_destroy (face_abc_subset);
127   hb_face_destroy (face_abc);
128   hb_face_destroy (face_ab);
129 }
130
131 static void
132 test_subset_hmtx_noop (void)
133 {
134   hb_face_t *face_abc = hb_subset_test_open_font("fonts/Roboto-Regular.abc.ttf");
135
136   hb_set_t *codepoints = hb_set_create();
137   hb_set_add (codepoints, 'a');
138   hb_set_add (codepoints, 'b');
139   hb_set_add (codepoints, 'c');
140   hb_face_t *face_abc_subset = hb_subset_test_create_subset (face_abc, hb_subset_test_create_input (codepoints));
141   hb_set_destroy (codepoints);
142
143   check_num_hmetrics(face_abc_subset, 4); /* nothing has same width */
144   hb_subset_test_check (face_abc, face_abc_subset, HB_TAG ('h','m','t','x'));
145
146   hb_face_destroy (face_abc_subset);
147   hb_face_destroy (face_abc);
148 }
149
150 int
151 main (int argc, char **argv)
152 {
153   hb_test_init (&argc, &argv);
154
155   hb_test_add (test_subset_hmtx_simple_subset);
156   hb_test_add (test_subset_hmtx_monospace);
157   hb_test_add (test_subset_hmtx_keep_num_metrics);
158   hb_test_add (test_subset_hmtx_decrease_num_metrics);
159   hb_test_add (test_subset_hmtx_noop);
160
161   return hb_test_run();
162 }