1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/ots/src/os2.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,292 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "os2.h" 1.9 + 1.10 +#include "head.h" 1.11 + 1.12 +// OS/2 - OS/2 and Windows Metrics 1.13 +// http://www.microsoft.com/typography/otspec/os2.htm 1.14 + 1.15 +#define TABLE_NAME "OS/2" 1.16 + 1.17 +namespace ots { 1.18 + 1.19 +bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { 1.20 + Buffer table(data, length); 1.21 + 1.22 + OpenTypeOS2 *os2 = new OpenTypeOS2; 1.23 + file->os2 = os2; 1.24 + 1.25 + if (!table.ReadU16(&os2->version) || 1.26 + !table.ReadS16(&os2->avg_char_width) || 1.27 + !table.ReadU16(&os2->weight_class) || 1.28 + !table.ReadU16(&os2->width_class) || 1.29 + !table.ReadU16(&os2->type) || 1.30 + !table.ReadS16(&os2->subscript_x_size) || 1.31 + !table.ReadS16(&os2->subscript_y_size) || 1.32 + !table.ReadS16(&os2->subscript_x_offset) || 1.33 + !table.ReadS16(&os2->subscript_y_offset) || 1.34 + !table.ReadS16(&os2->superscript_x_size) || 1.35 + !table.ReadS16(&os2->superscript_y_size) || 1.36 + !table.ReadS16(&os2->superscript_x_offset) || 1.37 + !table.ReadS16(&os2->superscript_y_offset) || 1.38 + !table.ReadS16(&os2->strikeout_size) || 1.39 + !table.ReadS16(&os2->strikeout_position) || 1.40 + !table.ReadS16(&os2->family_class)) { 1.41 + return OTS_FAILURE_MSG("Failed toi read basic os2 elements"); 1.42 + } 1.43 + 1.44 + if (os2->version > 4) { 1.45 + return OTS_FAILURE_MSG("os2 version too high %d", os2->version); 1.46 + } 1.47 + 1.48 + // Some linux fonts (e.g., Kedage-t.ttf and LucidaSansDemiOblique.ttf) have 1.49 + // weird weight/width classes. Overwrite them with FW_NORMAL/1/9. 1.50 + if (os2->weight_class < 100 || 1.51 + os2->weight_class > 900 || 1.52 + os2->weight_class % 100) { 1.53 + OTS_WARNING("bad weight: %u", os2->weight_class); 1.54 + os2->weight_class = 400; // FW_NORMAL 1.55 + } 1.56 + if (os2->width_class < 1) { 1.57 + OTS_WARNING("bad width: %u", os2->width_class); 1.58 + os2->width_class = 1; 1.59 + } else if (os2->width_class > 9) { 1.60 + OTS_WARNING("bad width: %u", os2->width_class); 1.61 + os2->width_class = 9; 1.62 + } 1.63 + 1.64 + // lowest 3 bits of fsType are exclusive. 1.65 + if (os2->type & 0x2) { 1.66 + // mask bits 2 & 3. 1.67 + os2->type &= 0xfff3u; 1.68 + } else if (os2->type & 0x4) { 1.69 + // mask bits 1 & 3. 1.70 + os2->type &= 0xfff4u; 1.71 + } else if (os2->type & 0x8) { 1.72 + // mask bits 1 & 2. 1.73 + os2->type &= 0xfff9u; 1.74 + } 1.75 + 1.76 + // mask reserved bits. use only 0..3, 8, 9 bits. 1.77 + os2->type &= 0x30f; 1.78 + 1.79 + if (os2->subscript_x_size < 0) { 1.80 + OTS_WARNING("bad subscript_x_size: %d", os2->subscript_x_size); 1.81 + os2->subscript_x_size = 0; 1.82 + } 1.83 + if (os2->subscript_y_size < 0) { 1.84 + OTS_WARNING("bad subscript_y_size: %d", os2->subscript_y_size); 1.85 + os2->subscript_y_size = 0; 1.86 + } 1.87 + if (os2->superscript_x_size < 0) { 1.88 + OTS_WARNING("bad superscript_x_size: %d", os2->superscript_x_size); 1.89 + os2->superscript_x_size = 0; 1.90 + } 1.91 + if (os2->superscript_y_size < 0) { 1.92 + OTS_WARNING("bad superscript_y_size: %d", os2->superscript_y_size); 1.93 + os2->superscript_y_size = 0; 1.94 + } 1.95 + if (os2->strikeout_size < 0) { 1.96 + OTS_WARNING("bad strikeout_size: %d", os2->strikeout_size); 1.97 + os2->strikeout_size = 0; 1.98 + } 1.99 + 1.100 + for (unsigned i = 0; i < 10; ++i) { 1.101 + if (!table.ReadU8(&os2->panose[i])) { 1.102 + return OTS_FAILURE_MSG("Failed to read panose in os2 table"); 1.103 + } 1.104 + } 1.105 + 1.106 + if (!table.ReadU32(&os2->unicode_range_1) || 1.107 + !table.ReadU32(&os2->unicode_range_2) || 1.108 + !table.ReadU32(&os2->unicode_range_3) || 1.109 + !table.ReadU32(&os2->unicode_range_4) || 1.110 + !table.ReadU32(&os2->vendor_id) || 1.111 + !table.ReadU16(&os2->selection) || 1.112 + !table.ReadU16(&os2->first_char_index) || 1.113 + !table.ReadU16(&os2->last_char_index) || 1.114 + !table.ReadS16(&os2->typo_ascender) || 1.115 + !table.ReadS16(&os2->typo_descender) || 1.116 + !table.ReadS16(&os2->typo_linegap) || 1.117 + !table.ReadU16(&os2->win_ascent) || 1.118 + !table.ReadU16(&os2->win_descent)) { 1.119 + return OTS_FAILURE_MSG("Failed to read more basic os2 fields"); 1.120 + } 1.121 + 1.122 + // If bit 6 is set, then bits 0 and 5 must be clear. 1.123 + if (os2->selection & 0x40) { 1.124 + os2->selection &= 0xffdeu; 1.125 + } 1.126 + 1.127 + // the settings of bits 0 and 1 must be reflected in the macStyle bits 1.128 + // in the 'head' table. 1.129 + if (!file->head) { 1.130 + return OTS_FAILURE_MSG("Head table missing from font as needed by os2 table"); 1.131 + } 1.132 + if ((os2->selection & 0x1) && 1.133 + !(file->head->mac_style & 0x2)) { 1.134 + OTS_WARNING("adjusting Mac style (italic)"); 1.135 + file->head->mac_style |= 0x2; 1.136 + } 1.137 + if ((os2->selection & 0x2) && 1.138 + !(file->head->mac_style & 0x4)) { 1.139 + OTS_WARNING("adjusting Mac style (underscore)"); 1.140 + file->head->mac_style |= 0x4; 1.141 + } 1.142 + 1.143 + // While bit 6 on implies that bits 0 and 1 of macStyle are clear, 1.144 + // the reverse is not true. 1.145 + if ((os2->selection & 0x40) && 1.146 + (file->head->mac_style & 0x3)) { 1.147 + OTS_WARNING("adjusting Mac style (regular)"); 1.148 + file->head->mac_style &= 0xfffcu; 1.149 + } 1.150 + 1.151 + if ((os2->version < 4) && 1.152 + (os2->selection & 0x300)) { 1.153 + // bit 8 and 9 must be unset in OS/2 table versions less than 4. 1.154 + return OTS_FAILURE_MSG("OS2 version %d incompatible with selection %d", os2->version, os2->selection); 1.155 + } 1.156 + 1.157 + // mask reserved bits. use only 0..9 bits. 1.158 + os2->selection &= 0x3ff; 1.159 + 1.160 + if (os2->first_char_index > os2->last_char_index) { 1.161 + return OTS_FAILURE_MSG("first char index %d > last char index %d in os2", os2->first_char_index, os2->last_char_index); 1.162 + } 1.163 + if (os2->typo_linegap < 0) { 1.164 + OTS_WARNING("bad linegap: %d", os2->typo_linegap); 1.165 + os2->typo_linegap = 0; 1.166 + } 1.167 + 1.168 + if (os2->version < 1) { 1.169 + // http://www.microsoft.com/typography/otspec/os2ver0.htm 1.170 + return true; 1.171 + } 1.172 + 1.173 + if (length < offsetof(OpenTypeOS2, code_page_range_2)) { 1.174 + OTS_WARNING("bad version number: %u", os2->version); 1.175 + // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version 1.176 + // numbers. Fix them. 1.177 + os2->version = 0; 1.178 + return true; 1.179 + } 1.180 + 1.181 + if (!table.ReadU32(&os2->code_page_range_1) || 1.182 + !table.ReadU32(&os2->code_page_range_2)) { 1.183 + return OTS_FAILURE_MSG("Failed to read codepage ranges"); 1.184 + } 1.185 + 1.186 + if (os2->version < 2) { 1.187 + // http://www.microsoft.com/typography/otspec/os2ver1.htm 1.188 + return true; 1.189 + } 1.190 + 1.191 + if (length < offsetof(OpenTypeOS2, max_context)) { 1.192 + OTS_WARNING("bad version number: %u", os2->version); 1.193 + // some Japanese fonts (e.g., mona.ttf) have weird version number. 1.194 + // fix them. 1.195 + os2->version = 1; 1.196 + return true; 1.197 + } 1.198 + 1.199 + if (!table.ReadS16(&os2->x_height) || 1.200 + !table.ReadS16(&os2->cap_height) || 1.201 + !table.ReadU16(&os2->default_char) || 1.202 + !table.ReadU16(&os2->break_char) || 1.203 + !table.ReadU16(&os2->max_context)) { 1.204 + return OTS_FAILURE_MSG("Failed to read os2 version 2 information"); 1.205 + } 1.206 + 1.207 + if (os2->x_height < 0) { 1.208 + OTS_WARNING("bad x_height: %d", os2->x_height); 1.209 + os2->x_height = 0; 1.210 + } 1.211 + if (os2->cap_height < 0) { 1.212 + OTS_WARNING("bad cap_height: %d", os2->cap_height); 1.213 + os2->cap_height = 0; 1.214 + } 1.215 + 1.216 + return true; 1.217 +} 1.218 + 1.219 +bool ots_os2_should_serialise(OpenTypeFile *file) { 1.220 + return file->os2 != NULL; 1.221 +} 1.222 + 1.223 +bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { 1.224 + const OpenTypeOS2 *os2 = file->os2; 1.225 + 1.226 + if (!out->WriteU16(os2->version) || 1.227 + !out->WriteS16(os2->avg_char_width) || 1.228 + !out->WriteU16(os2->weight_class) || 1.229 + !out->WriteU16(os2->width_class) || 1.230 + !out->WriteU16(os2->type) || 1.231 + !out->WriteS16(os2->subscript_x_size) || 1.232 + !out->WriteS16(os2->subscript_y_size) || 1.233 + !out->WriteS16(os2->subscript_x_offset) || 1.234 + !out->WriteS16(os2->subscript_y_offset) || 1.235 + !out->WriteS16(os2->superscript_x_size) || 1.236 + !out->WriteS16(os2->superscript_y_size) || 1.237 + !out->WriteS16(os2->superscript_x_offset) || 1.238 + !out->WriteS16(os2->superscript_y_offset) || 1.239 + !out->WriteS16(os2->strikeout_size) || 1.240 + !out->WriteS16(os2->strikeout_position) || 1.241 + !out->WriteS16(os2->family_class)) { 1.242 + return OTS_FAILURE_MSG("Failed to write basic OS2 information"); 1.243 + } 1.244 + 1.245 + for (unsigned i = 0; i < 10; ++i) { 1.246 + if (!out->Write(&os2->panose[i], 1)) { 1.247 + return OTS_FAILURE_MSG("Failed to write os2 panose information"); 1.248 + } 1.249 + } 1.250 + 1.251 + if (!out->WriteU32(os2->unicode_range_1) || 1.252 + !out->WriteU32(os2->unicode_range_2) || 1.253 + !out->WriteU32(os2->unicode_range_3) || 1.254 + !out->WriteU32(os2->unicode_range_4) || 1.255 + !out->WriteU32(os2->vendor_id) || 1.256 + !out->WriteU16(os2->selection) || 1.257 + !out->WriteU16(os2->first_char_index) || 1.258 + !out->WriteU16(os2->last_char_index) || 1.259 + !out->WriteS16(os2->typo_ascender) || 1.260 + !out->WriteS16(os2->typo_descender) || 1.261 + !out->WriteS16(os2->typo_linegap) || 1.262 + !out->WriteU16(os2->win_ascent) || 1.263 + !out->WriteU16(os2->win_descent)) { 1.264 + return OTS_FAILURE_MSG("Failed to write os2 version 1 information"); 1.265 + } 1.266 + 1.267 + if (os2->version < 1) { 1.268 + return true; 1.269 + } 1.270 + 1.271 + if (!out->WriteU32(os2->code_page_range_1) || 1.272 + !out->WriteU32(os2->code_page_range_2)) { 1.273 + return OTS_FAILURE_MSG("Failed to write codepage ranges"); 1.274 + } 1.275 + 1.276 + if (os2->version < 2) { 1.277 + return true; 1.278 + } 1.279 + 1.280 + if (!out->WriteS16(os2->x_height) || 1.281 + !out->WriteS16(os2->cap_height) || 1.282 + !out->WriteU16(os2->default_char) || 1.283 + !out->WriteU16(os2->break_char) || 1.284 + !out->WriteU16(os2->max_context)) { 1.285 + return OTS_FAILURE_MSG("Failed to write os2 version 2 information"); 1.286 + } 1.287 + 1.288 + return true; 1.289 +} 1.290 + 1.291 +void ots_os2_free(OpenTypeFile *file) { 1.292 + delete file->os2; 1.293 +} 1.294 + 1.295 +} // namespace ots