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, Rod Sheeter, Behdad Esfahbod
25  */
26 
27 #include "hb-subset.hh"
28 #include "hb-set.hh"
29 
30 /**
31  * hb_subset_input_create_or_fail:
32  *
33  * Creates a new subset input object.
34  *
35  * Return value: (transfer full): New subset input, or %NULL if failed. Destroy
36  * with hb_subset_input_destroy().
37  *
38  * Since: 1.8.0
39  **/
40 hb_subset_input_t *
hb_subset_input_create_or_fail(void)41 hb_subset_input_create_or_fail (void)
42 {
43   hb_subset_input_t *input = hb_object_create<hb_subset_input_t>();
44 
45   if (unlikely (!input))
46     return nullptr;
47 
48   for (auto& set : input->sets_iter ())
49     set = hb_set_create ();
50 
51   if (input->in_error ())
52   {
53     hb_subset_input_destroy (input);
54     return nullptr;
55   }
56 
57   input->flags = HB_SUBSET_FLAGS_DEFAULT;
58 
59   hb_set_add_range (input->sets.name_ids, 0, 6);
60   hb_set_add (input->sets.name_languages, 0x0409);
61 
62   hb_tag_t default_drop_tables[] = {
63     // Layout disabled by default
64     HB_TAG ('m', 'o', 'r', 'x'),
65     HB_TAG ('m', 'o', 'r', 't'),
66     HB_TAG ('k', 'e', 'r', 'x'),
67     HB_TAG ('k', 'e', 'r', 'n'),
68 
69     // Copied from fontTools:
70     HB_TAG ('B', 'A', 'S', 'E'),
71     HB_TAG ('J', 'S', 'T', 'F'),
72     HB_TAG ('D', 'S', 'I', 'G'),
73     HB_TAG ('E', 'B', 'D', 'T'),
74     HB_TAG ('E', 'B', 'L', 'C'),
75     HB_TAG ('E', 'B', 'S', 'C'),
76     HB_TAG ('S', 'V', 'G', ' '),
77     HB_TAG ('P', 'C', 'L', 'T'),
78     HB_TAG ('L', 'T', 'S', 'H'),
79     // Graphite tables
80     HB_TAG ('F', 'e', 'a', 't'),
81     HB_TAG ('G', 'l', 'a', 't'),
82     HB_TAG ('G', 'l', 'o', 'c'),
83     HB_TAG ('S', 'i', 'l', 'f'),
84     HB_TAG ('S', 'i', 'l', 'l'),
85   };
86   input->sets.drop_tables->add_array (default_drop_tables, ARRAY_LENGTH (default_drop_tables));
87 
88   hb_tag_t default_no_subset_tables[] = {
89     HB_TAG ('a', 'v', 'a', 'r'),
90     HB_TAG ('f', 'v', 'a', 'r'),
91     HB_TAG ('g', 'a', 's', 'p'),
92     HB_TAG ('c', 'v', 't', ' '),
93     HB_TAG ('f', 'p', 'g', 'm'),
94     HB_TAG ('p', 'r', 'e', 'p'),
95     HB_TAG ('V', 'D', 'M', 'X'),
96     HB_TAG ('D', 'S', 'I', 'G'),
97     HB_TAG ('M', 'V', 'A', 'R'),
98     HB_TAG ('c', 'v', 'a', 'r'),
99     HB_TAG ('S', 'T', 'A', 'T'),
100   };
101   input->sets.no_subset_tables->add_array (default_no_subset_tables,
102                                          ARRAY_LENGTH (default_no_subset_tables));
103 
104   //copied from _layout_features_groups in fonttools
105   hb_tag_t default_layout_features[] = {
106     // default shaper
107     // common
108     HB_TAG ('r', 'v', 'r', 'n'),
109     HB_TAG ('c', 'c', 'm', 'p'),
110     HB_TAG ('l', 'i', 'g', 'a'),
111     HB_TAG ('l', 'o', 'c', 'l'),
112     HB_TAG ('m', 'a', 'r', 'k'),
113     HB_TAG ('m', 'k', 'm', 'k'),
114     HB_TAG ('r', 'l', 'i', 'g'),
115 
116     //fractions
117     HB_TAG ('f', 'r', 'a', 'c'),
118     HB_TAG ('n', 'u', 'm', 'r'),
119     HB_TAG ('d', 'n', 'o', 'm'),
120 
121     //horizontal
122     HB_TAG ('c', 'a', 'l', 't'),
123     HB_TAG ('c', 'l', 'i', 'g'),
124     HB_TAG ('c', 'u', 'r', 's'),
125     HB_TAG ('k', 'e', 'r', 'n'),
126     HB_TAG ('r', 'c', 'l', 't'),
127 
128     //vertical
129     HB_TAG ('v', 'a', 'l', 't'),
130     HB_TAG ('v', 'e', 'r', 't'),
131     HB_TAG ('v', 'k', 'r', 'n'),
132     HB_TAG ('v', 'p', 'a', 'l'),
133     HB_TAG ('v', 'r', 't', '2'),
134 
135     //ltr
136     HB_TAG ('l', 't', 'r', 'a'),
137     HB_TAG ('l', 't', 'r', 'm'),
138 
139     //rtl
140     HB_TAG ('r', 't', 'l', 'a'),
141     HB_TAG ('r', 't', 'l', 'm'),
142 
143     //Complex shapers
144     //arabic
145     HB_TAG ('i', 'n', 'i', 't'),
146     HB_TAG ('m', 'e', 'd', 'i'),
147     HB_TAG ('f', 'i', 'n', 'a'),
148     HB_TAG ('i', 's', 'o', 'l'),
149     HB_TAG ('m', 'e', 'd', '2'),
150     HB_TAG ('f', 'i', 'n', '2'),
151     HB_TAG ('f', 'i', 'n', '3'),
152     HB_TAG ('c', 's', 'w', 'h'),
153     HB_TAG ('m', 's', 'e', 't'),
154     HB_TAG ('s', 't', 'c', 'h'),
155 
156     //hangul
157     HB_TAG ('l', 'j', 'm', 'o'),
158     HB_TAG ('v', 'j', 'm', 'o'),
159     HB_TAG ('t', 'j', 'm', 'o'),
160 
161     //tibetan
162     HB_TAG ('a', 'b', 'v', 's'),
163     HB_TAG ('b', 'l', 'w', 's'),
164     HB_TAG ('a', 'b', 'v', 'm'),
165     HB_TAG ('b', 'l', 'w', 'm'),
166 
167     //indic
168     HB_TAG ('n', 'u', 'k', 't'),
169     HB_TAG ('a', 'k', 'h', 'n'),
170     HB_TAG ('r', 'p', 'h', 'f'),
171     HB_TAG ('r', 'k', 'r', 'f'),
172     HB_TAG ('p', 'r', 'e', 'f'),
173     HB_TAG ('b', 'l', 'w', 'f'),
174     HB_TAG ('h', 'a', 'l', 'f'),
175     HB_TAG ('a', 'b', 'v', 'f'),
176     HB_TAG ('p', 's', 't', 'f'),
177     HB_TAG ('c', 'f', 'a', 'r'),
178     HB_TAG ('v', 'a', 't', 'u'),
179     HB_TAG ('c', 'j', 'c', 't'),
180     HB_TAG ('i', 'n', 'i', 't'),
181     HB_TAG ('p', 'r', 'e', 's'),
182     HB_TAG ('a', 'b', 'v', 's'),
183     HB_TAG ('b', 'l', 'w', 's'),
184     HB_TAG ('p', 's', 't', 's'),
185     HB_TAG ('h', 'a', 'l', 'n'),
186     HB_TAG ('d', 'i', 's', 't'),
187     HB_TAG ('a', 'b', 'v', 'm'),
188     HB_TAG ('b', 'l', 'w', 'm'),
189   };
190 
191   input->sets.layout_features->add_array (default_layout_features, ARRAY_LENGTH (default_layout_features));
192 
193   if (input->in_error ())
194   {
195     hb_subset_input_destroy (input);
196     return nullptr;
197   }
198   return input;
199 }
200 
201 /**
202  * hb_subset_input_reference: (skip)
203  * @input: a #hb_subset_input_t object.
204  *
205  * Increases the reference count on @input.
206  *
207  * Return value: @input.
208  *
209  * Since: 1.8.0
210  **/
211 hb_subset_input_t *
hb_subset_input_reference(hb_subset_input_t * input)212 hb_subset_input_reference (hb_subset_input_t *input)
213 {
214   return hb_object_reference (input);
215 }
216 
217 /**
218  * hb_subset_input_destroy:
219  * @input: a #hb_subset_input_t object.
220  *
221  * Decreases the reference count on @input, and if it reaches zero, destroys
222  * @input, freeing all memory.
223  *
224  * Since: 1.8.0
225  **/
226 void
hb_subset_input_destroy(hb_subset_input_t * input)227 hb_subset_input_destroy (hb_subset_input_t *input)
228 {
229   if (!hb_object_destroy (input)) return;
230 
231   for (hb_set_t* set : input->sets_iter ())
232     hb_set_destroy (set);
233 
234   hb_free (input);
235 }
236 
237 /**
238  * hb_subset_input_unicode_set:
239  * @input: a #hb_subset_input_t object.
240  *
241  * Gets the set of Unicode code points to retain, the caller should modify the
242  * set as needed.
243  *
244  * Return value: (transfer none): pointer to the #hb_set_t of Unicode code
245  * points.
246  *
247  * Since: 1.8.0
248  **/
249 HB_EXTERN hb_set_t *
hb_subset_input_unicode_set(hb_subset_input_t * input)250 hb_subset_input_unicode_set (hb_subset_input_t *input)
251 {
252   return input->sets.unicodes;
253 }
254 
255 /**
256  * hb_subset_input_glyph_set:
257  * @input: a #hb_subset_input_t object.
258  *
259  * Gets the set of glyph IDs to retain, the caller should modify the set as
260  * needed.
261  *
262  * Return value: (transfer none): pointer to the #hb_set_t of glyph IDs.
263  *
264  * Since: 1.8.0
265  **/
266 HB_EXTERN hb_set_t *
hb_subset_input_glyph_set(hb_subset_input_t * input)267 hb_subset_input_glyph_set (hb_subset_input_t *input)
268 {
269   return input->sets.glyphs;
270 }
271 
272 /**
273  * hb_subset_input_set:
274  * @input: a #hb_subset_input_t object.
275  * @set_type: a #hb_subset_sets_t set type.
276  *
277  * Gets the set of the specified type.
278  *
279  * Return value: (transfer none): pointer to the #hb_set_t of the specified type.
280  *
281  * Since: 2.9.1
282  **/
283 HB_EXTERN hb_set_t *
hb_subset_input_set(hb_subset_input_t * input,hb_subset_sets_t set_type)284 hb_subset_input_set (hb_subset_input_t *input, hb_subset_sets_t set_type)
285 {
286   return input->sets_iter () [set_type];
287 }
288 
289 /**
290  * hb_subset_input_get_flags:
291  * @input: a #hb_subset_input_t object.
292  *
293  * Gets all of the subsetting flags in the input object.
294  *
295  * Return value: the subsetting flags bit field.
296  *
297  * Since: 2.9.0
298  **/
299 HB_EXTERN hb_subset_flags_t
hb_subset_input_get_flags(hb_subset_input_t * input)300 hb_subset_input_get_flags (hb_subset_input_t *input)
301 {
302   return (hb_subset_flags_t) input->flags;
303 }
304 
305 /**
306  * hb_subset_input_set_flags:
307  * @input: a #hb_subset_input_t object.
308  * @value: bit field of flags
309  *
310  * Sets all of the flags in the input object to the values specified by the bit
311  * field.
312  *
313  * Since: 2.9.0
314  **/
315 HB_EXTERN void
hb_subset_input_set_flags(hb_subset_input_t * input,unsigned value)316 hb_subset_input_set_flags (hb_subset_input_t *input,
317 			   unsigned value)
318 {
319   input->flags = (hb_subset_flags_t) value;
320 }
321 
322 /**
323  * hb_subset_input_set_user_data: (skip)
324  * @input: a #hb_subset_input_t object.
325  * @key: The user-data key to set
326  * @data: A pointer to the user data
327  * @destroy: (nullable): A callback to call when @data is not needed anymore
328  * @replace: Whether to replace an existing data with the same key
329  *
330  * Attaches a user-data key/data pair to the given subset input object.
331  *
332  * Return value: %true if success, %false otherwise
333  *
334  * Since: 2.9.0
335  **/
336 hb_bool_t
hb_subset_input_set_user_data(hb_subset_input_t * input,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)337 hb_subset_input_set_user_data (hb_subset_input_t  *input,
338 			       hb_user_data_key_t *key,
339 			       void *		   data,
340 			       hb_destroy_func_t   destroy,
341 			       hb_bool_t	   replace)
342 {
343   return hb_object_set_user_data (input, key, data, destroy, replace);
344 }
345 
346 /**
347  * hb_subset_input_get_user_data: (skip)
348  * @input: a #hb_subset_input_t object.
349  * @key: The user-data key to query
350  *
351  * Fetches the user data associated with the specified key,
352  * attached to the specified subset input object.
353  *
354  * Return value: (transfer none): A pointer to the user data
355  *
356  * Since: 2.9.0
357  **/
358 void *
hb_subset_input_get_user_data(const hb_subset_input_t * input,hb_user_data_key_t * key)359 hb_subset_input_get_user_data (const hb_subset_input_t *input,
360 			       hb_user_data_key_t     *key)
361 {
362   return hb_object_get_user_data (input, key);
363 }
364