Coverage Report

Created: 2025-07-11 06:06

/src/harfbuzz/src/hb-ot-var-mvar-table.hh
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright © 2017  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
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#ifndef HB_OT_VAR_MVAR_TABLE_HH
28
#define HB_OT_VAR_MVAR_TABLE_HH
29
30
#include "hb-ot-var-common.hh"
31
32
33
namespace OT {
34
35
36
struct VariationValueRecord
37
{
38
  bool sanitize (hb_sanitize_context_t *c) const
39
0
  {
40
0
    TRACE_SANITIZE (this);
41
0
    return_trace (c->check_struct (this));
42
0
  }
43
44
  bool subset (hb_subset_context_t *c,
45
               const hb_map_t& varidx_map) const
46
0
  {
47
0
    TRACE_SUBSET (this);
48
0
    auto *out = c->serializer->embed (*this);
49
0
    if (unlikely (!out)) return_trace (false);
50
0
51
0
    hb_codepoint_t *new_idx;
52
0
    return_trace (c->serializer->check_assign (out->varIdx,
53
0
                                               (varidx_map.has (varIdx, &new_idx)) ? *new_idx : HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
54
0
                                               HB_SERIALIZE_ERROR_INT_OVERFLOW));
55
0
  }
56
57
  public:
58
  Tag   valueTag; /* Four-byte tag identifying a font-wide measure. */
59
  VarIdx  varIdx;   /* Outer/inner index into ItemVariationStore item. */
60
61
  public:
62
  DEFINE_SIZE_STATIC (8);
63
};
64
65
66
/*
67
 * MVAR -- Metrics Variations
68
 * https://docs.microsoft.com/en-us/typography/opentype/spec/mvar
69
 */
70
#define HB_OT_TAG_MVAR HB_TAG('M','V','A','R')
71
72
struct MVAR
73
{
74
  static constexpr hb_tag_t tableTag = HB_OT_TAG_MVAR;
75
76
  bool sanitize (hb_sanitize_context_t *c) const
77
572
  {
78
572
    TRACE_SANITIZE (this);
79
572
    return_trace (version.sanitize (c) &&
80
572
      hb_barrier () &&
81
572
      likely (version.major == 1) &&
82
572
      c->check_struct (this) &&
83
572
      hb_barrier () &&
84
572
      valueRecordSize >= VariationValueRecord::static_size &&
85
572
      varStore.sanitize (c, this) &&
86
572
      c->check_range (valuesZ.arrayZ,
87
572
          valueRecordCount,
88
572
          valueRecordSize));
89
572
  }
90
91
  bool subset (hb_subset_context_t *c) const
92
0
  {
93
0
    TRACE_SUBSET (this);
94
0
#ifdef HB_NO_VAR
95
0
    return_trace (false);
96
0
#endif
97
0
98
0
    if (c->plan->all_axes_pinned)
99
0
      return_trace (false);
100
0
101
0
    MVAR *out = c->serializer->start_embed (*this);
102
0
    if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
103
0
    out->version = version;
104
0
    out->reserved = reserved;
105
0
    out->valueRecordSize = valueRecordSize;
106
0
    out->valueRecordCount = valueRecordCount;
107
0
108
0
    item_variations_t item_vars;
109
0
    const ItemVariationStore& src_var_store = this+varStore;
110
0
111
0
    if (!item_vars.instantiate (src_var_store, c->plan))
112
0
      return_trace (false);
113
0
114
0
    /* serialize varstore */
115
0
    if (!out->varStore.serialize_serialize (c->serializer, item_vars.has_long_word (),
116
0
                                            c->plan->axis_tags,
117
0
                                            item_vars.get_region_list (),
118
0
                                            item_vars.get_vardata_encodings ()))
119
0
      return_trace (false);
120
0
121
0
    /* serialize value records array */
122
0
    unsigned value_rec_count = valueRecordCount;
123
0
    const VariationValueRecord *record = reinterpret_cast<const VariationValueRecord*> (valuesZ.arrayZ);
124
0
    for (unsigned i = 0; i < value_rec_count; i++)
125
0
    {
126
0
      if (!record->subset (c, item_vars.get_varidx_map ())) return_trace (false);
127
0
      record++;
128
0
    }
129
0
    return_trace (true);
130
0
  }
131
132
  float get_var (hb_tag_t tag,
133
     const int *coords, unsigned int coord_count) const
134
78.8k
  {
135
78.8k
    const VariationValueRecord *record;
136
78.8k
    record = (VariationValueRecord *) hb_bsearch (tag,
137
78.8k
              (const VariationValueRecord *)
138
78.8k
                (const HBUINT8 *) valuesZ,
139
78.8k
              valueRecordCount, valueRecordSize,
140
78.8k
              tag_compare);
141
78.8k
    if (!record)
142
78.1k
      return 0.;
143
144
769
    return (this+varStore).get_delta (record->varIdx, coords, coord_count);
145
78.8k
  }
146
147
protected:
148
  static int tag_compare (const void *pa, const void *pb)
149
10.8k
  {
150
10.8k
    const hb_tag_t *a = (const hb_tag_t *) pa;
151
10.8k
    const Tag *b = (const Tag *) pb;
152
10.8k
    return b->cmp (*a);
153
10.8k
  }
154
155
  protected:
156
  FixedVersion<>version;  /* Version of the metrics variation table
157
         * initially set to 0x00010000u */
158
  HBUINT16  reserved; /* Not used; set to 0. */
159
  HBUINT16  valueRecordSize;/* The size in bytes of each value record —
160
         * must be greater than zero. */
161
  HBUINT16  valueRecordCount;/* The number of value records — may be zero. */
162
  Offset16To<ItemVariationStore>
163
    varStore; /* Offset to item variation store table. */
164
  UnsizedArrayOf<HBUINT8>
165
    valuesZ;  /* Array of value records. The records must be
166
         * in binary order of their valueTag field. */
167
168
  public:
169
  DEFINE_SIZE_ARRAY (12, valuesZ);
170
};
171
172
} /* namespace OT */
173
174
175
#define HB_ADD_MVAR_VAR(tag, field) \
176
       c->serializer->check_assign (table->field, \
177
            roundf (table->field + \
178
              MVAR.get_var (tag, \
179
                c->plan->normalized_coords.arrayZ, \
180
                c->plan->normalized_coords.length)), \
181
            HB_SERIALIZE_ERROR_INT_OVERFLOW)
182
183
184
#endif /* HB_OT_VAR_MVAR_TABLE_HH */