hb-cff2-interp-cs.hh 7.0 KB
Newer Older
M
Michiharu Ariza 已提交
1
/*
M
Michiharu Ariza 已提交
2
 * Copyright © 2018 Adobe Inc.
M
Michiharu Ariza 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  This is part of HarfBuzz, a text shaping library.
 *
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the
 * above copyright notice and the following two paragraphs appear in
 * all copies of this software.
 *
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 *
 * Adobe Author(s): Michiharu Ariza
 */
#ifndef HB_CFF2_INTERP_CS_HH
#define HB_CFF2_INTERP_CS_HH

29 30
#include "hb.hh"
#include "hb-cff-interp-cs-common.hh"
M
Michiharu Ariza 已提交
31 32 33 34 35

namespace CFF {

using namespace OT;

M
Michiharu Ariza 已提交
36
struct blend_arg_t : number_t
37
{
38
  void init ()
39
  {
M
Michiharu Ariza 已提交
40
    number_t::init ();
41 42 43
    deltas.init ();
  }

44
  void fini ()
45
  {
M
Michiharu Ariza 已提交
46
    number_t::fini ();
M
Michiharu Ariza 已提交
47
    deltas.fini_deep ();
48 49
  }

M
Michiharu Ariza 已提交
50 51 52
  void set_int (int v) { reset_blends (); number_t::set_int (v); }
  void set_fixed (int32_t v) { reset_blends (); number_t::set_fixed (v); }
  void set_real (double v) { reset_blends (); number_t::set_real (v); }
53

54
  void set_blends (unsigned int numValues_, unsigned int valueIndex_,
M
Michiharu Ariza 已提交
55
			  unsigned int numBlends, hb_array_t<const blend_arg_t> blends_)
56 57 58 59 60 61 62 63
  {
    numValues = numValues_;
    valueIndex = valueIndex_;
    deltas.resize (numBlends);
    for (unsigned int i = 0; i < numBlends; i++)
      deltas[i] = blends_[i];
  }

64
  bool blending () const { return deltas.length > 0; }
65
  void reset_blends ()
66 67 68 69 70 71 72
  {
    numValues = valueIndex = 0;
    deltas.resize (0);
  }

  unsigned int numValues;
  unsigned int valueIndex;
M
Michiharu Ariza 已提交
73
  hb_vector_t<number_t> deltas;
74 75
};

M
Michiharu Ariza 已提交
76 77
typedef interp_env_t<blend_arg_t> BlendInterpEnv;
typedef biased_subrs_t<CFF2Subrs>   cff2_biased_subrs_t;
78

M
Michiharu Ariza 已提交
79
struct cff2_cs_interp_env_t : cs_interp_env_t<blend_arg_t, CFF2Subrs>
M
Michiharu Ariza 已提交
80
{
81
  template <typename ACC>
M
Michiharu Ariza 已提交
82
  void init (const byte_str_t &str, ACC &acc, unsigned int fd,
B
Behdad Esfahbod 已提交
83
		    const int *coords_=nullptr, unsigned int num_coords_=0)
M
Michiharu Ariza 已提交
84
  {
85
    SUPER::init (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs);
B
Behdad Esfahbod 已提交
86

M
Michiharu Ariza 已提交
87
    coords = coords_;
M
Michiharu Ariza 已提交
88 89
    num_coords = num_coords_;
    varStore = acc.varStore;
M
Michiharu Ariza 已提交
90
    seen_blend = false;
91
    seen_vsindex_ = false;
M
Michiharu Ariza 已提交
92 93 94 95 96
    scalars.init ();
    do_blend = (coords != nullptr) && num_coords && (varStore != &Null(CFF2VariationStore));
    set_ivs (acc.privateDicts[fd].ivs);
  }

97
  void fini ()
M
Michiharu Ariza 已提交
98 99 100
  {
    scalars.fini ();
    SUPER::fini ();
M
Michiharu Ariza 已提交
101 102
  }

M
Michiharu Ariza 已提交
103
  op_code_t fetch_op ()
M
Michiharu Ariza 已提交
104
  {
M
Michiharu Ariza 已提交
105
    if (this->str_ref.avail ())
106
      return SUPER::fetch_op ();
M
Michiharu Ariza 已提交
107 108

    /* make up return or endchar op */
109 110
    if (this->callStack.is_empty ())
      return OpCode_endchar;
M
Michiharu Ariza 已提交
111
    else
112
      return OpCode_return;
M
Michiharu Ariza 已提交
113 114
  }

M
Michiharu Ariza 已提交
115
  const blend_arg_t& eval_arg (unsigned int i)
M
Michiharu Ariza 已提交
116
  {
M
Michiharu Ariza 已提交
117
    blend_arg_t  &arg = argStack[i];
M
Michiharu Ariza 已提交
118 119
    blend_arg (arg);
    return arg;
M
Michiharu Ariza 已提交
120 121
  }

M
Michiharu Ariza 已提交
122
  const blend_arg_t& pop_arg ()
M
Michiharu Ariza 已提交
123
  {
M
Michiharu Ariza 已提交
124
    blend_arg_t  &arg = argStack.pop ();
M
Michiharu Ariza 已提交
125 126
    blend_arg (arg);
    return arg;
M
Michiharu Ariza 已提交
127 128
  }

129
  void process_blend ()
M
Michiharu Ariza 已提交
130 131 132 133 134 135
  {
    if (!seen_blend)
    {
      region_count = varStore->varStore.get_region_index_count (get_ivs ());
      if (do_blend)
      {
B
Behdad Esfahbod 已提交
136 137 138 139
	scalars.resize (region_count);
	varStore->varStore.get_scalars (get_ivs (),
					(int *)coords, num_coords,
					&scalars[0], region_count);
M
Michiharu Ariza 已提交
140 141 142 143 144
      }
      seen_blend = true;
    }
  }

145
  void process_vsindex ()
M
Michiharu Ariza 已提交
146
  {
147
    unsigned int  index = argStack.pop_uint ();
148
    if (unlikely (seen_vsindex () || seen_blend))
M
Michiharu Ariza 已提交
149
    {
150
      set_error ();
M
Michiharu Ariza 已提交
151
    }
152 153 154 155 156
    else
    {
      set_ivs (index);
    }
    seen_vsindex_ = true;
M
Michiharu Ariza 已提交
157 158
  }

159
  unsigned int get_region_count () const { return region_count; }
160
  void	 set_region_count (unsigned int region_count_) { region_count = region_count_; }
161
  unsigned int get_ivs () const { return ivs; }
162
  void	 set_ivs (unsigned int ivs_) { ivs = ivs_; }
163
  bool	 seen_vsindex () const { return seen_vsindex_; }
M
Michiharu Ariza 已提交
164 165

  protected:
M
Michiharu Ariza 已提交
166
  void blend_arg (blend_arg_t &arg)
M
Michiharu Ariza 已提交
167 168 169
  {
    if (do_blend && arg.blending ())
    {
170
      if (likely (scalars.length == arg.deltas.length))
M
Michiharu Ariza 已提交
171
      {
172
	double v = arg.to_real ();
173
	for (unsigned int i = 0; i < scalars.length; i++)
B
Behdad Esfahbod 已提交
174
	{
M
Michiharu Ariza 已提交
175
	  v += (double)scalars[i] * arg.deltas[i].to_real ();
B
Behdad Esfahbod 已提交
176 177 178
	}
	arg.set_real (v);
	arg.deltas.resize (0);
M
Michiharu Ariza 已提交
179 180 181 182 183 184 185
      }
    }
  }

  protected:
  const int     *coords;
  unsigned int  num_coords;
B
Behdad Esfahbod 已提交
186
  const	 CFF2VariationStore *varStore;
187
  unsigned int  region_count;
M
Michiharu Ariza 已提交
188 189
  unsigned int  ivs;
  hb_vector_t<float>  scalars;
B
Behdad Esfahbod 已提交
190 191 192
  bool	  do_blend;
  bool	  seen_vsindex_;
  bool	  seen_blend;
193

M
Michiharu Ariza 已提交
194
  typedef cs_interp_env_t<blend_arg_t, CFF2Subrs> SUPER;
M
Michiharu Ariza 已提交
195
};
196
template <typename OPSET, typename PARAM, typename PATH=path_procs_null_t<cff2_cs_interp_env_t, PARAM>>
M
Michiharu Ariza 已提交
197
struct cff2_cs_opset_t : cs_opset_t<blend_arg_t, OPSET, cff2_cs_interp_env_t, PARAM, PATH>
M
Michiharu Ariza 已提交
198
{
M
Michiharu Ariza 已提交
199
  static void process_op (op_code_t op, cff2_cs_interp_env_t &env, PARAM& param)
M
Michiharu Ariza 已提交
200 201
  {
    switch (op) {
202 203
      case OpCode_callsubr:
      case OpCode_callgsubr:
B
Behdad Esfahbod 已提交
204 205 206 207 208 209 210 211
	/* a subroutine number shoudln't be a blended value */
	if (unlikely (env.argStack.peek ().blending ()))
	{
	  env.set_error ();
	  break;
	}
	SUPER::process_op (op, env, param);
	break;
M
Michiharu Ariza 已提交
212 213

      case OpCode_blendcs:
B
Behdad Esfahbod 已提交
214 215
	OPSET::process_blend (env, param);
	break;
M
Michiharu Ariza 已提交
216

M
Michiharu Ariza 已提交
217
      case OpCode_vsindexcs:
B
Behdad Esfahbod 已提交
218 219 220 221 222 223 224
	if (unlikely (env.argStack.peek ().blending ()))
	{
	  env.set_error ();
	  break;
	}
	OPSET::process_vsindex (env, param);
	break;
M
Michiharu Ariza 已提交
225

M
Michiharu Ariza 已提交
226
      default:
B
Behdad Esfahbod 已提交
227
	SUPER::process_op (op, env, param);
M
Michiharu Ariza 已提交
228 229
    }
  }
M
Michiharu Ariza 已提交
230

M
Michiharu Ariza 已提交
231
  static void process_blend (cff2_cs_interp_env_t &env, PARAM& param)
M
Michiharu Ariza 已提交
232
  {
233 234
    unsigned int n, k;

M
Michiharu Ariza 已提交
235
    env.process_blend ();
236
    k = env.get_region_count ();
237
    n = env.argStack.pop_uint ();
238 239 240 241
    /* copy the blend values into blend array of the default values */
    unsigned int start = env.argStack.get_count () - ((k+1) * n);
    /* let an obvious error case fail, but note CFF2 spec doesn't forbid n==0 */
    if (unlikely (start > env.argStack.get_count ()))
242 243 244 245
    {
      env.set_error ();
      return;
    }
246
    for (unsigned int i = 0; i < n; i++)
247
    {
M
Michiharu Ariza 已提交
248
      const hb_array_t<const blend_arg_t>	blends = env.argStack.get_subarray (start + n + (i * k));
249 250
      env.argStack[start + i].set_blends (n, i, k, blends);
    }
251 252

    /* pop off blend values leaving default values now adorned with blend values */
253
    env.argStack.pop (k * n);
M
Michiharu Ariza 已提交
254 255
  }

M
Michiharu Ariza 已提交
256
  static void process_vsindex (cff2_cs_interp_env_t &env, PARAM& param)
M
Michiharu Ariza 已提交
257
  {
258
    env.process_vsindex ();
M
Michiharu Ariza 已提交
259
    env.clear_args ();
M
Michiharu Ariza 已提交
260
  }
261 262

  private:
M
Michiharu Ariza 已提交
263
  typedef cs_opset_t<blend_arg_t, OPSET, cff2_cs_interp_env_t, PARAM, PATH>  SUPER;
M
Michiharu Ariza 已提交
264 265 266
};

template <typename OPSET, typename PARAM>
M
Michiharu Ariza 已提交
267
struct cff2_cs_interpreter_t : cs_interpreter_t<cff2_cs_interp_env_t, OPSET, PARAM> {};
M
Michiharu Ariza 已提交
268 269 270 271

} /* namespace CFF */

#endif /* HB_CFF2_INTERP_CS_HH */