2 * Copyright © 2017 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_OT_VAR_MVAR_TABLE_HH
28 #define HB_OT_VAR_MVAR_TABLE_HH
30 #include "hb-ot-var-common.hh"
36 struct VariationValueRecord
38 bool sanitize (hb_sanitize_context_t *c) const
40 TRACE_SANITIZE (this);
41 return_trace (c->check_struct (this));
44 bool subset (hb_subset_context_t *c,
45 const hb_map_t& varidx_map) const
48 auto *out = c->serializer->embed (*this);
49 if (unlikely (!out)) return_trace (false);
51 hb_codepoint_t *new_idx;
52 return_trace (c->serializer->check_assign (out->varIdx,
53 (varidx_map.has (varIdx, &new_idx)) ? *new_idx : HB_OT_LAYOUT_NO_VARIATIONS_INDEX,
54 HB_SERIALIZE_ERROR_INT_OVERFLOW));
58 Tag valueTag; /* Four-byte tag identifying a font-wide measure. */
59 VarIdx varIdx; /* Outer/inner index into VariationStore item. */
62 DEFINE_SIZE_STATIC (8);
67 * MVAR -- Metrics Variations
68 * https://docs.microsoft.com/en-us/typography/opentype/spec/mvar
70 #define HB_OT_TAG_MVAR HB_TAG('M','V','A','R')
74 static constexpr hb_tag_t tableTag = HB_OT_TAG_MVAR;
76 bool sanitize (hb_sanitize_context_t *c) const
78 TRACE_SANITIZE (this);
79 return_trace (version.sanitize (c) &&
80 likely (version.major == 1) &&
81 c->check_struct (this) &&
82 valueRecordSize >= VariationValueRecord::static_size &&
83 varStore.sanitize (c, this) &&
84 c->check_range (valuesZ.arrayZ,
89 bool subset (hb_subset_context_t *c) const
96 if (c->plan->all_axes_pinned)
99 MVAR *out = c->serializer->start_embed (*this);
100 if (unlikely (!c->serializer->extend_min (out))) return_trace (false);
101 out->version = version;
102 out->reserved = reserved;
103 out->valueRecordSize = valueRecordSize;
104 out->valueRecordCount = valueRecordCount;
106 item_variations_t item_vars;
107 const VariationStore& src_var_store = this+varStore;
109 if (!item_vars.instantiate (src_var_store, c->plan))
110 return_trace (false);
112 /* serialize varstore */
113 if (!out->varStore.serialize_serialize (c->serializer, item_vars.has_long_word (),
115 item_vars.get_region_list (),
116 item_vars.get_vardata_encodings ()))
117 return_trace (false);
119 /* serialize value records array */
120 unsigned value_rec_count = valueRecordCount;
121 const VariationValueRecord *record = reinterpret_cast<const VariationValueRecord*> (valuesZ.arrayZ);
122 for (unsigned i = 0; i < value_rec_count; i++)
124 if (!record->subset (c, item_vars.get_varidx_map ())) return_trace (false);
130 float get_var (hb_tag_t tag,
131 const int *coords, unsigned int coord_count) const
133 const VariationValueRecord *record;
134 record = (VariationValueRecord *) hb_bsearch (tag,
135 (const VariationValueRecord *)
136 (const HBUINT8 *) valuesZ,
137 valueRecordCount, valueRecordSize,
142 return (this+varStore).get_delta (record->varIdx, coords, coord_count);
146 static int tag_compare (const void *pa, const void *pb)
148 const hb_tag_t *a = (const hb_tag_t *) pa;
149 const Tag *b = (const Tag *) pb;
154 FixedVersion<>version; /* Version of the metrics variation table
155 * initially set to 0x00010000u */
156 HBUINT16 reserved; /* Not used; set to 0. */
157 HBUINT16 valueRecordSize;/* The size in bytes of each value record —
158 * must be greater than zero. */
159 HBUINT16 valueRecordCount;/* The number of value records — may be zero. */
160 Offset16To<VariationStore>
161 varStore; /* Offset to item variation store table. */
162 UnsizedArrayOf<HBUINT8>
163 valuesZ; /* Array of value records. The records must be
164 * in binary order of their valueTag field. */
167 DEFINE_SIZE_ARRAY (12, valuesZ);
173 #define HB_ADD_MVAR_VAR(tag, field) \
174 c->serializer->check_assign (table->field, \
175 roundf (table->field + \
177 c->plan->normalized_coords.arrayZ, \
178 c->plan->normalized_coords.length)), \
179 HB_SERIALIZE_ERROR_INT_OVERFLOW)
182 #endif /* HB_OT_VAR_MVAR_TABLE_HH */