|
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 "os2.h" |
|
6 |
|
7 #include "head.h" |
|
8 |
|
9 // OS/2 - OS/2 and Windows Metrics |
|
10 // http://www.microsoft.com/typography/otspec/os2.htm |
|
11 |
|
12 #define TABLE_NAME "OS/2" |
|
13 |
|
14 namespace ots { |
|
15 |
|
16 bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
|
17 Buffer table(data, length); |
|
18 |
|
19 OpenTypeOS2 *os2 = new OpenTypeOS2; |
|
20 file->os2 = os2; |
|
21 |
|
22 if (!table.ReadU16(&os2->version) || |
|
23 !table.ReadS16(&os2->avg_char_width) || |
|
24 !table.ReadU16(&os2->weight_class) || |
|
25 !table.ReadU16(&os2->width_class) || |
|
26 !table.ReadU16(&os2->type) || |
|
27 !table.ReadS16(&os2->subscript_x_size) || |
|
28 !table.ReadS16(&os2->subscript_y_size) || |
|
29 !table.ReadS16(&os2->subscript_x_offset) || |
|
30 !table.ReadS16(&os2->subscript_y_offset) || |
|
31 !table.ReadS16(&os2->superscript_x_size) || |
|
32 !table.ReadS16(&os2->superscript_y_size) || |
|
33 !table.ReadS16(&os2->superscript_x_offset) || |
|
34 !table.ReadS16(&os2->superscript_y_offset) || |
|
35 !table.ReadS16(&os2->strikeout_size) || |
|
36 !table.ReadS16(&os2->strikeout_position) || |
|
37 !table.ReadS16(&os2->family_class)) { |
|
38 return OTS_FAILURE_MSG("Failed toi read basic os2 elements"); |
|
39 } |
|
40 |
|
41 if (os2->version > 4) { |
|
42 return OTS_FAILURE_MSG("os2 version too high %d", os2->version); |
|
43 } |
|
44 |
|
45 // Some linux fonts (e.g., Kedage-t.ttf and LucidaSansDemiOblique.ttf) have |
|
46 // weird weight/width classes. Overwrite them with FW_NORMAL/1/9. |
|
47 if (os2->weight_class < 100 || |
|
48 os2->weight_class > 900 || |
|
49 os2->weight_class % 100) { |
|
50 OTS_WARNING("bad weight: %u", os2->weight_class); |
|
51 os2->weight_class = 400; // FW_NORMAL |
|
52 } |
|
53 if (os2->width_class < 1) { |
|
54 OTS_WARNING("bad width: %u", os2->width_class); |
|
55 os2->width_class = 1; |
|
56 } else if (os2->width_class > 9) { |
|
57 OTS_WARNING("bad width: %u", os2->width_class); |
|
58 os2->width_class = 9; |
|
59 } |
|
60 |
|
61 // lowest 3 bits of fsType are exclusive. |
|
62 if (os2->type & 0x2) { |
|
63 // mask bits 2 & 3. |
|
64 os2->type &= 0xfff3u; |
|
65 } else if (os2->type & 0x4) { |
|
66 // mask bits 1 & 3. |
|
67 os2->type &= 0xfff4u; |
|
68 } else if (os2->type & 0x8) { |
|
69 // mask bits 1 & 2. |
|
70 os2->type &= 0xfff9u; |
|
71 } |
|
72 |
|
73 // mask reserved bits. use only 0..3, 8, 9 bits. |
|
74 os2->type &= 0x30f; |
|
75 |
|
76 if (os2->subscript_x_size < 0) { |
|
77 OTS_WARNING("bad subscript_x_size: %d", os2->subscript_x_size); |
|
78 os2->subscript_x_size = 0; |
|
79 } |
|
80 if (os2->subscript_y_size < 0) { |
|
81 OTS_WARNING("bad subscript_y_size: %d", os2->subscript_y_size); |
|
82 os2->subscript_y_size = 0; |
|
83 } |
|
84 if (os2->superscript_x_size < 0) { |
|
85 OTS_WARNING("bad superscript_x_size: %d", os2->superscript_x_size); |
|
86 os2->superscript_x_size = 0; |
|
87 } |
|
88 if (os2->superscript_y_size < 0) { |
|
89 OTS_WARNING("bad superscript_y_size: %d", os2->superscript_y_size); |
|
90 os2->superscript_y_size = 0; |
|
91 } |
|
92 if (os2->strikeout_size < 0) { |
|
93 OTS_WARNING("bad strikeout_size: %d", os2->strikeout_size); |
|
94 os2->strikeout_size = 0; |
|
95 } |
|
96 |
|
97 for (unsigned i = 0; i < 10; ++i) { |
|
98 if (!table.ReadU8(&os2->panose[i])) { |
|
99 return OTS_FAILURE_MSG("Failed to read panose in os2 table"); |
|
100 } |
|
101 } |
|
102 |
|
103 if (!table.ReadU32(&os2->unicode_range_1) || |
|
104 !table.ReadU32(&os2->unicode_range_2) || |
|
105 !table.ReadU32(&os2->unicode_range_3) || |
|
106 !table.ReadU32(&os2->unicode_range_4) || |
|
107 !table.ReadU32(&os2->vendor_id) || |
|
108 !table.ReadU16(&os2->selection) || |
|
109 !table.ReadU16(&os2->first_char_index) || |
|
110 !table.ReadU16(&os2->last_char_index) || |
|
111 !table.ReadS16(&os2->typo_ascender) || |
|
112 !table.ReadS16(&os2->typo_descender) || |
|
113 !table.ReadS16(&os2->typo_linegap) || |
|
114 !table.ReadU16(&os2->win_ascent) || |
|
115 !table.ReadU16(&os2->win_descent)) { |
|
116 return OTS_FAILURE_MSG("Failed to read more basic os2 fields"); |
|
117 } |
|
118 |
|
119 // If bit 6 is set, then bits 0 and 5 must be clear. |
|
120 if (os2->selection & 0x40) { |
|
121 os2->selection &= 0xffdeu; |
|
122 } |
|
123 |
|
124 // the settings of bits 0 and 1 must be reflected in the macStyle bits |
|
125 // in the 'head' table. |
|
126 if (!file->head) { |
|
127 return OTS_FAILURE_MSG("Head table missing from font as needed by os2 table"); |
|
128 } |
|
129 if ((os2->selection & 0x1) && |
|
130 !(file->head->mac_style & 0x2)) { |
|
131 OTS_WARNING("adjusting Mac style (italic)"); |
|
132 file->head->mac_style |= 0x2; |
|
133 } |
|
134 if ((os2->selection & 0x2) && |
|
135 !(file->head->mac_style & 0x4)) { |
|
136 OTS_WARNING("adjusting Mac style (underscore)"); |
|
137 file->head->mac_style |= 0x4; |
|
138 } |
|
139 |
|
140 // While bit 6 on implies that bits 0 and 1 of macStyle are clear, |
|
141 // the reverse is not true. |
|
142 if ((os2->selection & 0x40) && |
|
143 (file->head->mac_style & 0x3)) { |
|
144 OTS_WARNING("adjusting Mac style (regular)"); |
|
145 file->head->mac_style &= 0xfffcu; |
|
146 } |
|
147 |
|
148 if ((os2->version < 4) && |
|
149 (os2->selection & 0x300)) { |
|
150 // bit 8 and 9 must be unset in OS/2 table versions less than 4. |
|
151 return OTS_FAILURE_MSG("OS2 version %d incompatible with selection %d", os2->version, os2->selection); |
|
152 } |
|
153 |
|
154 // mask reserved bits. use only 0..9 bits. |
|
155 os2->selection &= 0x3ff; |
|
156 |
|
157 if (os2->first_char_index > os2->last_char_index) { |
|
158 return OTS_FAILURE_MSG("first char index %d > last char index %d in os2", os2->first_char_index, os2->last_char_index); |
|
159 } |
|
160 if (os2->typo_linegap < 0) { |
|
161 OTS_WARNING("bad linegap: %d", os2->typo_linegap); |
|
162 os2->typo_linegap = 0; |
|
163 } |
|
164 |
|
165 if (os2->version < 1) { |
|
166 // http://www.microsoft.com/typography/otspec/os2ver0.htm |
|
167 return true; |
|
168 } |
|
169 |
|
170 if (length < offsetof(OpenTypeOS2, code_page_range_2)) { |
|
171 OTS_WARNING("bad version number: %u", os2->version); |
|
172 // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version |
|
173 // numbers. Fix them. |
|
174 os2->version = 0; |
|
175 return true; |
|
176 } |
|
177 |
|
178 if (!table.ReadU32(&os2->code_page_range_1) || |
|
179 !table.ReadU32(&os2->code_page_range_2)) { |
|
180 return OTS_FAILURE_MSG("Failed to read codepage ranges"); |
|
181 } |
|
182 |
|
183 if (os2->version < 2) { |
|
184 // http://www.microsoft.com/typography/otspec/os2ver1.htm |
|
185 return true; |
|
186 } |
|
187 |
|
188 if (length < offsetof(OpenTypeOS2, max_context)) { |
|
189 OTS_WARNING("bad version number: %u", os2->version); |
|
190 // some Japanese fonts (e.g., mona.ttf) have weird version number. |
|
191 // fix them. |
|
192 os2->version = 1; |
|
193 return true; |
|
194 } |
|
195 |
|
196 if (!table.ReadS16(&os2->x_height) || |
|
197 !table.ReadS16(&os2->cap_height) || |
|
198 !table.ReadU16(&os2->default_char) || |
|
199 !table.ReadU16(&os2->break_char) || |
|
200 !table.ReadU16(&os2->max_context)) { |
|
201 return OTS_FAILURE_MSG("Failed to read os2 version 2 information"); |
|
202 } |
|
203 |
|
204 if (os2->x_height < 0) { |
|
205 OTS_WARNING("bad x_height: %d", os2->x_height); |
|
206 os2->x_height = 0; |
|
207 } |
|
208 if (os2->cap_height < 0) { |
|
209 OTS_WARNING("bad cap_height: %d", os2->cap_height); |
|
210 os2->cap_height = 0; |
|
211 } |
|
212 |
|
213 return true; |
|
214 } |
|
215 |
|
216 bool ots_os2_should_serialise(OpenTypeFile *file) { |
|
217 return file->os2 != NULL; |
|
218 } |
|
219 |
|
220 bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { |
|
221 const OpenTypeOS2 *os2 = file->os2; |
|
222 |
|
223 if (!out->WriteU16(os2->version) || |
|
224 !out->WriteS16(os2->avg_char_width) || |
|
225 !out->WriteU16(os2->weight_class) || |
|
226 !out->WriteU16(os2->width_class) || |
|
227 !out->WriteU16(os2->type) || |
|
228 !out->WriteS16(os2->subscript_x_size) || |
|
229 !out->WriteS16(os2->subscript_y_size) || |
|
230 !out->WriteS16(os2->subscript_x_offset) || |
|
231 !out->WriteS16(os2->subscript_y_offset) || |
|
232 !out->WriteS16(os2->superscript_x_size) || |
|
233 !out->WriteS16(os2->superscript_y_size) || |
|
234 !out->WriteS16(os2->superscript_x_offset) || |
|
235 !out->WriteS16(os2->superscript_y_offset) || |
|
236 !out->WriteS16(os2->strikeout_size) || |
|
237 !out->WriteS16(os2->strikeout_position) || |
|
238 !out->WriteS16(os2->family_class)) { |
|
239 return OTS_FAILURE_MSG("Failed to write basic OS2 information"); |
|
240 } |
|
241 |
|
242 for (unsigned i = 0; i < 10; ++i) { |
|
243 if (!out->Write(&os2->panose[i], 1)) { |
|
244 return OTS_FAILURE_MSG("Failed to write os2 panose information"); |
|
245 } |
|
246 } |
|
247 |
|
248 if (!out->WriteU32(os2->unicode_range_1) || |
|
249 !out->WriteU32(os2->unicode_range_2) || |
|
250 !out->WriteU32(os2->unicode_range_3) || |
|
251 !out->WriteU32(os2->unicode_range_4) || |
|
252 !out->WriteU32(os2->vendor_id) || |
|
253 !out->WriteU16(os2->selection) || |
|
254 !out->WriteU16(os2->first_char_index) || |
|
255 !out->WriteU16(os2->last_char_index) || |
|
256 !out->WriteS16(os2->typo_ascender) || |
|
257 !out->WriteS16(os2->typo_descender) || |
|
258 !out->WriteS16(os2->typo_linegap) || |
|
259 !out->WriteU16(os2->win_ascent) || |
|
260 !out->WriteU16(os2->win_descent)) { |
|
261 return OTS_FAILURE_MSG("Failed to write os2 version 1 information"); |
|
262 } |
|
263 |
|
264 if (os2->version < 1) { |
|
265 return true; |
|
266 } |
|
267 |
|
268 if (!out->WriteU32(os2->code_page_range_1) || |
|
269 !out->WriteU32(os2->code_page_range_2)) { |
|
270 return OTS_FAILURE_MSG("Failed to write codepage ranges"); |
|
271 } |
|
272 |
|
273 if (os2->version < 2) { |
|
274 return true; |
|
275 } |
|
276 |
|
277 if (!out->WriteS16(os2->x_height) || |
|
278 !out->WriteS16(os2->cap_height) || |
|
279 !out->WriteU16(os2->default_char) || |
|
280 !out->WriteU16(os2->break_char) || |
|
281 !out->WriteU16(os2->max_context)) { |
|
282 return OTS_FAILURE_MSG("Failed to write os2 version 2 information"); |
|
283 } |
|
284 |
|
285 return true; |
|
286 } |
|
287 |
|
288 void ots_os2_free(OpenTypeFile *file) { |
|
289 delete file->os2; |
|
290 } |
|
291 |
|
292 } // namespace ots |