OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include "os2.h" |
6 | 6 |
7 #include "os2.h" | |
8 #include "head.h" | 7 #include "head.h" |
9 | 8 |
10 // OS/2 - OS/2 and Windows Metrics | 9 // OS/2 - OS/2 and Windows Metrics |
11 // http://www.microsoft.com/typography/otspec/os2.htm | 10 // http://www.microsoft.com/typography/otspec/os2.htm |
12 | 11 |
13 #define TABLE_NAME "OS/2" | 12 #define TABLE_NAME "OS/2" |
14 | 13 |
15 namespace ots { | 14 namespace ots { |
16 | 15 |
17 bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 16 bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
(...skipping 11 matching lines...) Expand all Loading... |
29 !table.ReadS16(&os2->subscript_y_size) || | 28 !table.ReadS16(&os2->subscript_y_size) || |
30 !table.ReadS16(&os2->subscript_x_offset) || | 29 !table.ReadS16(&os2->subscript_x_offset) || |
31 !table.ReadS16(&os2->subscript_y_offset) || | 30 !table.ReadS16(&os2->subscript_y_offset) || |
32 !table.ReadS16(&os2->superscript_x_size) || | 31 !table.ReadS16(&os2->superscript_x_size) || |
33 !table.ReadS16(&os2->superscript_y_size) || | 32 !table.ReadS16(&os2->superscript_y_size) || |
34 !table.ReadS16(&os2->superscript_x_offset) || | 33 !table.ReadS16(&os2->superscript_x_offset) || |
35 !table.ReadS16(&os2->superscript_y_offset) || | 34 !table.ReadS16(&os2->superscript_y_offset) || |
36 !table.ReadS16(&os2->strikeout_size) || | 35 !table.ReadS16(&os2->strikeout_size) || |
37 !table.ReadS16(&os2->strikeout_position) || | 36 !table.ReadS16(&os2->strikeout_position) || |
38 !table.ReadS16(&os2->family_class)) { | 37 !table.ReadS16(&os2->family_class)) { |
39 return OTS_FAILURE_MSG("Error reading basic table elements"); | 38 return OTS_FAILURE_MSG("Failed toi read basic os2 elements"); |
40 } | 39 } |
41 | 40 |
42 if (os2->version > 5) { | 41 if (os2->version > 4) { |
43 return OTS_FAILURE_MSG("Unsupported table version: %u", os2->version); | 42 return OTS_FAILURE_MSG("os2 version too high %d", os2->version); |
44 } | 43 } |
45 | 44 |
46 // Follow WPF Font Selection Model's advice. | 45 // Some linux fonts (e.g., Kedage-t.ttf and LucidaSansDemiOblique.ttf) have |
47 if (1 <= os2->weight_class && os2->weight_class <= 9) { | 46 // weird weight/width classes. Overwrite them with FW_NORMAL/1/9. |
48 OTS_WARNING("Bad usWeightClass: %u, changing it to: %u", os2->weight_class,
os2->weight_class * 100); | 47 if (os2->weight_class < 100 || |
49 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 |
50 } | 52 } |
51 // Ditto. | |
52 if (os2->weight_class > 999) { | |
53 OTS_WARNING("Bad usWeightClass: %u, changing it to: %d", os2->weight_class,
999); | |
54 os2->weight_class = 999; | |
55 } | |
56 | |
57 if (os2->width_class < 1) { | 53 if (os2->width_class < 1) { |
58 OTS_WARNING("Bad usWidthClass: %u, changing it to: %d", os2->width_class, 1)
; | 54 OTS_WARNING("bad width: %u", os2->width_class); |
59 os2->width_class = 1; | 55 os2->width_class = 1; |
60 } else if (os2->width_class > 9) { | 56 } else if (os2->width_class > 9) { |
61 OTS_WARNING("Bad usWidthClass: %u, changing it to: %d", os2->width_class, 9)
; | 57 OTS_WARNING("bad width: %u", os2->width_class); |
62 os2->width_class = 9; | 58 os2->width_class = 9; |
63 } | 59 } |
64 | 60 |
65 // lowest 3 bits of fsType are exclusive. | 61 // lowest 3 bits of fsType are exclusive. |
66 if (os2->type & 0x2) { | 62 if (os2->type & 0x2) { |
67 // mask bits 2 & 3. | 63 // mask bits 2 & 3. |
68 os2->type &= 0xfff3u; | 64 os2->type &= 0xfff3u; |
69 } else if (os2->type & 0x4) { | 65 } else if (os2->type & 0x4) { |
70 // mask bits 1 & 3. | 66 // mask bits 1 & 3. |
71 os2->type &= 0xfff4u; | 67 os2->type &= 0xfff4u; |
72 } else if (os2->type & 0x8) { | 68 } else if (os2->type & 0x8) { |
73 // mask bits 1 & 2. | 69 // mask bits 1 & 2. |
74 os2->type &= 0xfff9u; | 70 os2->type &= 0xfff9u; |
75 } | 71 } |
76 | 72 |
77 // mask reserved bits. use only 0..3, 8, 9 bits. | 73 // mask reserved bits. use only 0..3, 8, 9 bits. |
78 os2->type &= 0x30f; | 74 os2->type &= 0x30f; |
79 | 75 |
80 #define SET_TO_ZERO(a, b) \ | 76 if (os2->subscript_x_size < 0) { |
81 if (os2->b < 0) { \ | 77 OTS_WARNING("bad subscript_x_size: %d", os2->subscript_x_size); |
82 OTS_WARNING("Bad " a ": %d, setting it to zero", os2->b); \ | 78 os2->subscript_x_size = 0; |
83 os2->b = 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; |
84 } | 95 } |
85 | 96 |
86 SET_TO_ZERO("ySubscriptXSize", subscript_x_size); | |
87 SET_TO_ZERO("ySubscriptYSize", subscript_y_size); | |
88 SET_TO_ZERO("ySuperscriptXSize", superscript_x_size); | |
89 SET_TO_ZERO("ySuperscriptYSize", superscript_y_size); | |
90 SET_TO_ZERO("yStrikeoutSize", strikeout_size); | |
91 #undef SET_TO_ZERO | |
92 | |
93 static std::string panose_strings[10] = { | |
94 "bFamilyType", | |
95 "bSerifStyle", | |
96 "bWeight", | |
97 "bProportion", | |
98 "bContrast", | |
99 "bStrokeVariation", | |
100 "bArmStyle", | |
101 "bLetterform", | |
102 "bMidline", | |
103 "bXHeight", | |
104 }; | |
105 for (unsigned i = 0; i < 10; ++i) { | 97 for (unsigned i = 0; i < 10; ++i) { |
106 if (!table.ReadU8(&os2->panose[i])) { | 98 if (!table.ReadU8(&os2->panose[i])) { |
107 return OTS_FAILURE_MSG("Error reading PANOSE %s", panose_strings[i].c_str(
)); | 99 return OTS_FAILURE_MSG("Failed to read panose in os2 table"); |
108 } | 100 } |
109 } | 101 } |
110 | 102 |
111 if (!table.ReadU32(&os2->unicode_range_1) || | 103 if (!table.ReadU32(&os2->unicode_range_1) || |
112 !table.ReadU32(&os2->unicode_range_2) || | 104 !table.ReadU32(&os2->unicode_range_2) || |
113 !table.ReadU32(&os2->unicode_range_3) || | 105 !table.ReadU32(&os2->unicode_range_3) || |
114 !table.ReadU32(&os2->unicode_range_4) || | 106 !table.ReadU32(&os2->unicode_range_4) || |
115 !table.ReadU32(&os2->vendor_id) || | 107 !table.ReadU32(&os2->vendor_id) || |
116 !table.ReadU16(&os2->selection) || | 108 !table.ReadU16(&os2->selection) || |
117 !table.ReadU16(&os2->first_char_index) || | 109 !table.ReadU16(&os2->first_char_index) || |
118 !table.ReadU16(&os2->last_char_index) || | 110 !table.ReadU16(&os2->last_char_index) || |
119 !table.ReadS16(&os2->typo_ascender) || | 111 !table.ReadS16(&os2->typo_ascender) || |
120 !table.ReadS16(&os2->typo_descender) || | 112 !table.ReadS16(&os2->typo_descender) || |
121 !table.ReadS16(&os2->typo_linegap) || | 113 !table.ReadS16(&os2->typo_linegap) || |
122 !table.ReadU16(&os2->win_ascent) || | 114 !table.ReadU16(&os2->win_ascent) || |
123 !table.ReadU16(&os2->win_descent)) { | 115 !table.ReadU16(&os2->win_descent)) { |
124 return OTS_FAILURE_MSG("Error reading more basic table fields"); | 116 return OTS_FAILURE_MSG("Failed to read more basic os2 fields"); |
125 } | 117 } |
126 | 118 |
127 // If bit 6 is set, then bits 0 and 5 must be clear. | 119 // If bit 6 is set, then bits 0 and 5 must be clear. |
128 if (os2->selection & 0x40) { | 120 if (os2->selection & 0x40) { |
129 os2->selection &= 0xffdeu; | 121 os2->selection &= 0xffdeu; |
130 } | 122 } |
131 | 123 |
132 // the settings of bits 0 and 1 must be reflected in the macStyle bits | 124 // the settings of bits 0 and 1 must be reflected in the macStyle bits |
133 // in the 'head' table. | 125 // in the 'head' table. |
134 if (!file->head) { | 126 if (!file->head) { |
135 return OTS_FAILURE_MSG("Needed head table is missing from the font"); | 127 return OTS_FAILURE_MSG("Head table missing from font as needed by os2 table"
); |
136 } | 128 } |
137 if ((os2->selection & 0x1) && | 129 if ((os2->selection & 0x1) && |
138 !(file->head->mac_style & 0x2)) { | 130 !(file->head->mac_style & 0x2)) { |
139 OTS_WARNING("adjusting Mac style (italic)"); | 131 OTS_WARNING("adjusting Mac style (italic)"); |
140 file->head->mac_style |= 0x2; | 132 file->head->mac_style |= 0x2; |
141 } | 133 } |
142 if ((os2->selection & 0x2) && | 134 if ((os2->selection & 0x2) && |
143 !(file->head->mac_style & 0x4)) { | 135 !(file->head->mac_style & 0x4)) { |
144 OTS_WARNING("adjusting Mac style (underscore)"); | 136 OTS_WARNING("adjusting Mac style (underscore)"); |
145 file->head->mac_style |= 0x4; | 137 file->head->mac_style |= 0x4; |
146 } | 138 } |
147 | 139 |
148 // While bit 6 on implies that bits 0 and 1 of macStyle are clear, | 140 // While bit 6 on implies that bits 0 and 1 of macStyle are clear, |
149 // the reverse is not true. | 141 // the reverse is not true. |
150 if ((os2->selection & 0x40) && | 142 if ((os2->selection & 0x40) && |
151 (file->head->mac_style & 0x3)) { | 143 (file->head->mac_style & 0x3)) { |
152 OTS_WARNING("adjusting Mac style (regular)"); | 144 OTS_WARNING("adjusting Mac style (regular)"); |
153 file->head->mac_style &= 0xfffcu; | 145 file->head->mac_style &= 0xfffcu; |
154 } | 146 } |
155 | 147 |
156 if ((os2->version < 4) && | 148 if ((os2->version < 4) && |
157 (os2->selection & 0x300)) { | 149 (os2->selection & 0x300)) { |
158 // bit 8 and 9 must be unset in OS/2 table versions less than 4. | 150 // bit 8 and 9 must be unset in OS/2 table versions less than 4. |
159 return OTS_FAILURE_MSG("Version %d incompatible with selection %d", os2->ver
sion, os2->selection); | 151 return OTS_FAILURE_MSG("OS2 version %d incompatible with selection %d", os2-
>version, os2->selection); |
160 } | 152 } |
161 | 153 |
162 // mask reserved bits. use only 0..9 bits. | 154 // mask reserved bits. use only 0..9 bits. |
163 os2->selection &= 0x3ff; | 155 os2->selection &= 0x3ff; |
164 | 156 |
165 if (os2->first_char_index > os2->last_char_index) { | 157 if (os2->first_char_index > os2->last_char_index) { |
166 return OTS_FAILURE_MSG("first char index %d > last char index %d in os2", os
2->first_char_index, os2->last_char_index); | 158 return OTS_FAILURE_MSG("first char index %d > last char index %d in os2", os
2->first_char_index, os2->last_char_index); |
167 } | 159 } |
168 if (os2->typo_linegap < 0) { | 160 if (os2->typo_linegap < 0) { |
169 OTS_WARNING("bad linegap: %d", os2->typo_linegap); | 161 OTS_WARNING("bad linegap: %d", os2->typo_linegap); |
(...skipping 29 matching lines...) Expand all Loading... |
199 // fix them. | 191 // fix them. |
200 os2->version = 1; | 192 os2->version = 1; |
201 return true; | 193 return true; |
202 } | 194 } |
203 | 195 |
204 if (!table.ReadS16(&os2->x_height) || | 196 if (!table.ReadS16(&os2->x_height) || |
205 !table.ReadS16(&os2->cap_height) || | 197 !table.ReadS16(&os2->cap_height) || |
206 !table.ReadU16(&os2->default_char) || | 198 !table.ReadU16(&os2->default_char) || |
207 !table.ReadU16(&os2->break_char) || | 199 !table.ReadU16(&os2->break_char) || |
208 !table.ReadU16(&os2->max_context)) { | 200 !table.ReadU16(&os2->max_context)) { |
209 return OTS_FAILURE_MSG("Failed to read version 2-specific fields"); | 201 return OTS_FAILURE_MSG("Failed to read os2 version 2 information"); |
210 } | 202 } |
211 | 203 |
212 if (os2->x_height < 0) { | 204 if (os2->x_height < 0) { |
213 OTS_WARNING("bad x_height: %d", os2->x_height); | 205 OTS_WARNING("bad x_height: %d", os2->x_height); |
214 os2->x_height = 0; | 206 os2->x_height = 0; |
215 } | 207 } |
216 if (os2->cap_height < 0) { | 208 if (os2->cap_height < 0) { |
217 OTS_WARNING("bad cap_height: %d", os2->cap_height); | 209 OTS_WARNING("bad cap_height: %d", os2->cap_height); |
218 os2->cap_height = 0; | 210 os2->cap_height = 0; |
219 } | 211 } |
220 | 212 |
221 if (os2->version < 5) { | |
222 // http://www.microsoft.com/typography/otspec/os2ver4.htm | |
223 return true; | |
224 } | |
225 | |
226 if (!table.ReadU16(&os2->lower_optical_pointsize) || | |
227 !table.ReadU16(&os2->upper_optical_pointsize)) { | |
228 return OTS_FAILURE_MSG("Failed to read version 5-specific fields"); | |
229 } | |
230 | |
231 if (os2->lower_optical_pointsize > 0xFFFE) { | |
232 OTS_WARNING("'usLowerOpticalPointSize' is bigger than 0xFFFE: %d", os2->lowe
r_optical_pointsize); | |
233 os2->lower_optical_pointsize = 0xFFFE; | |
234 } | |
235 | |
236 if (os2->upper_optical_pointsize < 2) { | |
237 OTS_WARNING("'usUpperOpticalPointSize' is lower than 2: %d", os2->upper_opti
cal_pointsize); | |
238 os2->upper_optical_pointsize = 2; | |
239 } | |
240 | |
241 return true; | 213 return true; |
242 } | 214 } |
243 | 215 |
244 bool ots_os2_should_serialise(OpenTypeFile *file) { | 216 bool ots_os2_should_serialise(OpenTypeFile *file) { |
245 return file->os2 != NULL; | 217 return file->os2 != NULL; |
246 } | 218 } |
247 | 219 |
248 bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { | 220 bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { |
249 const OpenTypeOS2 *os2 = file->os2; | 221 const OpenTypeOS2 *os2 = file->os2; |
250 | 222 |
(...skipping 28 matching lines...) Expand all Loading... |
279 !out->WriteU32(os2->unicode_range_4) || | 251 !out->WriteU32(os2->unicode_range_4) || |
280 !out->WriteU32(os2->vendor_id) || | 252 !out->WriteU32(os2->vendor_id) || |
281 !out->WriteU16(os2->selection) || | 253 !out->WriteU16(os2->selection) || |
282 !out->WriteU16(os2->first_char_index) || | 254 !out->WriteU16(os2->first_char_index) || |
283 !out->WriteU16(os2->last_char_index) || | 255 !out->WriteU16(os2->last_char_index) || |
284 !out->WriteS16(os2->typo_ascender) || | 256 !out->WriteS16(os2->typo_ascender) || |
285 !out->WriteS16(os2->typo_descender) || | 257 !out->WriteS16(os2->typo_descender) || |
286 !out->WriteS16(os2->typo_linegap) || | 258 !out->WriteS16(os2->typo_linegap) || |
287 !out->WriteU16(os2->win_ascent) || | 259 !out->WriteU16(os2->win_ascent) || |
288 !out->WriteU16(os2->win_descent)) { | 260 !out->WriteU16(os2->win_descent)) { |
289 return OTS_FAILURE_MSG("Failed to write version 1-specific fields"); | 261 return OTS_FAILURE_MSG("Failed to write os2 version 1 information"); |
290 } | 262 } |
291 | 263 |
292 if (os2->version < 1) { | 264 if (os2->version < 1) { |
293 return true; | 265 return true; |
294 } | 266 } |
295 | 267 |
296 if (!out->WriteU32(os2->code_page_range_1) || | 268 if (!out->WriteU32(os2->code_page_range_1) || |
297 !out->WriteU32(os2->code_page_range_2)) { | 269 !out->WriteU32(os2->code_page_range_2)) { |
298 return OTS_FAILURE_MSG("Failed to write codepage ranges"); | 270 return OTS_FAILURE_MSG("Failed to write codepage ranges"); |
299 } | 271 } |
300 | 272 |
301 if (os2->version < 2) { | 273 if (os2->version < 2) { |
302 return true; | 274 return true; |
303 } | 275 } |
304 | 276 |
305 if (!out->WriteS16(os2->x_height) || | 277 if (!out->WriteS16(os2->x_height) || |
306 !out->WriteS16(os2->cap_height) || | 278 !out->WriteS16(os2->cap_height) || |
307 !out->WriteU16(os2->default_char) || | 279 !out->WriteU16(os2->default_char) || |
308 !out->WriteU16(os2->break_char) || | 280 !out->WriteU16(os2->break_char) || |
309 !out->WriteU16(os2->max_context)) { | 281 !out->WriteU16(os2->max_context)) { |
310 return OTS_FAILURE_MSG("Failed to write version 2-specific fields"); | 282 return OTS_FAILURE_MSG("Failed to write os2 version 2 information"); |
311 } | |
312 | |
313 if (os2->version < 2) { | |
314 return true; | |
315 } | |
316 | |
317 if (!out->WriteU16(os2->lower_optical_pointsize) || | |
318 !out->WriteU16(os2->upper_optical_pointsize)) { | |
319 return OTS_FAILURE_MSG("Failed to write version 5-specific fields"); | |
320 } | 283 } |
321 | 284 |
322 return true; | 285 return true; |
323 } | 286 } |
324 | 287 |
325 void ots_os2_free(OpenTypeFile *file) { | 288 void ots_os2_free(OpenTypeFile *file) { |
326 delete file->os2; | 289 delete file->os2; |
327 } | 290 } |
328 | 291 |
329 } // namespace ots | 292 } // namespace ots |
330 | 293 |
331 #undef TABLE_NAME | 294 #undef TABLE_NAME |
OLD | NEW |