Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-ot-var-common.hh
Line
Count
Source
1
/*
2
 * Copyright © 2021  Google, 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
 */
25
26
#ifndef HB_OT_VAR_COMMON_HH
27
#define HB_OT_VAR_COMMON_HH
28
29
#include "hb-ot-layout-common.hh"
30
#include "hb-alloc-pool.hh"
31
#include "hb-priority-queue.hh"
32
#include "hb-subset-instancer-iup.hh"
33
34
35
namespace OT {
36
37
using rebase_tent_result_scratch_t = hb_pair_t<rebase_tent_result_t, rebase_tent_result_t>;
38
39
/* https://docs.microsoft.com/en-us/typography/opentype/spec/otvarcommonformats#tuplevariationheader */
40
struct TupleVariationHeader
41
{
42
  friend struct tuple_delta_t;
43
  unsigned get_size (unsigned axis_count_times_2) const
44
0
  {
45
    // This function is super hot in mega-var-fonts with hundreds of masters.
46
0
    unsigned ti = tupleIndex;
47
0
    if (unlikely ((ti & (TupleIndex::EmbeddedPeakTuple | TupleIndex::IntermediateRegion))))
48
0
    {
49
0
      unsigned count = ((ti & TupleIndex::EmbeddedPeakTuple) != 0) + ((ti & TupleIndex::IntermediateRegion) != 0) * 2;
50
0
      return min_size + count * axis_count_times_2;
51
0
    }
52
0
    return min_size;
53
0
  }
54
55
0
  unsigned get_data_size () const { return varDataSize; }
56
57
  const TupleVariationHeader &get_next (unsigned axis_count_times_2) const
58
0
  { return StructAtOffset<TupleVariationHeader> (this, get_size (axis_count_times_2)); }
59
60
  bool unpack_axis_tuples (unsigned axis_count,
61
                           const hb_array_t<const F2DOT14> shared_tuples,
62
                           const hb_map_t *axes_old_index_tag_map,
63
                           hb_hashmap_t<hb_tag_t, Triple>& axis_tuples /* OUT */) const
64
0
  {
65
0
    const F2DOT14 *peak_tuple = nullptr;
66
0
    if (has_peak ())
67
0
      peak_tuple = get_peak_tuple (axis_count);
68
0
    else
69
0
    {
70
0
      unsigned int index = get_index ();
71
0
      if (unlikely ((index + 1) * axis_count > shared_tuples.length))
72
0
        return false;
73
0
      peak_tuple = shared_tuples.sub_array (axis_count * index, axis_count).arrayZ;
74
0
    }
75
0
76
0
    const F2DOT14 *start_tuple = nullptr;
77
0
    const F2DOT14 *end_tuple = nullptr;
78
0
    bool has_interm = has_intermediate ();
79
0
80
0
    if (has_interm)
81
0
    {
82
0
      start_tuple = get_start_tuple (axis_count);
83
0
      end_tuple = get_end_tuple (axis_count);
84
0
    }
85
0
86
0
    for (unsigned i = 0; i < axis_count; i++)
87
0
    {
88
0
      float peak = peak_tuple[i].to_float ();
89
0
      if (peak == 0.f) continue;
90
0
91
0
      hb_tag_t *axis_tag;
92
0
      if (!axes_old_index_tag_map->has (i, &axis_tag))
93
0
        return false;
94
0
95
0
      float start, end;
96
0
      if (has_interm)
97
0
      {
98
0
        start = start_tuple[i].to_float ();
99
0
        end = end_tuple[i].to_float ();
100
0
      }
101
0
      else
102
0
      {
103
0
        start = hb_min (peak, 0.f);
104
0
        end = hb_max (peak, 0.f);
105
0
      }
106
0
      axis_tuples.set (*axis_tag, Triple ((double) start, (double) peak, (double) end));
107
0
    }
108
0
109
0
    return true;
110
0
  }
111
112
  HB_ALWAYS_INLINE
113
  double calculate_scalar (hb_array_t<const int> coords, unsigned int coord_count,
114
         const hb_array_t<const F2DOT14> shared_tuples,
115
         hb_scalar_cache_t *shared_tuple_scalar_cache = nullptr) const
116
0
  {
117
0
    unsigned tuple_index = tupleIndex;
118
119
0
    const F2DOT14 *peak_tuple;
120
121
0
    bool has_interm = tuple_index & TupleIndex::IntermediateRegion; // Inlined for performance
122
123
0
    if (unlikely (tuple_index & TupleIndex::EmbeddedPeakTuple)) // Inlined for performance
124
0
    {
125
0
      peak_tuple = get_peak_tuple (coord_count);
126
0
      shared_tuple_scalar_cache = nullptr;
127
0
    }
128
0
    else
129
0
    {
130
0
      unsigned int index = tuple_index & TupleIndex::TupleIndexMask; // Inlined for performance
131
132
0
      float scalar;
133
0
      if (shared_tuple_scalar_cache &&
134
0
    shared_tuple_scalar_cache->get (index, &scalar))
135
0
      {
136
0
        if (has_interm && (scalar != 0 && scalar != 1.f))
137
0
    shared_tuple_scalar_cache = nullptr;
138
0
  else
139
0
    return (double) scalar;
140
0
      }
141
142
0
      if (unlikely ((index + 1) * coord_count > shared_tuples.length))
143
0
        return 0.0;
144
0
      peak_tuple = shared_tuples.arrayZ + (coord_count * index);
145
146
0
    }
147
148
0
    const F2DOT14 *start_tuple = nullptr;
149
0
    const F2DOT14 *end_tuple = nullptr;
150
151
0
    if (has_interm)
152
0
    {
153
0
      start_tuple = get_start_tuple (coord_count);
154
0
      end_tuple = get_end_tuple (coord_count);
155
0
    }
156
157
0
    double scalar = 1.0;
158
0
#ifndef HB_OPTIMIZE_SIZE
159
0
#if HB_FAST_NUM_ACCESS
160
0
    bool skip = coord_count >= 16;
161
0
#endif
162
0
#endif
163
0
    for (unsigned int i = 0; i < coord_count; i++)
164
0
    {
165
0
#ifndef HB_OPTIMIZE_SIZE
166
0
#if HB_FAST_NUM_ACCESS
167
0
      if (skip)
168
0
      {
169
0
  while (i + 4 <= coord_count && * (HBUINT64LE *) &peak_tuple[i] == 0)
170
0
    i += 4;
171
0
  while (i < coord_count && peak_tuple[i].to_int () == 0)
172
0
    i += 1;
173
0
  if (i >= coord_count)
174
0
    break;
175
0
      }
176
0
#endif
177
0
#endif
178
179
0
      int peak = peak_tuple[i].to_int ();
180
0
      if (!peak) continue;
181
182
0
      int v = coords[i];
183
0
      if (!v) { scalar = 0.0; break; }
184
0
      if (v == peak) continue;
185
186
0
      if (has_interm)
187
0
      {
188
0
  shared_tuple_scalar_cache = nullptr;
189
0
        int start = start_tuple[i].to_int ();
190
0
        int end = end_tuple[i].to_int ();
191
0
        if (unlikely (start > peak || peak > end ||
192
0
                      (start < 0 && end > 0 && peak))) continue;
193
0
        if (v < start || v > end) { scalar = 0.0; break; }
194
0
        if (v < peak)
195
0
        { if (peak != start) scalar *= (double) (v - start) / (peak - start); }
196
0
        else
197
0
        { if (peak != end) scalar *= (double) (end - v) / (end - peak); }
198
0
      }
199
0
      else if (v < hb_min (0, peak) || v > hb_max (0, peak)) { scalar = 0.0; break; }
200
0
      else
201
0
        scalar *= (double) v / peak;
202
0
    }
203
0
    if (shared_tuple_scalar_cache)
204
0
      shared_tuple_scalar_cache->set (get_index (), scalar);
205
0
    return scalar;
206
0
  }
207
208
0
  bool           has_peak () const { return tupleIndex & TupleIndex::EmbeddedPeakTuple; }
209
0
  bool   has_intermediate () const { return tupleIndex & TupleIndex::IntermediateRegion; }
210
0
  bool has_private_points () const { return tupleIndex & TupleIndex::PrivatePointNumbers; }
211
0
  unsigned      get_index () const { return tupleIndex & TupleIndex::TupleIndexMask; }
212
213
  protected:
214
  struct TupleIndex : HBUINT16
215
  {
216
    enum Flags {
217
      EmbeddedPeakTuple   = 0x8000u,
218
      IntermediateRegion  = 0x4000u,
219
      PrivatePointNumbers = 0x2000u,
220
      TupleIndexMask      = 0x0FFFu
221
    };
222
223
0
    TupleIndex& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
224
    DEFINE_SIZE_STATIC (2);
225
  };
226
227
  hb_array_t<const F2DOT14> get_all_tuples (unsigned axis_count) const
228
0
  { return StructAfter<UnsizedArrayOf<F2DOT14>> (tupleIndex).as_array ((has_peak () + has_intermediate () * 2) * axis_count); }
229
  const F2DOT14* get_all_tuples_base (unsigned axis_count) const
230
0
  { return StructAfter<UnsizedArrayOf<F2DOT14>> (tupleIndex).arrayZ; }
231
  const F2DOT14* get_peak_tuple (unsigned axis_count) const
232
0
  { return get_all_tuples_base (axis_count); }
233
  const F2DOT14* get_start_tuple (unsigned axis_count) const
234
0
  { return get_all_tuples_base (axis_count) + has_peak () * axis_count; }
235
  const F2DOT14* get_end_tuple (unsigned axis_count) const
236
0
  { return get_all_tuples_base (axis_count) + has_peak () * axis_count + axis_count; }
237
238
  HBUINT16      varDataSize;    /* The size in bytes of the serialized
239
                                 * data for this tuple variation table. */
240
  TupleIndex    tupleIndex;     /* A packed field. The high 4 bits are flags (see below).
241
                                   The low 12 bits are an index into a shared tuple
242
                                   records array. */
243
  /* UnsizedArrayOf<F2DOT14> peakTuple - optional */
244
                                /* Peak tuple record for this tuple variation table — optional,
245
                                 * determined by flags in the tupleIndex value.
246
                                 *
247
                                 * Note that this must always be included in the 'cvar' table. */
248
  /* UnsizedArrayOf<F2DOT14> intermediateStartTuple - optional */
249
                                /* Intermediate start tuple record for this tuple variation table — optional,
250
                                   determined by flags in the tupleIndex value. */
251
  /* UnsizedArrayOf<F2DOT14> intermediateEndTuple - optional */
252
                                /* Intermediate end tuple record for this tuple variation table — optional,
253
                                 * determined by flags in the tupleIndex value. */
254
  public:
255
  DEFINE_SIZE_MIN (4);
256
};
257
258
struct optimize_scratch_t
259
{
260
  iup_scratch_t iup;
261
  hb_vector_t<bool> opt_indices;
262
  hb_vector_t<int> rounded_x_deltas;
263
  hb_vector_t<int> rounded_y_deltas;
264
  hb_vector_t<float> opt_deltas_x;
265
  hb_vector_t<float> opt_deltas_y;
266
  hb_vector_t<unsigned char> opt_point_data;
267
  hb_vector_t<unsigned char> opt_deltas_data;
268
  hb_vector_t<unsigned char> point_data;
269
  hb_vector_t<unsigned char> deltas_data;
270
  hb_vector_t<int> rounded_deltas;
271
};
272
273
struct tuple_delta_t
274
{
275
  static constexpr bool realloc_move = true;  // Watch out when adding new members!
276
277
  public:
278
  hb_hashmap_t<hb_tag_t, Triple> axis_tuples;
279
280
  /* indices_length = point_count, indice[i] = 1 means point i is referenced */
281
  hb_vector_t<bool> indices;
282
283
  hb_vector_t<float> deltas_x;
284
  /* empty for cvar tuples */
285
  hb_vector_t<float> deltas_y;
286
287
  /* compiled data: header and deltas
288
   * compiled point data is saved in a hashmap within tuple_variations_t cause
289
   * some point sets might be reused by different tuple variations */
290
  hb_vector_t<unsigned char> compiled_tuple_header;
291
  hb_vector_t<unsigned char> compiled_deltas;
292
293
  hb_vector_t<F2DOT14> compiled_peak_coords;
294
  hb_vector_t<F2DOT14> compiled_interm_coords;
295
296
0
  tuple_delta_t (hb_alloc_pool_t *pool = nullptr) {}
297
  tuple_delta_t (const tuple_delta_t& o) = default;
298
  tuple_delta_t& operator = (const tuple_delta_t& o) = default;
299
300
  friend void swap (tuple_delta_t& a, tuple_delta_t& b) noexcept
301
0
  {
302
0
    hb_swap (a.axis_tuples, b.axis_tuples);
303
0
    hb_swap (a.indices, b.indices);
304
0
    hb_swap (a.deltas_x, b.deltas_x);
305
0
    hb_swap (a.deltas_y, b.deltas_y);
306
0
    hb_swap (a.compiled_tuple_header, b.compiled_tuple_header);
307
0
    hb_swap (a.compiled_deltas, b.compiled_deltas);
308
0
    hb_swap (a.compiled_peak_coords, b.compiled_peak_coords);
309
0
  }
310
311
  tuple_delta_t (tuple_delta_t&& o)  noexcept : tuple_delta_t ()
312
0
  { hb_swap (*this, o); }
313
314
  tuple_delta_t& operator = (tuple_delta_t&& o) noexcept
315
0
  {
316
0
    hb_swap (*this, o);
317
0
    return *this;
318
0
  }
319
320
  void copy_from (const tuple_delta_t& o, hb_alloc_pool_t *pool = nullptr)
321
0
  {
322
0
    axis_tuples = o.axis_tuples;
323
0
    indices.allocate_from_pool (pool, o.indices);
324
0
    deltas_x.allocate_from_pool (pool, o.deltas_x);
325
0
    deltas_y.allocate_from_pool (pool, o.deltas_y);
326
0
    compiled_tuple_header.allocate_from_pool (pool, o.compiled_tuple_header);
327
0
    compiled_deltas.allocate_from_pool (pool, o.compiled_deltas);
328
0
    compiled_peak_coords.allocate_from_pool (pool, o.compiled_peak_coords);
329
0
    compiled_interm_coords.allocate_from_pool (pool, o.compiled_interm_coords);
330
0
  }
331
332
  void remove_axis (hb_tag_t axis_tag)
333
0
  { axis_tuples.del (axis_tag); }
334
335
  bool set_tent (hb_tag_t axis_tag, Triple tent)
336
0
  { return axis_tuples.set (axis_tag, tent); }
337
338
  tuple_delta_t& operator += (const tuple_delta_t& o)
339
0
  {
340
0
    unsigned num = indices.length;
341
0
    for (unsigned i = 0; i < num; i++)
342
0
    {
343
0
      if (indices.arrayZ[i])
344
0
      {
345
0
        if (o.indices.arrayZ[i])
346
0
        {
347
0
          deltas_x[i] += o.deltas_x[i];
348
0
          if (deltas_y && o.deltas_y)
349
0
            deltas_y[i] += o.deltas_y[i];
350
0
        }
351
0
      }
352
0
      else
353
0
      {
354
0
        if (!o.indices.arrayZ[i]) continue;
355
0
        indices.arrayZ[i] = true;
356
0
        deltas_x[i] = o.deltas_x[i];
357
0
        if (deltas_y && o.deltas_y)
358
0
          deltas_y[i] = o.deltas_y[i];
359
0
      }
360
0
    }
361
0
    return *this;
362
0
  }
363
364
  tuple_delta_t& operator *= (float scalar)
365
0
  {
366
0
    if (scalar == 1.0f)
367
0
      return *this;
368
0
369
0
    unsigned num = indices.length;
370
0
    if (deltas_y)
371
0
      for (unsigned i = 0; i < num; i++)
372
0
      {
373
0
  if (!indices.arrayZ[i]) continue;
374
0
  deltas_x[i] *= scalar;
375
0
  deltas_y[i] *= scalar;
376
0
      }
377
0
    else
378
0
      for (unsigned i = 0; i < num; i++)
379
0
      {
380
0
  if (!indices.arrayZ[i]) continue;
381
0
  deltas_x[i] *= scalar;
382
0
      }
383
0
    return *this;
384
0
  }
385
386
  void change_tuple_var_axis_limit (hb_tag_t axis_tag, Triple axis_limit,
387
            TripleDistances axis_triple_distances,
388
            hb_vector_t<tuple_delta_t>& out,
389
            rebase_tent_result_scratch_t &scratch,
390
            hb_alloc_pool_t *pool = nullptr)
391
0
  {
392
0
    // May move *this out.
393
0
394
0
    out.reset ();
395
0
    Triple *tent;
396
0
    if (!axis_tuples.has (axis_tag, &tent))
397
0
    {
398
0
      out.push (std::move (*this));
399
0
      return;
400
0
    }
401
0
402
0
    if ((tent->minimum < 0.0 && tent->maximum > 0.0) ||
403
0
        !(tent->minimum <= tent->middle && tent->middle <= tent->maximum))
404
0
      return;
405
0
406
0
    if (tent->middle == 0.0)
407
0
    {
408
0
      out.push (std::move (*this));
409
0
      return;
410
0
    }
411
0
412
0
    rebase_tent_result_t &solutions = scratch.first;
413
0
    rebase_tent (*tent, axis_limit, axis_triple_distances, solutions, scratch.second);
414
0
    for (unsigned i = 0; i < solutions.length; i++)
415
0
    {
416
0
      auto &t = solutions.arrayZ[i];
417
0
418
0
      tuple_delta_t new_var;
419
0
      if (i < solutions.length - 1)
420
0
  new_var.copy_from (*this, pool);
421
0
      else
422
0
  new_var = std::move (*this);
423
0
424
0
      if (t.second == Triple ())
425
0
        new_var.remove_axis (axis_tag);
426
0
      else
427
0
        new_var.set_tent (axis_tag, t.second);
428
0
429
0
      new_var *= t.first;
430
0
      out.push (std::move (new_var));
431
0
    }
432
0
  }
433
434
  bool compile_coords (const hb_map_t& axes_index_map,
435
           const hb_map_t& axes_old_index_tag_map,
436
           hb_alloc_pool_t *pool= nullptr)
437
0
  {
438
0
    unsigned cur_axis_count = axes_index_map.get_population ();
439
0
    if (pool)
440
0
    {
441
0
      if (unlikely (!compiled_peak_coords.allocate_from_pool (pool, cur_axis_count)))
442
0
  return false;
443
0
    }
444
0
    else if (unlikely (!compiled_peak_coords.resize (cur_axis_count)))
445
0
      return false;
446
0
447
0
    hb_array_t<F2DOT14> start_coords, end_coords;
448
0
449
0
    unsigned orig_axis_count = axes_old_index_tag_map.get_population ();
450
0
    unsigned j = 0;
451
0
    for (unsigned i = 0; i < orig_axis_count; i++)
452
0
    {
453
0
      if (!axes_index_map.has (i))
454
0
        continue;
455
0
456
0
      hb_tag_t axis_tag = axes_old_index_tag_map.get (i);
457
0
      Triple *coords = nullptr;
458
0
      if (axis_tuples.has (axis_tag, &coords))
459
0
      {
460
0
  float min_val = coords->minimum;
461
0
  float val = coords->middle;
462
0
  float max_val = coords->maximum;
463
0
464
0
  compiled_peak_coords.arrayZ[j].set_float (val);
465
0
466
0
  if (min_val != hb_min (val, 0.f) || max_val != hb_max (val, 0.f))
467
0
  {
468
0
    if (!compiled_interm_coords)
469
0
    {
470
0
      if (pool)
471
0
      {
472
0
        if (unlikely (!compiled_interm_coords.allocate_from_pool (pool, 2 * cur_axis_count)))
473
0
    return false;
474
0
      }
475
0
      else if (unlikely (!compiled_interm_coords.resize (2 * cur_axis_count)))
476
0
        return false;
477
0
      start_coords = compiled_interm_coords.as_array ().sub_array (0, cur_axis_count);
478
0
      end_coords = compiled_interm_coords.as_array ().sub_array (cur_axis_count);
479
0
480
0
      for (unsigned k = 0; k < j; k++)
481
0
      {
482
0
        signed peak = compiled_peak_coords.arrayZ[k].to_int ();
483
0
        if (!peak) continue;
484
0
        start_coords.arrayZ[k].set_int (hb_min (peak, 0));
485
0
        end_coords.arrayZ[k].set_int (hb_max (peak, 0));
486
0
      }
487
0
    }
488
0
489
0
  }
490
0
491
0
  if (compiled_interm_coords)
492
0
  {
493
0
    start_coords.arrayZ[j].set_float (min_val);
494
0
    end_coords.arrayZ[j].set_float (max_val);
495
0
  }
496
0
      }
497
0
498
0
      j++;
499
0
    }
500
0
501
0
    return !compiled_peak_coords.in_error () && !compiled_interm_coords.in_error ();
502
0
  }
503
504
  /* deltas should be compiled already before we compile tuple
505
   * variation header cause we need to fill in the size of the
506
   * serialized data for this tuple variation */
507
  bool compile_tuple_var_header (const hb_map_t& axes_index_map,
508
                                 unsigned points_data_length,
509
                                 const hb_map_t& axes_old_index_tag_map,
510
                                 const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* shared_tuples_idx_map,
511
         hb_alloc_pool_t *pool = nullptr)
512
0
  {
513
0
    /* compiled_deltas could be empty after iup delta optimization, we can skip
514
0
     * compiling this tuple and return true */
515
0
    if (!compiled_deltas) return true;
516
0
517
0
    unsigned cur_axis_count = axes_index_map.get_population ();
518
0
    /* allocate enough memory: 1 peak + 2 intermediate coords + fixed header size */
519
0
    unsigned alloc_len = 3 * cur_axis_count * (F2DOT14::static_size) + 4;
520
0
    if (unlikely (!compiled_tuple_header.allocate_from_pool (pool, alloc_len, false))) return false;
521
0
522
0
    unsigned flag = 0;
523
0
    /* skip the first 4 header bytes: variationDataSize+tupleIndex */
524
0
    F2DOT14* p = reinterpret_cast<F2DOT14 *> (compiled_tuple_header.begin () + 4);
525
0
    F2DOT14* end = reinterpret_cast<F2DOT14 *> (compiled_tuple_header.end ());
526
0
    hb_array_t<F2DOT14> coords (p, end - p);
527
0
528
0
    if (!shared_tuples_idx_map)
529
0
      compile_coords (axes_index_map, axes_old_index_tag_map); // non-gvar tuples do not have compiled coords yet
530
0
531
0
    /* encode peak coords */
532
0
    unsigned peak_count = 0;
533
0
    unsigned *shared_tuple_idx;
534
0
    if (shared_tuples_idx_map &&
535
0
        shared_tuples_idx_map->has (&compiled_peak_coords, &shared_tuple_idx))
536
0
    {
537
0
      flag = *shared_tuple_idx;
538
0
    }
539
0
    else
540
0
    {
541
0
      peak_count = encode_peak_coords(coords, flag);
542
0
      if (!peak_count) return false;
543
0
    }
544
0
545
0
    /* encode interim coords, it's optional so returned num could be 0 */
546
0
    unsigned interim_count = encode_interm_coords (coords.sub_array (peak_count), flag);
547
0
548
0
    /* pointdata length = 0 implies "use shared points" */
549
0
    if (points_data_length)
550
0
      flag |= TupleVariationHeader::TupleIndex::PrivatePointNumbers;
551
0
552
0
    unsigned serialized_data_size = points_data_length + compiled_deltas.length;
553
0
    TupleVariationHeader *o = reinterpret_cast<TupleVariationHeader *> (compiled_tuple_header.begin ());
554
0
    o->varDataSize = serialized_data_size;
555
0
    o->tupleIndex = flag;
556
0
557
0
    unsigned total_header_len = 4 + (peak_count + interim_count) * (F2DOT14::static_size);
558
0
    compiled_tuple_header.shrink_back_to_pool (pool, total_header_len);
559
0
    return true;
560
0
  }
561
562
  unsigned encode_peak_coords (hb_array_t<F2DOT14> peak_coords,
563
                               unsigned& flag) const
564
0
  {
565
0
    hb_memcpy (&peak_coords[0], &compiled_peak_coords[0], compiled_peak_coords.length * sizeof (compiled_peak_coords[0]));
566
0
    flag |= TupleVariationHeader::TupleIndex::EmbeddedPeakTuple;
567
0
    return compiled_peak_coords.length;
568
0
  }
569
570
  /* if no need to encode intermediate coords, then just return p */
571
  unsigned encode_interm_coords (hb_array_t<F2DOT14> coords,
572
                                 unsigned& flag) const
573
0
  {
574
0
    if (compiled_interm_coords)
575
0
    {
576
0
      hb_memcpy (&coords[0], &compiled_interm_coords[0], compiled_interm_coords.length * sizeof (compiled_interm_coords[0]));
577
0
      flag |= TupleVariationHeader::TupleIndex::IntermediateRegion;
578
0
    }
579
0
    return compiled_interm_coords.length;
580
0
  }
581
582
  bool compile_deltas (hb_vector_t<int> &rounded_deltas_scratch,
583
           hb_alloc_pool_t *pool = nullptr)
584
0
  { return compile_deltas (indices, deltas_x, deltas_y, compiled_deltas, rounded_deltas_scratch, pool); }
585
586
  static bool compile_deltas (hb_array_t<const bool> point_indices,
587
            hb_array_t<const float> x_deltas,
588
            hb_array_t<const float> y_deltas,
589
            hb_vector_t<unsigned char> &compiled_deltas, /* OUT */
590
            hb_vector_t<int> &rounded_deltas, /* scratch */
591
            hb_alloc_pool_t *pool = nullptr)
592
0
  {
593
0
    if (unlikely (!rounded_deltas.resize_dirty  (point_indices.length)))
594
0
      return false;
595
0
596
0
    unsigned j = 0;
597
0
    for (unsigned i = 0; i < point_indices.length; i++)
598
0
    {
599
0
      if (!point_indices[i]) continue;
600
0
      rounded_deltas.arrayZ[j++] = (int) roundf (x_deltas.arrayZ[i]);
601
0
    }
602
0
    rounded_deltas.resize (j);
603
0
604
0
    if (!rounded_deltas) return true;
605
0
    /* Allocate enough memory: this is the correct bound:
606
0
     * Worst case scenario is that each delta has to be encoded in 4 bytes, and there
607
0
     * are runs of 64 items each. Any delta encoded in less than 4 bytes (2, 1, or 0)
608
0
     * is still smaller than the 4-byte encoding even with their control byte.
609
0
     * The initial 2 is to handle length==0, for both x and y deltas. */
610
0
    unsigned alloc_len = 2 + 4 * rounded_deltas.length + (rounded_deltas.length + 63) / 64;
611
0
    if (y_deltas)
612
0
      alloc_len *= 2;
613
0
614
0
    if (unlikely (!compiled_deltas.allocate_from_pool (pool, alloc_len, false))) return false;
615
0
616
0
    unsigned encoded_len = compile_deltas (compiled_deltas, rounded_deltas);
617
0
618
0
    if (y_deltas)
619
0
    {
620
0
      /* reuse the rounded_deltas vector, check that y_deltas have the same num of deltas as x_deltas */
621
0
      unsigned j = 0;
622
0
      for (unsigned idx = 0; idx < point_indices.length; idx++)
623
0
      {
624
0
        if (!point_indices[idx]) continue;
625
0
        int rounded_delta = (int) roundf (y_deltas.arrayZ[idx]);
626
0
627
0
        if (j >= rounded_deltas.length) return false;
628
0
629
0
        rounded_deltas[j++] = rounded_delta;
630
0
      }
631
0
632
0
      if (j != rounded_deltas.length) return false;
633
0
      encoded_len += compile_deltas (compiled_deltas.as_array ().sub_array (encoded_len), rounded_deltas);
634
0
    }
635
0
    compiled_deltas.shrink_back_to_pool (pool, encoded_len);
636
0
    return true;
637
0
  }
638
639
  static unsigned compile_deltas (hb_array_t<unsigned char> encoded_bytes,
640
          hb_array_t<const int> deltas)
641
0
  {
642
0
    return TupleValues::compile_unsafe (deltas, encoded_bytes);
643
0
  }
644
645
  bool calc_inferred_deltas (const contour_point_vector_t& orig_points,
646
           hb_vector_t<unsigned> &scratch)
647
0
  {
648
0
    unsigned point_count = orig_points.length;
649
0
    if (point_count != indices.length)
650
0
      return false;
651
0
652
0
    unsigned ref_count = 0;
653
0
654
0
    hb_vector_t<unsigned> &end_points = scratch.reset ();
655
0
656
0
    for (unsigned i = 0; i < point_count; i++)
657
0
    {
658
0
      ref_count += indices.arrayZ[i];
659
0
      if (orig_points.arrayZ[i].is_end_point)
660
0
        end_points.push (i);
661
0
    }
662
0
    /* all points are referenced, nothing to do */
663
0
    if (ref_count == point_count)
664
0
      return true;
665
0
    if (unlikely (end_points.in_error ())) return false;
666
0
667
0
    hb_bit_set_t inferred_idxes;
668
0
    unsigned start_point = 0;
669
0
    for (unsigned end_point : end_points)
670
0
    {
671
0
      /* Check the number of unreferenced points in a contour. If no unref points or no ref points, nothing to do. */
672
0
      unsigned unref_count = 0;
673
0
      for (unsigned i = start_point; i < end_point + 1; i++)
674
0
        unref_count += indices.arrayZ[i];
675
0
      unref_count = (end_point - start_point + 1) - unref_count;
676
0
677
0
      unsigned j = start_point;
678
0
      if (unref_count == 0 || unref_count > end_point - start_point)
679
0
        goto no_more_gaps;
680
0
      for (;;)
681
0
      {
682
0
        /* Locate the next gap of unreferenced points between two referenced points prev and next.
683
0
         * Note that a gap may wrap around at left (start_point) and/or at right (end_point).
684
0
         */
685
0
        unsigned int prev, next, i;
686
0
        for (;;)
687
0
        {
688
0
          i = j;
689
0
          j = next_index (i, start_point, end_point);
690
0
          if (indices.arrayZ[i] && !indices.arrayZ[j]) break;
691
0
        }
692
0
        prev = j = i;
693
0
        for (;;)
694
0
        {
695
0
          i = j;
696
0
          j = next_index (i, start_point, end_point);
697
0
          if (!indices.arrayZ[i] && indices.arrayZ[j]) break;
698
0
        }
699
0
        next = j;
700
0
       /* Infer deltas for all unref points in the gap between prev and next */
701
0
        i = prev;
702
0
        for (;;)
703
0
        {
704
0
          i = next_index (i, start_point, end_point);
705
0
          if (i == next) break;
706
0
          deltas_x.arrayZ[i] = infer_delta ((double) orig_points.arrayZ[i].x,
707
0
                                            (double) orig_points.arrayZ[prev].x,
708
0
                                            (double) orig_points.arrayZ[next].x,
709
0
                                            (double) deltas_x.arrayZ[prev], (double) deltas_x.arrayZ[next]);
710
0
          deltas_y.arrayZ[i] = infer_delta ((double) orig_points.arrayZ[i].y,
711
0
                                            (double) orig_points.arrayZ[prev].y,
712
0
                                            (double) orig_points.arrayZ[next].y,
713
0
                                            (double) deltas_y.arrayZ[prev], (double) deltas_y.arrayZ[next]);
714
0
          inferred_idxes.add (i);
715
0
          if (--unref_count == 0) goto no_more_gaps;
716
0
        }
717
0
      }
718
0
    no_more_gaps:
719
0
      start_point = end_point + 1;
720
0
    }
721
0
722
0
    for (unsigned i = 0; i < point_count; i++)
723
0
    {
724
0
      /* if points are not referenced and deltas are not inferred, set to 0.
725
0
       * reference all points for gvar */
726
0
      if ( !indices[i])
727
0
      {
728
0
        if (!inferred_idxes.has (i))
729
0
        {
730
0
          deltas_x.arrayZ[i] = 0.0;
731
0
          deltas_y.arrayZ[i] = 0.0;
732
0
        }
733
0
        indices[i] = true;
734
0
      }
735
0
    }
736
0
    return true;
737
0
  }
738
739
  bool optimize (const contour_point_vector_t& contour_points,
740
                 bool is_composite,
741
     optimize_scratch_t &scratch,
742
                 double tolerance = 0.5 + 1e-10)
743
0
  {
744
0
    unsigned count = contour_points.length;
745
0
    if (deltas_x.length != count ||
746
0
        deltas_y.length != count)
747
0
      return false;
748
0
749
0
    hb_vector_t<bool> &opt_indices = scratch.opt_indices.reset ();
750
0
    hb_vector_t<int> &rounded_x_deltas = scratch.rounded_x_deltas;
751
0
    hb_vector_t<int> &rounded_y_deltas = scratch.rounded_y_deltas;
752
0
753
0
    if (unlikely (!rounded_x_deltas.resize_dirty  (count) ||
754
0
                  !rounded_y_deltas.resize_dirty  (count)))
755
0
      return false;
756
0
757
0
    for (unsigned i = 0; i < count; i++)
758
0
    {
759
0
      rounded_x_deltas.arrayZ[i] = (int) roundf (deltas_x.arrayZ[i]);
760
0
      rounded_y_deltas.arrayZ[i] = (int) roundf (deltas_y.arrayZ[i]);
761
0
    }
762
0
763
0
    if (!iup_delta_optimize (contour_points, rounded_x_deltas, rounded_y_deltas, opt_indices, scratch.iup, tolerance))
764
0
      return false;
765
0
766
0
    unsigned ref_count = 0;
767
0
    for (bool ref_flag : opt_indices)
768
0
       ref_count += ref_flag;
769
0
770
0
    if (ref_count == count) return true;
771
0
772
0
    hb_vector_t<float> &opt_deltas_x = scratch.opt_deltas_x.reset ();
773
0
    hb_vector_t<float> &opt_deltas_y = scratch.opt_deltas_y.reset ();
774
0
    bool is_comp_glyph_wo_deltas = (is_composite && ref_count == 0);
775
0
    if (is_comp_glyph_wo_deltas)
776
0
    {
777
0
      if (unlikely (!opt_deltas_x.resize (count) ||
778
0
                    !opt_deltas_y.resize (count)))
779
0
        return false;
780
0
781
0
      opt_indices.arrayZ[0] = true;
782
0
      for (unsigned i = 1; i < count; i++)
783
0
        opt_indices.arrayZ[i] = false;
784
0
    }
785
0
786
0
    hb_vector_t<unsigned char> &opt_point_data = scratch.opt_point_data.reset ();
787
0
    if (!compile_point_set (opt_indices, opt_point_data))
788
0
      return false;
789
0
    hb_vector_t<unsigned char> &opt_deltas_data = scratch.opt_deltas_data.reset ();
790
0
    if (!compile_deltas (opt_indices,
791
0
                         is_comp_glyph_wo_deltas ? opt_deltas_x : deltas_x,
792
0
                         is_comp_glyph_wo_deltas ? opt_deltas_y : deltas_y,
793
0
                         opt_deltas_data,
794
0
       scratch.rounded_deltas))
795
0
      return false;
796
0
797
0
    hb_vector_t<unsigned char> &point_data = scratch.point_data.reset ();
798
0
    if (!compile_point_set (indices, point_data))
799
0
      return false;
800
0
    hb_vector_t<unsigned char> &deltas_data = scratch.deltas_data.reset ();
801
0
    if (!compile_deltas (indices, deltas_x, deltas_y, deltas_data, scratch.rounded_deltas))
802
0
      return false;
803
0
804
0
    if (opt_point_data.length + opt_deltas_data.length < point_data.length + deltas_data.length)
805
0
    {
806
0
      indices = std::move (opt_indices);
807
0
808
0
      if (is_comp_glyph_wo_deltas)
809
0
      {
810
0
        deltas_x = std::move (opt_deltas_x);
811
0
        deltas_y = std::move (opt_deltas_y);
812
0
      }
813
0
    }
814
0
    return !indices.in_error () && !deltas_x.in_error () && !deltas_y.in_error ();
815
0
  }
816
817
  static bool compile_point_set (const hb_vector_t<bool> &point_indices,
818
                                 hb_vector_t<unsigned char>& compiled_points /* OUT */)
819
0
  {
820
0
    unsigned num_points = 0;
821
0
    for (bool i : point_indices)
822
0
      if (i) num_points++;
823
0
824
0
    /* when iup optimization is enabled, num of referenced points could be 0 */
825
0
    if (!num_points) return true;
826
0
827
0
    unsigned indices_length = point_indices.length;
828
0
    /* If the points set consists of all points in the glyph, it's encoded with a
829
0
     * single zero byte */
830
0
    if (num_points == indices_length)
831
0
      return compiled_points.resize (1);
832
0
833
0
    /* allocate enough memories: 2 bytes for count + 3 bytes for each point */
834
0
    unsigned num_bytes = 2 + 3 *num_points;
835
0
    if (unlikely (!compiled_points.resize_dirty  (num_bytes)))
836
0
      return false;
837
0
838
0
    unsigned pos = 0;
839
0
    /* binary data starts with the total number of reference points */
840
0
    if (num_points < 0x80)
841
0
      compiled_points.arrayZ[pos++] = num_points;
842
0
    else
843
0
    {
844
0
      compiled_points.arrayZ[pos++] = ((num_points >> 8) | 0x80);
845
0
      compiled_points.arrayZ[pos++] = num_points & 0xFF;
846
0
    }
847
0
848
0
    const unsigned max_run_length = 0x7F;
849
0
    unsigned i = 0;
850
0
    unsigned last_value = 0;
851
0
    unsigned num_encoded = 0;
852
0
    while (i < indices_length && num_encoded < num_points)
853
0
    {
854
0
      unsigned run_length = 0;
855
0
      unsigned header_pos = pos;
856
0
      compiled_points.arrayZ[pos++] = 0;
857
0
858
0
      bool use_byte_encoding = false;
859
0
      bool new_run = true;
860
0
      while (i < indices_length && num_encoded < num_points &&
861
0
             run_length <= max_run_length)
862
0
      {
863
0
        // find out next referenced point index
864
0
        while (i < indices_length && !point_indices[i])
865
0
          i++;
866
0
867
0
        if (i >= indices_length) break;
868
0
869
0
        unsigned cur_value = i;
870
0
        unsigned delta = cur_value - last_value;
871
0
872
0
        if (new_run)
873
0
        {
874
0
          use_byte_encoding = (delta <= 0xFF);
875
0
          new_run = false;
876
0
        }
877
0
878
0
        if (use_byte_encoding && delta > 0xFF)
879
0
          break;
880
0
881
0
        if (use_byte_encoding)
882
0
          compiled_points.arrayZ[pos++] = delta;
883
0
        else
884
0
        {
885
0
          compiled_points.arrayZ[pos++] = delta >> 8;
886
0
          compiled_points.arrayZ[pos++] = delta & 0xFF;
887
0
        }
888
0
        i++;
889
0
        last_value = cur_value;
890
0
        run_length++;
891
0
        num_encoded++;
892
0
      }
893
0
894
0
      if (use_byte_encoding)
895
0
        compiled_points.arrayZ[header_pos] = run_length - 1;
896
0
      else
897
0
        compiled_points.arrayZ[header_pos] = (run_length - 1) | 0x80;
898
0
    }
899
0
    return compiled_points.resize_dirty  (pos);
900
0
  }
901
902
  static double infer_delta (double target_val, double prev_val, double next_val, double prev_delta, double next_delta)
903
0
  {
904
0
    if (prev_val == next_val)
905
0
      return (prev_delta == next_delta) ? prev_delta : 0.0;
906
0
    else if (target_val <= hb_min (prev_val, next_val))
907
0
      return (prev_val < next_val) ? prev_delta : next_delta;
908
0
    else if (target_val >= hb_max (prev_val, next_val))
909
0
      return (prev_val > next_val) ? prev_delta : next_delta;
910
0
911
0
    double r = (target_val - prev_val) / (next_val - prev_val);
912
0
    return prev_delta + r * (next_delta - prev_delta);
913
0
  }
914
915
  static unsigned int next_index (unsigned int i, unsigned int start, unsigned int end)
916
0
  { return (i >= end) ? start : (i + 1); }
917
};
918
919
template <typename OffType = HBUINT16>
920
struct TupleVariationData
921
{
922
  bool sanitize (hb_sanitize_context_t *c) const
923
  {
924
    TRACE_SANITIZE (this);
925
    // here check on min_size only, TupleVariationHeader and var data will be
926
    // checked while accessing through iterator.
927
    return_trace (c->check_struct (this));
928
  }
929
930
  unsigned get_size (unsigned axis_count_times_2) const
931
  {
932
    unsigned total_size = min_size;
933
    unsigned count = tupleVarCount.get_count ();
934
    const TupleVariationHeader *tuple_var_header = &(get_tuple_var_header());
935
    for (unsigned i = 0; i < count; i++)
936
    {
937
      total_size += tuple_var_header->get_size (axis_count_times_2) + tuple_var_header->get_data_size ();
938
      tuple_var_header = &tuple_var_header->get_next (axis_count_times_2);
939
    }
940
941
    return total_size;
942
  }
943
944
  const TupleVariationHeader &get_tuple_var_header (void) const
945
0
  { return StructAfter<TupleVariationHeader> (data); }
946
947
  struct tuple_iterator_t;
948
  struct tuple_variations_t
949
  {
950
    hb_vector_t<tuple_delta_t> tuple_vars;
951
952
    private:
953
    /* referenced point set->compiled point data map */
954
    hb_hashmap_t<const hb_vector_t<bool>*, hb_vector_t<unsigned char>> point_data_map;
955
    /* referenced point set-> count map, used in finding shared points */
956
    hb_hashmap_t<const hb_vector_t<bool>*, unsigned> point_set_count_map;
957
958
    /* empty for non-gvar tuples.
959
     * shared_points_bytes is a pointer to some value in the point_data_map,
960
     * which will be freed during map destruction. Save it for serialization, so
961
     * no need to do find_shared_points () again */
962
    hb_vector_t<unsigned char> *shared_points_bytes = nullptr;
963
964
    /* total compiled byte size as TupleVariationData format, initialized to 0 */
965
    unsigned compiled_byte_size = 0;
966
    bool needs_padding = false;
967
968
    /* for gvar iup delta optimization: whether this is a composite glyph */
969
    bool is_composite = false;
970
971
    public:
972
    tuple_variations_t () = default;
973
    tuple_variations_t (const tuple_variations_t&) = delete;
974
    tuple_variations_t& operator=(const tuple_variations_t&) = delete;
975
    tuple_variations_t (tuple_variations_t&&) = default;
976
    tuple_variations_t& operator=(tuple_variations_t&&) = default;
977
    ~tuple_variations_t () = default;
978
979
    explicit operator bool () const { return bool (tuple_vars); }
980
    unsigned get_var_count () const
981
    {
982
      unsigned count = 0;
983
      /* when iup delta opt is enabled, compiled_deltas could be empty and we
984
       * should skip this tuple */
985
      for (auto& tuple: tuple_vars)
986
        if (tuple.compiled_deltas) count++;
987
988
      if (shared_points_bytes && shared_points_bytes->length)
989
        count |= TupleVarCount::SharedPointNumbers;
990
      return count;
991
    }
992
993
    unsigned get_compiled_byte_size () const
994
    { return compiled_byte_size; }
995
996
    bool create_from_tuple_var_data (tuple_iterator_t iterator,
997
                                     unsigned tuple_var_count,
998
                                     unsigned point_count,
999
                                     bool is_gvar,
1000
                                     const hb_map_t *axes_old_index_tag_map,
1001
                                     const hb_vector_t<unsigned> &shared_indices,
1002
                                     const hb_array_t<const F2DOT14> shared_tuples,
1003
             hb_alloc_pool_t *pool = nullptr,
1004
                                     bool is_composite_glyph = false)
1005
    {
1006
      hb_vector_t<unsigned> private_indices;
1007
      hb_vector_t<int> deltas_x;
1008
      hb_vector_t<int> deltas_y;
1009
      do
1010
      {
1011
        const HBUINT8 *p = iterator.get_serialized_data ();
1012
        unsigned int length = iterator.current_tuple->get_data_size ();
1013
        if (unlikely (!iterator.var_data_bytes.check_range (p, length)))
1014
          return false;
1015
1016
  hb_hashmap_t<hb_tag_t, Triple> axis_tuples;
1017
        if (!iterator.current_tuple->unpack_axis_tuples (iterator.get_axis_count (), shared_tuples, axes_old_index_tag_map, axis_tuples)
1018
            || axis_tuples.is_empty ())
1019
          return false;
1020
1021
  private_indices.reset ();
1022
        bool has_private_points = iterator.current_tuple->has_private_points ();
1023
        const HBUINT8 *end = p + length;
1024
        if (has_private_points &&
1025
            !TupleVariationData::decompile_points (p, private_indices, end))
1026
          return false;
1027
1028
        const hb_vector_t<unsigned> &indices = has_private_points ? private_indices : shared_indices;
1029
        bool apply_to_all = (indices.length == 0);
1030
        unsigned num_deltas = apply_to_all ? point_count : indices.length;
1031
1032
        if (unlikely (!deltas_x.resize_dirty  (num_deltas) ||
1033
                      !TupleVariationData::decompile_deltas (p, deltas_x, end)))
1034
          return false;
1035
1036
        if (is_gvar)
1037
        {
1038
          if (unlikely (!deltas_y.resize_dirty  (num_deltas) ||
1039
                        !TupleVariationData::decompile_deltas (p, deltas_y, end)))
1040
            return false;
1041
        }
1042
1043
        tuple_delta_t var;
1044
        var.axis_tuples = std::move (axis_tuples);
1045
        if (unlikely (!var.indices.allocate_from_pool (pool, point_count) ||
1046
                      !var.deltas_x.allocate_from_pool (pool, point_count, false)))
1047
          return false;
1048
1049
        if (is_gvar && unlikely (!var.deltas_y.allocate_from_pool (pool, point_count, false)))
1050
          return false;
1051
1052
        for (unsigned i = 0; i < num_deltas; i++)
1053
        {
1054
          unsigned idx = apply_to_all ? i : indices[i];
1055
          if (idx >= point_count) continue;
1056
          var.indices[idx] = true;
1057
          var.deltas_x[idx] = deltas_x[i];
1058
          if (is_gvar)
1059
            var.deltas_y[idx] = deltas_y[i];
1060
        }
1061
        tuple_vars.push (std::move (var));
1062
      } while (iterator.move_to_next ());
1063
1064
      is_composite = is_composite_glyph;
1065
      return true;
1066
    }
1067
1068
    bool create_from_item_var_data (const VarData &var_data,
1069
                                    const hb_vector_t<hb_hashmap_t<hb_tag_t, Triple>>& regions,
1070
                                    const hb_map_t& axes_old_index_tag_map,
1071
                                    unsigned& item_count,
1072
                                    const hb_inc_bimap_t* inner_map = nullptr)
1073
0
    {
1074
0
      /* NULL offset, to keep original varidx valid, just return */
1075
0
      if (&var_data == &Null (VarData))
1076
0
        return true;
1077
0
1078
0
      unsigned num_regions = var_data.get_region_index_count ();
1079
0
      if (!tuple_vars.alloc (num_regions)) return false;
1080
0
1081
0
      item_count = inner_map ? inner_map->get_population () : var_data.get_item_count ();
1082
0
      if (!item_count) return true;
1083
0
      unsigned row_size = var_data.get_row_size ();
1084
0
      const HBUINT8 *delta_bytes = var_data.get_delta_bytes ();
1085
0
1086
0
      for (unsigned r = 0; r < num_regions; r++)
1087
0
      {
1088
0
        /* In VarData, deltas are organized in rows, convert them into
1089
0
         * column(region) based tuples, resize deltas_x first */
1090
0
        tuple_delta_t tuple;
1091
0
        if (!tuple.deltas_x.resize_dirty  (item_count) ||
1092
0
            !tuple.indices.resize_dirty  (item_count))
1093
0
          return false;
1094
0
1095
0
        for (unsigned i = 0; i < item_count; i++)
1096
0
        {
1097
0
          tuple.indices.arrayZ[i] = true;
1098
0
          tuple.deltas_x.arrayZ[i] = var_data.get_item_delta_fast (inner_map ? inner_map->backward (i) : i,
1099
0
                                                                   r, delta_bytes, row_size);
1100
0
        }
1101
0
1102
0
        unsigned region_index = var_data.get_region_index (r);
1103
0
        if (region_index >= regions.length) return false;
1104
0
        tuple.axis_tuples = regions.arrayZ[region_index];
1105
0
1106
0
        tuple_vars.push (std::move (tuple));
1107
0
      }
1108
0
      return !tuple_vars.in_error ();
1109
0
    }
1110
1111
    private:
1112
    static int _cmp_axis_tag (const void *pa, const void *pb)
1113
0
    {
1114
0
      const hb_tag_t *a = (const hb_tag_t*) pa;
1115
0
      const hb_tag_t *b = (const hb_tag_t*) pb;
1116
0
      return (int)(*a) - (int)(*b);
1117
0
    }
1118
1119
    bool change_tuple_variations_axis_limits (const hb_hashmap_t<hb_tag_t, Triple>& normalized_axes_location,
1120
                                              const hb_hashmap_t<hb_tag_t, TripleDistances>& axes_triple_distances,
1121
                hb_alloc_pool_t *pool = nullptr)
1122
0
    {
1123
0
      /* sort axis_tag/axis_limits, make result deterministic */
1124
0
      hb_vector_t<hb_tag_t> axis_tags;
1125
0
      if (!axis_tags.alloc (normalized_axes_location.get_population ()))
1126
0
        return false;
1127
0
      for (auto t : normalized_axes_location.keys ())
1128
0
        axis_tags.push (t);
1129
0
1130
0
      // Reused vectors for reduced malloc pressure.
1131
0
      rebase_tent_result_scratch_t scratch;
1132
0
      hb_vector_t<tuple_delta_t> out;
1133
0
1134
0
      axis_tags.qsort (_cmp_axis_tag);
1135
0
      for (auto axis_tag : axis_tags)
1136
0
      {
1137
0
        Triple *axis_limit;
1138
0
        if (!normalized_axes_location.has (axis_tag, &axis_limit))
1139
0
          return false;
1140
0
        TripleDistances axis_triple_distances{1.0, 1.0};
1141
0
        if (axes_triple_distances.has (axis_tag))
1142
0
          axis_triple_distances = axes_triple_distances.get (axis_tag);
1143
0
1144
0
        hb_vector_t<tuple_delta_t> new_vars;
1145
0
        for (tuple_delta_t& var : tuple_vars)
1146
0
        {
1147
0
    // This may move var out.
1148
0
    var.change_tuple_var_axis_limit (axis_tag, *axis_limit, axis_triple_distances, out, scratch, pool);
1149
0
          if (!out) continue;
1150
0
1151
0
          unsigned new_len = new_vars.length + out.length;
1152
0
1153
0
          if (unlikely (!new_vars.alloc (new_len, false)))
1154
0
            return false;
1155
0
1156
0
          for (unsigned i = 0; i < out.length; i++)
1157
0
            new_vars.push (std::move (out[i]));
1158
0
        }
1159
0
        tuple_vars = std::move (new_vars);
1160
0
      }
1161
0
      return true;
1162
0
    }
1163
1164
    /* merge tuple variations with overlapping tents, if iup delta optimization
1165
     * is enabled, add default deltas to contour_points */
1166
    bool merge_tuple_variations (contour_point_vector_t* contour_points = nullptr)
1167
0
    {
1168
0
      hb_vector_t<tuple_delta_t> new_vars;
1169
0
      // The pre-allocation is essential for address stability of pointers
1170
0
      // we store in the hashmap.
1171
0
      if (unlikely (!new_vars.alloc (tuple_vars.length)))
1172
0
  return false;
1173
0
      hb_hashmap_t<const hb_hashmap_t<hb_tag_t, Triple>*, unsigned> m;
1174
0
      for (tuple_delta_t& var : tuple_vars)
1175
0
      {
1176
0
        /* if all axes are pinned, drop the tuple variation */
1177
0
        if (var.axis_tuples.is_empty ())
1178
0
        {
1179
0
          /* if iup_delta_optimize is enabled, add deltas to contour coords */
1180
0
          if (contour_points && !contour_points->add_deltas (var.deltas_x,
1181
0
                                                             var.deltas_y,
1182
0
                                                             var.indices))
1183
0
            return false;
1184
0
          continue;
1185
0
        }
1186
0
1187
0
        unsigned *idx;
1188
0
        if (m.has (&(var.axis_tuples), &idx))
1189
0
        {
1190
0
          new_vars[*idx] += var;
1191
0
        }
1192
0
        else
1193
0
        {
1194
0
    auto *new_var = new_vars.push ();
1195
0
    if (unlikely (new_vars.in_error ()))
1196
0
      return false;
1197
0
          hb_swap (*new_var, var);
1198
0
          if (unlikely (!m.set (&(new_var->axis_tuples), new_vars.length - 1)))
1199
0
            return false;
1200
0
        }
1201
0
      }
1202
0
      m.fini (); // Just in case, since it points into new_vars data.
1203
0
     // Shouldn't be necessary though, since we only move new_vars, not its
1204
0
     // contents.
1205
0
      tuple_vars = std::move (new_vars);
1206
0
      return true;
1207
0
    }
1208
1209
    /* compile all point set and store byte data in a point_set->hb_bytes_t hashmap,
1210
     * also update point_set->count map, which will be used in finding shared
1211
     * point set*/
1212
    bool compile_all_point_sets ()
1213
    {
1214
      for (const auto& tuple: tuple_vars)
1215
      {
1216
        const hb_vector_t<bool>* points_set = &(tuple.indices);
1217
        if (point_data_map.has (points_set))
1218
        {
1219
          unsigned *count;
1220
          if (unlikely (!point_set_count_map.has (points_set, &count) ||
1221
                        !point_set_count_map.set (points_set, (*count) + 1)))
1222
            return false;
1223
          continue;
1224
        }
1225
1226
        hb_vector_t<unsigned char> compiled_point_data;
1227
        if (!tuple_delta_t::compile_point_set (*points_set, compiled_point_data))
1228
          return false;
1229
1230
        if (!point_data_map.set (points_set, std::move (compiled_point_data)) ||
1231
            !point_set_count_map.set (points_set, 1))
1232
          return false;
1233
      }
1234
      return true;
1235
    }
1236
1237
    /* find shared points set which saves most bytes */
1238
    void find_shared_points ()
1239
    {
1240
      unsigned max_saved_bytes = 0;
1241
1242
      for (const auto& _ : point_data_map.iter_ref ())
1243
      {
1244
        const hb_vector_t<bool>* points_set = _.first;
1245
        unsigned data_length = _.second.length;
1246
        if (!data_length) continue;
1247
        unsigned *count;
1248
        if (unlikely (!point_set_count_map.has (points_set, &count) ||
1249
                      *count <= 1))
1250
        {
1251
          shared_points_bytes = nullptr;
1252
          return;
1253
        }
1254
1255
        unsigned saved_bytes = data_length * ((*count) -1);
1256
        if (saved_bytes > max_saved_bytes)
1257
        {
1258
          max_saved_bytes = saved_bytes;
1259
          shared_points_bytes = &(_.second);
1260
        }
1261
      }
1262
    }
1263
1264
    bool calc_inferred_deltas (const contour_point_vector_t& contour_points,
1265
             hb_vector_t<unsigned> &scratch)
1266
0
    {
1267
0
      for (tuple_delta_t& var : tuple_vars)
1268
0
        if (!var.calc_inferred_deltas (contour_points, scratch))
1269
0
          return false;
1270
0
1271
0
      return true;
1272
0
    }
1273
1274
    bool iup_optimize (const contour_point_vector_t& contour_points,
1275
           optimize_scratch_t &scratch)
1276
0
    {
1277
0
      for (tuple_delta_t& var : tuple_vars)
1278
0
      {
1279
0
        if (!var.optimize (contour_points, is_composite, scratch))
1280
0
          return false;
1281
0
      }
1282
0
      return true;
1283
0
    }
1284
1285
    public:
1286
    bool instantiate (const hb_hashmap_t<hb_tag_t, Triple>& normalized_axes_location,
1287
                      const hb_hashmap_t<hb_tag_t, TripleDistances>& axes_triple_distances,
1288
          optimize_scratch_t &scratch,
1289
          hb_alloc_pool_t *pool = nullptr,
1290
                      contour_point_vector_t* contour_points = nullptr,
1291
                      bool optimize = false)
1292
0
    {
1293
0
      if (!tuple_vars) return true;
1294
0
      if (!change_tuple_variations_axis_limits (normalized_axes_location, axes_triple_distances, pool))
1295
0
        return false;
1296
0
      /* compute inferred deltas only for gvar */
1297
0
      if (contour_points)
1298
0
      {
1299
0
  hb_vector_t<unsigned> scratch;
1300
0
  if (!calc_inferred_deltas (*contour_points, scratch))
1301
0
    return false;
1302
0
      }
1303
0
1304
0
      /* if iup delta opt is on, contour_points can't be null */
1305
0
      if (optimize && !contour_points)
1306
0
        return false;
1307
0
1308
0
      if (!merge_tuple_variations (optimize ? contour_points : nullptr))
1309
0
        return false;
1310
0
1311
0
      if (optimize && !iup_optimize (*contour_points, scratch)) return false;
1312
0
      return !tuple_vars.in_error ();
1313
0
    }
1314
1315
    bool compile_bytes (const hb_map_t& axes_index_map,
1316
                        const hb_map_t& axes_old_index_tag_map,
1317
                        bool use_shared_points,
1318
                        bool is_gvar = false,
1319
                        const hb_hashmap_t<const hb_vector_t<F2DOT14>*, unsigned>* shared_tuples_idx_map = nullptr,
1320
      hb_alloc_pool_t *pool = nullptr)
1321
    {
1322
      // return true for empty glyph
1323
      if (!tuple_vars)
1324
        return true;
1325
1326
      // compile points set and store data in hashmap
1327
      if (!compile_all_point_sets ())
1328
        return false;
1329
1330
      /* total compiled byte size as TupleVariationData format, initialized to its
1331
       * min_size: 4 */
1332
      compiled_byte_size += 4;
1333
1334
      if (use_shared_points)
1335
      {
1336
        find_shared_points ();
1337
        if (shared_points_bytes)
1338
          compiled_byte_size += shared_points_bytes->length;
1339
      }
1340
      hb_vector_t<int> rounded_deltas_scratch;
1341
      // compile delta and tuple var header for each tuple variation
1342
      for (auto& tuple: tuple_vars)
1343
      {
1344
        const hb_vector_t<bool>* points_set = &(tuple.indices);
1345
        hb_vector_t<unsigned char> *points_data;
1346
        if (unlikely (!point_data_map.has (points_set, &points_data)))
1347
          return false;
1348
1349
        /* when iup optimization is enabled, num of referenced points could be 0
1350
         * and thus the compiled points bytes is empty, we should skip compiling
1351
         * this tuple */
1352
        if (!points_data->length)
1353
          continue;
1354
        if (!tuple.compile_deltas (rounded_deltas_scratch, pool))
1355
          return false;
1356
1357
        unsigned points_data_length = (points_data != shared_points_bytes) ? points_data->length : 0;
1358
        if (!tuple.compile_tuple_var_header (axes_index_map, points_data_length, axes_old_index_tag_map,
1359
                                             shared_tuples_idx_map,
1360
               pool))
1361
          return false;
1362
        compiled_byte_size += tuple.compiled_tuple_header.length + points_data_length + tuple.compiled_deltas.length;
1363
      }
1364
1365
      if (is_gvar && (compiled_byte_size % 2))
1366
      {
1367
        needs_padding = true;
1368
        compiled_byte_size += 1;
1369
      }
1370
1371
      return true;
1372
    }
1373
1374
    bool serialize_var_headers (hb_serialize_context_t *c, unsigned& total_header_len) const
1375
    {
1376
      TRACE_SERIALIZE (this);
1377
      for (const auto& tuple: tuple_vars)
1378
      {
1379
        tuple.compiled_tuple_header.as_array ().copy (c);
1380
        if (c->in_error ()) return_trace (false);
1381
        total_header_len += tuple.compiled_tuple_header.length;
1382
      }
1383
      return_trace (true);
1384
    }
1385
1386
    bool serialize_var_data (hb_serialize_context_t *c, bool is_gvar) const
1387
    {
1388
      TRACE_SERIALIZE (this);
1389
      if (is_gvar && shared_points_bytes)
1390
      {
1391
        hb_ubytes_t s (shared_points_bytes->arrayZ, shared_points_bytes->length);
1392
        s.copy (c);
1393
      }
1394
1395
      for (const auto& tuple: tuple_vars)
1396
      {
1397
        const hb_vector_t<bool>* points_set = &(tuple.indices);
1398
        hb_vector_t<unsigned char> *point_data;
1399
        if (!point_data_map.has (points_set, &point_data))
1400
          return_trace (false);
1401
1402
        if (!is_gvar || point_data != shared_points_bytes)
1403
        {
1404
          hb_ubytes_t s (point_data->arrayZ, point_data->length);
1405
          s.copy (c);
1406
        }
1407
1408
        tuple.compiled_deltas.as_array ().copy (c);
1409
        if (c->in_error ()) return_trace (false);
1410
      }
1411
1412
      /* padding for gvar */
1413
      if (is_gvar && needs_padding)
1414
      {
1415
        HBUINT8 pad;
1416
        pad = 0;
1417
        if (!c->embed (pad)) return_trace (false);
1418
      }
1419
      return_trace (true);
1420
    }
1421
  };
1422
1423
  struct tuple_iterator_t
1424
  {
1425
    unsigned get_axis_count () const { return axis_count; }
1426
1427
    void init (hb_bytes_t var_data_bytes_, unsigned int axis_count_, const void *table_base_)
1428
0
    {
1429
0
      var_data_bytes = var_data_bytes_;
1430
0
      var_data = var_data_bytes_.as<TupleVariationData> ();
1431
0
      tuples_left = var_data->tupleVarCount.get_count ();
1432
0
      axis_count = axis_count_;
1433
0
      axis_count_times_2 = axis_count_ * 2;
1434
0
      current_tuple = &var_data->get_tuple_var_header ();
1435
0
      data_offset = 0;
1436
0
      table_base = table_base_;
1437
0
    }
1438
1439
    bool get_shared_indices (hb_vector_t<unsigned int> &shared_indices /* OUT */)
1440
0
    {
1441
0
      if (var_data->has_shared_point_numbers ())
1442
0
      {
1443
0
        const HBUINT8 *base = &(table_base+var_data->data);
1444
0
        const HBUINT8 *p = base;
1445
0
        if (!decompile_points (p, shared_indices, (const HBUINT8 *) (var_data_bytes.arrayZ + var_data_bytes.length))) return false;
1446
0
        data_offset = p - base;
1447
0
      }
1448
0
      return true;
1449
0
    }
1450
1451
    bool is_valid ()
1452
0
    {
1453
0
      if (unlikely (tuples_left <= 0))
1454
0
  return false;
1455
1456
0
      current_tuple_size = TupleVariationHeader::min_size;
1457
0
      if (unlikely (!var_data_bytes.check_end ((const char *) current_tuple + current_tuple_size)))
1458
0
  return false;
1459
1460
0
      current_tuple_size = current_tuple->get_size (axis_count_times_2);
1461
0
      if (unlikely (!var_data_bytes.check_end ((const char *) current_tuple + current_tuple_size)))
1462
0
  return false;
1463
1464
0
      return true;
1465
0
    }
1466
1467
    HB_ALWAYS_INLINE
1468
    bool move_to_next ()
1469
0
    {
1470
0
      data_offset += current_tuple->get_data_size ();
1471
0
      current_tuple = &StructAtOffset<TupleVariationHeader> (current_tuple, current_tuple_size);
1472
0
      tuples_left--;
1473
0
      return is_valid ();
1474
0
    }
1475
1476
    // TODO: Make it return (sanitized) hb_bytes_t
1477
    const HBUINT8 *get_serialized_data () const
1478
0
    { return &(table_base+var_data->data) + data_offset; }
1479
1480
    private:
1481
    signed tuples_left;
1482
    const TupleVariationData *var_data;
1483
    unsigned int axis_count;
1484
    unsigned int axis_count_times_2;
1485
    unsigned int data_offset;
1486
    unsigned int current_tuple_size;
1487
    const void *table_base;
1488
1489
    public:
1490
    hb_bytes_t var_data_bytes;
1491
    const TupleVariationHeader *current_tuple;
1492
  };
1493
1494
  static bool get_tuple_iterator (hb_bytes_t var_data_bytes, unsigned axis_count,
1495
                                  const void *table_base,
1496
                                  hb_vector_t<unsigned int> &shared_indices /* OUT */,
1497
                                  tuple_iterator_t *iterator /* OUT */)
1498
0
  {
1499
0
    iterator->init (var_data_bytes, axis_count, table_base);
1500
0
    if (!iterator->get_shared_indices (shared_indices))
1501
0
      return false;
1502
0
    return iterator->is_valid ();
1503
0
  }
1504
1505
0
  bool has_shared_point_numbers () const { return tupleVarCount.has_shared_point_numbers (); }
1506
1507
  static bool decompile_points (const HBUINT8 *&p /* IN/OUT */,
1508
        hb_vector_t<unsigned int> &points /* OUT */,
1509
        const HBUINT8 *end)
1510
0
  {
1511
0
    enum packed_point_flag_t
1512
0
    {
1513
0
      POINTS_ARE_WORDS     = 0x80,
1514
0
      POINT_RUN_COUNT_MASK = 0x7F
1515
0
    };
1516
1517
0
    if (unlikely (p + 1 > end)) return false;
1518
1519
0
    unsigned count = *p++;
1520
0
    if (count & POINTS_ARE_WORDS)
1521
0
    {
1522
0
      if (unlikely (p + 1 > end)) return false;
1523
0
      count = ((count & POINT_RUN_COUNT_MASK) << 8) | *p++;
1524
0
    }
1525
0
    if (unlikely (!points.resize_dirty  (count))) return false;
1526
1527
0
    unsigned n = 0;
1528
0
    unsigned i = 0;
1529
0
    while (i < count)
1530
0
    {
1531
0
      if (unlikely (p + 1 > end)) return false;
1532
0
      unsigned control = *p++;
1533
0
      unsigned run_count = (control & POINT_RUN_COUNT_MASK) + 1;
1534
0
      unsigned stop = i + run_count;
1535
0
      if (unlikely (stop > count)) return false;
1536
0
      if (control & POINTS_ARE_WORDS)
1537
0
      {
1538
0
        if (unlikely (p + run_count * HBUINT16::static_size > end)) return false;
1539
0
        for (; i < stop; i++)
1540
0
        {
1541
0
          n += *(const HBUINT16 *)p;
1542
0
          points.arrayZ[i] = n;
1543
0
          p += HBUINT16::static_size;
1544
0
        }
1545
0
      }
1546
0
      else
1547
0
      {
1548
0
        if (unlikely (p + run_count > end)) return false;
1549
0
        for (; i < stop; i++)
1550
0
        {
1551
0
          n += *p++;
1552
0
          points.arrayZ[i] = n;
1553
0
        }
1554
0
      }
1555
0
    }
1556
0
    return true;
1557
0
  }
1558
1559
  template <typename T>
1560
  HB_ALWAYS_INLINE
1561
  static bool decompile_deltas (const HBUINT8 *&p /* IN/OUT */,
1562
        hb_vector_t<T> &deltas /* IN/OUT */,
1563
        const HBUINT8 *end,
1564
        bool consume_all = false,
1565
        unsigned start = 0)
1566
0
  {
1567
0
    return TupleValues::decompile (p, deltas, end, consume_all, start);
1568
0
  }
1569
1570
0
  bool has_data () const { return tupleVarCount; }
1571
1572
  bool decompile_tuple_variations (unsigned point_count,
1573
                                   bool is_gvar,
1574
                                   tuple_iterator_t iterator,
1575
                                   const hb_map_t *axes_old_index_tag_map,
1576
                                   const hb_vector_t<unsigned> &shared_indices,
1577
                                   const hb_array_t<const F2DOT14> shared_tuples,
1578
                                   tuple_variations_t& tuple_variations, /* OUT */
1579
           hb_alloc_pool_t *pool = nullptr,
1580
                                   bool is_composite_glyph = false) const
1581
  {
1582
    return tuple_variations.create_from_tuple_var_data (iterator, tupleVarCount,
1583
                                                        point_count, is_gvar,
1584
                                                        axes_old_index_tag_map,
1585
                                                        shared_indices,
1586
                                                        shared_tuples,
1587
              pool,
1588
                                                        is_composite_glyph);
1589
  }
1590
1591
  bool serialize (hb_serialize_context_t *c,
1592
                  bool is_gvar,
1593
                  const tuple_variations_t& tuple_variations) const
1594
  {
1595
    TRACE_SERIALIZE (this);
1596
    /* empty tuple variations, just return and skip serialization. */
1597
    if (!tuple_variations) return_trace (true);
1598
1599
    auto *out = c->start_embed (this);
1600
    if (unlikely (!c->extend_min (out))) return_trace (false);
1601
1602
    if (!c->check_assign (out->tupleVarCount, tuple_variations.get_var_count (),
1603
                          HB_SERIALIZE_ERROR_INT_OVERFLOW)) return_trace (false);
1604
1605
    unsigned total_header_len = 0;
1606
1607
    if (!tuple_variations.serialize_var_headers (c, total_header_len))
1608
      return_trace (false);
1609
1610
    unsigned data_offset = min_size + total_header_len;
1611
    if (!is_gvar) data_offset += 4;
1612
    if (!c->check_assign (out->data, data_offset, HB_SERIALIZE_ERROR_INT_OVERFLOW)) return_trace (false);
1613
1614
    return tuple_variations.serialize_var_data (c, is_gvar);
1615
  }
1616
1617
  protected:
1618
  struct TupleVarCount : HBUINT16
1619
  {
1620
    friend struct tuple_variations_t;
1621
0
    bool has_shared_point_numbers () const { return ((*this) & SharedPointNumbers); }
1622
0
    unsigned int get_count () const { return (*this) & CountMask; }
1623
    TupleVarCount& operator = (uint16_t i) { HBUINT16::operator= (i); return *this; }
1624
    explicit operator bool () const { return get_count (); }
1625
1626
    protected:
1627
    enum Flags
1628
    {
1629
      SharedPointNumbers= 0x8000u,
1630
      CountMask         = 0x0FFFu
1631
    };
1632
    public:
1633
    DEFINE_SIZE_STATIC (2);
1634
  };
1635
1636
  TupleVarCount tupleVarCount;  /* A packed field. The high 4 bits are flags, and the
1637
                                 * low 12 bits are the number of tuple variation tables
1638
                                 * for this glyph. The number of tuple variation tables
1639
                                 * can be any number between 1 and 4095. */
1640
  OffsetTo<HBUINT8, OffType>
1641
                data;           /* Offset from the start of the base table
1642
                                 * to the serialized data. */
1643
  /* TupleVariationHeader tupleVariationHeaders[] *//* Array of tuple variation headers. */
1644
  public:
1645
  DEFINE_SIZE_MIN (2 + OffType::static_size);
1646
};
1647
1648
// TODO: Move tuple_variations_t to outside of TupleVariationData
1649
using tuple_variations_t = TupleVariationData<HBUINT16>::tuple_variations_t;
1650
struct item_variations_t
1651
{
1652
  using region_t = const hb_hashmap_t<hb_tag_t, Triple>*;
1653
  private:
1654
  /* each subtable is decompiled into a tuple_variations_t, in which all tuples
1655
   * have the same num of deltas (rows) */
1656
  hb_vector_t<tuple_variations_t> vars;
1657
1658
  /* num of retained rows for each subtable, there're 2 cases when var_data is empty:
1659
   * 1. retained item_count is zero
1660
   * 2. regions is empty and item_count is non-zero.
1661
   * when converting to tuples, both will be dropped because the tuple is empty,
1662
   * however, we need to retain 2. as all-zero rows to keep original varidx
1663
   * valid, so we need a way to remember the num of rows for each subtable */
1664
  hb_vector_t<unsigned> var_data_num_rows;
1665
1666
  /* original region list, decompiled from item varstore, used when rebuilding
1667
   * region list after instantiation */
1668
  hb_vector_t<hb_hashmap_t<hb_tag_t, Triple>> orig_region_list;
1669
1670
  /* region list: vector of Regions, maintain the original order for the regions
1671
   * that existed before instantiate (), append the new regions at the end.
1672
   * Regions are stored in each tuple already, save pointers only.
1673
   * When converting back to item varstore, unused regions will be pruned */
1674
  hb_vector_t<region_t> region_list;
1675
1676
  /* region -> idx map after instantiation and pruning unused regions */
1677
  hb_hashmap_t<region_t, unsigned> region_map;
1678
1679
  /* all delta rows after instantiation */
1680
  hb_vector_t<hb_vector_t<int>> delta_rows;
1681
  /* final optimized vector of encoding objects used to assemble the varstore */
1682
  hb_vector_t<delta_row_encoding_t> encodings;
1683
1684
  /* old varidxes -> new var_idxes map */
1685
  hb_map_t varidx_map;
1686
1687
  /* has long words */
1688
  bool has_long = false;
1689
1690
  public:
1691
  bool has_long_word () const
1692
0
  { return has_long; }
1693
1694
  const hb_vector_t<region_t>& get_region_list () const
1695
0
  { return region_list; }
1696
1697
  const hb_vector_t<delta_row_encoding_t>& get_vardata_encodings () const
1698
0
  { return encodings; }
1699
1700
  const hb_map_t& get_varidx_map () const
1701
0
  { return varidx_map; }
1702
1703
  bool instantiate (const ItemVariationStore& varStore,
1704
                    const hb_subset_plan_t *plan,
1705
                    bool optimize=true,
1706
                    bool use_no_variation_idx=true,
1707
                    const hb_array_t <const hb_inc_bimap_t> inner_maps = hb_array_t<const hb_inc_bimap_t> ())
1708
0
  {
1709
0
    if (!create_from_item_varstore (varStore, plan->axes_old_index_tag_map, inner_maps))
1710
0
      return false;
1711
0
    if (!instantiate_tuple_vars (plan->axes_location, plan->axes_triple_distances))
1712
0
      return false;
1713
0
    return as_item_varstore (optimize, use_no_variation_idx);
1714
0
  }
1715
1716
  /* keep below APIs public only for unit test: test-item-varstore */
1717
  bool create_from_item_varstore (const ItemVariationStore& varStore,
1718
                                  const hb_map_t& axes_old_index_tag_map,
1719
                                  const hb_array_t <const hb_inc_bimap_t> inner_maps = hb_array_t<const hb_inc_bimap_t> ())
1720
0
  {
1721
0
    const VarRegionList& regionList = varStore.get_region_list ();
1722
0
    if (!regionList.get_var_regions (axes_old_index_tag_map, orig_region_list))
1723
0
      return false;
1724
0
1725
0
    unsigned num_var_data = varStore.get_sub_table_count ();
1726
0
    if (inner_maps && inner_maps.length != num_var_data) return false;
1727
0
    if (!vars.alloc (num_var_data) ||
1728
0
        !var_data_num_rows.alloc (num_var_data)) return false;
1729
0
1730
0
    for (unsigned i = 0; i < num_var_data; i++)
1731
0
    {
1732
0
      if (inner_maps && !inner_maps.arrayZ[i].get_population ())
1733
0
          continue;
1734
0
      tuple_variations_t var_data_tuples;
1735
0
      unsigned item_count = 0;
1736
0
      if (!var_data_tuples.create_from_item_var_data (varStore.get_sub_table (i),
1737
0
                                                      orig_region_list,
1738
0
                                                      axes_old_index_tag_map,
1739
0
                                                      item_count,
1740
0
                                                      inner_maps ? &(inner_maps.arrayZ[i]) : nullptr))
1741
0
        return false;
1742
0
1743
0
      var_data_num_rows.push (item_count);
1744
0
      vars.push (std::move (var_data_tuples));
1745
0
    }
1746
0
    return !vars.in_error () && !var_data_num_rows.in_error () && vars.length == var_data_num_rows.length;
1747
0
  }
1748
1749
  bool instantiate_tuple_vars (const hb_hashmap_t<hb_tag_t, Triple>& normalized_axes_location,
1750
                               const hb_hashmap_t<hb_tag_t, TripleDistances>& axes_triple_distances)
1751
0
  {
1752
0
    optimize_scratch_t scratch;
1753
0
    for (tuple_variations_t& tuple_vars : vars)
1754
0
      if (!tuple_vars.instantiate (normalized_axes_location, axes_triple_distances, scratch))
1755
0
        return false;
1756
0
1757
0
    if (!build_region_list ()) return false;
1758
0
    return true;
1759
0
  }
1760
1761
  bool build_region_list ()
1762
0
  {
1763
0
    /* scan all tuples and collect all unique regions, prune unused regions */
1764
0
    hb_hashmap_t<region_t, unsigned> all_regions;
1765
0
    hb_hashmap_t<region_t, unsigned> used_regions;
1766
0
1767
0
    /* use a vector when inserting new regions, make result deterministic */
1768
0
    hb_vector_t<region_t> all_unique_regions;
1769
0
    for (const tuple_variations_t& sub_table : vars)
1770
0
    {
1771
0
      for (const tuple_delta_t& tuple : sub_table.tuple_vars)
1772
0
      {
1773
0
        region_t r = &(tuple.axis_tuples);
1774
0
        if (!used_regions.has (r))
1775
0
        {
1776
0
          bool all_zeros = true;
1777
0
          for (float d : tuple.deltas_x)
1778
0
          {
1779
0
            int delta = (int) roundf (d);
1780
0
            if (delta != 0)
1781
0
            {
1782
0
              all_zeros = false;
1783
0
              break;
1784
0
            }
1785
0
          }
1786
0
          if (!all_zeros)
1787
0
          {
1788
0
            if (!used_regions.set (r, 1))
1789
0
              return false;
1790
0
          }
1791
0
        }
1792
0
        if (all_regions.has (r))
1793
0
          continue;
1794
0
        if (!all_regions.set (r, 1))
1795
0
          return false;
1796
0
        all_unique_regions.push (r);
1797
0
      }
1798
0
    }
1799
0
1800
0
    /* regions are empty means no variation data, return true */
1801
0
    if (!all_regions || !all_unique_regions) return true;
1802
0
1803
0
    if (!region_list.alloc (all_regions.get_population ()))
1804
0
      return false;
1805
0
1806
0
    unsigned idx = 0;
1807
0
    /* append the original regions that pre-existed */
1808
0
    for (const auto& r : orig_region_list)
1809
0
    {
1810
0
      if (!all_regions.has (&r) || !used_regions.has (&r))
1811
0
        continue;
1812
0
1813
0
      region_list.push (&r);
1814
0
      if (!region_map.set (&r, idx))
1815
0
        return false;
1816
0
      all_regions.del (&r);
1817
0
      idx++;
1818
0
    }
1819
0
1820
0
    /* append the new regions at the end */
1821
0
    for (const auto& r: all_unique_regions)
1822
0
    {
1823
0
      if (!all_regions.has (r) || !used_regions.has (r))
1824
0
        continue;
1825
0
      region_list.push (r);
1826
0
      if (!region_map.set (r, idx))
1827
0
        return false;
1828
0
      all_regions.del (r);
1829
0
      idx++;
1830
0
    }
1831
0
    return (!region_list.in_error ()) && (!region_map.in_error ());
1832
0
  }
1833
1834
  /* main algorithm ported from fonttools VarStore_optimize() method, optimize
1835
   * varstore by default */
1836
1837
  struct combined_gain_idx_tuple_t
1838
  {
1839
    uint64_t encoded;
1840
1841
    combined_gain_idx_tuple_t () = default;
1842
    combined_gain_idx_tuple_t (unsigned gain, unsigned i, unsigned j)
1843
        : encoded ((uint64_t (0xFFFFFF - gain) << 40) | (uint64_t (i) << 20) | uint64_t (j))
1844
0
    {
1845
0
      assert (gain < 0xFFFFFF);
1846
0
      assert (i < 0xFFFFFFF && j < 0xFFFFFFF);
1847
0
    }
1848
1849
    bool operator < (const combined_gain_idx_tuple_t& o)
1850
0
    {
1851
0
      return encoded < o.encoded;
1852
0
    }
1853
1854
    bool operator <= (const combined_gain_idx_tuple_t& o)
1855
0
    {
1856
0
      return encoded <= o.encoded;
1857
0
    }
1858
1859
0
    unsigned idx_1 () const { return (encoded >> 20) & 0xFFFFF; };
1860
0
    unsigned idx_2 () const { return encoded & 0xFFFFF; };
1861
  };
1862
1863
  bool as_item_varstore (bool optimize=true, bool use_no_variation_idx=true)
1864
0
  {
1865
0
    /* return true if no variation data */
1866
0
    if (!region_list) return true;
1867
0
    unsigned num_cols = region_list.length;
1868
0
    /* pre-alloc a 2D vector for all sub_table's VarData rows */
1869
0
    unsigned total_rows = 0;
1870
0
    for (unsigned major = 0; major < var_data_num_rows.length; major++)
1871
0
      total_rows += var_data_num_rows[major];
1872
0
1873
0
    if (!delta_rows.resize (total_rows)) return false;
1874
0
    /* init all rows to [0]*num_cols */
1875
0
    for (unsigned i = 0; i < total_rows; i++)
1876
0
      if (!(delta_rows[i].resize (num_cols))) return false;
1877
0
1878
0
    /* old VarIdxes -> full encoding_row mapping */
1879
0
    hb_hashmap_t<unsigned, const hb_vector_t<int>*> front_mapping;
1880
0
    unsigned start_row = 0;
1881
0
    hb_vector_t<delta_row_encoding_t> encoding_objs;
1882
0
1883
0
    /* delta_rows map, used for filtering out duplicate rows */
1884
0
    hb_vector_t<const hb_vector_t<int> *> major_rows;
1885
0
    hb_hashmap_t<const hb_vector_t<int>*, unsigned> delta_rows_map;
1886
0
    for (unsigned major = 0; major < vars.length; major++)
1887
0
    {
1888
0
      /* deltas are stored in tuples(column based), convert them back into items
1889
0
       * (row based) delta */
1890
0
      const tuple_variations_t& tuples = vars[major];
1891
0
      unsigned num_rows = var_data_num_rows[major];
1892
0
1893
0
      if (!num_rows) continue;
1894
0
1895
0
      for (const tuple_delta_t& tuple: tuples.tuple_vars)
1896
0
      {
1897
0
        if (tuple.deltas_x.length != num_rows)
1898
0
          return false;
1899
0
1900
0
        /* skip unused regions */
1901
0
        unsigned *col_idx;
1902
0
        if (!region_map.has (&(tuple.axis_tuples), &col_idx))
1903
0
          continue;
1904
0
1905
0
        for (unsigned i = 0; i < num_rows; i++)
1906
0
        {
1907
0
          int rounded_delta = roundf (tuple.deltas_x[i]);
1908
0
          delta_rows[start_row + i][*col_idx] += rounded_delta;
1909
0
          has_long |= rounded_delta < -65536 || rounded_delta > 65535;
1910
0
        }
1911
0
      }
1912
0
1913
0
      major_rows.reset ();
1914
0
      for (unsigned minor = 0; minor < num_rows; minor++)
1915
0
      {
1916
0
  const hb_vector_t<int>& row = delta_rows[start_row + minor];
1917
0
  if (use_no_variation_idx)
1918
0
  {
1919
0
    bool all_zeros = true;
1920
0
    for (int delta : row)
1921
0
    {
1922
0
      if (delta != 0)
1923
0
      {
1924
0
        all_zeros = false;
1925
0
        break;
1926
0
      }
1927
0
    }
1928
0
    if (all_zeros)
1929
0
      continue;
1930
0
  }
1931
0
1932
0
  if (!front_mapping.set ((major<<16) + minor, &row))
1933
0
    return false;
1934
0
1935
0
  if (delta_rows_map.has (&row))
1936
0
    continue;
1937
0
1938
0
  delta_rows_map.set (&row, 1);
1939
0
1940
0
  major_rows.push (&row);
1941
0
      }
1942
0
1943
0
      if (major_rows)
1944
0
  encoding_objs.push (delta_row_encoding_t (std::move (major_rows), num_cols));
1945
0
1946
0
      start_row += num_rows;
1947
0
    }
1948
0
1949
0
    /* return directly if no optimization, maintain original VariationIndex so
1950
0
     * varidx_map would be empty */
1951
0
    if (!optimize)
1952
0
    {
1953
0
      encodings = std::move (encoding_objs);
1954
0
      return !encodings.in_error ();
1955
0
    }
1956
0
1957
0
    /* NOTE: Fonttools instancer always optimizes VarStore from scratch. This
1958
0
     * is too costly for large fonts. So, instead, we retain the encodings of
1959
0
     * the original VarStore, and just try to combine them if possible. This
1960
0
     * is a compromise between optimization and performance and practically
1961
0
     * works very well. */
1962
0
1963
0
    // This produces slightly smaller results in some cases.
1964
0
    encoding_objs.qsort ();
1965
0
1966
0
    /* main algorithm: repeatedly pick 2 best encodings to combine, and combine them */
1967
0
    using item_t = hb_priority_queue_t<combined_gain_idx_tuple_t>::item_t;
1968
0
    hb_vector_t<item_t> queue_items;
1969
0
    unsigned num_todos = encoding_objs.length;
1970
0
    for (unsigned i = 0; i < num_todos; i++)
1971
0
    {
1972
0
      for (unsigned j = i + 1; j < num_todos; j++)
1973
0
      {
1974
0
        int combining_gain = encoding_objs.arrayZ[i].gain_from_merging (encoding_objs.arrayZ[j]);
1975
0
        if (combining_gain > 0)
1976
0
          queue_items.push (item_t (combined_gain_idx_tuple_t (combining_gain, i, j), 0));
1977
0
      }
1978
0
    }
1979
0
1980
0
    hb_priority_queue_t<combined_gain_idx_tuple_t> queue (std::move (queue_items));
1981
0
1982
0
    hb_bit_set_t removed_todo_idxes;
1983
0
    while (queue)
1984
0
    {
1985
0
      auto t = queue.pop_minimum ().first;
1986
0
      unsigned i = t.idx_1 ();
1987
0
      unsigned j = t.idx_2 ();
1988
0
1989
0
      if (removed_todo_idxes.has (i) || removed_todo_idxes.has (j))
1990
0
        continue;
1991
0
1992
0
      delta_row_encoding_t& encoding = encoding_objs.arrayZ[i];
1993
0
      delta_row_encoding_t& other_encoding = encoding_objs.arrayZ[j];
1994
0
1995
0
      removed_todo_idxes.add (i);
1996
0
      removed_todo_idxes.add (j);
1997
0
1998
0
      encoding.merge (other_encoding);
1999
0
2000
0
      for (unsigned idx = 0; idx < encoding_objs.length; idx++)
2001
0
      {
2002
0
        if (removed_todo_idxes.has (idx)) continue;
2003
0
2004
0
        const delta_row_encoding_t& obj = encoding_objs.arrayZ[idx];
2005
0
  // In the unlikely event that the same encoding exists already, combine it.
2006
0
        if (obj.width == encoding.width && obj.chars == encoding.chars)
2007
0
        {
2008
0
    // This is straight port from fonttools algorithm. I added this branch there
2009
0
    // because I thought it can happen. But looks like we never get in here in
2010
0
    // practice. I'm not confident enough to remove it though; in theory it can
2011
0
    // happen. I think it's just that our tests are not extensive enough to hit
2012
0
    // this path.
2013
0
2014
0
          for (const auto& row : obj.items)
2015
0
            encoding.add_row (row);
2016
0
2017
0
          removed_todo_idxes.add (idx);
2018
0
          continue;
2019
0
        }
2020
0
2021
0
        int combined_gain = encoding.gain_from_merging (obj);
2022
0
        if (combined_gain > 0)
2023
0
          queue.insert (combined_gain_idx_tuple_t (combined_gain, idx, encoding_objs.length), 0);
2024
0
      }
2025
0
2026
0
      auto moved_encoding = std::move (encoding);
2027
0
      encoding_objs.push (moved_encoding);
2028
0
    }
2029
0
2030
0
    int num_final_encodings = (int) encoding_objs.length - (int) removed_todo_idxes.get_population ();
2031
0
    if (num_final_encodings <= 0) return false;
2032
0
2033
0
    if (!encodings.alloc (num_final_encodings)) return false;
2034
0
    for (unsigned i = 0; i < encoding_objs.length; i++)
2035
0
    {
2036
0
      if (removed_todo_idxes.has (i)) continue;
2037
0
      encodings.push (std::move (encoding_objs.arrayZ[i]));
2038
0
    }
2039
0
2040
0
    return compile_varidx_map (front_mapping);
2041
0
  }
2042
2043
  private:
2044
  /* compile varidx_map for one VarData subtable (index specified by major) */
2045
  bool compile_varidx_map (const hb_hashmap_t<unsigned, const hb_vector_t<int>*>& front_mapping)
2046
0
  {
2047
0
    /* full encoding_row -> new VarIdxes mapping */
2048
0
    hb_hashmap_t<const hb_vector_t<int>*, unsigned> back_mapping;
2049
0
2050
0
    for (unsigned major = 0; major < encodings.length; major++)
2051
0
    {
2052
0
      delta_row_encoding_t& encoding = encodings[major];
2053
0
      /* just sanity check, this shouldn't happen */
2054
0
      if (encoding.is_empty ())
2055
0
        return false;
2056
0
2057
0
      unsigned num_rows = encoding.items.length;
2058
0
2059
0
      /* sort rows, make result deterministic */
2060
0
      encoding.items.qsort (_cmp_row);
2061
0
2062
0
      /* compile old to new var_idxes mapping */
2063
0
      for (unsigned minor = 0; minor < num_rows; minor++)
2064
0
      {
2065
0
        unsigned new_varidx = (major << 16) + minor;
2066
0
        back_mapping.set (encoding.items.arrayZ[minor], new_varidx);
2067
0
      }
2068
0
    }
2069
0
2070
0
    for (auto _ : front_mapping.iter ())
2071
0
    {
2072
0
      unsigned old_varidx = _.first;
2073
0
      unsigned *new_varidx;
2074
0
      if (back_mapping.has (_.second, &new_varidx))
2075
0
        varidx_map.set (old_varidx, *new_varidx);
2076
0
      else
2077
0
        varidx_map.set (old_varidx, HB_OT_LAYOUT_NO_VARIATIONS_INDEX);
2078
0
    }
2079
0
    return !varidx_map.in_error ();
2080
0
  }
2081
2082
  static int _cmp_row (const void *pa, const void *pb)
2083
0
  {
2084
0
    /* compare pointers of vectors(const hb_vector_t<int>*) that represent a row */
2085
0
    const hb_vector_t<int>** a = (const hb_vector_t<int>**) pa;
2086
0
    const hb_vector_t<int>** b = (const hb_vector_t<int>**) pb;
2087
0
2088
0
    for (unsigned i = 0; i < (*b)->length; i++)
2089
0
    {
2090
0
      int va = (*a)->arrayZ[i];
2091
0
      int vb = (*b)->arrayZ[i];
2092
0
      if (va != vb)
2093
0
        return va < vb ? -1 : 1;
2094
0
    }
2095
0
    return 0;
2096
0
  }
2097
};
2098
2099
2100
} /* namespace OT */
2101
2102
2103
#endif /* HB_OT_VAR_COMMON_HH */