blob: 4c4957bd71175196d717282481ed14214ee2f859 [file] [log] [blame]
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -08001/*
2 * Copyright © 2017 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): Behdad Esfahbod
25 */
26
27#ifndef HB_OT_VAR_FVAR_TABLE_HH
28#define HB_OT_VAR_FVAR_TABLE_HH
29
Behdad Esfahbodc77ae402018-08-25 22:36:36 -070030#include "hb-open-type.hh"
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -080031
Ebrahim Byagowia02c3ee2018-04-12 13:38:19 +043032/*
33 * fvar -- Font Variations
34 * https://docs.microsoft.com/en-us/typography/opentype/spec/fvar
35 */
36
37#define HB_OT_TAG_fvar HB_TAG('f','v','a','r')
38
39
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -080040namespace OT {
41
Qunxin Liu39ac79a2023-05-10 10:22:49 -070042static bool axis_coord_pinned_or_within_axis_range (const hb_array_t<const F16DOT16> coords,
43 unsigned axis_index,
44 Triple axis_limit)
45{
46 float axis_coord = coords[axis_index].to_float ();
47 if (axis_limit.is_point ())
48 {
49 if (axis_limit.minimum != axis_coord)
50 return false;
51 }
52 else
53 {
54 if (axis_coord < axis_limit.minimum ||
55 axis_coord > axis_limit.maximum)
56 return false;
57 }
58 return true;
59}
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -080060
61struct InstanceRecord
62{
Behdad Esfahbod587d49f2018-11-19 14:27:19 -050063 friend struct fvar;
64
Behdad Esfahbod8c29dca2022-10-13 12:04:32 -060065 hb_array_t<const F16DOT16> get_coordinates (unsigned int axis_count) const
Behdad Esfahbod587d49f2018-11-19 14:27:19 -050066 { return coordinatesZ.as_array (axis_count); }
67
Qunxin Liu51c74512023-05-01 13:38:02 -070068 bool keep_instance (unsigned axis_count,
69 const hb_map_t *axes_index_tag_map,
70 const hb_hashmap_t<hb_tag_t, Triple> *axes_location) const
71 {
72 if (axes_location->is_empty ()) return true;
73 const hb_array_t<const F16DOT16> coords = get_coordinates (axis_count);
74 for (unsigned i = 0 ; i < axis_count; i++)
75 {
76 uint32_t *axis_tag;
77 if (!axes_index_tag_map->has (i, &axis_tag))
78 return false;
79 if (!axes_location->has (*axis_tag))
80 continue;
81
82 Triple axis_limit = axes_location->get (*axis_tag);
Qunxin Liu39ac79a2023-05-10 10:22:49 -070083 if (!axis_coord_pinned_or_within_axis_range (coords, i, axis_limit))
84 return false;
Qunxin Liu51c74512023-05-01 13:38:02 -070085 }
86 return true;
87 }
88
Qunxin Liu0a6c16a2022-08-08 13:47:39 -070089 bool subset (hb_subset_context_t *c,
90 unsigned axis_count,
91 bool has_postscript_nameid) const
92 {
93 TRACE_SUBSET (this);
94 if (unlikely (!c->serializer->embed (subfamilyNameID))) return_trace (false);
95 if (unlikely (!c->serializer->embed (flags))) return_trace (false);
96
Behdad Esfahbod8c29dca2022-10-13 12:04:32 -060097 const hb_array_t<const F16DOT16> coords = get_coordinates (axis_count);
Qunxin Liu51c74512023-05-01 13:38:02 -070098 const hb_hashmap_t<hb_tag_t, Triple> *axes_location = &c->plan->user_axes_location;
Qunxin Liu0a6c16a2022-08-08 13:47:39 -070099 for (unsigned i = 0 ; i < axis_count; i++)
100 {
Joel Autersonc813f842022-10-20 19:45:23 +0100101 uint32_t *axis_tag;
Qunxin Liu73d94db2023-07-19 10:33:57 -0700102 Triple *axis_limit;
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700103 // only keep instances whose coordinates == pinned axis location
Qunxin Liu73d94db2023-07-19 10:33:57 -0700104 if (!c->plan->axes_old_index_tag_map.has (i, &axis_tag)) return_trace (false);
105 if (axes_location->has (*axis_tag, &axis_limit))
Qunxin Liu51c74512023-05-01 13:38:02 -0700106 {
Qunxin Liu73d94db2023-07-19 10:33:57 -0700107 if (!axis_coord_pinned_or_within_axis_range (coords, i, *axis_limit))
Qunxin Liu39ac79a2023-05-10 10:22:49 -0700108 return_trace (false);
109
110 //skip pinned axis
Qunxin Liu73d94db2023-07-19 10:33:57 -0700111 if (axis_limit->is_point ())
Qunxin Liu51c74512023-05-01 13:38:02 -0700112 continue;
Qunxin Liu51c74512023-05-01 13:38:02 -0700113 }
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700114
115 if (!c->serializer->embed (coords[i]))
116 return_trace (false);
117 }
118
119 if (has_postscript_nameid)
120 {
121 NameID name_id;
122 name_id = StructAfter<NameID> (coords);
123 if (!c->serializer->embed (name_id))
124 return_trace (false);
125 }
126
127 return_trace (true);
128 }
129
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330130 bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800131 {
132 TRACE_SANITIZE (this);
133 return_trace (c->check_struct (this) &&
Behdad Esfahbod9507b052018-09-10 23:18:07 +0200134 c->check_array (coordinatesZ.arrayZ, axis_count));
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800135 }
136
137 protected:
Behdad Esfahboda0dccb62018-03-14 16:31:53 +0100138 NameID subfamilyNameID;/* The name ID for entries in the 'name' table
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800139 * that provide subfamily names for this instance. */
Behdad Esfahbodbd6b2ba2018-11-19 11:34:56 -0500140 HBUINT16 flags; /* Reserved for future use — set to 0. */
Behdad Esfahbod8c29dca2022-10-13 12:04:32 -0600141 UnsizedArrayOf<F16DOT16>
Behdad Esfahbodbc485a92018-09-10 23:02:24 +0200142 coordinatesZ; /* The coordinates array for this instance. */
Behdad Esfahboda0dccb62018-03-14 16:31:53 +0100143 //NameID postScriptNameIDX;/*Optional. The name ID for entries in the 'name'
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800144 // * table that provide PostScript names for this
145 // * instance. */
146
147 public:
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -0500148 DEFINE_SIZE_UNBOUNDED (4);
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800149};
150
151struct AxisRecord
152{
Ebrahim Byagowi08d57d92020-06-28 13:13:25 +0430153 int cmp (hb_tag_t key) const { return axisTag.cmp (key); }
154
Behdad Esfahbodbd6b2ba2018-11-19 11:34:56 -0500155 enum
156 {
157 AXIS_FLAG_HIDDEN = 0x0001,
158 };
159
Ebrahim Byagowi61c240f2020-06-20 13:24:32 +0430160#ifndef HB_DISABLE_DEPRECATED
161 void get_axis_deprecated (hb_ot_var_axis_t *info) const
162 {
163 info->tag = axisTag;
164 info->name_id = axisNameID;
Ebrahim Byagowiea8e7842020-06-30 12:54:29 +0430165 get_coordinates (info->min_value, info->default_value, info->max_value);
Ebrahim Byagowi61c240f2020-06-20 13:24:32 +0430166 }
167#endif
168
169 void get_axis_info (unsigned axis_index, hb_ot_var_axis_info_t *info) const
170 {
171 info->axis_index = axis_index;
172 info->tag = axisTag;
173 info->name_id = axisNameID;
174 info->flags = (hb_ot_var_axis_flags_t) (unsigned int) flags;
Ebrahim Byagowiea8e7842020-06-30 12:54:29 +0430175 get_coordinates (info->min_value, info->default_value, info->max_value);
Ebrahim Byagowi61c240f2020-06-20 13:24:32 +0430176 info->reserved = 0;
177 }
178
Qunxin Liu2a4773e2022-06-21 19:29:52 -0700179 hb_tag_t get_axis_tag () const { return axisTag; }
180
Ebrahim Byagowi5de07b82020-06-30 11:08:42 +0430181 int normalize_axis_value (float v) const
182 {
Ebrahim Byagowiea8e7842020-06-30 12:54:29 +0430183 float min_value, default_value, max_value;
184 get_coordinates (min_value, default_value, max_value);
Ebrahim Byagowi5de07b82020-06-30 11:08:42 +0430185
186 v = hb_clamp (v, min_value, max_value);
187
188 if (v == default_value)
189 return 0;
190 else if (v < default_value)
191 v = (v - default_value) / (default_value - min_value);
192 else
193 v = (v - default_value) / (max_value - default_value);
194 return roundf (v * 16384.f);
195 }
196
197 float unnormalize_axis_value (int v) const
198 {
Ebrahim Byagowiea8e7842020-06-30 12:54:29 +0430199 float min_value, default_value, max_value;
200 get_coordinates (min_value, default_value, max_value);
Ebrahim Byagowi5de07b82020-06-30 11:08:42 +0430201
202 if (v == 0)
203 return default_value;
204 else if (v < 0)
205 return v * (default_value - min_value) / 16384.f + default_value;
206 else
207 return v * (max_value - default_value) / 16384.f + default_value;
208 }
209
Ebrahim Byagowi98c42b32020-06-30 10:48:36 +0430210 hb_ot_name_id_t get_name_id () const { return axisNameID; }
211
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330212 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800213 {
214 TRACE_SANITIZE (this);
215 return_trace (c->check_struct (this));
216 }
217
Ebrahim Byagowiea8e7842020-06-30 12:54:29 +0430218 void get_coordinates (float &min, float &default_, float &max) const
Ebrahim Byagowi0f8dda22020-06-30 10:53:12 +0430219 {
Behdad Esfahbodc8486b62023-01-07 14:15:17 -0700220 default_ = defaultValue.to_float ();
Ebrahim Byagowi0f8dda22020-06-30 10:53:12 +0430221 /* Ensure order, to simplify client math. */
Behdad Esfahbodc8486b62023-01-07 14:15:17 -0700222 min = hb_min (default_, minValue.to_float ());
223 max = hb_max (default_, maxValue.to_float ());
Ebrahim Byagowi0f8dda22020-06-30 10:53:12 +0430224 }
225
Behdad Esfahbod473a5e52022-07-23 13:25:40 -0600226 float get_default () const
227 {
Behdad Esfahbodc8486b62023-01-07 14:15:17 -0700228 return defaultValue.to_float ();
Behdad Esfahbod473a5e52022-07-23 13:25:40 -0600229 }
230
Qunxin Liu165f3e62023-07-17 09:46:03 -0700231 TripleDistances get_triple_distances () const
232 {
233 float min, default_, max;
234 get_coordinates (min, default_, max);
235 return TripleDistances (min, default_, max);
236 }
237
Qunxin Liu73d94db2023-07-19 10:33:57 -0700238 bool subset (hb_subset_context_t *c) const
239 {
240 TRACE_SUBSET (this);
241 auto *out = c->serializer->embed (this);
242 if (unlikely (!out)) return_trace (false);
243
244 const hb_hashmap_t<hb_tag_t, Triple>& user_axes_location = c->plan->user_axes_location;
245 Triple *axis_limit;
246 if (user_axes_location.has (axisTag, &axis_limit))
247 {
248 out->minValue.set_float (axis_limit->minimum);
249 out->defaultValue.set_float (axis_limit->middle);
250 out->maxValue.set_float (axis_limit->maximum);
251 }
252 return_trace (true);
253 }
254
Behdad Esfahbod0a44fea2021-07-22 12:18:48 -0700255 public:
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800256 Tag axisTag; /* Tag identifying the design variation for the axis. */
Behdad Esfahbod0a44fea2021-07-22 12:18:48 -0700257 protected:
Behdad Esfahbod8c29dca2022-10-13 12:04:32 -0600258 F16DOT16 minValue; /* The minimum coordinate value for the axis. */
259 F16DOT16 defaultValue; /* The default coordinate value for the axis. */
260 F16DOT16 maxValue; /* The maximum coordinate value for the axis. */
Behdad Esfahbod0a44fea2021-07-22 12:18:48 -0700261 public:
Behdad Esfahbodbd6b2ba2018-11-19 11:34:56 -0500262 HBUINT16 flags; /* Axis flags. */
Behdad Esfahboda0dccb62018-03-14 16:31:53 +0100263 NameID axisNameID; /* The name ID for entries in the 'name' table that
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800264 * provide a display name for this axis. */
265
266 public:
267 DEFINE_SIZE_STATIC (20);
268};
269
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800270struct fvar
271{
Behdad Esfahbodef006542019-01-22 12:08:57 +0100272 static constexpr hb_tag_t tableTag = HB_OT_TAG_fvar;
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800273
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330274 bool has_data () const { return version.to_int (); }
Behdad Esfahbodb912fbe2018-08-06 06:30:12 -0700275
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330276 bool sanitize (hb_sanitize_context_t *c) const
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800277 {
278 TRACE_SANITIZE (this);
279 return_trace (version.sanitize (c) &&
Behdad Esfahbod422c0c32017-01-20 19:14:54 -0800280 likely (version.major == 1) &&
281 c->check_struct (this) &&
Behdad Esfahbod56c92382018-11-19 13:09:53 -0500282 axisSize == 20 && /* Assumed in our code. */
Behdad Esfahbod422c0c32017-01-20 19:14:54 -0800283 instanceSize >= axisCount * 4 + 4 &&
Behdad Esfahbod56c92382018-11-19 13:09:53 -0500284 get_axes ().sanitize (c) &&
Behdad Esfahbod6a683ea2023-07-07 21:02:15 -0600285 c->check_range (&StructAfter<InstanceRecord> (get_axes ()),
286 instanceCount, instanceSize));
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800287 }
288
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330289 unsigned int get_axis_count () const { return axisCount; }
Behdad Esfahbod422c0c32017-01-20 19:14:54 -0800290
Behdad Esfahbodfca27862019-05-11 00:37:01 -0700291#ifndef HB_DISABLE_DEPRECATED
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330292 unsigned int get_axes_deprecated (unsigned int start_offset,
293 unsigned int *axes_count /* IN/OUT */,
294 hb_ot_var_axis_t *axes_array /* OUT */) const
Behdad Esfahbod785982b2017-01-20 19:57:27 -0800295 {
296 if (axes_count)
297 {
Ebrahim Byagowi82ec9ef2020-06-21 01:36:16 +0430298 hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
Ebrahim Byagowi6924e292020-03-03 13:11:11 +0330299 for (unsigned i = 0; i < arr.length; ++i)
Ebrahim Byagowi82ec9ef2020-06-21 01:36:16 +0430300 arr[i].get_axis_deprecated (&axes_array[i]);
Behdad Esfahbod785982b2017-01-20 19:57:27 -0800301 }
302 return axisCount;
303 }
Behdad Esfahbodfca27862019-05-11 00:37:01 -0700304#endif
Behdad Esfahbod785982b2017-01-20 19:57:27 -0800305
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330306 unsigned int get_axis_infos (unsigned int start_offset,
307 unsigned int *axes_count /* IN/OUT */,
308 hb_ot_var_axis_info_t *axes_array /* OUT */) const
Behdad Esfahbodf48bb9a2018-11-20 20:40:55 -0500309 {
310 if (axes_count)
311 {
Ebrahim Byagowi82ec9ef2020-06-21 01:36:16 +0430312 hb_array_t<const AxisRecord> arr = get_axes ().sub_array (start_offset, axes_count);
Ebrahim Byagowi6924e292020-03-03 13:11:11 +0330313 for (unsigned i = 0; i < arr.length; ++i)
Ebrahim Byagowi82ec9ef2020-06-21 01:36:16 +0430314 arr[i].get_axis_info (start_offset + i, &axes_array[i]);
Behdad Esfahbodf48bb9a2018-11-20 20:40:55 -0500315 }
316 return axisCount;
317 }
318
Behdad Esfahbodfca27862019-05-11 00:37:01 -0700319#ifndef HB_DISABLE_DEPRECATED
Ebrahim Byagowi8bca9a42020-06-21 09:18:12 +0430320 bool
321 find_axis_deprecated (hb_tag_t tag, unsigned *axis_index, hb_ot_var_axis_t *info) const
Behdad Esfahbod422c0c32017-01-20 19:14:54 -0800322 {
Ebrahim Byagowi08d57d92020-06-28 13:13:25 +0430323 unsigned i;
324 if (!axis_index) axis_index = &i;
325 *axis_index = HB_OT_VAR_NO_AXIS_INDEX;
Ebrahim Byagowid5439232020-07-02 01:30:24 +0430326 auto axes = get_axes ();
Behdad Esfahbode9c0a742022-06-15 16:57:16 -0600327 return axes.lfind (tag, axis_index) && ((void) axes[*axis_index].get_axis_deprecated (info), true);
Behdad Esfahbodf48bb9a2018-11-20 20:40:55 -0500328 }
Behdad Esfahbodfca27862019-05-11 00:37:01 -0700329#endif
Ebrahim Byagowi8bca9a42020-06-21 09:18:12 +0430330 bool
331 find_axis_info (hb_tag_t tag, hb_ot_var_axis_info_t *info) const
Behdad Esfahbodf48bb9a2018-11-20 20:40:55 -0500332 {
Ebrahim Byagowi08d57d92020-06-28 13:13:25 +0430333 unsigned i;
Ebrahim Byagowid5439232020-07-02 01:30:24 +0430334 auto axes = get_axes ();
Behdad Esfahbode9c0a742022-06-15 16:57:16 -0600335 return axes.lfind (tag, &i) && ((void) axes[i].get_axis_info (i, info), true);
Behdad Esfahbod422c0c32017-01-20 19:14:54 -0800336 }
337
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330338 int normalize_axis_value (unsigned int axis_index, float v) const
Ebrahim Byagowi5de07b82020-06-30 11:08:42 +0430339 { return get_axes ()[axis_index].normalize_axis_value (v); }
Behdad Esfahbod422c0c32017-01-20 19:14:54 -0800340
Ebrahim Byagowi5de07b82020-06-30 11:08:42 +0430341 float unnormalize_axis_value (unsigned int axis_index, int v) const
342 { return get_axes ()[axis_index].unnormalize_axis_value (v); }
Ebrahim Byagowi981f5a52019-08-07 18:45:39 +0430343
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330344 unsigned int get_instance_count () const { return instanceCount; }
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500345
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330346 hb_ot_name_id_t get_instance_subfamily_name_id (unsigned int instance_index) const
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500347 {
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -0500348 const InstanceRecord *instance = get_instance (instance_index);
349 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
350 return instance->subfamilyNameID;
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500351 }
352
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330353 hb_ot_name_id_t get_instance_postscript_name_id (unsigned int instance_index) const
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500354 {
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -0500355 const InstanceRecord *instance = get_instance (instance_index);
356 if (unlikely (!instance)) return HB_OT_NAME_ID_INVALID;
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500357 if (instanceSize >= axisCount * 4 + 6)
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -0500358 return StructAfter<NameID> (instance->get_coordinates (axisCount));
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500359 return HB_OT_NAME_ID_INVALID;
360 }
361
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330362 unsigned int get_instance_coords (unsigned int instance_index,
Behdad Esfahbode6d6f4b2019-05-14 22:45:03 -0700363 unsigned int *coords_length, /* IN/OUT */
364 float *coords /* OUT */) const
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500365 {
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -0500366 const InstanceRecord *instance = get_instance (instance_index);
367 if (unlikely (!instance))
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500368 {
369 if (coords_length)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430370 *coords_length = 0;
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500371 return 0;
372 }
373
374 if (coords_length && *coords_length)
375 {
Behdad Esfahbod8c29dca2022-10-13 12:04:32 -0600376 hb_array_t<const F16DOT16> instanceCoords = instance->get_coordinates (axisCount)
Behdad Esfahbod1b51be52021-12-22 21:43:48 -0700377 .sub_array (0, coords_length);
Behdad Esfahbod474a1202018-12-21 18:46:51 -0500378 for (unsigned int i = 0; i < instanceCoords.length; i++)
Ebrahim Byagowia0b4ac42019-08-24 17:57:14 +0430379 coords[i] = instanceCoords.arrayZ[i].to_float ();
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500380 }
381 return axisCount;
382 }
383
Qunxin Liu51c74512023-05-01 13:38:02 -0700384 void collect_name_ids (hb_hashmap_t<hb_tag_t, Triple> *user_axes_location,
385 hb_map_t *axes_old_index_tag_map,
Qunxin Liube8e8e82022-06-30 14:24:36 -0700386 hb_set_t *nameids /* IN/OUT */) const
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700387 {
388 if (!has_data ()) return;
389
Qunxin Liube8e8e82022-06-30 14:24:36 -0700390 auto axis_records = get_axes ();
391 for (unsigned i = 0 ; i < (unsigned)axisCount; i++)
392 {
393 hb_tag_t axis_tag = axis_records[i].get_axis_tag ();
Qunxin Liu51c74512023-05-01 13:38:02 -0700394 if (user_axes_location->has (axis_tag) &&
395 user_axes_location->get (axis_tag).is_point ())
Qunxin Liube8e8e82022-06-30 14:24:36 -0700396 continue;
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700397
Qunxin Liube8e8e82022-06-30 14:24:36 -0700398 nameids->add (axis_records[i].get_name_id ());
399 }
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700400
Qunxin Liube8e8e82022-06-30 14:24:36 -0700401 for (unsigned i = 0 ; i < (unsigned)instanceCount; i++)
402 {
403 const InstanceRecord *instance = get_instance (i);
404
Qunxin Liu51c74512023-05-01 13:38:02 -0700405 if (!instance->keep_instance (axisCount, axes_old_index_tag_map, user_axes_location))
Qunxin Liube8e8e82022-06-30 14:24:36 -0700406 continue;
407
408 nameids->add (instance->subfamilyNameID);
409
410 if (instanceSize >= axisCount * 4 + 6)
411 {
412 unsigned post_script_name_id = StructAfter<NameID> (instance->get_coordinates (axisCount));
413 if (post_script_name_id != HB_OT_NAME_ID_INVALID) nameids->add (post_script_name_id);
414 }
415 }
Qunxin Liu02e5e5d2019-05-13 09:38:42 -0700416 }
417
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700418 bool subset (hb_subset_context_t *c) const
419 {
420 TRACE_SUBSET (this);
Behdad Esfahbod60418fc2023-01-11 12:33:25 -0700421 unsigned retained_axis_count = c->plan->axes_index_map.get_population ();
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700422 if (!retained_axis_count) //all axes are pinned
423 return_trace (false);
424
425 fvar *out = c->serializer->embed (this);
426 if (unlikely (!out)) return_trace (false);
427
428 if (!c->serializer->check_assign (out->axisCount, retained_axis_count, HB_SERIALIZE_ERROR_INT_OVERFLOW))
429 return_trace (false);
430
431 bool has_postscript_nameid = false;
432 if (instanceSize >= axisCount * 4 + 6)
433 has_postscript_nameid = true;
434
435 if (!c->serializer->check_assign (out->instanceSize, retained_axis_count * 4 + (has_postscript_nameid ? 6 : 4),
436 HB_SERIALIZE_ERROR_INT_OVERFLOW))
437 return_trace (false);
438
439 auto axes_records = get_axes ();
440 for (unsigned i = 0 ; i < (unsigned)axisCount; i++)
441 {
Behdad Esfahbod60418fc2023-01-11 12:33:25 -0700442 if (!c->plan->axes_index_map.has (i)) continue;
Qunxin Liu73d94db2023-07-19 10:33:57 -0700443 if (unlikely (!axes_records[i].subset (c)))
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700444 return_trace (false);
445 }
446
447 if (!c->serializer->check_assign (out->firstAxis, get_size (), HB_SERIALIZE_ERROR_INT_OVERFLOW))
448 return_trace (false);
449
Qunxin Liu73d94db2023-07-19 10:33:57 -0700450 unsigned num_retained_instances = 0;
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700451 for (unsigned i = 0 ; i < (unsigned)instanceCount; i++)
452 {
453 const InstanceRecord *instance = get_instance (i);
454 auto snap = c->serializer->snapshot ();
455 if (!instance->subset (c, axisCount, has_postscript_nameid))
456 c->serializer->revert (snap);
Qunxin Liu73d94db2023-07-19 10:33:57 -0700457 else
458 num_retained_instances++;
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700459 }
Qunxin Liu73d94db2023-07-19 10:33:57 -0700460
461 return_trace (c->serializer->check_assign (out->instanceCount, num_retained_instances, HB_SERIALIZE_ERROR_INT_OVERFLOW));
Qunxin Liu0a6c16a2022-08-08 13:47:39 -0700462 }
463
Behdad Esfahboda9a607d2021-07-22 16:49:56 -0700464 public:
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330465 hb_array_t<const AxisRecord> get_axes () const
Behdad Esfahbod4a6a6922018-11-19 13:04:43 -0500466 { return hb_array (&(this+firstAxis), axisCount); }
Behdad Esfahbod785982b2017-01-20 19:57:27 -0800467
Ebrahim Byagowib2ebaa92018-12-16 22:38:10 +0330468 const InstanceRecord *get_instance (unsigned int i) const
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500469 {
Behdad Esfahbod3b9fd172018-11-22 01:18:55 -0500470 if (unlikely (i >= instanceCount)) return nullptr;
471 return &StructAtOffset<InstanceRecord> (&StructAfter<InstanceRecord> (get_axes ()),
472 i * instanceSize);
Behdad Esfahbod587d49f2018-11-19 14:27:19 -0500473 }
Behdad Esfahbod785982b2017-01-20 19:57:27 -0800474
475 protected:
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800476 FixedVersion<>version; /* Version of the fvar table
477 * initially set to 0x00010000u */
Behdad Esfahbodad28f972021-03-31 12:49:14 -0600478 Offset16To<AxisRecord>
Behdad Esfahbode0097392018-11-19 12:53:53 -0500479 firstAxis; /* Offset in bytes from the beginning of the table
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800480 * to the start of the AxisRecord array. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100481 HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */
482 HBUINT16 axisCount; /* The number of variation axes in the font (the
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800483 * number of records in the axes array). */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100484 HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord —
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800485 * set to 20 (0x0014) for this version. */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100486 HBUINT16 instanceCount; /* The number of named instances defined in the font
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800487 * (the number of records in the instances array). */
Behdad Esfahbod6b191782018-01-10 03:07:30 +0100488 HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set
Behdad Esfahbod8c29dca2022-10-13 12:04:32 -0600489 * to either axisCount * sizeof(F16DOT16) + 4, or to
490 * axisCount * sizeof(F16DOT16) + 6. */
Behdad Esfahbod55d42fd2017-01-19 19:35:48 -0800491
492 public:
493 DEFINE_SIZE_STATIC (16);
494};
495
496} /* namespace OT */
497
498
499#endif /* HB_OT_VAR_FVAR_TABLE_HH */