Coverage Report

Created: 2025-07-18 07:02

/src/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
57.4k
    : SUPER (str, acc.globalSubrs, acc.privateDicts[fd].localSubrs),
75
57.4k
      cached_scalars_vector (&acc.cached_scalars_vector)
76
57.4k
  {
77
57.4k
    coords = coords_;
78
57.4k
    num_coords = num_coords_;
79
57.4k
    varStore = acc.varStore;
80
57.4k
    do_blend = num_coords && varStore->size;
81
57.4k
    set_ivs (acc.privateDicts[fd].ivs);
82
57.4k
  }
83
84
  ~cff2_cs_interp_env_t ()
85
57.4k
  {
86
57.4k
    release_scalars_vector (scalars);
87
57.4k
  }
88
89
  hb_vector_t<float> *acquire_scalars_vector () const
90
8.24k
  {
91
8.24k
    hb_vector_t<float> *scalars = cached_scalars_vector->get_acquire ();
92
93
8.24k
    if (!scalars || !cached_scalars_vector->cmpexch (scalars, nullptr))
94
673
    {
95
673
      scalars = (hb_vector_t<float> *) hb_calloc (1, sizeof (hb_vector_t<float>));
96
673
      if (unlikely (!scalars))
97
243
  return nullptr;
98
430
      scalars->init ();
99
430
    }
100
101
7.99k
    return scalars;
102
8.24k
  }
103
104
  void release_scalars_vector (hb_vector_t<float> *scalars) const
105
57.4k
  {
106
57.4k
    if (!scalars)
107
49.4k
      return;
108
109
7.99k
    scalars->clear ();
110
111
7.99k
    if (!cached_scalars_vector->cmpexch (nullptr, scalars))
112
0
    {
113
0
      scalars->fini ();
114
0
      hb_free (scalars);
115
0
    }
116
7.99k
    scalars = nullptr;
117
7.99k
  }
118
119
  op_code_t fetch_op ()
120
7.96M
  {
121
7.96M
    if (this->str_ref.avail ())
122
7.75M
      return SUPER::fetch_op ();
123
124
    /* make up return or endchar op */
125
206k
    if (this->callStack.is_empty ())
126
22.0k
      return OpCode_endchar;
127
183k
    else
128
183k
      return OpCode_return;
129
206k
  }
130
131
  const ELEM& eval_arg (unsigned int i)
132
4.09M
  {
133
4.09M
    return SUPER::argStack[i];
134
4.09M
  }
135
136
  const ELEM& pop_arg ()
137
78.3k
  {
138
78.3k
    return SUPER::argStack.pop ();
139
78.3k
  }
140
141
  void process_blend ()
142
15.0k
  {
143
15.0k
    if (!seen_blend)
144
8.24k
    {
145
8.24k
      scalars = acquire_scalars_vector ();
146
8.24k
      if (unlikely (!scalars))
147
243
  SUPER::set_error ();
148
7.99k
      else
149
7.99k
      {
150
7.99k
  region_count = varStore->varStore.get_region_index_count (get_ivs ());
151
7.99k
  if (do_blend)
152
1.12k
  {
153
1.12k
    if (unlikely (!scalars->resize_exact (region_count)))
154
119
      SUPER::set_error ();
155
1.01k
    else
156
1.01k
      varStore->varStore.get_region_scalars (get_ivs (), coords, num_coords,
157
1.01k
               &(*scalars)[0], region_count);
158
1.12k
  }
159
7.99k
      }
160
8.24k
      seen_blend = true;
161
8.24k
    }
162
15.0k
  }
163
164
  void process_vsindex ()
165
8.24k
  {
166
8.24k
    unsigned int  index = SUPER::argStack.pop_uint ();
167
8.24k
    if (unlikely (seen_vsindex () || seen_blend))
168
1.09k
    {
169
1.09k
     SUPER::set_error ();
170
1.09k
    }
171
7.14k
    else
172
7.14k
    {
173
7.14k
      set_ivs (index);
174
7.14k
    }
175
8.24k
    seen_vsindex_ = true;
176
8.24k
  }
