2 * Copyright © 2018 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): Roderick Sheeter
30 #include "hb-subset-test.h"
32 /* Unit tests for hmtx subsetting */
34 static void check_num_hmetrics(hb_face_t *face, uint16_t expected_num_hmetrics)
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'));
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);
45 hb_blob_destroy (hhea_blob);
46 hb_blob_destroy (hmtx_blob);
50 test_subset_hmtx_simple_subset (void)
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");
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);
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'));
64 hb_face_destroy (face_abc_subset);
65 hb_face_destroy (face_abc);
66 hb_face_destroy (face_ac);
71 test_subset_hmtx_monospace (void)
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");
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);
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'));
85 hb_face_destroy (face_abc_subset);
86 hb_face_destroy (face_abc);
87 hb_face_destroy (face_ac);
92 test_subset_hmtx_keep_num_metrics (void)
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");
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);
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'));
106 hb_face_destroy (face_abc_subset);
107 hb_face_destroy (face_abc);
108 hb_face_destroy (face_ac);
112 test_subset_hmtx_decrease_num_metrics (void)
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");
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);
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'));
126 hb_face_destroy (face_abc_subset);
127 hb_face_destroy (face_abc);
128 hb_face_destroy (face_ab);
132 test_subset_hmtx_noop (void)
134 hb_face_t *face_abc = hb_subset_test_open_font("fonts/Roboto-Regular.abc.ttf");
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);
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'));
146 hb_face_destroy (face_abc_subset);
147 hb_face_destroy (face_abc);
151 main (int argc, char **argv)
153 hb_test_init (&argc, &argv);
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);
161 return hb_test_run();