Line data Source code
1 : // Copyright (c) 2009 The Chromium 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 : #include "vorg.h"
6 :
7 : #include <vector>
8 :
9 : // VORG - Vertical Origin Table
10 : // http://www.microsoft.com/typography/otspec/vorg.htm
11 :
12 : #define TABLE_NAME "VORG"
13 :
14 : #define DROP_THIS_TABLE(...) \
15 : do { \
16 : OTS_FAILURE_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__); \
17 : OTS_FAILURE_MSG("Table discarded"); \
18 : delete font->vorg; \
19 : font->vorg = 0; \
20 : } while (0)
21 :
22 : namespace ots {
23 :
24 0 : bool ots_vorg_parse(Font *font, const uint8_t *data, size_t length) {
25 0 : Buffer table(data, length);
26 0 : font->vorg = new OpenTypeVORG;
27 0 : OpenTypeVORG * const vorg = font->vorg;
28 :
29 : uint16_t num_recs;
30 0 : if (!table.ReadU16(&vorg->major_version) ||
31 0 : !table.ReadU16(&vorg->minor_version) ||
32 0 : !table.ReadS16(&vorg->default_vert_origin_y) ||
33 0 : !table.ReadU16(&num_recs)) {
34 0 : return OTS_FAILURE_MSG("Failed to read header");
35 : }
36 0 : if (vorg->major_version != 1) {
37 0 : DROP_THIS_TABLE("bad major version: %u", vorg->major_version);
38 0 : return true;
39 : }
40 0 : if (vorg->minor_version != 0) {
41 0 : DROP_THIS_TABLE("bad minor version: %u", vorg->minor_version);
42 0 : return true;
43 : }
44 :
45 : // num_recs might be zero (e.g., DFHSMinchoPro5-W3-Demo.otf).
46 0 : if (!num_recs) {
47 0 : return true;
48 : }
49 :
50 0 : uint16_t last_glyph_index = 0;
51 0 : vorg->metrics.reserve(num_recs);
52 0 : for (unsigned i = 0; i < num_recs; ++i) {
53 : OpenTypeVORGMetrics rec;
54 :
55 0 : if (!table.ReadU16(&rec.glyph_index) ||
56 0 : !table.ReadS16(&rec.vert_origin_y)) {
57 0 : return OTS_FAILURE_MSG("Failed to read record %d", i);
58 : }
59 0 : if ((i != 0) && (rec.glyph_index <= last_glyph_index)) {
60 0 : DROP_THIS_TABLE("the table is not sorted");
61 0 : return true;
62 : }
63 0 : last_glyph_index = rec.glyph_index;
64 :
65 0 : vorg->metrics.push_back(rec);
66 : }
67 :
68 0 : return true;
69 : }
70 :
71 0 : bool ots_vorg_should_serialise(Font *font) {
72 0 : if (!font->cff) return false; // this table is not for fonts with TT glyphs.
73 0 : return font->vorg != NULL;
74 : }
75 :
76 0 : bool ots_vorg_serialise(OTSStream *out, Font *font) {
77 0 : OpenTypeVORG * const vorg = font->vorg;
78 :
79 0 : const uint16_t num_metrics = static_cast<uint16_t>(vorg->metrics.size());
80 0 : if (num_metrics != vorg->metrics.size() ||
81 0 : !out->WriteU16(vorg->major_version) ||
82 0 : !out->WriteU16(vorg->minor_version) ||
83 0 : !out->WriteS16(vorg->default_vert_origin_y) ||
84 0 : !out->WriteU16(num_metrics)) {
85 0 : return OTS_FAILURE_MSG("Failed to write table header");
86 : }
87 :
88 0 : for (uint16_t i = 0; i < num_metrics; ++i) {
89 0 : const OpenTypeVORGMetrics& rec = vorg->metrics[i];
90 0 : if (!out->WriteU16(rec.glyph_index) ||
91 0 : !out->WriteS16(rec.vert_origin_y)) {
92 0 : return OTS_FAILURE_MSG("Failed to write record %d", i);
93 : }
94 : }
95 :
96 0 : return true;
97 : }
98 :
99 0 : void ots_vorg_reuse(Font *font, Font *other) {
100 0 : font->vorg = other->vorg;
101 0 : font->vorg_reused = true;
102 0 : }
103 :
104 0 : void ots_vorg_free(Font *font) {
105 0 : delete font->vorg;
106 0 : }
107 :
108 : } // namespace ots
109 :
110 : #undef TABLE_NAME
111 : #undef DROP_THIS_TABLE
|