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