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 "vdmx.h"
6 :
7 : // VDMX - Vertical Device Metrics
8 : // http://www.microsoft.com/opentype/otspec/vdmx.htm
9 :
10 : #define DROP_THIS_TABLE \
11 : do { delete file->vdmx; file->vdmx = 0; } while (0)
12 :
13 : namespace ots {
14 :
15 0 : bool ots_vdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
16 0 : Buffer table(data, length);
17 0 : file->vdmx = new OpenTypeVDMX;
18 0 : OpenTypeVDMX * const vdmx = file->vdmx;
19 :
20 0 : if (!table.ReadU16(&vdmx->version) ||
21 0 : !table.ReadU16(&vdmx->num_recs) ||
22 0 : !table.ReadU16(&vdmx->num_ratios)) {
23 0 : return OTS_FAILURE();
24 : }
25 :
26 0 : if (vdmx->version > 1) {
27 : OTS_WARNING("bad version: %u", vdmx->version);
28 0 : DROP_THIS_TABLE;
29 0 : return true; // continue transcoding
30 : }
31 :
32 0 : vdmx->rat_ranges.reserve(vdmx->num_ratios);
33 0 : for (unsigned i = 0; i < vdmx->num_ratios; ++i) {
34 : OpenTypeVDMXRatioRecord rec;
35 :
36 0 : if (!table.ReadU8(&rec.charset) ||
37 0 : !table.ReadU8(&rec.x_ratio) ||
38 0 : !table.ReadU8(&rec.y_start_ratio) ||
39 0 : !table.ReadU8(&rec.y_end_ratio)) {
40 0 : return OTS_FAILURE();
41 : }
42 :
43 0 : if (rec.charset > 1) {
44 : OTS_WARNING("bad charset: %u", rec.charset);
45 0 : DROP_THIS_TABLE;
46 0 : return true;
47 : }
48 :
49 0 : if (rec.y_start_ratio > rec.y_end_ratio) {
50 : OTS_WARNING("bad y ratio");
51 0 : DROP_THIS_TABLE;
52 0 : return true;
53 : }
54 :
55 : // All values set to zero signal the default grouping to use;
56 : // if present, this must be the last Ratio group in the table.
57 0 : if ((i < vdmx->num_ratios - 1u) &&
58 : (rec.x_ratio == 0) &&
59 : (rec.y_start_ratio == 0) &&
60 : (rec.y_end_ratio == 0)) {
61 : // workaround for fonts which have 2 or more {0, 0, 0} terminators.
62 : OTS_WARNING("superfluous terminator found");
63 0 : DROP_THIS_TABLE;
64 0 : return true;
65 : }
66 :
67 0 : vdmx->rat_ranges.push_back(rec);
68 : }
69 :
70 0 : vdmx->offsets.reserve(vdmx->num_ratios);
71 0 : const size_t current_offset = table.offset();
72 : // current_offset is less than (2 bytes * 3) + (4 bytes * USHRT_MAX) = 256k.
73 0 : for (unsigned i = 0; i < vdmx->num_ratios; ++i) {
74 : uint16_t offset;
75 0 : if (!table.ReadU16(&offset)) {
76 0 : return OTS_FAILURE();
77 : }
78 0 : if (current_offset + offset >= length) { // thus doesn't overflow.
79 0 : return OTS_FAILURE();
80 : }
81 :
82 0 : vdmx->offsets.push_back(offset);
83 : }
84 :
85 0 : vdmx->groups.reserve(vdmx->num_recs);
86 0 : for (unsigned i = 0; i < vdmx->num_recs; ++i) {
87 0 : OpenTypeVDMXGroup group;
88 0 : if (!table.ReadU16(&group.recs) ||
89 0 : !table.ReadU8(&group.startsz) ||
90 0 : !table.ReadU8(&group.endsz)) {
91 0 : return OTS_FAILURE();
92 : }
93 0 : group.entries.reserve(group.recs);
94 0 : for (unsigned j = 0; j < group.recs; ++j) {
95 : OpenTypeVDMXVTable vt;
96 0 : if (!table.ReadU16(&vt.y_pel_height) ||
97 0 : !table.ReadS16(&vt.y_max) ||
98 0 : !table.ReadS16(&vt.y_min)) {
99 0 : return OTS_FAILURE();
100 : }
101 0 : if (vt.y_max < vt.y_min) {
102 : OTS_WARNING("bad y min/max");
103 0 : DROP_THIS_TABLE;
104 0 : return true;
105 : }
106 :
107 : // This table must appear in sorted order (sorted by yPelHeight),
108 : // but need not be continuous.
109 0 : if ((j != 0) && (group.entries[j - 1].y_pel_height >= vt.y_pel_height)) {
110 : OTS_WARNING("the table is not sorted");
111 0 : DROP_THIS_TABLE;
112 0 : return true;
113 : }
114 :
115 0 : group.entries.push_back(vt);
116 : }
117 0 : vdmx->groups.push_back(group);
118 : }
119 :
120 0 : return true;
121 : }
122 :
123 0 : bool ots_vdmx_should_serialise(OpenTypeFile *file) {
124 0 : if (!file->glyf) return false; // this table is not for CFF fonts.
125 0 : return file->vdmx != NULL;
126 : }
127 :
128 0 : bool ots_vdmx_serialise(OTSStream *out, OpenTypeFile *file) {
129 0 : OpenTypeVDMX * const vdmx = file->vdmx;
130 :
131 0 : if (!out->WriteU16(vdmx->version) ||
132 0 : !out->WriteU16(vdmx->num_recs) ||
133 0 : !out->WriteU16(vdmx->num_ratios)) {
134 0 : return OTS_FAILURE();
135 : }
136 :
137 0 : for (unsigned i = 0; i < vdmx->rat_ranges.size(); ++i) {
138 0 : const OpenTypeVDMXRatioRecord& rec = vdmx->rat_ranges[i];
139 0 : if (!out->Write(&rec.charset, 1) ||
140 0 : !out->Write(&rec.x_ratio, 1) ||
141 0 : !out->Write(&rec.y_start_ratio, 1) ||
142 0 : !out->Write(&rec.y_end_ratio, 1)) {
143 0 : return OTS_FAILURE();
144 : }
145 : }
146 :
147 0 : for (unsigned i = 0; i < vdmx->offsets.size(); ++i) {
148 0 : if (!out->WriteU16(vdmx->offsets[i])) {
149 0 : return OTS_FAILURE();
150 : }
151 : }
152 :
153 0 : for (unsigned i = 0; i < vdmx->groups.size(); ++i) {
154 0 : const OpenTypeVDMXGroup& group = vdmx->groups[i];
155 0 : if (!out->WriteU16(group.recs) ||
156 0 : !out->Write(&group.startsz, 1) ||
157 0 : !out->Write(&group.endsz, 1)) {
158 0 : return OTS_FAILURE();
159 : }
160 0 : for (unsigned j = 0; j < group.entries.size(); ++j) {
161 0 : const OpenTypeVDMXVTable& vt = group.entries[j];
162 0 : if (!out->WriteU16(vt.y_pel_height) ||
163 0 : !out->WriteS16(vt.y_max) ||
164 0 : !out->WriteS16(vt.y_min)) {
165 0 : return OTS_FAILURE();
166 : }
167 : }
168 : }
169 :
170 0 : return true;
171 : }
172 :
173 0 : void ots_vdmx_free(OpenTypeFile *file) {
174 0 : delete file->vdmx;
175 0 : }
176 :
177 : } // namespace ots
|