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