Coverage Report

Created: 2026-08-14 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-cff2-interp-cs.hh
Line
Count
Source
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.clear ();
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
      region_count (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 && varStore->size;
81
0
    orig_ivs = acc.privateDicts[fd].ivs;
82
0
    set_ivs (orig_ivs);
83
0
  }
84
85
  ~cff2_cs_interp_env_t ()
86
0
  {
87
0
    release_scalars_vector (scalars);
88
0
  }
89
90
  hb_vector_t<float> *acquire_scalars_vector () const
91
0
  {
92
0
    hb_vector_t<float> *scalars = cached_scalars_vector->get_acquire ();
93
94
0
    if (!scalars || !cached_scalars_vector->cmpexch (scalars, nullptr))
95
0
    {
96
0
      scalars = (hb_vector_t<float> *) hb_calloc (1, sizeof (hb_vector_t<float>));
97
0
      if (unlikely (!scalars))
98
0
  return nullptr;
99
0
      scalars->init ();
100
0
    }
101
102
0
    return scalars;
103
0
  }
104
105
  void release_scalars_vector (hb_vector_t<float> *scalars) const
106
0
  {
107
0
    if (!scalars)
108
0
      return;
109
110
0
    scalars->clear ();
111
112
0
    if (!cached_scalars_vector->cmpexch (nullptr, scalars))
113
0
    {
114
0
      scalars->fini ();
115
0
      hb_free (scalars);
116
0
    }
117
0
    scalars = nullptr;
118
0
  }
119
120
  op_code_t fetch_op ()
121
0
  {
122
0
    if (this->str_ref.avail ())
123
0
      return SUPER::fetch_op ();
124
125
    /* make up return or endchar op */
126
0
    if (this->callStack.is_empty ())
127
0
      return OpCode_endchar;
128
0
    else
129
0
      return OpCode_return;
130
0
  }
131
132
  const ELEM& eval_arg (unsigned int i)
133
0
  {
134
0
    return SUPER::argStack[i];
135
0
  }
136
137
  const ELEM& pop_arg ()
138
0
  {
139
0
    return SUPER::argStack.pop ();
140
0
  }
141
142
  void process_blend ()
143
0
  {
144
0
    if (!seen_blend)
145
0
    {
146
0
      scalars = acquire_scalars_vector ();
147
0
      if (unlikely (!scalars))
148
0
  SUPER::set_error ();
149
0
      else
150
0
      {
151
0
  region_count = varStore->varStore.get_region_index_count (get_ivs ());
152
0
  if (do_blend)
153
0
  {
154
0
    if (unlikely (!scalars->resize_exact (region_count)))
155
0
      SUPER::set_error ();
156
0
    else
157
0
      varStore->varStore.get_region_scalars (get_ivs (), coords, num_coords,
158
0
               &(*scalars)[0], region_count);
159
0
  }
160
0
      }
161
0
      seen_blend = true;
162
0
    }
163
0
  }
164
165
  void process_vsindex ()
166
0
  {
167
0
    unsigned int  index = SUPER::argStack.pop_uint ();
168
0
    if (unlikely (seen_vsindex () || seen_blend))
169
0
    {
170
0
     SUPER::set_error ();
171
0
    }
172
0
    else
173
0
    {
174
0
      set_ivs (index);
175
0
    }
176
0
    seen_vsindex_ = true;
177
0
  }
178
179
0
  unsigned int get_region_count () const { return region_count; }
180
  void   set_region_count (unsigned int region_count_) { region_count = region_count_; }
181
0
  unsigned int get_ivs () const { return ivs; }
182
0
  void   set_ivs (unsigned int ivs_) { ivs = ivs_; }
183
  /* The FD's private-dict ivs, before any vsindex op in the charstring. */
184
  unsigned int get_orig_ivs () const { return orig_ivs; }
185
0
  bool   seen_vsindex () const { return seen_vsindex_; }
186
187
  double blend_deltas (hb_array_t<const ELEM> deltas) const
188
0
  {
189
0
    double v = 0;
190
0
    if (do_blend)
191
0
    {
192
0
      if (likely (scalars && scalars->length == deltas.length))
193
0
      {
194
0
        unsigned count = scalars->length;
195
0
  for (unsigned i = 0; i < count; i++)
196
0
    v += (double) scalars->arrayZ[i] * deltas.arrayZ[i].to_real ();
197
0
      }
198
0
    }
199
0
    return v;
200
0
  }
201
202
  bool have_coords () const { return num_coords; }
203
204
  protected:
205
  const int     *coords;
206
  unsigned int  num_coords;
207
  const  CFF2ItemVariationStore *varStore;
208
  unsigned int  region_count;
209
  unsigned int  ivs;
210
  unsigned int  orig_ivs;
211
  hb_vector_t<float>  *scalars = nullptr;
