Coverage Report

Created: 2025-07-07 10:01

/work/workdir/UnpackedTarball/harfbuzz/src/hb-cff2-interp-cs.hh
Line
Count
Source (jump to first uncovered line)
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
0
  void set_int (int v) { reset_blends (); number_t::set_int (v); }
39
0
  void set_fixed (int32_t v) { reset_blends (); number_t::set_fixed (v); }
40
0
  void set_real (double v) { reset_blends (); number_t::set_real (v); }
41
42
  void set_blends (unsigned int numValues_, unsigned int valueIndex_,
43
       hb_array_t<const blend_arg_t> blends_)
44
0
  {
45
0
    numValues = numValues_;
46
0
    valueIndex = valueIndex_;
47
0
    unsigned numBlends = blends_.length;
48
0
    if (unlikely (!deltas.resize_exact (numBlends)))
49
0
      return;
50
0
    for (unsigned int i = 0; i < numBlends; i++)
51
0
      deltas.arrayZ[i] = blends_.arrayZ[i];
52
0
  }
53
54
0
  bool blending () const { return deltas.length > 0; }
55
  void reset_blends ()
56
0
  {
57
0
    numValues = valueIndex = 0;
58
0
    deltas.shrink (0);
59
0
  }
60
61
  unsigned int numValues;
62
  unsigned int valueIndex;
63
  hb_vector_t<number_t> deltas;