177
178
15.0k
  unsigned int get_region_count () const { return region_count; }
179
  void   set_region_count (unsigned int region_count_) { region_count = region_count_; }
180
9.00k
  unsigned int get_ivs () const { return ivs; }
181
64.5k
  void   set_ivs (unsigned int ivs_) { ivs = ivs_; }
182
8.24k
  bool   seen_vsindex () const { return seen_vsindex_; }
183
184
  double blend_deltas (hb_array_t<const ELEM> deltas) const
185
25.5k
  {
186
25.5k
    double v = 0;
187
25.5k
    if (do_blend)
188
7.91k
    {
189
7.91k
      if (likely (scalars && scalars->length == deltas.length))
190
7.67k
      {
191
7.67k
        unsigned count = scalars->length;
192
23.6k
  for (unsigned i = 0; i < count; i++)
193
16.0k
    v += (double) scalars->arrayZ[i] * deltas.arrayZ[i].to_real ();
194
7.67k
      }
195
7.91k
    }
196
25.5k
    return v;
197
25.5k
  }
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
7.96M
  {
220
7.96M
    switch (op) {
221
48.7k
      case OpCode_callsubr:
222
216k
      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
216k
  SUPER::process_op (op, env, param);
232
216k
  break;
233
234
15.0k
      case OpCode_blendcs:
235
15.0k
  OPSET::process_blend (env, param);
236
15.0k
  break;
237
238
8.24k
      case OpCode_vsindexcs:
239
#if 0
240
  if (unlikely (env.argStack.peek ().blending ()))
241
  {
242
    env.set_error ();
243
    break;
244
  }
245
#endif
246
8.24k
  OPSET::process_vsindex (env, param);
247
8.24k
  break;
248
249
7.72M
      default:
250
7.72M
  SUPER::process_op (op, env, param);
251
7.96M
    }
252
7.96M
  }
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&)
Line
Count
Source
219
3.84M
  {
220
3.84M
    switch (op) {
221
10.2k
      case OpCode_callsubr:
222
105k
      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
105k
  SUPER::process_op (op, env, param);
232
105k
  break;
233
234
6.00k
      case OpCode_blendcs:
235
6.00k
  OPSET::process_blend (env, param);
236
6.00k
  break;
237
238
1.08k
      case OpCode_vsindexcs:
239
#if 0
240
  if (unlikely (env.argStack.peek ().blending ()))
241
  {
242
    env.set_error ();
243
    break;
244
  }
245
#endif
246
1.08k
  OPSET::process_vsindex (env, param);
247
1.08k
  break;
248
249
3.72M
      default:
250
3.72M
  SUPER::process_op (op, env, param);
251
3.84M
    }
252
3.84M
  }
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&)
Line
Count
Source
219
4.12M
  {
220
4.12M
    switch (op) {
221
38.5k
      case OpCode_callsubr:
222
110k
      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
110k
  SUPER::process_op (op, env, param);
232
110k
  break;
233
234
9.07k
      case OpCode_blendcs:
235
9.07k
  OPSET::process_blend (env, param);
236
9.07k
  break;
237
238
7.16k
      case OpCode_vsindexcs:
239
#if 0
240
  if (unlikely (env.argStack.peek ().blending ()))
241
  {
242
    env.set_error ();
243
    break;
244
  }
245
#endif
246
7.16k
  OPSET::process_vsindex (env, param);
247
7.16k
  break;
248
249
3.99M
      default:
250
3.99M
  SUPER::process_op (op, env, param);
251
4.12M
    }
252
4.12M
  }
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
25.5k
  {
273
25.5k
    arg.set_real (arg.to_real () + env.blend_deltas (blends));
274
25.5k
  }
