Imported Upstream version 3.4.0
[platform/upstream/harfbuzz.git] / src / hb-cff2-interp-cs.hh
1 /*
2  * Copyright © 2018 Adobe 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  * Adobe Author(s): Michiharu Ariza
25  */
26 #ifndef HB_CFF2_INTERP_CS_HH
27 #define HB_CFF2_INTERP_CS_HH
28
29 #include "hb.hh"
30 #include "hb-cff-interp-cs-common.hh"
31
32 namespace CFF {
33
34 using namespace OT;
35
36 struct blend_arg_t : number_t
37 {
38   void set_int (int v) { reset_blends (); number_t::set_int (v); }
39   void set_fixed (int32_t v) { reset_blends (); number_t::set_fixed (v); }
40   void set_real (double v) { reset_blends (); number_t::set_real (v); }
41
42   void set_blends (unsigned int numValues_, unsigned int valueIndex_,
43                    unsigned int numBlends, hb_array_t<const blend_arg_t> blends_)
44   {
45     numValues = numValues_;
46     valueIndex = valueIndex_;
47     deltas.resize (numBlends);
48     for (unsigned int i = 0; i < numBlends; i++)
49       deltas[i] = blends_[i];
50   }
51
52   bool blending () const { return deltas.length > 0; }
53   void reset_blends ()
54   {
55     numValues = valueIndex = 0;
56     deltas.resize (0);
57   }
58
59   unsigned int numValues;
60   unsigned int valueIndex;
61   hb_vector_t<number_t> deltas;
62 };
63
64 typedef interp_env_t<blend_arg_t> BlendInterpEnv;
65 typedef biased_subrs_t<CFF2Subrs>   cff2_biased_subrs_t;
66
67 struct cff2_cs_interp_env_t : cs_interp_env_t<blend_arg_t, CFF2Subrs>
68 {
69   template <typename ACC>
70   void init (const byte_str_t &str, ACC &acc, unsigned int fd,
71              const int *coords_=nullptr, unsigned int num_coords_=0)
72   {
73     SUPER::init (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs);
74
75     coords = coords_;
76     num_coords = num_coords_;
77     varStore = acc.varStore;
78     seen_blend = false;
79     seen_vsindex_ = false;
80     scalars.init ();
81     do_blend = num_coords && coords && varStore->size;
82     set_ivs (acc.privateDicts[fd].ivs);
83   }
84
85   void fini ()
86   {
87     scalars.fini ();
88     SUPER::fini ();
89   }
90
91   op_code_t fetch_op ()
92   {
93     if (this->str_ref.avail ())
94       return SUPER::fetch_op ();
95
96     /* make up return or endchar op */
97     if (this->callStack.is_empty ())
98       return OpCode_endchar;
99     else
100       return OpCode_return;
101   }
102
103   const blend_arg_t& eval_arg (unsigned int i)
104   {
105     blend_arg_t  &arg = argStack[i];
106     blend_arg (arg);
107     return arg;
108   }
109
110   const blend_arg_t& pop_arg ()
111   {
112     blend_arg_t  &arg = argStack.pop ();
113     blend_arg (arg);
114     return arg;
115   }
116
117   void process_blend ()
118   {
119     if (!seen_blend)
120     {
121       region_count = varStore->varStore.get_region_index_count (get_ivs ());
122       if (do_blend)
123       {
124         if (unlikely (!scalars.resize (region_count)))
125           set_error ();
126         else
127           varStore->varStore.get_region_scalars (get_ivs (), coords, num_coords,
128                                                  &scalars[0], region_count);
129       }
130       seen_blend = true;
131     }
132   }
133
134   void process_vsindex ()
135   {
136     unsigned int  index = argStack.pop_uint ();
137     if (unlikely (seen_vsindex () || seen_blend))
138     {
139       set_error ();
140     }
141     else
142     {
143       set_ivs (index);
144     }
145     seen_vsindex_ = true;
146   }
147
148   unsigned int get_region_count () const { return region_count; }
149   void   set_region_count (unsigned int region_count_) { region_count = region_count_; }
150   unsigned int get_ivs () const { return ivs; }
151   void   set_ivs (unsigned int ivs_) { ivs = ivs_; }
152   bool   seen_vsindex () const { return seen_vsindex_; }
153
154   protected:
155   void blend_arg (blend_arg_t &arg)
156   {
157     if (do_blend && arg.blending ())
158     {
159       if (likely (scalars.length == arg.deltas.length))
160       {
161         double v = arg.to_real ();
162         for (unsigned int i = 0; i < scalars.length; i++)
163         {
164           v += (double)scalars[i] * arg.deltas[i].to_real ();
165         }
166         arg.set_real (v);
167         arg.deltas.resize (0);
168       }
169     }
170   }
171
172   protected:
173   const int     *coords;
174   unsigned int  num_coords;
175   const  CFF2VariationStore *varStore;
176   unsigned int  region_count;
177   unsigned int  ivs;
178   hb_vector_t<float>  scalars;
179   bool    do_blend;
180   bool    seen_vsindex_;
181   bool    seen_blend;
182
183   typedef cs_interp_env_t<blend_arg_t, CFF2Subrs> SUPER;
184 };
185 template <typename OPSET, typename PARAM, typename PATH=path_procs_null_t<cff2_cs_interp_env_t, PARAM>>
186 struct cff2_cs_opset_t : cs_opset_t<blend_arg_t, OPSET, cff2_cs_interp_env_t, PARAM, PATH>
187 {
188   static void process_op (op_code_t op, cff2_cs_interp_env_t &env, PARAM& param)
189   {
190     switch (op) {
191       case OpCode_callsubr:
192       case OpCode_callgsubr:
193         /* a subroutine number shouldn't be a blended value */
194         if (unlikely (env.argStack.peek ().blending ()))
195         {
196           env.set_error ();
197           break;
198         }
199         SUPER::process_op (op, env, param);
200         break;
201
202       case OpCode_blendcs:
203         OPSET::process_blend (env, param);
204         break;
205
206       case OpCode_vsindexcs:
207         if (unlikely (env.argStack.peek ().blending ()))
208         {
209           env.set_error ();
210           break;
211         }
212         OPSET::process_vsindex (env, param);
213         break;
214
215       default:
216         SUPER::process_op (op, env, param);
217     }
218   }
219
220   static void process_blend (cff2_cs_interp_env_t &env, PARAM& param)
221   {
222     unsigned int n, k;
223
224     env.process_blend ();
225     k = env.get_region_count ();
226     n = env.argStack.pop_uint ();
227     /* copy the blend values into blend array of the default values */
228     unsigned int start = env.argStack.get_count () - ((k+1) * n);
229     /* let an obvious error case fail, but note CFF2 spec doesn't forbid n==0 */
230     if (unlikely (start > env.argStack.get_count ()))
231     {
232       env.set_error ();
233       return;
234     }
235     for (unsigned int i = 0; i < n; i++)
236     {
237       const hb_array_t<const blend_arg_t>       blends = env.argStack.get_subarray (start + n + (i * k));
238       env.argStack[start + i].set_blends (n, i, k, blends);
239     }
240
241     /* pop off blend values leaving default values now adorned with blend values */
242     env.argStack.pop (k * n);
243   }
244
245   static void process_vsindex (cff2_cs_interp_env_t &env, PARAM& param)
246   {
247     env.process_vsindex ();
248     env.clear_args ();
249   }
250
251   private:
252   typedef cs_opset_t<blend_arg_t, OPSET, cff2_cs_interp_env_t, PARAM, PATH>  SUPER;
253 };
254
255 template <typename OPSET, typename PARAM>
256 struct cff2_cs_interpreter_t : cs_interpreter_t<cff2_cs_interp_env_t, OPSET, PARAM> {};
257
258 } /* namespace CFF */
259
260 #endif /* HB_CFF2_INTERP_CS_HH */