64
};
65
66
typedef biased_subrs_t<CFF2Subrs>   cff2_biased_subrs_t;
67
68
template <typename ELEM>
69
struct cff2_cs_interp_env_t : cs_interp_env_t<ELEM, CFF2Subrs>
70
{
71
  template <typename ACC>
72
  cff2_cs_interp_env_t (const hb_ubytes_t &str, ACC &acc, unsigned int fd,
73
      const int *coords_=nullptr, unsigned int num_coords_=0)
74
0
    : SUPER (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs),
75
0
      cached_scalars_vector (&acc.cached_scalars_vector)
76
0
  {
77
0
    coords = coords_;
78
0
    num_coords = num_coords_;
79
0
    varStore = acc.varStore;
80
0
    do_blend = num_coords && coords && varStore->size;
81
0
    set_ivs (acc.privateDicts[fd].ivs);
82
0
  }
83
84
  ~cff2_cs_interp_env_t ()
85
0
  {
86
0
    release_scalars_vector (scalars);
87
0
  }
88
89
  hb_vector_t<float> *acquire_scalars_vector () const
90
0
  {
91
0
    hb_vector_t<float> *scalars = cached_scalars_vector->get_acquire ();
92
93
0
    if (!scalars || !cached_scalars_vector->cmpexch (scalars, nullptr))
94
0
    {
95
0
      scalars = (hb_vector_t<float> *) hb_calloc (1, sizeof (hb_vector_t<float>));
96
0
      if (unlikely (!scalars))
97
0
  return nullptr;
98
0
      scalars->init ();
99
0
    }
100
101
0
    return scalars;
102
0
  }
103
104
  void release_scalars_vector (hb_vector_t<float> *scalars) const
105
0
  {
106
0
    if (!scalars)
107
0
      return;
108
109
0
    scalars->clear ();
110
111
0
    if (!cached_scalars_vector->cmpexch (nullptr, scalars))
112
0
    {
113
0
      scalars->fini ();
114
0
      hb_free (scalars);
115
0
    }
116
0
    scalars = nullptr;
117
0
  }
118
119
  op_code_t fetch_op ()
120
0
  {
121
0
    if (this->str_ref.avail ())
122
0
      return SUPER::fetch_op ();
123
124
    /* make up return or endchar op */
125
0
    if (this->callStack.is_empty ())
126
0
      return OpCode_endchar;
127
0
    else
128
0
      return OpCode_return;
129
0
  }
130
131
  const ELEM& eval_arg (unsigned int i)
132
0
  {
133
0
    return SUPER::argStack[i];
134
0
  }
135
136
  const ELEM& pop_arg ()
137
0
  {
138
0
    return SUPER::argStack.pop ();
139
0
  }
140
141
  void process_blend ()
142
0
  {
143
0
    if (!seen_blend)
144
0
    {
145
0
      scalars = acquire_scalars_vector ();
146
0
      if (unlikely (!scalars))
147
0
  SUPER::set_error ();
148
0
      else
149
0
      {
150
0
  region_count = varStore->varStore.get_region_index_count (get_ivs ());
151
0
  if (do_blend)
152
0
  {
153
0
    if (unlikely (!scalars->resize_exact (region_count)))
154
0
      SUPER::set_error ();
155
0
    else
156
0
      varStore->varStore.get_region_scalars (get_ivs (), coords, num_coords,
157
0
               &(*scalars)[0], region_count);
158
0
  }
159
0
      }
160
0
      seen_blend = true;
161
0
    }
162
0
  }
163
164
  void process_vsindex ()
165
0
  {
166
0
    unsigned int  index = SUPER::argStack.pop_uint ();
167
0
    if (unlikely (seen_vsindex () || seen_blend))
168
0
    {
169
0
     SUPER::set_error ();
170
0
    }
171
0
    else
172
0
    {
173
0
      set_ivs (index);
174
0
    }
175
0
    seen_vsindex_ = true;
176
0
  }
177
178
0
  unsigned int get_region_count () const { return region_count; }
179
  void   set_region_count (unsigned int region_count_) { region_count = region_count_; }
180
0
  unsigned int get_ivs () const { return ivs; }
181
0
  void   set_ivs (unsigned int ivs_) { ivs = ivs_; }
182
0
  bool   seen_vsindex () const { return seen_vsindex_; }
183
184
  double blend_deltas (hb_array_t<const ELEM> deltas) const
185
0
  {
186
0
    double v = 0;
187
0
    if (do_blend)
188
0
    {
189
0
      if (likely (scalars && scalars->length == deltas.length))
190
0
      {
191
0
        unsigned count = scalars->length;
192
0
  for (unsigned i = 0; i < count; i++)
193
0
    v += (double) scalars->arrayZ[i] * deltas.arrayZ[i].to_real ();
194
0
      }
195
0
    }
196
0
    return v;
197
0
  }
198
199
  bool have_coords () const { return num_coords; }
200
201
  protected:
202
  const int     *coords;
203
  unsigned int  num_coords;
204
  const  CFF2ItemVariationStore *varStore;
205
  unsigned int  region_count;
206
  unsigned int  ivs;
207
  hb_vector_t<float>  *scalars = nullptr;
208
  hb_atomic_t<hb_vector_t<float> *> *cached_scalars_vector = nullptr;
209
  bool    do_blend;
210
  bool    seen_vsindex_ = false;
211
  bool    seen_blend = false;
212
213
  typedef cs_interp_env_t<ELEM, CFF2Subrs> SUPER;
214
};
215
template <typename OPSET, typename PARAM, typename ELEM, typename PATH=path_procs_null_t<cff2_cs_interp_env_t<ELEM>, PARAM>>
216
struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH>
217
{
218
  static void process_op (op_code_t op, cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
219
0
  {
220
0
    switch (op) {
221
0
      case OpCode_callsubr:
222
0
      case OpCode_callgsubr:
223
  /* a subroutine number shouldn't be a blended value */
224
#if 0
225
  if (unlikely (env.argStack.peek ().blending ()))
226
  {
227
    env.set_error ();
228
    break;
229
  }
230
#endif
231
0
  SUPER::process_op (op, env, param);
232
0
  break;
233
234
0
      case OpCode_blendcs:
235
0
  OPSET::process_blend (env, param);
236
0
  break;
237
238
0
      case OpCode_vsindexcs:
239
#if 0
240
  if (unlikely (env.argStack.peek ().blending ()))
241
  {
242
    env.set_error ();
243
    break;
244
  }
245
#endif
246
0
  OPSET::process_vsindex (env, param);
247
0
  break;
248
249
0
      default:
250
0
  SUPER::process_op (op, env, param);
251
0
    }
252
0
  }
Unexecuted instantiation: CFF::cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, CFF::number_t, cff2_path_procs_extents_t>::process_op(unsigned int, CFF::cff2_cs_interp_env_t<CFF::number_t>&, cff2_extents_param_t&)
Unexecuted instantiation: CFF::cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, CFF::number_t, cff2_path_procs_path_t>::process_op(unsigned int, CFF::cff2_cs_interp_env_t<CFF::number_t>&, cff2_path_param_t&)
253
254
  template <typename T = ELEM,
255
      hb_enable_if (hb_is_same (T, blend_arg_t))>
256
  static void process_arg_blend (cff2_cs_interp_env_t<ELEM> &env,
257
         ELEM &arg,
258
         const hb_array_t<const ELEM> blends,
259
         unsigned n, unsigned i)
260
  {
261
    if (env.have_coords ())
262
      arg.set_int (round (arg.to_real () + env.blend_deltas (blends)));
263
    else
264
      arg.set_blends (n, i, blends);
265
  }
266
  template <typename T = ELEM,
267
      hb_enable_if (!hb_is_same (T, blend_arg_t))>
268
  static void process_arg_blend (cff2_cs_interp_env_t<ELEM> &env,
269
         ELEM &arg,
270
         const hb_array_t<const ELEM> blends,
271
         unsigned n, unsigned i)
272
0
  {
273
0
    arg.set_real (arg.to_real () + env.blend_deltas (blends));
274
0
  }
Unexecuted instantiation: _ZN3CFF15cff2_cs_opset_tI23cff2_cs_opset_extents_t20cff2_extents_param_tNS_8number_tE25cff2_path_procs_extents_tE17process_arg_blendIS3_TnPN12hb_enable_ifIXntsr10hb_is_sameIT_NS_11blend_arg_tEEE5valueEvE4typeELPv0EEEvRNS_20cff2_cs_interp_env_tIS3_EERS3_10hb_array_tIKS3_Ejj
Unexecuted instantiation: _ZN3CFF15cff2_cs_opset_tI20cff2_cs_opset_path_t17cff2_path_param_tNS_8number_tE22cff2_path_procs_path_tE17process_arg_blendIS3_TnPN12hb_enable_ifIXntsr10hb_is_sameIT_NS_11blend_arg_tEEE5valueEvE4typeELPv0EEEvRNS_20cff2_cs_interp_env_tIS3_EERS3_10hb_array_tIKS3_Ejj
275
276
  static void process_blend (cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
277
0
  {
278
0
    unsigned int n, k;
279
280
0
    env.process_blend ();
281
0
    k = env.get_region_count ();
282
0
    n = env.argStack.pop_uint ();
283
    /* copy the blend values into blend array of the default values */
284
0
    unsigned int start = env.argStack.get_count () - ((k+1) * n);
285
    /* let an obvious error case fail, but note CFF2 spec doesn't forbid n==0 */
286
0
    if (unlikely (start > env.argStack.get_count ()))
287
0
    {
288
0
      env.set_error ();
289
0
      return;
290
0
    }
291
0
    for (unsigned int i = 0; i < n; i++)
292
0
    {
293
0
      const hb_array_t<const ELEM> blends = env.argStack.sub_array (start + n + (i * k), k);
294
0
      process_arg_blend (env, env.argStack[start + i], blends, n, i);
295
0
    }
296
297
    /* pop off blend values leaving default values now adorned with blend values */
298
0
    env.argStack.pop (k * n);
299
0
  }
Unexecuted instantiation: CFF::cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, CFF::number_t, cff2_path_procs_extents_t>::process_blend(CFF::cff2_cs_interp_env_t<CFF::number_t>&, cff2_extents_param_t&)
Unexecuted instantiation: CFF::cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, CFF::number_t, cff2_path_procs_path_t>::process_blend(CFF::cff2_cs_interp_env_t<CFF::number_t>&, cff2_path_param_t&)
300
301
  static void process_vsindex (cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
302
0
  {
303
0
    env.process_vsindex ();
304
0
    env.clear_args ();
305
0
  }
Unexecuted instantiation: CFF::cff2_cs_opset_t<cff2_cs_opset_extents_t, cff2_extents_param_t, CFF::number_t, cff2_path_procs_extents_t>::process_vsindex(CFF::cff2_cs_interp_env_t<CFF::number_t>&, cff2_extents_param_t&)
Unexecuted instantiation: CFF::cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, CFF::number_t, cff2_path_procs_path_t>::process_vsindex(CFF::cff2_cs_interp_env_t<CFF::number_t>&, cff2_path_param_t&)
306
307
  private:
308
  typedef cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH>  SUPER;
309
};
310
311
template <typename OPSET, typename PARAM, typename ELEM>
312
using cff2_cs_interpreter_t = cs_interpreter_t<cff2_cs_interp_env_t<ELEM>, OPSET, PARAM>;
313
314
} /* namespace CFF */
315
316
#endif /* HB_CFF2_INTERP_CS_HH */