2 * Copyright © 2017 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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_OT_VAR_HVAR_TABLE_HH
28 #define HB_OT_VAR_HVAR_TABLE_HH
30 #include "hb-ot-layout-common.hh"
31 #include "hb-ot-var-common.hh"
36 struct index_map_subset_plan_t
38 enum index_map_index_t {
40 LSB_INDEX, /* dual as TSB */
41 RSB_INDEX, /* dual as BSB */
45 void init (const DeltaSetIndexMap &index_map,
46 hb_inc_bimap_t &outer_map,
47 hb_vector_t<hb_set_t *> &inner_sets,
48 const hb_subset_plan_t *plan)
56 if (&index_map == &Null (DeltaSetIndexMap)) return;
58 unsigned int last_val = (unsigned int)-1;
59 hb_codepoint_t last_gid = (hb_codepoint_t)-1;
60 hb_codepoint_t gid = (hb_codepoint_t) hb_min (index_map.get_map_count (), plan->num_output_glyphs ());
62 outer_bit_count = (index_map.get_width () * 8) - index_map.get_inner_bit_count ();
63 max_inners.resize (inner_sets.length);
64 for (unsigned i = 0; i < inner_sets.length; i++) max_inners[i] = 0;
66 /* Search backwards for a map value different from the last map value */
67 for (; gid > 0; gid--)
69 hb_codepoint_t old_gid;
70 if (!plan->old_gid_for_new_gid (gid - 1, &old_gid))
72 if (last_gid == (hb_codepoint_t) -1)
78 unsigned int v = index_map.map (old_gid);
79 if (last_gid == (hb_codepoint_t) -1)
85 if (v != last_val) break;
90 if (unlikely (last_gid == (hb_codepoint_t)-1)) return;
92 for (gid = 0; gid < map_count; gid++)
94 hb_codepoint_t old_gid;
95 if (plan->old_gid_for_new_gid (gid, &old_gid))
97 unsigned int v = index_map.map (old_gid);
98 unsigned int outer = v >> 16;
99 unsigned int inner = v & 0xFFFF;
100 outer_map.add (outer);
101 if (inner > max_inners[outer]) max_inners[outer] = inner;
102 if (outer >= inner_sets.length) return;
103 inner_sets[outer]->add (inner);
114 void remap (const DeltaSetIndexMap *input_map,
115 const hb_inc_bimap_t &outer_map,
116 const hb_vector_t<hb_inc_bimap_t> &inner_maps,
117 const hb_subset_plan_t *plan)
119 if (input_map == &Null (DeltaSetIndexMap)) return;
121 for (unsigned int i = 0; i < max_inners.length; i++)
123 if (inner_maps[i].get_population () == 0) continue;
124 unsigned int bit_count = (max_inners[i]==0)? 1: hb_bit_storage (inner_maps[i][max_inners[i]]);
125 if (bit_count > inner_bit_count) inner_bit_count = bit_count;
128 output_map.resize (map_count);
129 for (hb_codepoint_t gid = 0; gid < output_map.length; gid++)
131 hb_codepoint_t old_gid;
132 if (plan->old_gid_for_new_gid (gid, &old_gid))
134 uint32_t v = input_map->map (old_gid);
135 unsigned int outer = v >> 16;
136 output_map[gid] = (outer_map[outer] << 16) | (inner_maps[outer][v & 0xFFFF]);
139 output_map[gid] = 0; /* Map unused glyph to outer/inner=0/0 */
143 unsigned int get_inner_bit_count () const { return inner_bit_count; }
144 unsigned int get_width () const { return ((outer_bit_count + inner_bit_count + 7) / 8); }
145 unsigned int get_map_count () const { return map_count; }
147 unsigned int get_size () const
148 { return (map_count? (DeltaSetIndexMap::min_size + get_width () * map_count): 0); }
150 bool is_identity () const { return get_output_map ().length == 0; }
151 hb_array_t<const uint32_t> get_output_map () const { return output_map.as_array (); }
154 unsigned int map_count;
155 hb_vector_t<unsigned int> max_inners;
156 unsigned int outer_bit_count;
157 unsigned int inner_bit_count;
158 hb_vector_t<uint32_t> output_map;
161 struct hvarvvar_subset_plan_t
163 hvarvvar_subset_plan_t() : inner_maps (), index_map_plans () {}
164 ~hvarvvar_subset_plan_t() { fini (); }
166 void init (const hb_array_t<const DeltaSetIndexMap *> &index_maps,
167 const VariationStore &_var_store,
168 const hb_subset_plan_t *plan)
170 index_map_plans.resize (index_maps.length);
172 var_store = &_var_store;
173 inner_sets.resize (var_store->get_sub_table_count ());
174 for (unsigned int i = 0; i < inner_sets.length; i++)
175 inner_sets[i] = hb_set_create ();
176 adv_set = hb_set_create ();
178 inner_maps.resize (var_store->get_sub_table_count ());
180 if (unlikely (!index_map_plans.length || !inner_sets.length || !inner_maps.length)) return;
182 bool retain_adv_map = false;
183 index_map_plans[0].init (*index_maps[0], outer_map, inner_sets, plan);
184 if (index_maps[0] == &Null (DeltaSetIndexMap))
186 retain_adv_map = plan->flags & HB_SUBSET_FLAGS_RETAIN_GIDS;
188 for (hb_codepoint_t gid = 0; gid < plan->num_output_glyphs (); gid++)
190 hb_codepoint_t old_gid;
191 if (plan->old_gid_for_new_gid (gid, &old_gid))
192 inner_sets[0]->add (old_gid);
194 hb_set_union (adv_set, inner_sets[0]);
197 for (unsigned int i = 1; i < index_maps.length; i++)
198 index_map_plans[i].init (*index_maps[i], outer_map, inner_sets, plan);
204 for (hb_codepoint_t gid = 0; gid < plan->num_output_glyphs (); gid++)
205 if (inner_sets[0]->has (gid))
206 inner_maps[0].add (gid);
208 inner_maps[0].skip ();
212 inner_maps[0].add_set (adv_set);
213 hb_set_subtract (inner_sets[0], adv_set);
214 inner_maps[0].add_set (inner_sets[0]);
217 for (unsigned int i = 1; i < inner_maps.length; i++)
218 inner_maps[i].add_set (inner_sets[i]);
220 for (unsigned int i = 0; i < index_maps.length; i++)
221 index_map_plans[i].remap (index_maps[i], outer_map, inner_maps, plan);
226 for (unsigned int i = 0; i < inner_sets.length; i++)
227 hb_set_destroy (inner_sets[i]);
228 hb_set_destroy (adv_set);
230 index_map_plans.fini ();
233 hb_inc_bimap_t outer_map;
234 hb_vector_t<hb_inc_bimap_t> inner_maps;
235 hb_vector_t<index_map_subset_plan_t> index_map_plans;
236 const VariationStore *var_store;
239 hb_vector_t<hb_set_t *> inner_sets;
244 * HVAR -- Horizontal Metrics Variations
245 * https://docs.microsoft.com/en-us/typography/opentype/spec/hvar
246 * VVAR -- Vertical Metrics Variations
247 * https://docs.microsoft.com/en-us/typography/opentype/spec/vvar
249 #define HB_OT_TAG_HVAR HB_TAG('H','V','A','R')
250 #define HB_OT_TAG_VVAR HB_TAG('V','V','A','R')
254 static constexpr hb_tag_t HVARTag = HB_OT_TAG_HVAR;
255 static constexpr hb_tag_t VVARTag = HB_OT_TAG_VVAR;
257 bool sanitize (hb_sanitize_context_t *c) const
259 TRACE_SANITIZE (this);
260 return_trace (version.sanitize (c) &&
261 likely (version.major == 1) &&
262 varStore.sanitize (c, this) &&
263 advMap.sanitize (c, this) &&
264 lsbMap.sanitize (c, this) &&
265 rsbMap.sanitize (c, this));
268 void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
270 index_maps.push (&(this+advMap));
271 index_maps.push (&(this+lsbMap));
272 index_maps.push (&(this+rsbMap));
275 bool serialize_index_maps (hb_serialize_context_t *c,
276 const hb_array_t<index_map_subset_plan_t> &im_plans)
278 TRACE_SERIALIZE (this);
279 if (im_plans[index_map_subset_plan_t::ADV_INDEX].is_identity ())
281 else if (unlikely (!advMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::ADV_INDEX])))
282 return_trace (false);
283 if (im_plans[index_map_subset_plan_t::LSB_INDEX].is_identity ())
285 else if (unlikely (!lsbMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::LSB_INDEX])))
286 return_trace (false);
287 if (im_plans[index_map_subset_plan_t::RSB_INDEX].is_identity ())
289 else if (unlikely (!rsbMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::RSB_INDEX])))
290 return_trace (false);
295 template <typename T>
296 bool _subset (hb_subset_context_t *c) const
299 hvarvvar_subset_plan_t hvar_plan;
300 hb_vector_t<const DeltaSetIndexMap *>
303 ((T*)this)->listup_index_maps (index_maps);
304 hvar_plan.init (index_maps.as_array (), this+varStore, c->plan);
306 T *out = c->serializer->allocate_min<T> ();
307 if (unlikely (!out)) return_trace (false);
309 out->version.major = 1;
310 out->version.minor = 0;
312 if (unlikely (!out->varStore
313 .serialize_serialize (c->serializer,
315 hvar_plan.inner_maps.as_array ())))
316 return_trace (false);
318 return_trace (out->T::serialize_index_maps (c->serializer,
319 hvar_plan.index_map_plans.as_array ()));
322 float get_advance_var (hb_codepoint_t glyph, hb_font_t *font) const
324 uint32_t varidx = (this+advMap).map (glyph);
325 return (this+varStore).get_delta (varidx, font->coords, font->num_coords);
328 float get_side_bearing_var (hb_codepoint_t glyph,
329 const int *coords, unsigned int coord_count) const
331 if (!has_side_bearing_deltas ()) return 0.f;
332 uint32_t varidx = (this+lsbMap).map (glyph);
333 return (this+varStore).get_delta (varidx, coords, coord_count);
336 bool has_side_bearing_deltas () const { return lsbMap && rsbMap; }
339 FixedVersion<>version; /* Version of the metrics variation table
340 * initially set to 0x00010000u */
341 Offset32To<VariationStore>
342 varStore; /* Offset to item variation store table. */
343 Offset32To<DeltaSetIndexMap>
344 advMap; /* Offset to advance var-idx mapping. */
345 Offset32To<DeltaSetIndexMap>
346 lsbMap; /* Offset to lsb/tsb var-idx mapping. */
347 Offset32To<DeltaSetIndexMap>
348 rsbMap; /* Offset to rsb/bsb var-idx mapping. */
351 DEFINE_SIZE_STATIC (20);
354 struct HVAR : HVARVVAR {
355 static constexpr hb_tag_t tableTag = HB_OT_TAG_HVAR;
356 bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<HVAR> (c); }
358 struct VVAR : HVARVVAR {
359 static constexpr hb_tag_t tableTag = HB_OT_TAG_VVAR;
361 bool sanitize (hb_sanitize_context_t *c) const
363 TRACE_SANITIZE (this);
364 return_trace (static_cast<const HVARVVAR *> (this)->sanitize (c) &&
365 vorgMap.sanitize (c, this));
368 void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
370 HVARVVAR::listup_index_maps (index_maps);
371 index_maps.push (&(this+vorgMap));
374 bool serialize_index_maps (hb_serialize_context_t *c,
375 const hb_array_t<index_map_subset_plan_t> &im_plans)
377 TRACE_SERIALIZE (this);
378 if (unlikely (!HVARVVAR::serialize_index_maps (c, im_plans)))
379 return_trace (false);
380 if (!im_plans[index_map_subset_plan_t::VORG_INDEX].get_map_count ())
382 else if (unlikely (!vorgMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::VORG_INDEX])))
383 return_trace (false);
388 bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<VVAR> (c); }
391 Offset32To<DeltaSetIndexMap>
392 vorgMap; /* Offset to vertical-origin var-idx mapping. */
395 DEFINE_SIZE_STATIC (24);
401 #endif /* HB_OT_VAR_HVAR_TABLE_HH */