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;

36 37 38 39 40 41 42 43 44 45 46
struct BlendArg : Number
{
  inline void init (void)
  {
    Number::init ();
    deltas.init ();
  }

  inline void fini (void)
  {
    Number::fini ();
M
Michiharu Ariza 已提交
47
    deltas.fini_deep ();
48 49 50 51
  }

  inline void set_int (int v) { reset_blends (); Number::set_int (v); }
  inline void set_fixed (int32_t v) { reset_blends (); Number::set_fixed (v); }
M
Michiharu Ariza 已提交
52
  inline void set_real (double v) { reset_blends (); Number::set_real (v); }
53 54

  inline void set_blends (unsigned int numValues_, unsigned int valueIndex_,
55
			  unsigned int numBlends, const hb_array_t<const BlendArg> &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];
  }

M
Michiharu Ariza 已提交
64
  inline bool blending (void) const { return deltas.len > 0; }
65 66 67 68 69 70 71 72 73 74 75 76
  inline void reset_blends (void)
  {
    numValues = valueIndex = 0;
    deltas.resize (0);
  }

  unsigned int numValues;
  unsigned int valueIndex;
  hb_vector_t<Number> deltas;
};

typedef InterpEnv<BlendArg> BlendInterpEnv;
77
typedef BiasedSubrs<CFF2Subrs>   CFF2BiasedSubrs;
78

79
struct CFF2CSInterpEnv : CSInterpEnv<BlendArg, CFF2Subrs>
M
Michiharu Ariza 已提交
80
{
81
  template <typename ACC>
M
Michiharu Ariza 已提交
82
  inline void init (const ByteStr &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 97 98 99 100
    scalars.init ();
    do_blend = (coords != nullptr) && num_coords && (varStore != &Null(CFF2VariationStore));
    set_ivs (acc.privateDicts[fd].ivs);
  }

  inline void fini (void)
  {
    scalars.fini ();
    SUPER::fini ();
M
Michiharu Ariza 已提交
101 102
  }

103
  inline OpCode fetch_op (void)
M
Michiharu Ariza 已提交
104
  {
105
    if (this->substr.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 116
  inline const BlendArg& eval_arg (unsigned int i)
  {
M
Michiharu Ariza 已提交
117 118 119
    BlendArg  &arg = argStack[i];
    blend_arg (arg);
    return arg;
M
Michiharu Ariza 已提交
120 121 122 123
  }

  inline const BlendArg& pop_arg (void)
  {
M
Michiharu Ariza 已提交
124 125 126
    BlendArg  &arg = argStack.pop ();
    blend_arg (arg);
    return arg;
M
Michiharu Ariza 已提交
127 128 129 130 131 132 133 134 135
  }

  inline void process_blend (void)
  {
    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
  inline void process_vsindex (void)
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
  inline unsigned int get_region_count (void) const { return region_count; }
B
Behdad Esfahbod 已提交
160
  inline void	 set_region_count (unsigned int region_count_) { region_count = region_count_; }
M
Michiharu Ariza 已提交
161
  inline unsigned int get_ivs (void) const { return ivs; }
B
Behdad Esfahbod 已提交
162 163
  inline void	 set_ivs (unsigned int ivs_) { ivs = ivs_; }
  inline bool	 seen_vsindex (void) const { return seen_vsindex_; }
M
Michiharu Ariza 已提交
164 165

  protected:
M
Michiharu Ariza 已提交
166
  inline void blend_arg (BlendArg &arg)
M
Michiharu Ariza 已提交
167 168 169 170 171
  {
    if (do_blend && arg.blending ())
    {
      if (likely (scalars.len == arg.deltas.len))
      {
172
	double v = arg.to_real ();
B
Behdad Esfahbod 已提交
173 174
	for (unsigned int i = 0; i < scalars.len; i++)
	{
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 194

  typedef CSInterpEnv<BlendArg, CFF2Subrs> SUPER;
M
Michiharu Ariza 已提交
195
};
196 197
template <typename OPSET, typename PARAM, typename PATH=PathProcsNull<CFF2CSInterpEnv, PARAM> >
struct CFF2CSOpSet : CSOpSet<BlendArg, OPSET, CFF2CSInterpEnv, PARAM, PATH>
M
Michiharu Ariza 已提交
198
{
199
  static inline void process_op (OpCode op, CFF2CSInterpEnv &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

231
  static inline void process_blend (CFF2CSInterpEnv &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 248 249 250
    {
      const hb_array_t<const BlendArg>	blends = env.argStack.get_subarray (start + n + (i * k));
      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
  }

256
  static inline void process_vsindex (CFF2CSInterpEnv &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:
263
  typedef CSOpSet<BlendArg, OPSET, CFF2CSInterpEnv, PARAM, PATH>  SUPER;
M
Michiharu Ariza 已提交
264 265 266 267 268 269 270 271
};

template <typename OPSET, typename PARAM>
struct CFF2CSInterpreter : CSInterpreter<CFF2CSInterpEnv, OPSET, PARAM> {};

} /* namespace CFF */

#endif /* HB_CFF2_INTERP_CS_HH */