_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
Line
Count
Source
272
13.2k
  {
273
13.2k
    arg.set_real (arg.to_real () + env.blend_deltas (blends));
274
13.2k
  }
_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
Line
Count
Source
272
12.3k
  {
273
12.3k
    arg.set_real (arg.to_real () + env.blend_deltas (blends));
274
12.3k
  }
275
276
  static void process_blend (cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
277
15.0k
  {
278
15.0k
    unsigned int n, k;
279
280
15.0k
    env.process_blend ();
281
15.0k
    k = env.get_region_count ();
282
15.0k
    n = env.argStack.pop_uint ();
283
    /* copy the blend values into blend array of the default values */
284
15.0k
    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
15.0k
    if (unlikely (start > env.argStack.get_count ()))
287
1.79k
    {
288
1.79k
      env.set_error ();
289
1.79k
      return;
290
1.79k
    }
291
38.8k
    for (unsigned int i = 0; i < n; i++)
292
25.5k
    {
293
25.5k
      const hb_array_t<const ELEM> blends = env.argStack.sub_array (start + n + (i * k), k);
294
25.5k
      process_arg_blend (env, env.argStack[start + i], blends, n, i);
295
25.5k
    }
296
297
    /* pop off blend values leaving default values now adorned with blend values */
298
13.2k
    env.argStack.pop (k * n);
299
13.2k
  }
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&)
Line
Count
Source
277
6.00k
  {
278
6.00k
    unsigned int n, k;
279
280
6.00k
    env.process_blend ();
281
6.00k
    k = env.get_region_count ();
282
6.00k
    n = env.argStack.pop_uint ();
283
    /* copy the blend values into blend array of the default values */
284
6.00k
    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
6.00k
    if (unlikely (start > env.argStack.get_count ()))
287
510
    {
288
510
      env.set_error ();
289
510
      return;
290
510
    }
291
18.7k
    for (unsigned int i = 0; i < n; i++)
292
13.2k
    {
293
13.2k
      const hb_array_t<const ELEM> blends = env.argStack.sub_array (start + n + (i * k), k);
294
13.2k
      process_arg_blend (env, env.argStack[start + i], blends, n, i);
295
13.2k
    }
296
297
    /* pop off blend values leaving default values now adorned with blend values */
298
5.49k
    env.argStack.pop (k * n);
299
5.49k
  }
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&)
Line
Count
Source
277
9.07k
  {
278
9.07k
    unsigned int n, k;
279
280
9.07k
    env.process_blend ();
281
9.07k
    k = env.get_region_count ();
282
9.07k
    n = env.argStack.pop_uint ();
283
    /* copy the blend values into blend array of the default values */
284
9.07k
    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
9.07k
    if (unlikely (start > env.argStack.get_count ()))
287
1.28k
    {
288
1.28k
      env.set_error ();
289
1.28k
      return;
290
1.28k
    }
291
20.1k
    for (unsigned int i = 0; i < n; i++)
292
12.3k
    {
293
12.3k
      const hb_array_t<const ELEM> blends = env.argStack.sub_array (start + n + (i * k), k);
294
12.3k
      process_arg_blend (env, env.argStack[start + i], blends, n, i);
295
12.3k
    }
296
297
    /* pop off blend values leaving default values now adorned with blend values */
298
7.79k
    env.argStack.pop (k * n);
299
7.79k
  }
300
301
  static void process_vsindex (cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
302
8.24k
  {
303
8.24k
    env.process_vsindex ();
304
8.24k
    env.clear_args ();
305
8.24k
  }
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&)
Line
Count
Source
302
1.08k
  {
303
1.08k
    env.process_vsindex ();
304
1.08k
    env.clear_args ();
305
1.08k
  }
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&)
Line
Count
Source
302
7.16k
  {
303
7.16k
    env.process_vsindex ();
304
7.16k
    env.clear_args ();
305
7.16k
  }
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 */