1 // Copyright (c) 2009-2017 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 #include <string>
6
7 #include "os2.h"
8 #include "head.h"
9
10 // OS/2 - OS/2 and Windows Metrics
11 // http://www.microsoft.com/typography/otspec/os2.htm
12
13 namespace ots {
14
Parse(const uint8_t * data,size_t length)15 bool OpenTypeOS2::Parse(const uint8_t *data, size_t length) {
16 Buffer table(data, length);
17
18 if (!table.ReadU16(&this->table.version) ||
19 !table.ReadS16(&this->table.avg_char_width) ||
20 !table.ReadU16(&this->table.weight_class) ||
21 !table.ReadU16(&this->table.width_class) ||
22 !table.ReadU16(&this->table.type) ||
23 !table.ReadS16(&this->table.subscript_x_size) ||
24 !table.ReadS16(&this->table.subscript_y_size) ||
25 !table.ReadS16(&this->table.subscript_x_offset) ||
26 !table.ReadS16(&this->table.subscript_y_offset) ||
27 !table.ReadS16(&this->table.superscript_x_size) ||
28 !table.ReadS16(&this->table.superscript_y_size) ||
29 !table.ReadS16(&this->table.superscript_x_offset) ||
30 !table.ReadS16(&this->table.superscript_y_offset) ||
31 !table.ReadS16(&this->table.strikeout_size) ||
32 !table.ReadS16(&this->table.strikeout_position) ||
33 !table.ReadS16(&this->table.family_class)) {
34 return Error("Error reading basic table elements");
35 }
36
37 if (this->table.version > 5) {
38 return Error("Unsupported table version: %u", this->table.version);
39 }
40
41 // Follow WPF Font Selection Model's advice.
42 if (1 <= this->table.weight_class && this->table.weight_class <= 9) {
43 Warning("Bad usWeightClass: %u, changing it to %u",
44 this->table.weight_class, this->table.weight_class * 100);
45 this->table.weight_class *= 100;
46 }
47 // Ditto.
48 if (this->table.weight_class > 999) {
49 Warning("Bad usWeightClass: %u, changing it to %d",
50 this->table.weight_class, 999);
51 this->table.weight_class = 999;
52 }
53
54 if (this->table.width_class < 1) {
55 Warning("Bad usWidthClass: %u, changing it to %d",
56 this->table.width_class, 1);
57 this->table.width_class = 1;
58 } else if (this->table.width_class > 9) {
59 Warning("Bad usWidthClass: %u, changing it to %d",
60 this->table.width_class, 9);
61 this->table.width_class = 9;
62 }
63
64 // lowest 3 bits of fsType are exclusive.
65 if (this->table.type & 0x2) {
66 // mask bits 2 & 3.
67 this->table.type &= 0xfff3u;
68 } else if (this->table.type & 0x4) {
69 // mask bits 1 & 3.
70 this->table.type &= 0xfff4u;
71 } else if (this->table.type & 0x8) {
72 // mask bits 1 & 2.
73 this->table.type &= 0xfff9u;
74 }
75
76 // mask reserved bits. use only 0..3, 8, 9 bits.
77 this->table.type &= 0x30f;
78
79 #define SET_TO_ZERO(a, b) \
80 if (this->table.b < 0) { \
81 Warning("Bad " a ": %d, setting it to zero", this->table.b); \
82 this->table.b = 0; \
83 }
84
85 SET_TO_ZERO("ySubscriptXSize", subscript_x_size);
86 SET_TO_ZERO("ySubscriptYSize", subscript_y_size);
87 SET_TO_ZERO("ySuperscriptXSize", superscript_x_size);
88 SET_TO_ZERO("ySuperscriptYSize", superscript_y_size);
89 SET_TO_ZERO("yStrikeoutSize", strikeout_size);
90 #undef SET_TO_ZERO
91
92 static std::string panose_strings[10] = {
93 "bFamilyType",
94 "bSerifStyle",
95 "bWeight",
96 "bProportion",
97 "bContrast",
98 "bStrokeVariation",
99 "bArmStyle",
100 "bLetterform",
101 "bMidline",
102 "bXHeight",
103 };
104 for (unsigned i = 0; i < 10; ++i) {
105 if (!table.ReadU8(&this->table.panose[i])) {
106 return Error("Failed to read PANOSE %s", panose_strings[i].c_str());
107 }
108 }
109
110 if (!table.ReadU32(&this->table.unicode_range_1) ||
111 !table.ReadU32(&this->table.unicode_range_2) ||
112 !table.ReadU32(&this->table.unicode_range_3) ||
113 !table.ReadU32(&this->table.unicode_range_4) ||
114 !table.ReadU32(&this->table.vendor_id) ||
115 !table.ReadU16(&this->table.selection) ||
116 !table.ReadU16(&this->table.first_char_index) ||
117 !table.ReadU16(&this->table.last_char_index) ||
118 !table.ReadS16(&this->table.typo_ascender) ||
119 !table.ReadS16(&this->table.typo_descender) ||
120 !table.ReadS16(&this->table.typo_linegap) ||
121 !table.ReadU16(&this->table.win_ascent) ||
122 !table.ReadU16(&this->table.win_descent)) {
123 return Error("Error reading more basic table fields");
124 }
125
126 // If bit 6 is set, then bits 0 and 5 must be clear.
127 if (this->table.selection & 0x40) {
128 this->table.selection &= 0xffdeu;
129 }
130
131 // the settings of bits 0 and 1 must be reflected in the macStyle bits
132 // in the 'head' table.
133 OpenTypeHEAD *head = static_cast<OpenTypeHEAD*>(
134 GetFont()->GetTypedTable(OTS_TAG_HEAD));
135
136 if ((this->table.selection & 0x1) &&
137 head && !(head->mac_style & 0x2)) {
138 Warning("Adjusting head.macStyle (italic) to match fsSelection");
139 head->mac_style |= 0x2;
140 }
141 if ((this->table.selection & 0x2) &&
142 head && !(head->mac_style & 0x4)) {
143 Warning("Adjusting head.macStyle (underscore) to match fsSelection");
144 head->mac_style |= 0x4;
145 }
146
147 // While bit 6 on implies that bits 0 and 1 of macStyle are clear,
148 // the reverse is not true.
149 if ((this->table.selection & 0x40) &&
150 head && (head->mac_style & 0x3)) {
151 Warning("Adjusting head.macStyle (regular) to match fsSelection");
152 head->mac_style &= 0xfffcu;
153 }
154
155 if ((this->table.version < 4) &&
156 (this->table.selection & 0x300)) {
157 // bit 8 and 9 must be unset in OS/2 table versions less than 4.
158 return Error("fSelection bits 8 and 9 must be unset for table version %d",
159 this->table.version);
160 }
161
162 // mask reserved bits. use only 0..9 bits.
163 this->table.selection &= 0x3ff;
164
165 if (this->table.first_char_index > this->table.last_char_index) {
166 return Error("usFirstCharIndex %d > usLastCharIndex %d",
167 this->table.first_char_index, this->table.last_char_index);
168 }
169 if (this->table.typo_linegap < 0) {
170 Warning("Bad sTypoLineGap, setting it to 0: %d", this->table.typo_linegap);
171 this->table.typo_linegap = 0;
172 }
173
174 if (this->table.version < 1) {
175 // http://www.microsoft.com/typography/otspec/os2ver0.htm
176 return true;
177 }
178
179 if (length < offsetof(OS2Data, code_page_range_2)) {
180 Warning("Bad version number, setting it to 0: %u", this->table.version);
181 // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version
182 // numbers. Fix them.
183 this->table.version = 0;
184 return true;
185 }
186
187 if (!table.ReadU32(&this->table.code_page_range_1) ||
188 !table.ReadU32(&this->table.code_page_range_2)) {
189 return Error("Failed to read ulCodePageRange1 or ulCodePageRange2");
190 }
191
192 if (this->table.version < 2) {
193 // http://www.microsoft.com/typography/otspec/os2ver1.htm
194 return true;
195 }
196
197 if (length < offsetof(OS2Data, max_context)) {
198 Warning("Bad version number, setting it to 1: %u", this->table.version);
199 // some Japanese fonts (e.g., mona.ttf) have weird version number.
200 // fix them.
201 this->table.version = 1;
202 return true;
203 }
204
205 if (!table.ReadS16(&this->table.x_height) ||
206 !table.ReadS16(&this->table.cap_height) ||
207 !table.ReadU16(&this->table.default_char) ||
208 !table.ReadU16(&this->table.break_char) ||
209 !table.ReadU16(&this->table.max_context)) {
210 return Error("Failed to read version 2-specific fields");
211 }
212
213 if (this->table.x_height < 0) {
214 Warning("Bad sxHeight settig it to 0: %d", this->table.x_height);
215 this->table.x_height = 0;
216 }
217 if (this->table.cap_height < 0) {
218 Warning("Bad sCapHeight setting it to 0: %d", this->table.cap_height);
219 this->table.cap_height = 0;
220 }
221
222 if (this->table.version < 5) {
223 // http://www.microsoft.com/typography/otspec/os2ver4.htm
224 return true;
225 }
226
227 if (!table.ReadU16(&this->table.lower_optical_pointsize) ||
228 !table.ReadU16(&this->table.upper_optical_pointsize)) {
229 return Error("Failed to read version 5-specific fields");
230 }
231
232 if (this->table.lower_optical_pointsize > 0xFFFE) {
233 Warning("usLowerOpticalPointSize is bigger than 0xFFFE: %d",
234 this->table.lower_optical_pointsize);
235 this->table.lower_optical_pointsize = 0xFFFE;
236 }
237
238 if (this->table.upper_optical_pointsize < 2) {
239 Warning("usUpperOpticalPointSize is lower than 2: %d",
240 this->table.upper_optical_pointsize);
241 this->table.upper_optical_pointsize = 2;
242 }
243
244 return true;
245 }
246
Serialize(OTSStream * out)247 bool OpenTypeOS2::Serialize(OTSStream *out) {
248 if (!out->WriteU16(this->table.version) ||
249 !out->WriteS16(this->table.avg_char_width) ||
250 !out->WriteU16(this->table.weight_class) ||
251 !out->WriteU16(this->table.width_class) ||
252 !out->WriteU16(this->table.type) ||
253 !out->WriteS16(this->table.subscript_x_size) ||
254 !out->WriteS16(this->table.subscript_y_size) ||
255 !out->WriteS16(this->table.subscript_x_offset) ||
256 !out->WriteS16(this->table.subscript_y_offset) ||
257 !out->WriteS16(this->table.superscript_x_size) ||
258 !out->WriteS16(this->table.superscript_y_size) ||
259 !out->WriteS16(this->table.superscript_x_offset) ||
260 !out->WriteS16(this->table.superscript_y_offset) ||
261 !out->WriteS16(this->table.strikeout_size) ||
262 !out->WriteS16(this->table.strikeout_position) ||
263 !out->WriteS16(this->table.family_class)) {
264 return Error("Failed to write basic table data");
265 }
266
267 for (unsigned i = 0; i < 10; ++i) {
268 if (!out->Write(&this->table.panose[i], 1)) {
269 return Error("Failed to write PANOSE data");
270 }
271 }
272
273 if (!out->WriteU32(this->table.unicode_range_1) ||
274 !out->WriteU32(this->table.unicode_range_2) ||
275 !out->WriteU32(this->table.unicode_range_3) ||
276 !out->WriteU32(this->table.unicode_range_4) ||
277 !out->WriteU32(this->table.vendor_id) ||
278 !out->WriteU16(this->table.selection) ||
279 !out->WriteU16(this->table.first_char_index) ||
280 !out->WriteU16(this->table.last_char_index) ||
281 !out->WriteS16(this->table.typo_ascender) ||
282 !out->WriteS16(this->table.typo_descender) ||
283 !out->WriteS16(this->table.typo_linegap) ||
284 !out->WriteU16(this->table.win_ascent) ||
285 !out->WriteU16(this->table.win_descent)) {
286 return Error("Failed to write version 1-specific fields");
287 }
288
289 if (this->table.version < 1) {
290 return true;
291 }
292
293 if (!out->WriteU32(this->table.code_page_range_1) ||
294 !out->WriteU32(this->table.code_page_range_2)) {
295 return Error("Failed to write codepage ranges");
296 }
297
298 if (this->table.version < 2) {
299 return true;
300 }
301
302 if (!out->WriteS16(this->table.x_height) ||
303 !out->WriteS16(this->table.cap_height) ||
304 !out->WriteU16(this->table.default_char) ||
305 !out->WriteU16(this->table.break_char) ||
306 !out->WriteU16(this->table.max_context)) {
307 return Error("Failed to write version 2-specific fields");
308 }
309
310 if (this->table.version < 5) {
311 return true;
312 }
313
314 if (!out->WriteU16(this->table.lower_optical_pointsize) ||
315 !out->WriteU16(this->table.upper_optical_pointsize)) {
316 return Error("Failed to write version 5-specific fields");
317 }
318
319 return true;
320 }
321
322 } // namespace ots
323