1 /* 2 * Copyright © 2018 Ebrahim Byagowi 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 #ifndef HB_AAT_LAYOUT_FEAT_TABLE_HH 26 #define HB_AAT_LAYOUT_FEAT_TABLE_HH 27 28 #include "hb-aat-layout-common.hh" 29 30 /* 31 * feat -- Feature Name 32 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6feat.html 33 */ 34 #define HB_AAT_TAG_feat HB_TAG('f','e','a','t') 35 36 37 namespace AAT { 38 39 40 struct SettingName 41 { 42 friend struct FeatureName; 43 cmpAAT::SettingName44 int cmp (hb_aat_layout_feature_selector_t key) const 45 { return (int) key - (int) setting; } 46 get_selectorAAT::SettingName47 hb_aat_layout_feature_selector_t get_selector () const 48 { return (hb_aat_layout_feature_selector_t) (unsigned) setting; } 49 get_infoAAT::SettingName50 void get_info (hb_aat_layout_feature_selector_info_t *s, 51 hb_aat_layout_feature_selector_t default_selector) const 52 { 53 s->name_id = nameIndex; 54 55 s->enable = (hb_aat_layout_feature_selector_t) (unsigned int) setting; 56 s->disable = default_selector == HB_AAT_LAYOUT_FEATURE_SELECTOR_INVALID ? 57 (hb_aat_layout_feature_selector_t) (s->enable + 1) : 58 default_selector; 59 60 s->reserved = 0; 61 } 62 sanitizeAAT::SettingName63 bool sanitize (hb_sanitize_context_t *c) const 64 { 65 TRACE_SANITIZE (this); 66 return_trace (likely (c->check_struct (this))); 67 } 68 69 protected: 70 HBUINT16 setting; /* The setting. */ 71 NameID nameIndex; /* The name table index for the setting's name. */ 72 public: 73 DEFINE_SIZE_STATIC (4); 74 }; 75 DECLARE_NULL_NAMESPACE_BYTES (AAT, SettingName); 76 77 struct feat; 78 79 struct FeatureName 80 { cmpAAT::FeatureName81 int cmp (hb_aat_layout_feature_type_t key) const 82 { return (int) key - (int) feature; } 83 84 enum { 85 Exclusive = 0x8000, /* If set, the feature settings are mutually exclusive. */ 86 NotDefault = 0x4000, /* If clear, then the setting with an index of 0 in 87 * the setting name array for this feature should 88 * be taken as the default for the feature 89 * (if one is required). If set, then bits 0-15 of this 90 * featureFlags field contain the index of the setting 91 * which is to be taken as the default. */ 92 IndexMask = 0x00FF /* If bits 30 and 31 are set, then these sixteen bits 93 * indicate the index of the setting in the setting name 94 * array for this feature which should be taken 95 * as the default. */ 96 }; 97 get_selector_infosAAT::FeatureName98 unsigned int get_selector_infos (unsigned int start_offset, 99 unsigned int *selectors_count, /* IN/OUT. May be NULL. */ 100 hb_aat_layout_feature_selector_info_t *selectors, /* OUT. May be NULL. */ 101 unsigned int *pdefault_index, /* OUT. May be NULL. */ 102 const void *base) const 103 { 104 hb_array_t< const SettingName> settings_table = (base+settingTableZ).as_array (nSettings); 105 106 static_assert (Index::NOT_FOUND_INDEX == HB_AAT_LAYOUT_NO_SELECTOR_INDEX, ""); 107 108 hb_aat_layout_feature_selector_t default_selector = HB_AAT_LAYOUT_FEATURE_SELECTOR_INVALID; 109 unsigned int default_index = Index::NOT_FOUND_INDEX; 110 if (featureFlags & Exclusive) 111 { 112 default_index = (featureFlags & NotDefault) ? featureFlags & IndexMask : 0; 113 default_selector = settings_table[default_index].get_selector (); 114 } 115 if (pdefault_index) 116 *pdefault_index = default_index; 117 118 if (selectors_count) 119 { 120 hb_array_t<const SettingName> arr = settings_table.sub_array (start_offset, selectors_count); 121 unsigned int count = arr.len; 122 for (unsigned int i = 0; i < count; i++) 123 settings_table[start_offset + i].get_info (&selectors[i], default_selector); 124 } 125 return settings_table.len; 126 } 127 get_feature_typeAAT::FeatureName128 hb_aat_layout_feature_type_t get_feature_type () const 129 { return (hb_aat_layout_feature_type_t) (unsigned int) feature; } 130 get_feature_name_idAAT::FeatureName131 hb_ot_name_id_t get_feature_name_id () const { return nameIndex; } 132 sanitizeAAT::FeatureName133 bool sanitize (hb_sanitize_context_t *c, const void *base) const 134 { 135 TRACE_SANITIZE (this); 136 return_trace (likely (c->check_struct (this) && 137 (base+settingTableZ).sanitize (c, nSettings))); 138 } 139 140 protected: 141 HBUINT16 feature; /* Feature type. */ 142 HBUINT16 nSettings; /* The number of records in the setting name array. */ 143 LOffsetTo<UnsizedArrayOf<SettingName>, false> 144 settingTableZ; /* Offset in bytes from the beginning of this table to 145 * this feature's setting name array. The actual type of 146 * record this offset refers to will depend on the 147 * exclusivity value, as described below. */ 148 HBUINT16 featureFlags; /* Single-bit flags associated with the feature type. */ 149 HBINT16 nameIndex; /* The name table index for the feature's name. 150 * This index has values greater than 255 and 151 * less than 32768. */ 152 public: 153 DEFINE_SIZE_STATIC (12); 154 }; 155 156 struct feat 157 { 158 enum { tableTag = HB_AAT_TAG_feat }; 159 has_dataAAT::feat160 bool has_data () const { return version.to_int (); } 161 get_feature_typesAAT::feat162 unsigned int get_feature_types (unsigned int start_offset, 163 unsigned int *count, 164 hb_aat_layout_feature_type_t *features) const 165 { 166 unsigned int feature_count = featureNameCount; 167 if (count && *count) 168 { 169 unsigned int len = MIN (feature_count - start_offset, *count); 170 for (unsigned int i = 0; i < len; i++) 171 features[i] = namesZ[i + start_offset].get_feature_type (); 172 *count = len; 173 } 174 return featureNameCount; 175 } 176 get_featureAAT::feat177 const FeatureName& get_feature (hb_aat_layout_feature_type_t feature_type) const 178 { 179 return namesZ.bsearch (featureNameCount, feature_type); 180 } 181 get_feature_name_idAAT::feat182 hb_ot_name_id_t get_feature_name_id (hb_aat_layout_feature_type_t feature) const 183 { return get_feature (feature).get_feature_name_id (); } 184 get_selector_infosAAT::feat185 unsigned int get_selector_infos (hb_aat_layout_feature_type_t feature_type, 186 unsigned int start_offset, 187 unsigned int *selectors_count, /* IN/OUT. May be NULL. */ 188 hb_aat_layout_feature_selector_info_t *selectors, /* OUT. May be NULL. */ 189 unsigned int *default_index /* OUT. May be NULL. */) const 190 { 191 return get_feature (feature_type).get_selector_infos (start_offset, selectors_count, selectors, 192 default_index, this); 193 } 194 sanitizeAAT::feat195 bool sanitize (hb_sanitize_context_t *c) const 196 { 197 TRACE_SANITIZE (this); 198 return_trace (likely (c->check_struct (this) && 199 version.major == 1 && 200 namesZ.sanitize (c, featureNameCount, this))); 201 } 202 203 protected: 204 FixedVersion<>version; /* Version number of the feature name table 205 * (0x00010000 for the current version). */ 206 HBUINT16 featureNameCount; 207 /* The number of entries in the feature name array. */ 208 HBUINT16 reserved1; /* Reserved (set to zero). */ 209 HBUINT32 reserved2; /* Reserved (set to zero). */ 210 SortedUnsizedArrayOf<FeatureName> 211 namesZ; /* The feature name array. */ 212 public: 213 DEFINE_SIZE_STATIC (24); 214 }; 215 216 } /* namespace AAT */ 217 218 #endif /* HB_AAT_LAYOUT_FEAT_TABLE_HH */ 219