• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2017  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  * Google Author(s): Behdad Esfahbod
25  */
26 
27 #ifndef HB_OT_VAR_HVAR_TABLE_HH
28 #define HB_OT_VAR_HVAR_TABLE_HH
29 
30 #include "hb-ot-layout-common.hh"
31 #include "hb-ot-var-common.hh"
32 
33 namespace OT {
34 
35 
36 struct index_map_subset_plan_t
37 {
38   enum index_map_index_t {
39     ADV_INDEX,
40     LSB_INDEX,	/* dual as TSB */
41     RSB_INDEX,	/* dual as BSB */
42     VORG_INDEX
43   };
44 
initOT::index_map_subset_plan_t45   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)
49   {
50     map_count = 0;
51     outer_bit_count = 0;
52     inner_bit_count = 1;
53     max_inners.init ();
54     output_map.init ();
55 
56     if (&index_map == &Null (DeltaSetIndexMap)) return;
57 
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 ());
61 
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;
65 
66     /* Search backwards for a map value different from the last map value */
67     for (; gid > 0; gid--)
68     {
69       hb_codepoint_t	old_gid;
70       if (!plan->old_gid_for_new_gid (gid - 1, &old_gid))
71       {
72 	if (last_gid == (hb_codepoint_t) -1)
73 	  continue;
74 	else
75 	  break;
76       }
77 
78       unsigned int v = index_map.map (old_gid);
79       if (last_gid == (hb_codepoint_t) -1)
80       {
81 	last_val = v;
82 	last_gid = gid;
83 	continue;
84       }
85       if (v != last_val) break;
86 
87       last_gid = gid;
88     }
89 
90     if (unlikely (last_gid == (hb_codepoint_t)-1)) return;
91     map_count = last_gid;
92     for (gid = 0; gid < map_count; gid++)
93     {
94       hb_codepoint_t	old_gid;
95       if (plan->old_gid_for_new_gid (gid, &old_gid))
96       {
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);
104       }
105     }
106   }
107 
finiOT::index_map_subset_plan_t108   void fini ()
109   {
110     max_inners.fini ();
111     output_map.fini ();
112   }
113 
remapOT::index_map_subset_plan_t114   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)
118   {
119     if (input_map == &Null (DeltaSetIndexMap)) return;
120 
121     for (unsigned int i = 0; i < max_inners.length; i++)
122     {
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;
126     }
127 
128     output_map.resize (map_count);
129     for (hb_codepoint_t gid = 0; gid < output_map.length; gid++)
130     {
131       hb_codepoint_t	old_gid;
132       if (plan->old_gid_for_new_gid (gid, &old_gid))
133       {
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]);
137       }
138       else
139 	output_map[gid] = 0;	/* Map unused glyph to outer/inner=0/0 */
140     }
141   }
142 
get_inner_bit_countOT::index_map_subset_plan_t143   unsigned int get_inner_bit_count () const { return inner_bit_count; }
get_widthOT::index_map_subset_plan_t144   unsigned int get_width ()           const { return ((outer_bit_count + inner_bit_count + 7) / 8); }
get_map_countOT::index_map_subset_plan_t145   unsigned int get_map_count ()       const { return map_count; }
146 
get_sizeOT::index_map_subset_plan_t147   unsigned int get_size () const
148   { return (map_count? (DeltaSetIndexMap::min_size + get_width () * map_count): 0); }
149 
is_identityOT::index_map_subset_plan_t150   bool is_identity () const { return get_output_map ().length == 0; }
get_output_mapOT::index_map_subset_plan_t151   hb_array_t<const uint32_t> get_output_map () const { return output_map.as_array (); }
152 
153   protected:
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;
159 };
160 
161 struct hvarvvar_subset_plan_t
162 {
hvarvvar_subset_plan_tOT::hvarvvar_subset_plan_t163   hvarvvar_subset_plan_t() : inner_maps (), index_map_plans () {}
~hvarvvar_subset_plan_tOT::hvarvvar_subset_plan_t164   ~hvarvvar_subset_plan_t() { fini (); }
165 
initOT::hvarvvar_subset_plan_t166   void init (const hb_array_t<const DeltaSetIndexMap *> &index_maps,
167 	     const VariationStore &_var_store,
168 	     const hb_subset_plan_t *plan)
169   {
170     index_map_plans.resize (index_maps.length);
171 
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 ();
177 
178     inner_maps.resize (var_store->get_sub_table_count ());
179 
180     for (unsigned int i = 0; i < inner_maps.length; i++)
181       inner_maps[i].init ();
182 
183     if (unlikely (!index_map_plans.length || !inner_sets.length || !inner_maps.length)) return;
184 
185     bool retain_adv_map = false;
186     index_map_plans[0].init (*index_maps[0], outer_map, inner_sets, plan);
187     if (index_maps[0] == &Null (DeltaSetIndexMap))
188     {
189       retain_adv_map = plan->flags & HB_SUBSET_FLAGS_RETAIN_GIDS;
190       outer_map.add (0);
191       for (hb_codepoint_t gid = 0; gid < plan->num_output_glyphs (); gid++)
192       {
193 	hb_codepoint_t old_gid;
194 	if (plan->old_gid_for_new_gid (gid, &old_gid))
195 	  inner_sets[0]->add (old_gid);
196       }
197       hb_set_union (adv_set, inner_sets[0]);
198     }
199 
200     for (unsigned int i = 1; i < index_maps.length; i++)
201       index_map_plans[i].init (*index_maps[i], outer_map, inner_sets, plan);
202 
203     outer_map.sort ();
204 
205     if (retain_adv_map)
206     {
207       for (hb_codepoint_t gid = 0; gid < plan->num_output_glyphs (); gid++)
208 	if (inner_sets[0]->has (gid))
209 	  inner_maps[0].add (gid);
210 	else
211 	  inner_maps[0].skip ();
212     }
213     else
214     {
215       inner_maps[0].add_set (adv_set);
216       hb_set_subtract (inner_sets[0], adv_set);
217       inner_maps[0].add_set (inner_sets[0]);
218     }
219 
220     for (unsigned int i = 1; i < inner_maps.length; i++)
221       inner_maps[i].add_set (inner_sets[i]);
222 
223     for (unsigned int i = 0; i < index_maps.length; i++)
224       index_map_plans[i].remap (index_maps[i], outer_map, inner_maps, plan);
225   }
226 
finiOT::hvarvvar_subset_plan_t227   void fini ()
228   {
229     for (unsigned int i = 0; i < inner_sets.length; i++)
230       hb_set_destroy (inner_sets[i]);
231     hb_set_destroy (adv_set);
232     inner_maps.fini_deep ();
233     index_map_plans.fini_deep ();
234   }
235 
236   hb_inc_bimap_t outer_map;
237   hb_vector_t<hb_inc_bimap_t> inner_maps;
238   hb_vector_t<index_map_subset_plan_t> index_map_plans;
239   const VariationStore *var_store;
240 
241   protected:
242   hb_vector_t<hb_set_t *> inner_sets;
243   hb_set_t *adv_set;
244 };
245 
246 /*
247  * HVAR -- Horizontal Metrics Variations
248  * https://docs.microsoft.com/en-us/typography/opentype/spec/hvar
249  * VVAR -- Vertical Metrics Variations
250  * https://docs.microsoft.com/en-us/typography/opentype/spec/vvar
251  */
252 #define HB_OT_TAG_HVAR HB_TAG('H','V','A','R')
253 #define HB_OT_TAG_VVAR HB_TAG('V','V','A','R')
254 
255 struct HVARVVAR
256 {
257   static constexpr hb_tag_t HVARTag = HB_OT_TAG_HVAR;
258   static constexpr hb_tag_t VVARTag = HB_OT_TAG_VVAR;
259 
sanitizeOT::HVARVVAR260   bool sanitize (hb_sanitize_context_t *c) const
261   {
262     TRACE_SANITIZE (this);
263     return_trace (version.sanitize (c) &&
264 		  likely (version.major == 1) &&
265 		  varStore.sanitize (c, this) &&
266 		  advMap.sanitize (c, this) &&
267 		  lsbMap.sanitize (c, this) &&
268 		  rsbMap.sanitize (c, this));
269   }
270 
listup_index_mapsOT::HVARVVAR271   void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
272   {
273     index_maps.push (&(this+advMap));
274     index_maps.push (&(this+lsbMap));
275     index_maps.push (&(this+rsbMap));
276   }
277 
serialize_index_mapsOT::HVARVVAR278   bool serialize_index_maps (hb_serialize_context_t *c,
279 			     const hb_array_t<index_map_subset_plan_t> &im_plans)
280   {
281     TRACE_SERIALIZE (this);
282     if (im_plans[index_map_subset_plan_t::ADV_INDEX].is_identity ())
283       advMap = 0;
284     else if (unlikely (!advMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::ADV_INDEX])))
285       return_trace (false);
286     if (im_plans[index_map_subset_plan_t::LSB_INDEX].is_identity ())
287       lsbMap = 0;
288     else if (unlikely (!lsbMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::LSB_INDEX])))
289       return_trace (false);
290     if (im_plans[index_map_subset_plan_t::RSB_INDEX].is_identity ())
291       rsbMap = 0;
292     else if (unlikely (!rsbMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::RSB_INDEX])))
293       return_trace (false);
294 
295     return_trace (true);
296   }
297 
298   template <typename T>
_subsetOT::HVARVVAR299   bool _subset (hb_subset_context_t *c) const
300   {
301     TRACE_SUBSET (this);
302     hvarvvar_subset_plan_t	hvar_plan;
303     hb_vector_t<const DeltaSetIndexMap *>
304 				index_maps;
305 
306     ((T*)this)->listup_index_maps (index_maps);
307     hvar_plan.init (index_maps.as_array (), this+varStore, c->plan);
308 
309     T *out = c->serializer->allocate_min<T> ();
310     if (unlikely (!out)) return_trace (false);
311 
312     out->version.major = 1;
313     out->version.minor = 0;
314 
315     if (unlikely (!out->varStore
316 		      .serialize_serialize (c->serializer,
317 					    hvar_plan.var_store,
318 					    hvar_plan.inner_maps.as_array ())))
319       return_trace (false);
320 
321     return_trace (out->T::serialize_index_maps (c->serializer,
322 						hvar_plan.index_map_plans.as_array ()));
323   }
324 
get_advance_varOT::HVARVVAR325   float get_advance_var (hb_codepoint_t glyph, hb_font_t *font) const
326   {
327     uint32_t varidx = (this+advMap).map (glyph);
328     return (this+varStore).get_delta (varidx, font->coords, font->num_coords);
329   }
330 
get_side_bearing_varOT::HVARVVAR331   float get_side_bearing_var (hb_codepoint_t glyph,
332 			      const int *coords, unsigned int coord_count) const
333   {
334     if (!has_side_bearing_deltas ()) return 0.f;
335     uint32_t varidx = (this+lsbMap).map (glyph);
336     return (this+varStore).get_delta (varidx, coords, coord_count);
337   }
338 
has_side_bearing_deltasOT::HVARVVAR339   bool has_side_bearing_deltas () const { return lsbMap && rsbMap; }
340 
341   protected:
342   FixedVersion<>version;	/* Version of the metrics variation table
343 				 * initially set to 0x00010000u */
344   Offset32To<VariationStore>
345 		varStore;	/* Offset to item variation store table. */
346   Offset32To<DeltaSetIndexMap>
347 		advMap;		/* Offset to advance var-idx mapping. */
348   Offset32To<DeltaSetIndexMap>
349 		lsbMap;		/* Offset to lsb/tsb var-idx mapping. */
350   Offset32To<DeltaSetIndexMap>
351 		rsbMap;		/* Offset to rsb/bsb var-idx mapping. */
352 
353   public:
354   DEFINE_SIZE_STATIC (20);
355 };
356 
357 struct HVAR : HVARVVAR {
358   static constexpr hb_tag_t tableTag = HB_OT_TAG_HVAR;
subsetOT::HVAR359   bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<HVAR> (c); }
360 };
361 struct VVAR : HVARVVAR {
362   static constexpr hb_tag_t tableTag = HB_OT_TAG_VVAR;
363 
sanitizeOT::VVAR364   bool sanitize (hb_sanitize_context_t *c) const
365   {
366     TRACE_SANITIZE (this);
367     return_trace (static_cast<const HVARVVAR *> (this)->sanitize (c) &&
368 		  vorgMap.sanitize (c, this));
369   }
370 
listup_index_mapsOT::VVAR371   void listup_index_maps (hb_vector_t<const DeltaSetIndexMap *> &index_maps) const
372   {
373     HVARVVAR::listup_index_maps (index_maps);
374     index_maps.push (&(this+vorgMap));
375   }
376 
serialize_index_mapsOT::VVAR377   bool serialize_index_maps (hb_serialize_context_t *c,
378 			     const hb_array_t<index_map_subset_plan_t> &im_plans)
379   {
380     TRACE_SERIALIZE (this);
381     if (unlikely (!HVARVVAR::serialize_index_maps (c, im_plans)))
382       return_trace (false);
383     if (!im_plans[index_map_subset_plan_t::VORG_INDEX].get_map_count ())
384       vorgMap = 0;
385     else if (unlikely (!vorgMap.serialize_serialize (c, im_plans[index_map_subset_plan_t::VORG_INDEX])))
386       return_trace (false);
387 
388     return_trace (true);
389   }
390 
subsetOT::VVAR391   bool subset (hb_subset_context_t *c) const { return HVARVVAR::_subset<VVAR> (c); }
392 
393   protected:
394   Offset32To<DeltaSetIndexMap>
395 		vorgMap;	/* Offset to vertical-origin var-idx mapping. */
396 
397   public:
398   DEFINE_SIZE_STATIC (24);
399 };
400 
401 } /* namespace OT */
402 
403 
404 #endif /* HB_OT_VAR_HVAR_TABLE_HH */
405