stat.h (3596B)
1 // Copyright (c) 2018 The OTS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef OTS_STAT_H_ 6 #define OTS_STAT_H_ 7 8 #include <vector> 9 10 #include "ots.h" 11 12 namespace ots { 13 14 // ----------------------------------------------------------------------------- 15 // OpenTypeSTAT Interface 16 // ----------------------------------------------------------------------------- 17 18 class OpenTypeSTAT : public Table { 19 public: 20 explicit OpenTypeSTAT(Font* font, uint32_t tag) 21 : Table(font, tag, tag) { } 22 23 bool Parse(const uint8_t* data, size_t length); 24 bool Serialize(OTSStream* out); 25 26 private: 27 bool ValidateNameId(uint16_t nameid); 28 29 uint16_t majorVersion; 30 uint16_t minorVersion; 31 uint16_t designAxisSize; 32 uint16_t designAxisCount; 33 uint32_t designAxesOffset; 34 uint16_t axisValueCount; 35 uint32_t offsetToAxisValueOffsets; 36 uint16_t elidedFallbackNameID; 37 38 struct AxisRecord { 39 uint32_t axisTag; 40 uint16_t axisNameID; 41 uint16_t axisOrdering; 42 }; 43 std::vector<AxisRecord> designAxes; 44 45 typedef int32_t Fixed; /* 16.16 fixed-point value */ 46 47 struct AxisValueFormat1 { 48 uint16_t axisIndex; 49 uint16_t flags; 50 uint16_t valueNameID; 51 Fixed value; 52 static size_t Length() { 53 return 3 * sizeof(uint16_t) + sizeof(Fixed); 54 } 55 }; 56 57 struct AxisValueFormat2 { 58 uint16_t axisIndex; 59 uint16_t flags; 60 uint16_t valueNameID; 61 Fixed nominalValue; 62 Fixed rangeMinValue; 63 Fixed rangeMaxValue; 64 static size_t Length() { 65 return 3 * sizeof(uint16_t) + 3 * sizeof(Fixed); 66 } 67 }; 68 69 struct AxisValueFormat3 { 70 uint16_t axisIndex; 71 uint16_t flags; 72 uint16_t valueNameID; 73 Fixed value; 74 Fixed linkedValue; 75 static size_t Length() { 76 return 3 * sizeof(uint16_t) + 2 * sizeof(Fixed); 77 } 78 }; 79 80 struct AxisValueFormat4 { 81 uint16_t axisCount; 82 uint16_t flags; 83 uint16_t valueNameID; 84 struct AxisValue { 85 uint16_t axisIndex; 86 Fixed value; 87 }; 88 std::vector<AxisValue> axisValues; 89 size_t Length() const { 90 return 3 * sizeof(uint16_t) + axisValues.size() * (sizeof(uint16_t) + sizeof(Fixed)); 91 } 92 }; 93 94 struct AxisValueRecord { 95 uint16_t format; 96 union { 97 AxisValueFormat1 format1; 98 AxisValueFormat2 format2; 99 AxisValueFormat3 format3; 100 AxisValueFormat4 format4; 101 }; 102 explicit AxisValueRecord(uint16_t format_) 103 : format(format_) 104 { 105 if (format == 4) { 106 new (&this->format4) AxisValueFormat4(); 107 } 108 } 109 AxisValueRecord(const AxisValueRecord& other_) 110 : format(other_.format) 111 { 112 switch (format) { 113 case 1: 114 format1 = other_.format1; 115 break; 116 case 2: 117 format2 = other_.format2; 118 break; 119 case 3: 120 format3 = other_.format3; 121 break; 122 case 4: 123 new (&this->format4) AxisValueFormat4(); 124 format4 = other_.format4; 125 break; 126 } 127 } 128 ~AxisValueRecord() { 129 if (format == 4) { 130 this->format4.~AxisValueFormat4(); 131 } 132 } 133 uint32_t Length() const { 134 switch (format) { 135 case 1: 136 return sizeof(uint16_t) + format1.Length(); 137 case 2: 138 return sizeof(uint16_t) + format2.Length(); 139 case 3: 140 return sizeof(uint16_t) + format3.Length(); 141 case 4: 142 return sizeof(uint16_t) + format4.Length(); 143 default: 144 // can't happen 145 return 0; 146 } 147 } 148 }; 149 150 std::vector<AxisValueRecord> axisValues; 151 }; 152 153 } // namespace ots 154 155 #endif // OTS_STAT_H_