/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-var-cvar-table.hh
Line | Count | Source |
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 | 0 | { |
46 | 0 | TRACE_SANITIZE (this); |
47 | 0 | return_trace (c->check_struct (this) && |
48 | 0 | hb_barrier () && |
49 | 0 | likely (version.major == 1) && |
50 | 0 | tupleVariationData.sanitize (c)); |
51 | 0 | } |
52 | | |
53 | | const TupleVariationData<>* get_tuple_var_data (void) const |
54 | 0 | { return &tupleVariationData; } |
55 | | |
56 | | bool decompile_tuple_variations (unsigned axis_count, |
57 | | unsigned point_count, |
58 | | hb_blob_t *blob, |
59 | | bool is_gvar, |
60 | | const hb_map_t *axes_old_index_tag_map, |
61 | | TupleVariationData<>::tuple_variations_t& tuple_variations /* OUT */) const |
62 | 0 | { |
63 | 0 | hb_vector_t<unsigned> shared_indices; |
64 | 0 | TupleVariationData<>::tuple_iterator_t iterator; |
65 | 0 | hb_bytes_t var_data_bytes = blob->as_bytes ().sub_array (4); |
66 | 0 | if (!TupleVariationData<>::get_tuple_iterator (var_data_bytes, axis_count, this, |
67 | 0 | shared_indices, &iterator)) |
68 | 0 | return false; |
69 | | |
70 | 0 | return tupleVariationData.decompile_tuple_variations (point_count, is_gvar, iterator, |
71 | 0 | axes_old_index_tag_map, |
72 | 0 | shared_indices, |
73 | 0 | hb_array<const F2DOT14> (), |
74 | 0 | tuple_variations); |
75 | 0 | } |
76 | | |
77 | | static bool calculate_cvt_deltas (unsigned axis_count, |
78 | | hb_array_t<int> coords, |
79 | | unsigned num_cvt_item, |
80 | | const TupleVariationData<> *tuple_var_data, |
81 | | const void *base, |
82 | | hb_vector_t<float>& cvt_deltas /* OUT */) |
83 | 0 | { |
84 | 0 | if (!coords) return true; |
85 | 0 | hb_vector_t<unsigned> shared_indices; |
86 | 0 | TupleVariationData<>::tuple_iterator_t iterator; |
87 | 0 | unsigned var_data_length = tuple_var_data->get_size (axis_count * 2); |
88 | 0 | hb_bytes_t var_data_bytes = hb_bytes_t (reinterpret_cast<const char*> (tuple_var_data), var_data_length); |
89 | 0 | if (!TupleVariationData<>::get_tuple_iterator (var_data_bytes, axis_count, base, |
90 | 0 | shared_indices, &iterator)) |
91 | 0 | return true; /* isn't applied at all */ |
92 | | |
93 | 0 | hb_array_t<const F2DOT14> shared_tuples = hb_array<F2DOT14> (); |
94 | 0 | hb_vector_t<unsigned> private_indices; |
95 | 0 | hb_vector_t<int> unpacked_deltas; |
96 | |
|
97 | 0 | do |
98 | 0 | { |
99 | 0 | float scalar = iterator.current_tuple->calculate_scalar (coords, axis_count, shared_tuples); |
100 | 0 | if (scalar == 0.f) continue; |
101 | 0 | const HBUINT8 *p = iterator.get_serialized_data (); |
102 | 0 | unsigned int length = iterator.current_tuple->get_data_size (); |
103 | 0 | if (unlikely (!iterator.var_data_bytes.check_range (p, length))) |
104 | 0 | return false; |
105 | | |
106 | 0 | const HBUINT8 *end = p + length; |
107 | |
|
108 | 0 | bool has_private_points = iterator.current_tuple->has_private_points (); |
109 | 0 | if (has_private_points && |
110 | 0 | !TupleVariationData<>::decompile_points (p, private_indices, end)) |
111 | 0 | return false; |
112 | 0 | const hb_vector_t<unsigned int> &indices = has_private_points ? private_indices : shared_indices; |
113 | |
|
114 | 0 | bool apply_to_all = (indices.length == 0); |
115 | 0 | unsigned num_deltas = apply_to_all ? num_cvt_item : indices.length; |
116 | 0 | if (unlikely (!unpacked_deltas.resize_dirty (num_deltas))) return false; |
117 | 0 | if (unlikely (!TupleVariationData<>::decompile_deltas (p, unpacked_deltas, end))) return false; |
118 | | |
119 | 0 | for (unsigned int i = 0; i < num_deltas; i++) |
120 | 0 | { |
121 | 0 | unsigned int idx = apply_to_all ? i : indices[i]; |
122 | 0 | if (unlikely (idx >= num_cvt_item)) continue; |
123 | 0 | if (scalar != 1.0f) cvt_deltas[idx] += unpacked_deltas[i] * scalar ; |
124 | 0 | else cvt_deltas[idx] += unpacked_deltas[i]; |
125 | 0 | } |
126 | 0 | } while (iterator.move_to_next ()); |
127 | | |
128 | 0 | return true; |
129 | 0 | } |
130 | | |
131 | | bool serialize (hb_serialize_context_t *c, |
132 | | TupleVariationData<>::tuple_variations_t& tuple_variations) const |
133 | 0 | { |
134 | 0 | TRACE_SERIALIZE (this); |
135 | 0 | if (!tuple_variations) return_trace (false); |
136 | 0 | if (unlikely (!c->embed (version))) return_trace (false); |
137 | | |
138 | 0 | return_trace (tupleVariationData.serialize (c, false, tuple_variations)); |
139 | 0 | } |
140 | | |
141 | | bool subset (hb_subset_context_t *c) const |
142 | 0 | { |
143 | 0 | TRACE_SUBSET (this); |
144 | 0 | if (c->plan->all_axes_pinned) |
145 | 0 | return_trace (false); |
146 | | |
147 | 0 | OT::TupleVariationData<>::tuple_variations_t tuple_variations; |
148 | 0 | unsigned axis_count = c->plan->axes_old_index_tag_map.get_population (); |
149 | |
|
150 | 0 | const hb_tag_t cvt = HB_TAG('c','v','t',' '); |
151 | 0 | hb_blob_t *cvt_blob = hb_face_reference_table (c->plan->source, cvt); |
152 | 0 | unsigned point_count = hb_blob_get_length (cvt_blob) / FWORD::static_size; |
153 | 0 | hb_blob_destroy (cvt_blob); |
154 | |
|
155 | 0 | if (!decompile_tuple_variations (axis_count, point_count, |
156 | 0 | c->source_blob, false, |
157 | 0 | &(c->plan->axes_old_index_tag_map), |
158 | 0 | tuple_variations)) |
159 | 0 | return_trace (false); |
160 | | |
161 | 0 | optimize_scratch_t scratch; |
162 | 0 | if (!tuple_variations.instantiate (c->plan->axes_location, c->plan->axes_triple_distances, scratch)) |
163 | 0 | return_trace (false); |
164 | | |
165 | 0 | if (!tuple_variations.compile_bytes (c->plan->axes_index_map, c->plan->axes_old_index_tag_map, |
166 | 0 | false /* do not use shared points */)) |
167 | 0 | return_trace (false); |
168 | | |
169 | 0 | return_trace (serialize (c->serializer, tuple_variations)); |
170 | 0 | } |
171 | | |
172 | | static bool add_cvt_and_apply_deltas (hb_subset_plan_t *plan, |
173 | | const TupleVariationData<> *tuple_var_data, |
174 | | const void *base) |
175 | 0 | { |
176 | 0 | const hb_tag_t cvt = HB_TAG('c','v','t',' '); |
177 | 0 | hb_blob_t *cvt_blob = hb_face_reference_table (plan->source, cvt); |
178 | 0 | hb_blob_t *cvt_prime_blob = hb_blob_copy_writable_or_fail (cvt_blob); |
179 | 0 | hb_blob_destroy (cvt_blob); |
180 | | |
181 | 0 | if (unlikely (!cvt_prime_blob)) |
182 | 0 | return false; |
183 | | |
184 | 0 | unsigned cvt_blob_length = hb_blob_get_length (cvt_prime_blob); |
185 | 0 | unsigned num_cvt_item = cvt_blob_length / FWORD::static_size; |
186 | |
|
187 | 0 | hb_vector_t<float> cvt_deltas; |
188 | 0 | if (unlikely (!cvt_deltas.resize (num_cvt_item))) |
189 | 0 | { |
190 | 0 | hb_blob_destroy (cvt_prime_blob); |
191 | 0 | return false; |
192 | 0 | } |
193 | | |
194 | 0 | if (!calculate_cvt_deltas (plan->normalized_coords.length, plan->normalized_coords.as_array (), |
195 | 0 | num_cvt_item, tuple_var_data, base, cvt_deltas)) |
196 | 0 | { |
197 | 0 | hb_blob_destroy (cvt_prime_blob); |
198 | 0 | return false; |
199 | 0 | } |
200 | | |
201 | 0 | FWORD *cvt_prime = (FWORD *) hb_blob_get_data_writable (cvt_prime_blob, nullptr); |
202 | 0 | for (unsigned i = 0; i < num_cvt_item; i++) |
203 | 0 | cvt_prime[i] += (int) roundf (cvt_deltas[i]); |
204 | | |
205 | 0 | bool success = plan->add_table (cvt, cvt_prime_blob); |
206 | 0 | hb_blob_destroy (cvt_prime_blob); |
207 | 0 | return success; |
208 | 0 | } |
209 | | |
210 | | protected: |
211 | | FixedVersion<>version; /* Version of the CVT variation table |
212 | | * initially set to 0x00010000u */ |
213 | | TupleVariationData<> tupleVariationData; /* TupleVariationDate for cvar table */ |
214 | | public: |
215 | | DEFINE_SIZE_MIN (8); |
216 | | }; |
217 | | |
218 | | } /* namespace OT */ |
219 | | |
220 | | |
221 | | #endif /* HB_OT_VAR_CVAR_TABLE_HH */ |