Coverage Report

Created: 2026-10-30 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/OT/glyf/CompositeGlyph.hh
Line
Count
Source
1
#ifndef OT_GLYF_COMPOSITEGLYPH_HH
2
#define OT_GLYF_COMPOSITEGLYPH_HH
3
4
5
#include "../../hb-open-type.hh"
6
#include "composite-iter.hh"
7
8
9
namespace OT {
10
namespace glyf_impl {
11
12
13
struct CompositeGlyphRecord
14
{
15
  protected:
16
  enum composite_glyph_flag_t
17
  {
18
    ARG_1_AND_2_ARE_WORDS = 0x0001,
19
    ARGS_ARE_XY_VALUES    = 0x0002,
20
    ROUND_XY_TO_GRID    = 0x0004,
21
    WE_HAVE_A_SCALE   = 0x0008,
22
    MORE_COMPONENTS   = 0x0020,
23
    WE_HAVE_AN_X_AND_Y_SCALE  = 0x0040,
24
    WE_HAVE_A_TWO_BY_TWO  = 0x0080,
25
    WE_HAVE_INSTRUCTIONS  = 0x0100,
26
    USE_MY_METRICS    = 0x0200,
27
    OVERLAP_COMPOUND    = 0x0400,
28
    SCALED_COMPONENT_OFFSET = 0x0800,
29
    UNSCALED_COMPONENT_OFFSET = 0x1000,
30
#ifndef HB_NO_BEYOND_64K
31
    GID_IS_24BIT    = 0x2000
32
#endif
33
  };
34
35
  public:
36
  size_t get_size (bool extended) const
37
0
  {
38
0
    unsigned int size = min_size;
39
    /* glyphIndex is 24bit instead of 16bit */
40
0
#ifndef HB_NO_BEYOND_64K
41
0
    if (extended && (flags & GID_IS_24BIT)) size += HBGlyphID24::static_size - HBGlyphID16::static_size;
42
0
#endif
43
    /* arg1 and 2 are int16 */
44
0
    if (flags & ARG_1_AND_2_ARE_WORDS) size += 4;
45
    /* arg1 and 2 are int8 */
46
0
    else size += 2;
47
48
    /* One x 16 bit (scale) */
49
0
    if (flags & WE_HAVE_A_SCALE) size += 2;
50
    /* Two x 16 bit (xscale, yscale) */
51
0
    else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) size += 4;
52
    /* Four x 16 bit (xscale, scale01, scale10, yscale) */
53
0
    else if (flags & WE_HAVE_A_TWO_BY_TWO) size += 8;
54
55
0
    return size;
56
0
  }
57
58
0
  void drop_instructions_flag ()  { flags = (uint16_t) flags & ~WE_HAVE_INSTRUCTIONS; }
59
  void set_overlaps_flag ()
60
0
  {
61
0
    flags = (uint16_t) flags | OVERLAP_COMPOUND;
62
0
  }
63
64
0
  bool has_instructions ()  const { return   flags & WE_HAVE_INSTRUCTIONS; }
65
66
0
  bool has_more ()          const { return   flags & MORE_COMPONENTS; }
67
0
  bool is_use_my_metrics () const { return   flags & USE_MY_METRICS; }
68
0
  bool is_anchored ()       const { return !(flags & ARGS_ARE_XY_VALUES); }
69
  void get_anchor_points (unsigned int &point1, unsigned int &point2, bool extended) const
70
0
  {
71
0
    const auto *p = &StructAfter<const HBUINT8> (flags);
72
0
#ifndef HB_NO_BEYOND_64K
73
0
    if (extended && (flags & GID_IS_24BIT))
74
0
      p += HBGlyphID24::static_size;
75
0
    else
76
0
#endif
77
0
      p += HBGlyphID16::static_size;
78
0
    if (flags & ARG_1_AND_2_ARE_WORDS)
79
0
    {
80
0
      point1 = ((const HBUINT16 *) p)[0];
81
0
      point2 = ((const HBUINT16 *) p)[1];
82
0
    }
83
0
    else
84
0
    {
85
0
      point1 = p[0];
86
0
      point2 = p[1];
87
0
    }
88
0
  }
89
90
  static void transform (const float (&matrix)[4],
91
       hb_array_t<contour_point_t> points)
92
0
  {
93
0
    if (matrix[0] != 1.f || matrix[1] != 0.f ||
94
0
  matrix[2] != 0.f || matrix[3] != 1.f)
95
0
      for (auto &point : points)
96
0
        point.transform (matrix);
97
0
  }
98
99
  static void translate (const contour_point_t &trans,
100
       hb_array_t<contour_point_t> points)
101
0
  {
102
0
    if (HB_OPTIMIZE_SIZE_VAL)
103
0
    {
104
0
      if (trans.x != 0.f || trans.y != 0.f)
105
0
        for (auto &point : points)
106
0
    point.translate (trans);
107
0
    }
108
0
    else
109
0
    {
110
0
      if (trans.x != 0.f && trans.y != 0.f)
111
0
        for (auto &point : points)
112
0
    point.translate (trans);
113
0
      else
114
0
      {
115
0
  if (trans.x != 0.f)
116
0
    for (auto &point : points)
117
0
      point.x += trans.x;
118
0
  else if (trans.y != 0.f)
119
0
    for (auto &point : points)
120
0
      point.y += trans.y;
121
0
      }
122
0
    }
123
0
  }
124
125
  void transform_points (hb_array_t<contour_point_t> points,
126
       const float (&matrix)[4],
127
       const contour_point_t &trans) const
128
0
  {
129
0
    if (scaled_offsets ())
130
0
    {
131
0
      translate (trans, points);
132
0
      transform (matrix, points);
133
0
    }
134
0
    else
135
0
    {
136
0
      transform (matrix, points);
137
0
      translate (trans, points);
138
0
    }
139
0
  }
140
141
  bool get_points (contour_point_vector_t &points, bool extended) const
142
0
  {
143
0
    float matrix[4];
144
0
    contour_point_t trans;
145
0
    get_transformation (matrix, trans, extended);
146
0
    if (unlikely (!points.alloc (points.length + 1 + 4))) return false; // For phantom points
147
0
    points.push (trans);
148
0
    return true;
149
0
  }
150
151
  unsigned compile_with_point (const contour_point_t &point,
152
                               char *out,
153
             bool extended) const
154
0
  {
155
0
    const HBINT8 *p = &StructAfter<const HBINT8> (flags);
156
0
#ifndef HB_NO_BEYOND_64K
157
0
    if (extended && (flags & GID_IS_24BIT))
158
0
      p += HBGlyphID24::static_size;
159
0
    else
160
0
#endif
161
0
      p += HBGlyphID16::static_size;
162
0
163
0
    unsigned len = get_size (extended);
164
0
    unsigned len_before_val = (const char *)p - (const char *)this;
165
0
    if (flags & ARG_1_AND_2_ARE_WORDS)
166
0
    {
167
0
      // no overflow, copy value
168
0
      hb_memcpy (out, this, len);
169
0
170
0
      HBINT16 *o = reinterpret_cast<HBINT16 *> (out + len_before_val);
171
0
      o[0] = roundf (point.x);
172
0
      o[1] = roundf (point.y);
173
0
    }
174
0
    else
175
0
    {
176
0
      int new_x = roundf (point.x);
177
0
      int new_y = roundf (point.y);
178
0
      if (new_x <= 127 && new_x >= -128 &&
179
0
          new_y <= 127 && new_y >= -128)
180
0
      {
181
0
        hb_memcpy (out, this, len);
182
0
        HBINT8 *o = reinterpret_cast<HBINT8 *> (out + len_before_val);
183
0
        o[0] = new_x;
184
0
        o[1] = new_y;
185
0
      }
186
0
      else
187
0
      {
188
0
        // new point value has an int8 overflow
189
0
        hb_memcpy (out, this, len_before_val);
190
0
        
191
0
        //update flags
192
0
        CompositeGlyphRecord *o = reinterpret_cast<CompositeGlyphRecord *> (out);
193
0
        o->flags = flags | ARG_1_AND_2_ARE_WORDS;
194
0
        out += len_before_val;
195
0
196
0
        HBINT16 new_value;
197
0
        new_value = new_x;
198
0
        hb_memcpy (out, &new_value, HBINT16::static_size);
199
0
        out += HBINT16::static_size;
200
0
201
0
        new_value = new_y;
202
0
        hb_memcpy (out, &new_value, HBINT16::static_size);
203
0
        out += HBINT16::static_size;
204
0
205
0
        hb_memcpy (out, p+2, len - len_before_val - 2);
206
0
        len += 2;
207
0
      }
208
0
    }
209
0
    return len;
210
0
  }
211
212
  protected:
213
  bool scaled_offsets () const
214
0
  { return (flags & (SCALED_COMPONENT_OFFSET | UNSCALED_COMPONENT_OFFSET)) == SCALED_COMPONENT_OFFSET; }
215
216
  public:
217
  bool get_transformation (float (&matrix)[4], contour_point_t &trans, bool extended) const
218
0
  {
219
0
    matrix[0] = matrix[3] = 1.f;
220
0
    matrix[1] = matrix[2] = 0.f;
221
222
0
    const auto *p = &StructAfter<const HBINT8> (flags);
223
0
#ifndef HB_NO_BEYOND_64K
224
0
    if (extended && (flags & GID_IS_24BIT))
225
0
      p += HBGlyphID24::static_size;
226
0
    else
227
0
#endif
228
0
      p += HBGlyphID16::static_size;
229
0
    int tx, ty;
230
0
    if (flags & ARG_1_AND_2_ARE_WORDS)
231
0
    {
232
0
      tx = *(const HBINT16 *) p;
233
0
      p += HBINT16::static_size;
234
0
      ty = *(const HBINT16 *) p;
235
0
      p += HBINT16::static_size;
236
0
    }
237
0
    else
238
0
    {
239
0
      tx = *p++;
240
0
      ty = *p++;
241
0
    }
242
0
    if (is_anchored ()) tx = ty = 0;
243
244
    /* set is_end_point flag to true, used by IUP delta optimization */
245
0
    trans.init ((float) tx, (float) ty, true);
246
247
0
    {
248
0
      const F2DOT14 *points = (const F2DOT14 *) p;
249
0
      if (flags & WE_HAVE_A_SCALE)
250
0
      {
251
0
  matrix[0] = matrix[3] = points[0].to_float ();
252
0
  return true;
253
0
      }
254
0
      else if (flags & WE_HAVE_AN_X_AND_Y_SCALE)
255
0
      {
256
0
  matrix[0] = points[0].to_float ();
257
0
  matrix[3] = points[1].to_float ();
258
0
  return true;
259
0
      }
260
0
      else if (flags & WE_HAVE_A_TWO_BY_TWO)
261
0
      {
262
0
  matrix[0] = points[0].to_float ();
263
0
  matrix[1] = points[1].to_float ();
264
0
  matrix[2] = points[2].to_float ();
265
0
  matrix[3] = points[3].to_float ();
266
0
  return true;
267
0
      }
268
0
    }
269
0
    return tx || ty;
270
0
  }
271
272
  hb_codepoint_t get_gid (bool extended) const
273
0
  {
274
0
#ifndef HB_NO_BEYOND_64K
275
0
    if (extended && (flags & GID_IS_24BIT))
276
0
      return StructAfter<const HBGlyphID24> (flags);
277
0
    else
278
0
#endif
279
0
      return StructAfter<const HBGlyphID16> (flags);
280
0
  }
281
  void set_gid (hb_codepoint_t gid, bool extended)
282
0
  {
283
0
#ifndef HB_NO_BEYOND_64K
284
0
    if (extended && (flags & GID_IS_24BIT))
285
0
      StructAfter<HBGlyphID24> (flags) = gid;
286
0
    else
287
0
#endif
288
0
      /* TODO assert? */
289
0
      StructAfter<HBGlyphID16> (flags) = gid;
290
0
  }
291
292
#ifndef HB_NO_BEYOND_64K
293
  void lower_gid_24_to_16 ()
294
0
  {
295
0
    hb_codepoint_t gid = get_gid (true);
296
0
    if (!(flags & GID_IS_24BIT) || gid > 0xFFFFu)
297
0
      return;
298
0
299
0
    /* Lower the flag and move the rest of the struct down. */
300
0
301
0
    unsigned size = get_size (true);
302
0
    char *end = (char *) this + size;
303
0
    char *p = &StructAfter<char> (flags);
304
0
    p += HBGlyphID24::static_size;
305
0
306
0
    flags = flags & ~GID_IS_24BIT;
307
0
    set_gid (gid, true);
308
0
309
0
    memmove (p - HBGlyphID24::static_size + HBGlyphID16::static_size, p, end - p);
310
0
  }
311
#endif
312
313
  protected:
314
  HBUINT16  flags;
315
  HBUINT24  pad;
316
  public:
317
  DEFINE_SIZE_MIN (4);
318
};
319
320
using composite_iter_t = composite_iter_tmpl<CompositeGlyphRecord>;
321
322
struct CompositeGlyph
323
{
324
  const GlyphHeader &header;
325
  hb_bytes_t bytes;
326
  bool extended;
327
  CompositeGlyph (const GlyphHeader &header_, hb_bytes_t bytes_, bool extended_) :
328
0
    header (header_), bytes (bytes_), extended (extended_) {}
329
330
  composite_iter_t iter () const
331
0
  { return composite_iter_t (bytes, &StructAfter<CompositeGlyphRecord, GlyphHeader> (header), extended); }
332
333
  unsigned int instructions_length (hb_bytes_t bytes) const
334
0
  {
335
0
    unsigned int start = bytes.length;
336
0
    unsigned int end = bytes.length;
337
0
    const CompositeGlyphRecord *last = nullptr;
338
0
    for (auto &item : iter ())
339
0
      last = &item;
340
0
    if (unlikely (!last)) return 0;
341
0
342
0
    if (last->has_instructions ())
343
0
      start = (char *) last - &bytes + last->get_size (extended);
344
0
    if (unlikely (start > end)) return 0;
345
0
    return end - start;
346
0
  }
347
348
  /* Trimming for composites not implemented.
349
   * If removing hints it falls out of that. */
350
0
  const hb_bytes_t trim_padding () const { return bytes; }
351
352
  void drop_hints ()
353
0
  {
354
0
    for (const auto &_ : iter ())
355
0
      const_cast<CompositeGlyphRecord &> (_).drop_instructions_flag ();
356
0
  }
357
358
  /* Chop instructions off the end */
359
  void drop_hints_bytes (hb_bytes_t &dest_start) const
360
0
  { dest_start = bytes.sub_array (0, bytes.length - instructions_length (bytes)); }
361
362
  void set_overlaps_flag ()
363
0
  {
364
0
    CompositeGlyphRecord& glyph_chain = const_cast<CompositeGlyphRecord &> (
365
0
  StructAfter<CompositeGlyphRecord, GlyphHeader> (header));
366
0
    if (!bytes.check_range(&glyph_chain, CompositeGlyphRecord::min_size))
367
0
      return;
368
0
    glyph_chain.set_overlaps_flag ();
369
0
  }
370
371
  bool compile_bytes_with_deltas (const hb_bytes_t &source_bytes,
372
                                  const contour_point_vector_t &points_with_deltas,
373
                                  hb_bytes_t &dest_bytes /* OUT */)
374
0
  {
375
0
    if (source_bytes.length <= GlyphHeader::static_size ||
376
0
        header.numberOfContours != -1)
377
0
    {
378
0
      dest_bytes = hb_bytes_t ();
379
0
      return true;
380
0
    }
381
0
382
0
    unsigned source_len = source_bytes.length - GlyphHeader::static_size;
383
0
384
0
    /* try to allocate more memories than source glyph bytes
385
0
     * in case that there might be an overflow for int8 value
386
0
     * and we would need to use int16 instead */
387
0
    char *o = (char *) hb_calloc (source_len * 2, sizeof (char));
388
0
    if (unlikely (!o)) return false;
389
0
390
0
    const CompositeGlyphRecord *c = reinterpret_cast<const CompositeGlyphRecord *> (source_bytes.arrayZ + GlyphHeader::static_size);
391
0
    auto it = composite_iter_t (hb_bytes_t ((const char *)c, source_len), c, extended);
392
0
393
0
    char *p = o;
394
0
    unsigned i = 0, source_comp_len = 0;
395
0
    for (const auto &component : it)
396
0
    {
397
0
      /* last 4 points in points_with_deltas are phantom points and should not be included */
398
0
      if (i >= points_with_deltas.length - 4) {
399
0
        hb_free (o);
400
0
        return false;
401
0
      }
402
0
403
0
      unsigned comp_len = component.get_size (extended);
404
0
      if (component.is_anchored ())
405
0
      {
406
0
        hb_memcpy (p, &component, comp_len);
407
0
        p += comp_len;
408
0
      }
409
0
      else
410
0
      {
411
0
        unsigned new_len = component.compile_with_point (points_with_deltas[i], p, extended);
412
0
        p += new_len;
413
0
      }
414
0
      i++;
415
0
      source_comp_len += comp_len;
416
0
    }
417
0
418
0
    //copy instructions if any
419
0
    if (source_len > source_comp_len)
420
0
    {
421
0
      unsigned instr_len = source_len - source_comp_len;
422
0
      hb_memcpy (p, (const char *)c + source_comp_len, instr_len);
423
0
      p += instr_len;
424
0
    }
425
0
426
0
    unsigned len = p - o;
427
0
    dest_bytes = hb_bytes_t (o, len);
428
0
    return true;
429
0
  }
430
};
431
432
433
} /* namespace glyf_impl */
434
} /* namespace OT */
435
436
437
#endif /* OT_GLYF_COMPOSITEGLYPH_HH */