summaryrefslogtreecommitdiffstats
path: root/gfx/ots/src/vorg.cc
blob: 3b4b51c533a138f836dcd22514045cc3f6a4956a (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
// Copyright (c) 2009-2017 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.

#include "vorg.h"

#include <vector>

// VORG - Vertical Origin Table
// http://www.microsoft.com/typography/otspec/vorg.htm

namespace ots {

bool OpenTypeVORG::Parse(const uint8_t *data, size_t length) {
  Buffer table(data, length);

  uint16_t num_recs;
  if (!table.ReadU16(&this->major_version) ||
      !table.ReadU16(&this->minor_version) ||
      !table.ReadS16(&this->default_vert_origin_y) ||
      !table.ReadU16(&num_recs)) {
    return Error("Failed to read header");
  }
  if (this->major_version != 1) {
    return Drop("Unsupported majorVersion: %u", this->major_version);
  }
  if (this->minor_version != 0) {
    return Drop("Unsupported minorVersion: %u", this->minor_version);
  }

  // num_recs might be zero (e.g., DFHSMinchoPro5-W3-Demo.otf).
  if (!num_recs) {
    return true;
  }

  uint16_t last_glyph_index = 0;
  this->metrics.reserve(num_recs);
  for (unsigned i = 0; i < num_recs; ++i) {
    OpenTypeVORGMetrics rec;

    if (!table.ReadU16(&rec.glyph_index) ||
        !table.ReadS16(&rec.vert_origin_y)) {
      return Error("Failed to read record %d", i);
    }
    if ((i != 0) && (rec.glyph_index <= last_glyph_index)) {
      return Drop("The table is not sorted");
    }
    last_glyph_index = rec.glyph_index;

    this->metrics.push_back(rec);
  }

  return true;
}

bool OpenTypeVORG::Serialize(OTSStream *out) {
  const uint16_t num_metrics = static_cast<uint16_t>(this->metrics.size());
  if (num_metrics != this->metrics.size() ||
      !out->WriteU16(this->major_version) ||
      !out->WriteU16(this->minor_version) ||
      !out->WriteS16(this->default_vert_origin_y) ||
      !out->WriteU16(num_metrics)) {
    return Error("Failed to write table header");
  }

  for (uint16_t i = 0; i < num_metrics; ++i) {
    const OpenTypeVORGMetrics& rec = this->metrics[i];
    if (!out->WriteU16(rec.glyph_index) ||
        !out->WriteS16(rec.vert_origin_y)) {
      return Error("Failed to write record %d", i);
    }
  }

  return true;
}

bool OpenTypeVORG::ShouldSerialize() {
  return Table::ShouldSerialize() &&
         // this table is not for fonts with TT glyphs.
         GetFont()->GetTable(OTS_TAG_CFF) != NULL;
}

}  // namespace ots