• 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_AVAR_TABLE_HH
28 #define HB_OT_VAR_AVAR_TABLE_HH
29 
30 #include "hb-open-type.hh"
31 #include "hb-ot-var-common.hh"
32 
33 
34 /*
35  * avar -- Axis Variations
36  * https://docs.microsoft.com/en-us/typography/opentype/spec/avar
37  */
38 
39 #define HB_OT_TAG_avar HB_TAG('a','v','a','r')
40 
41 
42 namespace OT {
43 
44 
45 /* "Spec": https://github.com/be-fonts/boring-expansion-spec/issues/14 */
46 struct avarV2Tail
47 {
48   friend struct avar;
49 
sanitizeOT::avarV2Tail50   bool sanitize (hb_sanitize_context_t *c,
51 		 const void *base) const
52   {
53     TRACE_SANITIZE (this);
54     return_trace (varIdxMap.sanitize (c, base) &&
55 		  varStore.sanitize (c, base));
56   }
57 
58   protected:
59   Offset32To<DeltaSetIndexMap>	varIdxMap;	/* Offset from the beginning of 'avar' table. */
60   Offset32To<VariationStore>	varStore;	/* Offset from the beginning of 'avar' table. */
61 
62   public:
63   DEFINE_SIZE_STATIC (8);
64 };
65 
66 
67 struct AxisValueMap
68 {
sanitizeOT::AxisValueMap69   bool sanitize (hb_sanitize_context_t *c) const
70   {
71     TRACE_SANITIZE (this);
72     return_trace (c->check_struct (this));
73   }
74 
75   public:
76   F2DOT14	coords[2];
77 //   F2DOT14	fromCoord;	/* A normalized coordinate value obtained using
78 //				 * default normalization. */
79 //   F2DOT14	toCoord;	/* The modified, normalized coordinate value. */
80 
81   public:
82   DEFINE_SIZE_STATIC (4);
83 };
84 
85 struct SegmentMaps : Array16Of<AxisValueMap>
86 {
mapOT::SegmentMaps87   int map (int value, unsigned int from_offset = 0, unsigned int to_offset = 1) const
88   {
89 #define fromCoord coords[from_offset]
90 #define toCoord coords[to_offset]
91     /* The following special-cases are not part of OpenType, which requires
92      * that at least -1, 0, and +1 must be mapped. But we include these as
93      * part of a better error recovery scheme. */
94     if (len < 2)
95     {
96       if (!len)
97 	return value;
98       else /* len == 1*/
99 	return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
100     }
101 
102     if (value <= arrayZ[0].fromCoord)
103       return value - arrayZ[0].fromCoord + arrayZ[0].toCoord;
104 
105     unsigned int i;
106     unsigned int count = len - 1;
107     for (i = 1; i < count && value > arrayZ[i].fromCoord; i++)
108       ;
109 
110     if (value >= arrayZ[i].fromCoord)
111       return value - arrayZ[i].fromCoord + arrayZ[i].toCoord;
112 
113     if (unlikely (arrayZ[i-1].fromCoord == arrayZ[i].fromCoord))
114       return arrayZ[i-1].toCoord;
115 
116     int denom = arrayZ[i].fromCoord - arrayZ[i-1].fromCoord;
117     return roundf (arrayZ[i-1].toCoord + ((float) (arrayZ[i].toCoord - arrayZ[i-1].toCoord) *
118 					  (value - arrayZ[i-1].fromCoord)) / denom);
119 #undef toCoord
120 #undef fromCoord
121   }
122 
unmapOT::SegmentMaps123   int unmap (int value) const { return map (value, 1, 0); }
124 
125   public:
126   DEFINE_SIZE_ARRAY (2, *this);
127 };
128 
129 struct avar
130 {
131   static constexpr hb_tag_t tableTag = HB_OT_TAG_avar;
132 
has_dataOT::avar133   bool has_data () const { return version.to_int (); }
134 
get_segment_mapsOT::avar135   const SegmentMaps* get_segment_maps () const
136   { return &firstAxisSegmentMaps; }
137 
get_axis_countOT::avar138   unsigned get_axis_count () const
139   { return axisCount; }
140 
sanitizeOT::avar141   bool sanitize (hb_sanitize_context_t *c) const
142   {
143     TRACE_SANITIZE (this);
144     if (!(version.sanitize (c) &&
145 	  (version.major == 1
146 #ifndef HB_NO_AVAR2
147 	   || version.major == 2
148 #endif
149 	   ) &&
150 	  c->check_struct (this)))
151       return_trace (false);
152 
153     const SegmentMaps *map = &firstAxisSegmentMaps;
154     unsigned int count = axisCount;
155     for (unsigned int i = 0; i < count; i++)
156     {
157       if (unlikely (!map->sanitize (c)))
158 	return_trace (false);
159       map = &StructAfter<SegmentMaps> (*map);
160     }
161 
162 #ifndef HB_NO_AVAR2
163     if (version.major < 2)
164       return_trace (true);
165 
166     const auto &v2 = * (const avarV2Tail *) map;
167     if (unlikely (!v2.sanitize (c, this)))
168       return_trace (false);
169 #endif
170 
171     return_trace (true);
172   }
173 
map_coordsOT::avar174   void map_coords (int *coords, unsigned int coords_length) const
175   {
176     unsigned int count = hb_min (coords_length, axisCount);
177 
178     const SegmentMaps *map = &firstAxisSegmentMaps;
179     for (unsigned int i = 0; i < count; i++)
180     {
181       coords[i] = map->map (coords[i]);
182       map = &StructAfter<SegmentMaps> (*map);
183     }
184 
185 #ifndef HB_NO_AVAR2
186     if (version.major < 2)
187       return;
188 
189     for (; count < axisCount; count++)
190       map = &StructAfter<SegmentMaps> (*map);
191 
192     const auto &v2 = * (const avarV2Tail *) map;
193 
194     const auto &varidx_map = this+v2.varIdxMap;
195     const auto &var_store = this+v2.varStore;
196     auto *var_store_cache = var_store.create_cache ();
197 
198     hb_vector_t<int> out;
199     out.alloc (coords_length);
200     for (unsigned i = 0; i < coords_length; i++)
201     {
202       int v = coords[i];
203       uint32_t varidx = varidx_map.map (i);
204       float delta = var_store.get_delta (varidx, coords, coords_length, var_store_cache);
205       v += roundf (delta);
206       v = hb_clamp (v, -(1<<14), +(1<<14));
207       out.push (v);
208     }
209     for (unsigned i = 0; i < coords_length; i++)
210       coords[i] = out[i];
211 
212     OT::VariationStore::destroy_cache (var_store_cache);
213 #endif
214   }
215 
unmap_coordsOT::avar216   void unmap_coords (int *coords, unsigned int coords_length) const
217   {
218     unsigned int count = hb_min (coords_length, axisCount);
219 
220     const SegmentMaps *map = &firstAxisSegmentMaps;
221     for (unsigned int i = 0; i < count; i++)
222     {
223       coords[i] = map->unmap (coords[i]);
224       map = &StructAfter<SegmentMaps> (*map);
225     }
226   }
227 
228   protected:
229   FixedVersion<>version;	/* Version of the avar table
230 				 * initially set to 0x00010000u */
231   HBUINT16	reserved;	/* This field is permanently reserved. Set to 0. */
232   HBUINT16	axisCount;	/* The number of variation axes in the font. This
233 				 * must be the same number as axisCount in the
234 				 * 'fvar' table. */
235   SegmentMaps	firstAxisSegmentMaps;
236 
237   public:
238   DEFINE_SIZE_MIN (8);
239 };
240 
241 } /* namespace OT */
242 
243 
244 #endif /* HB_OT_VAR_AVAR_TABLE_HH */
245