1 /*
2 * Copyright © 2023 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 #ifndef HELPER_SUBSET_HH
27 #define HELPER_SUBSET_HH
28
29 #include "glib.h"
30 #include <errno.h>
31 #include <math.h>
32 #include <stdbool.h>
33 #include "hb-subset.h"
34
35 #ifndef HB_NO_VAR
36
37 static gboolean
parse_instancing_spec(const char * arg,hb_face_t * face,hb_subset_input_t * input,GError ** error)38 parse_instancing_spec (const char *arg,
39 hb_face_t* face,
40 hb_subset_input_t* input,
41 GError **error)
42 {
43 char* s;
44 while ((s = strtok((char *) arg, "=")))
45 {
46 arg = NULL;
47 unsigned len = strlen (s);
48 if (len > 4) //Axis tags are 4 bytes.
49 {
50 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
51 "Failed parsing axis tag at: '%s'", s);
52 return false;
53 }
54
55 /* support *=drop */
56 if (0 == strcmp (s, "*"))
57 {
58 s = strtok(NULL, ", ");
59 if (0 != strcmp (s, "drop"))
60 {
61 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
62 "Failed parsing axis position at: '%s'", s);
63 return false;
64 }
65
66 if (!hb_subset_input_pin_all_axes_to_default (input, face))
67 {
68 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
69 "Failed pinning all axes to default.");
70 return false;
71 }
72 continue;
73 }
74
75 hb_tag_t axis_tag = hb_tag_from_string (s, len);
76
77 s = strtok(NULL, ", ");
78 if (!s)
79 {
80 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
81 "Value not specified for axis: %c%c%c%c", HB_UNTAG (axis_tag));
82 return false;
83 }
84
85 if (strcmp (s, "drop") == 0) {
86 if (!hb_subset_input_pin_axis_to_default (input,
87 face,
88 axis_tag))
89 {
90 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
91 "Cannot pin axis: '%c%c%c%c', not present in fvar", HB_UNTAG (axis_tag));
92 return false;
93 }
94 continue;
95 }
96 float min, def, max;
97 if (!hb_subset_axis_range_from_string(s, -1, &min, &max, &def))
98 return false;
99
100 if (!hb_subset_input_set_axis_range (input,
101 face, axis_tag,
102 min, max, def))
103 {
104 g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
105 "Cannot pin axis: '%c%c%c%c', not present in fvar", HB_UNTAG (axis_tag));
106 return false;
107 }
108 continue;
109 }
110
111 return true;
112 }
113
114 #endif
115
116 #endif
117