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 | */ |
25 | |
26 | #ifndef HB_OT_VAR_CVAR_TABLE_HH |
27 | #define HB_OT_VAR_CVAR_TABLE_HH |
28 | |
29 | #include "hb-ot-var-common.hh" |
30 | #include "hb-ot-var-fvar-table.hh" |
31 | |
32 | |
33 | namespace OT { |
34 | /* |
35 | * cvar -- control value table (CVT) Variations |
36 | * https://docs.microsoft.com/en-us/typography/opentype/spec/cvar |
37 | */ |
38 | #define HB_OT_TAG_cvar HB_TAG('c','v','a','r') |
39 | |
40 | struct cvar |
41 | { |
42 | static constexpr hb_tag_t tableTag = HB_OT_TAG_cvar; |
43 | |
44 | bool sanitize (hb_sanitize_context_t *c) const |
45 | { |
46 | TRACE_SANITIZE (this); |
47 | return_trace (c->check_struct (this) && |
48 | version.sanitize (c) && likely (version.major == 1) && |
49 | tupleVariationData.sanitize (c)); |
50 | } |
51 | |
52 | const TupleVariationData* get_tuple_var_data (void) const |
53 | { return &tupleVariationData; } |
54 | |
55 | bool decompile_tuple_variations (unsigned axis_count, |
56 | unsigned point_count, |
57 | bool is_gvar, |
58 | const hb_map_t *axes_old_index_tag_map, |
59 | TupleVariationData::tuple_variations_t& tuple_variations /* OUT */) const |
60 | { |
61 | hb_vector_t<unsigned> shared_indices; |
62 | TupleVariationData::tuple_iterator_t iterator; |
63 | unsigned var_data_length = tupleVariationData.get_size (axis_count); |
64 | hb_bytes_t var_data_bytes = hb_bytes_t (reinterpret_cast<const char*> (get_tuple_var_data ()), var_data_length); |
65 | if (!TupleVariationData::get_tuple_iterator (var_data_bytes, axis_count, this, |
66 | shared_indices, &iterator)) |
67 | return false; |
68 | |
69 | return tupleVariationData.decompile_tuple_variations (point_count, is_gvar, iterator, |
70 | axes_old_index_tag_map, |
71 | shared_indices, |
72 | hb_array<const F2DOT14> (), |
73 | tuple_variations); |
74 | } |
75 | |
76 | static bool calculate_cvt_deltas (unsigned axis_count, |
77 | hb_array_t<int> coords, |
78 | unsigned num_cvt_item, |
79 | const TupleVariationData *tuple_var_data, |
80 | const void *base, |
81 | hb_vector_t<float>& cvt_deltas /* OUT */) |
82 | { |
83 | if (!coords) return true; |
84 | hb_vector_t<unsigned> shared_indices; |
85 | TupleVariationData::tuple_iterator_t iterator; |
86 | unsigned var_data_length = tuple_var_data->get_size (axis_count); |
87 | hb_bytes_t var_data_bytes = hb_bytes_t (reinterpret_cast<const char*> (tuple_var_data), var_data_length); |
88 | if (!TupleVariationData::get_tuple_iterator (var_data_bytes, axis_count, base, |
89 | shared_indices, &iterator)) |
90 | return true; /* isn't applied at all */ |
91 | |
92 | hb_array_t<const F2DOT14> shared_tuples = hb_array<F2DOT14> (); |
93 | hb_vector_t<unsigned> private_indices; |
94 | hb_vector_t<int> unpacked_deltas; |
95 | |
96 | do |
97 | { |
98 | float scalar = iterator.current_tuple->calculate_scalar (coords, axis_count, shared_tuples); |
99 | if (scalar == 0.f) continue; |
100 | const HBUINT8 *p = iterator.get_serialized_data (); |
101 | unsigned int length = iterator.current_tuple->get_data_size (); |
102 | if (unlikely (!iterator.var_data_bytes.check_range (p, length))) |
103 | return false; |
104 | |
105 | const HBUINT8 *end = p + length; |
106 | |
107 | bool has_private_points = iterator.current_tuple->has_private_points (); |
108 | if (has_private_points && |
109 | !TupleVariationData::unpack_points (p, private_indices, end)) |
110 | return false; |
111 | const hb_vector_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices; |
112 | |
113 | bool apply_to_all = (indices.length == 0); |
114 | unsigned num_deltas = apply_to_all ? num_cvt_item : indices.length; |
115 | if (unlikely (!unpacked_deltas.resize (num_deltas, false))) return false; |
116 | if (unlikely (!TupleVariationData::unpack_deltas (p, unpacked_deltas, end))) return false; |
117 | |
118 | for (unsigned int i = 0; i < num_deltas; i++) |
119 | { |
120 | unsigned int idx = apply_to_all ? i : indices[i]; |
121 | if (unlikely (idx >= num_cvt_item)) continue; |
122 | if (scalar != 1.0f) cvt_deltas[idx] += unpacked_deltas[i] * scalar ; |
123 | else cvt_deltas[idx] += unpacked_deltas[i]; |
124 | } |
125 | } while (iterator.move_to_next ()); |
126 | |
127 | return true; |
128 | } |
129 | |
130 | bool serialize (hb_serialize_context_t *c, |
131 | TupleVariationData::tuple_variations_t& tuple_variations) const |
132 | { |
133 | TRACE_SERIALIZE (this); |
134 | if (unlikely (!c->embed (version))) return_trace (false); |
135 | |
136 | return_trace (tupleVariationData.serialize (c, false, tuple_variations)); |
137 | } |
138 | |
139 | bool subset (hb_subset_context_t *c) const |
140 | { |
141 | TRACE_SUBSET (this); |
142 | if (c->plan->all_axes_pinned) |
143 | return_trace (false); |
144 | |
145 | /* subset() for cvar is called by partial instancing only, we always pass |
146 | * through cvar table in other cases */ |
147 | if (!c->plan->normalized_coords) |
148 | { |
149 | unsigned axis_count = c->plan->source->table.fvar->get_axis_count (); |
150 | unsigned total_size = min_size + tupleVariationData.get_size (axis_count); |
151 | char *out = c->serializer->allocate_size<char> (total_size); |
152 | if (unlikely (!out)) return_trace (false); |
153 | |
154 | hb_memcpy (out, this, total_size); |
155 | return_trace (true); |
156 | } |
157 | |
158 | OT::TupleVariationData::tuple_variations_t tuple_variations; |
159 | unsigned axis_count = c->plan->axes_old_index_tag_map.get_population (); |
160 | |
161 | const hb_tag_t cvt = HB_TAG('c','v','t',' '); |
162 | hb_blob_t *cvt_blob = hb_face_reference_table (c->plan->source, cvt); |
163 | unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size; |
164 | hb_blob_destroy (cvt_blob); |
165 | |
166 | if (!decompile_tuple_variations (axis_count, point_count, false, |
167 | &(c->plan->axes_old_index_tag_map), |
168 | tuple_variations)) |
169 | return_trace (false); |
170 | |
171 | tuple_variations.instantiate (c->plan->axes_location, c->plan->axes_triple_distances); |
172 | if (!tuple_variations.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map)) |
173 | return_trace (false); |
174 | |
175 | return_trace (serialize (c->serializer, tuple_variations)); |
176 | } |
177 | |
178 | static bool add_cvt_and_apply_deltas (hb_subset_plan_t *plan, |
179 | const TupleVariationData *tuple_var_data, |
180 | const void *base) |
181 | { |
182 | const hb_tag_t cvt = HB_TAG('c','v','t',' '); |
183 | hb_blob_t *cvt_blob = hb_face_reference_table (plan->source, cvt); |
184 | hb_blob_t *cvt_prime_blob = hb_blob_copy_writable_or_fail (cvt_blob); |
185 | hb_blob_destroy (cvt_blob); |
186 | |
187 | if (unlikely (!cvt_prime_blob)) |
188 | return false; |
189 | |
190 | unsigned cvt_blob_length = hb_blob_get_length (cvt_prime_blob); |
191 | unsigned num_cvt_item = cvt_blob_length / FWORD::static_size; |
192 | |
193 | hb_vector_t<float> cvt_deltas; |
194 | if (unlikely (!cvt_deltas.resize (num_cvt_item))) |
195 | { |
196 | hb_blob_destroy (cvt_prime_blob); |
197 | return false; |
198 | } |
199 | |
200 | if (!calculate_cvt_deltas (plan->normalized_coords.length, plan->normalized_coords.as_array (), |
201 | num_cvt_item, tuple_var_data, base, cvt_deltas)) |
202 | { |
203 | hb_blob_destroy (cvt_prime_blob); |
204 | return false; |
205 | } |
206 | |
207 | FWORD *cvt_prime = (FWORD *) hb_blob_get_data_writable (cvt_prime_blob, nullptr); |
208 | for (unsigned i = 0; i < num_cvt_item; i++) |
209 | cvt_prime[i] += (int) roundf (cvt_deltas[i]); |
210 | |
211 | bool success = plan->add_table (cvt, cvt_prime_blob); |
212 | hb_blob_destroy (cvt_prime_blob); |
213 | return success; |
214 | } |
215 | |
216 | protected: |
217 | FixedVersion<>version; /* Version of the CVT variation table |
218 | * initially set to 0x00010000u */ |
219 | TupleVariationData tupleVariationData; /* TupleVariationDate for cvar table */ |
220 | public: |
221 | DEFINE_SIZE_MIN (8); |
222 | }; |
223 | |
224 | } /* namespace OT */ |
225 | |
226 | |
227 | #endif /* HB_OT_VAR_CVAR_TABLE_HH */ |
228 | |