Imported Upstream version 8.2.2
[platform/upstream/harfbuzz.git] / src / hb-ot-var-cvar-table.hh
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                                    hb_blob_t *blob,
58                                    bool is_gvar,
59                                    const hb_map_t *axes_old_index_tag_map,
60                                    TupleVariationData::tuple_variations_t& tuple_variations /* OUT */) const
61   {
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))
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 (!tuple_variations) return_trace (false);
135     if (unlikely (!c->embed (version))) return_trace (false);
136
137     return_trace (tupleVariationData.serialize (c, false, tuple_variations));
138   }
139
140   bool subset (hb_subset_context_t *c) const
141   {
142     TRACE_SUBSET (this);
143     if (c->plan->all_axes_pinned)
144       return_trace (false);
145
146     OT::TupleVariationData::tuple_variations_t tuple_variations;
147     unsigned axis_count = c->plan->axes_old_index_tag_map.get_population ();
148
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);
153
154     if (!decompile_tuple_variations (axis_count, point_count,
155                                      c->source_blob, false,
156                                      &(c->plan->axes_old_index_tag_map),
157                                      tuple_variations))
158       return_trace (false);
159
160     if (!tuple_variations.instantiate (c->plan->axes_location, c->plan->axes_triple_distances))
161       return_trace (false);
162
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);
166
167     return_trace (serialize (c->serializer, tuple_variations));
168   }
169
170   static bool add_cvt_and_apply_deltas (hb_subset_plan_t *plan,
171                                         const TupleVariationData *tuple_var_data,
172                                         const void *base)
173   {
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);
178   
179     if (unlikely (!cvt_prime_blob))
180       return false;
181  
182     unsigned cvt_blob_length = hb_blob_get_length (cvt_prime_blob);
183     unsigned num_cvt_item = cvt_blob_length / FWORD::static_size;
184
185     hb_vector_t<float> cvt_deltas;
186     if (unlikely (!cvt_deltas.resize (num_cvt_item)))
187     {
188       hb_blob_destroy (cvt_prime_blob);
189       return false;
190     }
191
192     if (!calculate_cvt_deltas (plan->normalized_coords.length, plan->normalized_coords.as_array (),
193                                num_cvt_item, tuple_var_data, base, cvt_deltas))
194     {
195       hb_blob_destroy (cvt_prime_blob);
196       return false;
197     }
198
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]);
202     
203     bool success = plan->add_table (cvt, cvt_prime_blob);
204     hb_blob_destroy (cvt_prime_blob);
205     return success;
206   }
207
208   protected:
209   FixedVersion<>version;                /* Version of the CVT variation table
210                                          * initially set to 0x00010000u */
211   TupleVariationData tupleVariationData; /* TupleVariationDate for cvar table */
212   public:
213   DEFINE_SIZE_MIN (8);
214 };
215
216 } /* namespace OT */
217
218
219 #endif /* HB_OT_VAR_CVAR_TABLE_HH */