summaryrefslogtreecommitdiffstats
path: root/gfx/ots/src/stat.h
blob: 5fd710856425a928579e7ae6b4527e34faea6274 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright (c) 2018 The OTS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef OTS_STAT_H_
#define OTS_STAT_H_

#include <vector>

#include "ots.h"

namespace ots {

// -----------------------------------------------------------------------------
// OpenTypeSTAT Interface
// -----------------------------------------------------------------------------

class OpenTypeSTAT : public Table {
 public:
  explicit OpenTypeSTAT(Font* font, uint32_t tag)
      : Table(font, tag, tag) { }

  bool Parse(const uint8_t* data, size_t length);
  bool Serialize(OTSStream* out);

 private:
  bool ValidateNameId(uint16_t nameid);

  uint16_t majorVersion;
  uint16_t minorVersion;
  uint16_t designAxisSize;
  uint16_t designAxisCount;
  uint32_t designAxesOffset;
  uint16_t axisValueCount;
  uint32_t offsetToAxisValueOffsets;
  uint16_t elidedFallbackNameID;

  struct AxisRecord {
    uint32_t axisTag;
    uint16_t axisNameID;
    uint16_t axisOrdering;
  };
  std::vector<AxisRecord> designAxes;

  typedef int32_t Fixed; /* 16.16 fixed-point value */

  struct AxisValueFormat1 {
    uint16_t axisIndex;
    uint16_t flags;
    uint16_t valueNameID;
    Fixed    value;
    static size_t Length() {
      return 3 * sizeof(uint16_t) + sizeof(Fixed);
    }
  };

  struct AxisValueFormat2 {
    uint16_t axisIndex;
    uint16_t flags;
    uint16_t valueNameID;
    Fixed    nominalValue;
    Fixed    rangeMinValue;
    Fixed    rangeMaxValue;
    static size_t Length() {
      return 3 * sizeof(uint16_t) + 3 * sizeof(Fixed);
    }
  };

  struct AxisValueFormat3 {
    uint16_t axisIndex;
    uint16_t flags;
    uint16_t valueNameID;
    Fixed    value;
    Fixed    linkedValue;
    static size_t Length() {
      return 3 * sizeof(uint16_t) + 2 * sizeof(Fixed);
    }
  };

  struct AxisValueFormat4 {
    uint16_t axisCount;
    uint16_t flags;
    uint16_t valueNameID;
    struct AxisValue {
      uint16_t axisIndex;
      Fixed    value;
    };
    std::vector<AxisValue> axisValues;
    size_t Length() const {
      return 3 * sizeof(uint16_t) + axisValues.size() * (sizeof(uint16_t) + sizeof(Fixed));
    }
  };

  struct AxisValueRecord {
    uint16_t format;
    union {
      AxisValueFormat1 format1;
      AxisValueFormat2 format2;
      AxisValueFormat3 format3;
      AxisValueFormat4 format4;
    };
    explicit AxisValueRecord(uint16_t format_)
      : format(format_)
    {
      if (format == 4) {
        new (&this->format4) AxisValueFormat4();
      }
    }
    AxisValueRecord(const AxisValueRecord& other_)
      : format(other_.format)
    {
      switch (format) {
      case 1:
        format1 = other_.format1;
        break;
      case 2:
        format2 = other_.format2;
        break;
      case 3:
        format3 = other_.format3;
        break;
      case 4:
        new (&this->format4) AxisValueFormat4();
        format4 = other_.format4;
        break;
      }
    }
    ~AxisValueRecord() {
      if (format == 4) {
        this->format4.~AxisValueFormat4();
      }
    }
    uint32_t Length() const {
      switch (format) {
      case 1:
        return sizeof(uint16_t) + format1.Length();
      case 2:
        return sizeof(uint16_t) + format2.Length();
      case 3:
        return sizeof(uint16_t) + format3.Length();
      case 4:
        return sizeof(uint16_t) + format4.Length();
      default:
        // can't happen
        return 0;
      }
    }
  };

  std::vector<AxisValueRecord> axisValues;
};

}  // namespace ots

#endif  // OTS_STAT_H_