212
  hb_atomic_t<hb_vector_t<float> *> *cached_scalars_vector = nullptr;
213
  bool    do_blend;
214
  bool    seen_vsindex_ = false;
215
  bool    seen_blend = false;
216
217
  typedef cs_interp_env_t<ELEM, CFF2Subrs> SUPER;
218
};
219
template <typename OPSET, typename PARAM, typename ELEM, typename PATH=path_procs_null_t<cff2_cs_interp_env_t<ELEM>, PARAM>>
220
struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH>
221
{
222
  static void process_op (op_code_t op, cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
223
0
  {
224
0
    switch (op) {
225
0
      case OpCode_callsubr:
226
0
      case OpCode_callgsubr:
227
  /* a subroutine number shouldn't be a blended value */
228
#if 0
229
  if (unlikely (env.argStack.peek ().blending ()))
230
  {
231
    env.set_error ();
232
    break;
233
  }
234
#endif
235
0
  SUPER::process_op (op, env, param);
236
0
  break;
237
238
0
      case OpCode_blendcs:
239
0
  OPSET::process_blend (env, param);
240
0
  break;
241
242
0
      case OpCode_vsindexcs:
243
#if 0
244
  if (unlikely (env.argStack.peek ().blending ()))
245
  {
246
    env.set_error ();
247
    break;
248
  }
249
#endif
250
0
  OPSET::process_vsindex (env, param);
251
0
  break;
252
253
0
      default:
254
0
  SUPER::process_op (op, env, param);
255
0
    }
256
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&)
257
258
  template <typename T = ELEM,
259
      hb_enable_if (hb_is_same (T, blend_arg_t))>
260
  static void process_arg_blend (cff2_cs_interp_env_t<ELEM> &env,
261
         ELEM &arg,
262
         const hb_array_t<const ELEM> blends,
263
         unsigned n, unsigned i)
264
  {
265
    if (env.have_coords ())
266
      arg.set_int (round (arg.to_real () + env.blend_deltas (blends)));
267
    else if (unlikely (arg.blending ()))
268
    {
269
      /* A blend result used as an operand of a later blend: the value is
270
       * default + inner deltas + outer deltas, so accumulate the deltas. */
271
      hb_vector_t<number_t> inner = std::move (arg.deltas);
272
      arg.set_blends (n, i, blends);
273
      if (likely (inner.length == arg.deltas.length))
274
  for (unsigned j = 0; j < inner.length; j++)
275
    arg.deltas.arrayZ[j].set_real (arg.deltas.arrayZ[j].to_real () +
276
           inner.arrayZ[j].to_real ());
277
      else
278
  env.set_error ();
279
    }
280
    else
281
      arg.set_blends (n, i, blends);
282
  }
283
  template <typename T = ELEM,
284
      hb_enable_if (!hb_is_same (T, blend_arg_t))>
285
  static void process_arg_blend (cff2_cs_interp_env_t<ELEM> &env,
286
         ELEM &arg,
287
         const hb_array_t<const ELEM> blends,
288
         unsigned n, unsigned i)
289
0
  {
290
0
    arg.set_real (arg.to_real () + env.blend_deltas (blends));
291
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
292
293
  static void process_blend (cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
294
0
  {
295
0
    unsigned int n, k;
296
297
0
    env.process_blend ();
298
0
    k = env.get_region_count ();
299
0
    n = env.argStack.pop_uint ();
300
    /* copy the blend values into blend array of the default values */
301
0
    unsigned int count = env.argStack.get_count ();
302
0
    unsigned int total;
303
0
    if (unlikely (hb_unsigned_mul_overflows (k + 1, n, &total) || total > count))
304
0
    {
305
0
      env.set_error ();
306
0
      return;
307
0
    }
308
0
    unsigned int start = count - total;
309
0
    for (unsigned int i = 0; i < n; i++)
310
0
    {
311
0
      const hb_array_t<const ELEM> blends = env.argStack.sub_array (start + n + (i * k), k);
312
0
      process_arg_blend (env, env.argStack.arrayZ[start + i], blends, n, i);
313
0
    }
314
315
    /* pop off blend values leaving default values now adorned with blend values */
316
0
    env.argStack.pop (k * n);
317
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&)
318
319
  static void process_vsindex (cff2_cs_interp_env_t<ELEM> &env, PARAM& param)
320
0
  {
321
0
    env.process_vsindex ();
322
0
    env.clear_args ();
323
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&)
324
325
  private:
326
  typedef cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, PARAM, PATH>  SUPER;
327
};
328
329
template <typename OPSET, typename PARAM, typename ELEM>
330
using cff2_cs_interpreter_t = cs_interpreter_t<cff2_cs_interp_env_t<ELEM>, OPSET, PARAM>;
331
332
} /* namespace CFF */
333
334
#endif /* HB_CFF2_INTERP_CS_HH */