2 * Copyright © 2023 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 #ifndef HB_OT_VAR_CVAR_TABLE_HH
27 #define HB_OT_VAR_CVAR_TABLE_HH
29 #include "hb-ot-var-common.hh"
30 #include "hb-ot-var-fvar-table.hh"
35 * cvar -- control value table (CVT) Variations
36 * https://docs.microsoft.com/en-us/typography/opentype/spec/cvar
38 #define HB_OT_TAG_cvar HB_TAG('c','v','a','r')
42 static constexpr hb_tag_t tableTag = HB_OT_TAG_cvar;
44 bool sanitize (hb_sanitize_context_t *c) const
46 TRACE_SANITIZE (this);
47 return_trace (c->check_struct (this) &&
48 version.sanitize (c) && likely (version.major == 1) &&
49 tupleVariationData.sanitize (c));
52 const TupleVariationData* get_tuple_var_data (void) const
53 { return &tupleVariationData; }
55 bool decompile_tuple_variations (unsigned axis_count,
59 const hb_map_t *axes_old_index_tag_map,
60 TupleVariationData::tuple_variations_t& tuple_variations /* OUT */) const
62 hb_vector_t<unsigned> shared_indices;
63 TupleVariationData::tuple_iterator_t iterator;
64 hb_bytes_t var_data_bytes = blob->as_bytes ().sub_array (4);
65 if (!TupleVariationData::get_tuple_iterator (var_data_bytes, axis_count, this,
66 shared_indices, &iterator))
69 return tupleVariationData.decompile_tuple_variations (point_count, is_gvar, iterator,
70 axes_old_index_tag_map,
72 hb_array<const F2DOT14> (),
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,
81 hb_vector_t<float>& cvt_deltas /* OUT */)
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 */
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;
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)))
105 const HBUINT8 *end = p + length;
107 bool has_private_points = iterator.current_tuple->has_private_points ();
108 if (has_private_points &&
109 !TupleVariationData::unpack_points (p, private_indices, end))
111 const hb_vector_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices;
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;
118 for (unsigned int i = 0; i < num_deltas; i++)
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];
125 } while (iterator.move_to_next ());
130 bool serialize (hb_serialize_context_t *c,
131 TupleVariationData::tuple_variations_t& tuple_variations) const
133 TRACE_SERIALIZE (this);
134 if (!tuple_variations) return_trace (false);
135 if (unlikely (!c->embed (version))) return_trace (false);
137 return_trace (tupleVariationData.serialize (c, false, tuple_variations));
140 bool subset (hb_subset_context_t *c) const
143 if (c->plan->all_axes_pinned)
144 return_trace (false);
146 OT::TupleVariationData::tuple_variations_t tuple_variations;
147 unsigned axis_count = c->plan->axes_old_index_tag_map.get_population ();
149 const hb_tag_t cvt = HB_TAG('c','v','t',' ');
150 hb_blob_t *cvt_blob = hb_face_reference_table (c->plan->source, cvt);
151 unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size;
152 hb_blob_destroy (cvt_blob);
154 if (!decompile_tuple_variations (axis_count, point_count,
155 c->source_blob, false,
156 &(c->plan->axes_old_index_tag_map),
158 return_trace (false);
160 if (!tuple_variations.instantiate (c->plan->axes_location, c->plan->axes_triple_distances))
161 return_trace (false);
163 if (!tuple_variations.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map,
164 false /* do not use shared points */))
165 return_trace (false);
167 return_trace (serialize (c->serializer, tuple_variations));
170 static bool add_cvt_and_apply_deltas (hb_subset_plan_t *plan,
171 const TupleVariationData *tuple_var_data,
174 const hb_tag_t cvt = HB_TAG('c','v','t',' ');
175 hb_blob_t *cvt_blob = hb_face_reference_table (plan->source, cvt);
176 hb_blob_t *cvt_prime_blob = hb_blob_copy_writable_or_fail (cvt_blob);
177 hb_blob_destroy (cvt_blob);
179 if (unlikely (!cvt_prime_blob))
182 unsigned cvt_blob_length = hb_blob_get_length (cvt_prime_blob);
183 unsigned num_cvt_item = cvt_blob_length / FWORD::static_size;
185 hb_vector_t<float> cvt_deltas;
186 if (unlikely (!cvt_deltas.resize (num_cvt_item)))
188 hb_blob_destroy (cvt_prime_blob);
192 if (!calculate_cvt_deltas (plan->normalized_coords.length, plan->normalized_coords.as_array (),
193 num_cvt_item, tuple_var_data, base, cvt_deltas))
195 hb_blob_destroy (cvt_prime_blob);
199 FWORD *cvt_prime = (FWORD *) hb_blob_get_data_writable (cvt_prime_blob, nullptr);
200 for (unsigned i = 0; i < num_cvt_item; i++)
201 cvt_prime[i] += (int) roundf (cvt_deltas[i]);
203 bool success = plan->add_table (cvt, cvt_prime_blob);
204 hb_blob_destroy (cvt_prime_blob);
209 FixedVersion<>version; /* Version of the CVT variation table
210 * initially set to 0x00010000u */
211 TupleVariationData tupleVariationData; /* TupleVariationDate for cvar table */
219 #endif /* HB_OT_VAR_CVAR_TABLE_HH */