1/*
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
30#include "hb-open-type.hh"
31
32/*
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
40namespace OT {
41
42
43struct InstanceRecord
44{
45 inline bool sanitize (hb_sanitize_context_t *c, unsigned int axis_count) const
46 {
47 TRACE_SANITIZE (this);
48 return_trace (c->check_struct (this) &&
49 c->check_array (coordinates, coordinates[0].static_size, axis_count));
50 }
51
52 protected:
53 NameID subfamilyNameID;/* The name ID for entries in the 'name' table
54 * that provide subfamily names for this instance. */
55 HBUINT16 reserved; /* Reserved for future use — set to 0. */
56 Fixed coordinates[VAR];/* The coordinates array for this instance. */
57 //NameID postScriptNameIDX;/*Optional. The name ID for entries in the 'name'
58 // * table that provide PostScript names for this
59 // * instance. */
60
61 public:
62 DEFINE_SIZE_ARRAY (4, coordinates);
63};
64
65struct AxisRecord
66{
67 inline bool sanitize (hb_sanitize_context_t *c) const
68 {
69 TRACE_SANITIZE (this);
70 return_trace (c->check_struct (this));
71 }
72
73 public:
74 Tag axisTag; /* Tag identifying the design variation for the axis. */
75 Fixed minValue; /* The minimum coordinate value for the axis. */
76 Fixed defaultValue; /* The default coordinate value for the axis. */
77 Fixed maxValue; /* The maximum coordinate value for the axis. */
78 HBUINT16 reserved; /* Reserved for future use — set to 0. */
79 NameID axisNameID; /* The name ID for entries in the 'name' table that
80 * provide a display name for this axis. */
81
82 public:
83 DEFINE_SIZE_STATIC (20);
84};
85
86struct fvar
87{
88 static const hb_tag_t tableTag = HB_OT_TAG_fvar;
89
90 inline bool has_data (void) const { return version.to_int () != 0; }
91
92 inline bool sanitize (hb_sanitize_context_t *c) const
93 {
94 TRACE_SANITIZE (this);
95 return_trace (version.sanitize (c) &&
96 likely (version.major == 1) &&
97 c->check_struct (this) &&
98 instanceSize >= axisCount * 4 + 4 &&
99 axisSize <= 1024 && /* Arbitrary, just to simplify overflow checks. */
100 instanceSize <= 1024 && /* Arbitrary, just to simplify overflow checks. */
101 c->check_range (this, things) &&
102 c->check_range (&StructAtOffset<char> (this, things),
103 axisCount * axisSize + instanceCount * instanceSize));
104 }
105
106 inline unsigned int get_axis_count (void) const
107 { return axisCount; }
108
109 inline bool get_axis (unsigned int index, hb_ot_var_axis_t *info) const
110 {
111 if (unlikely (index >= axisCount))
112 return false;
113
114 if (info)
115 {
116 const AxisRecord &axis = get_axes ()[index];
117 info->tag = axis.axisTag;
118 info->name_id = axis.axisNameID;
119 info->default_value = axis.defaultValue / 65536.;
120 /* Ensure order, to simplify client math. */
121 info->min_value = MIN<float> (info->default_value, axis.minValue / 65536.);
122 info->max_value = MAX<float> (info->default_value, axis.maxValue / 65536.);
123 }
124
125 return true;
126 }
127
128 inline unsigned int get_axis_infos (unsigned int start_offset,
129 unsigned int *axes_count /* IN/OUT */,
130 hb_ot_var_axis_t *axes_array /* OUT */) const
131 {
132 if (axes_count)
133 {
134 unsigned int count = axisCount;
135 start_offset = MIN (start_offset, count);
136
137 count -= start_offset;
138 axes_array += start_offset;
139
140 count = MIN (count, *axes_count);
141 *axes_count = count;
142
143 for (unsigned int i = 0; i < count; i++)
144 get_axis (start_offset + i, axes_array + i);
145 }
146 return axisCount;
147 }
148
149 inline bool find_axis (hb_tag_t tag, unsigned int *index, hb_ot_var_axis_t *info) const
150 {
151 const AxisRecord *axes = get_axes ();
152 unsigned int count = get_axis_count ();
153 for (unsigned int i = 0; i < count; i++)
154 if (axes[i].axisTag == tag)
155 {
156 if (index)
157 *index = i;
158 return get_axis (i, info);
159 }
160 if (index)
161 *index = HB_OT_VAR_NO_AXIS_INDEX;
162 return false;
163 }
164
165 inline int normalize_axis_value (unsigned int axis_index, float v) const
166 {
167 hb_ot_var_axis_t axis;
168 if (!get_axis (axis_index, &axis))
169 return 0;
170
171 v = MAX (MIN (v, axis.max_value), axis.min_value); /* Clamp. */
172
173 if (v == axis.default_value)
174 return 0;
175 else if (v < axis.default_value)
176 v = (v - axis.default_value) / (axis.default_value - axis.min_value);
177 else
178 v = (v - axis.default_value) / (axis.max_value - axis.default_value);
179 return (int) (v * 16384. + (v >= 0. ? .5 : -.5));
180 }
181
182 protected:
183 inline const AxisRecord * get_axes (void) const
184 { return &StructAtOffset<AxisRecord> (this, things); }
185
186 inline const InstanceRecord * get_instances (void) const
187 { return &StructAtOffset<InstanceRecord> (get_axes () + axisCount, 0); }
188
189 protected:
190 FixedVersion<>version; /* Version of the fvar table
191 * initially set to 0x00010000u */
192 Offset16 things; /* Offset in bytes from the beginning of the table
193 * to the start of the AxisRecord array. */
194 HBUINT16 reserved; /* This field is permanently reserved. Set to 2. */
195 HBUINT16 axisCount; /* The number of variation axes in the font (the
196 * number of records in the axes array). */
197 HBUINT16 axisSize; /* The size in bytes of each VariationAxisRecord —
198 * set to 20 (0x0014) for this version. */
199 HBUINT16 instanceCount; /* The number of named instances defined in the font
200 * (the number of records in the instances array). */
201 HBUINT16 instanceSize; /* The size in bytes of each InstanceRecord — set
202 * to either axisCount * sizeof(Fixed) + 4, or to
203 * axisCount * sizeof(Fixed) + 6. */
204
205 public:
206 DEFINE_SIZE_STATIC (16);
207};
208
209} /* namespace OT */
210
211
212#endif /* HB_OT_VAR_FVAR_TABLE_HH */
213