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 { sanitizeOT::VariationValueRecord38 bool sanitize (hb_sanitize_context_t *c) const 39 { 40 TRACE_SANITIZE (this); 41 return_trace (c->check_struct (this)); 42 } 43 subsetOT::VariationValueRecord44 bool subset (hb_subset_context_t *c, 45 const hb_map_t& varidx_map) const 46 { 47 TRACE_SUBSET (this); 48 auto *out = c->serializer->embed (*this); 49 if (unlikely (!out)) return_trace (false); 50 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)); 55 } 56 57 public: 58 Tag valueTag; /* Four-byte tag identifying a font-wide measure. */ 59 VarIdx varIdx; /* Outer/inner index into VariationStore 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 sanitizeOT::MVAR76 bool sanitize (hb_sanitize_context_t *c) const 77 { 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, 85 valueRecordCount, 86 valueRecordSize)); 87 } 88 subsetOT::MVAR89 bool subset (hb_subset_context_t *c) const 90 { 91 TRACE_SUBSET (this); 92 #ifdef HB_NO_VAR 93 return_trace (false); 94 #endif 95 96 if (c->plan->all_axes_pinned) 97 return_trace (false); 98 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; 105 106 item_variations_t item_vars; 107 const VariationStore& src_var_store = this+varStore; 108 109 if (!item_vars.instantiate (src_var_store, c->plan)) 110 return_trace (false); 111 112 /* serialize varstore */ 113 if (!out->varStore.serialize_serialize (c->serializer, item_vars.has_long_word (), 114 c->plan->axis_tags, 115 item_vars.get_region_list (), 116 item_vars.get_vardata_encodings ())) 117 return_trace (false); 118 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++) 123 { 124 if (!record->subset (c, item_vars.get_varidx_map ())) return_trace (false); 125 record++; 126 } 127 return_trace (true); 128 } 129 get_varOT::MVAR130 float get_var (hb_tag_t tag, 131 const int *coords, unsigned int coord_count) const 132 { 133 const VariationValueRecord *record; 134 record = (VariationValueRecord *) hb_bsearch (tag, 135 (const VariationValueRecord *) 136 (const HBUINT8 *) valuesZ, 137 valueRecordCount, valueRecordSize, 138 tag_compare); 139 if (!record) 140 return 0.; 141 142 return (this+varStore).get_delta (record->varIdx, coords, coord_count); 143 } 144 145 protected: tag_compareOT::MVAR146 static int tag_compare (const void *pa, const void *pb) 147 { 148 const hb_tag_t *a = (const hb_tag_t *) pa; 149 const Tag *b = (const Tag *) pb; 150 return b->cmp (*a); 151 } 152 153 protected: 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. */ 165 166 public: 167 DEFINE_SIZE_ARRAY (12, valuesZ); 168 }; 169 170 } /* namespace OT */ 171 172 173 #define HB_ADD_MVAR_VAR(tag, field) \ 174 c->serializer->check_assign (table->field, \ 175 roundf (table->field + \ 176 MVAR.get_var (tag, \ 177 c->plan->normalized_coords.arrayZ, \ 178 c->plan->normalized_coords.length)), \ 179 HB_SERIALIZE_ERROR_INT_OVERFLOW) 180 181 182 #endif /* HB_OT_VAR_MVAR_TABLE_HH */ 183