michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "os2.h" michael@0: michael@0: #include "head.h" michael@0: michael@0: // OS/2 - OS/2 and Windows Metrics michael@0: // http://www.microsoft.com/typography/otspec/os2.htm michael@0: michael@0: #define TABLE_NAME "OS/2" michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_os2_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: michael@0: OpenTypeOS2 *os2 = new OpenTypeOS2; michael@0: file->os2 = os2; michael@0: michael@0: if (!table.ReadU16(&os2->version) || michael@0: !table.ReadS16(&os2->avg_char_width) || michael@0: !table.ReadU16(&os2->weight_class) || michael@0: !table.ReadU16(&os2->width_class) || michael@0: !table.ReadU16(&os2->type) || michael@0: !table.ReadS16(&os2->subscript_x_size) || michael@0: !table.ReadS16(&os2->subscript_y_size) || michael@0: !table.ReadS16(&os2->subscript_x_offset) || michael@0: !table.ReadS16(&os2->subscript_y_offset) || michael@0: !table.ReadS16(&os2->superscript_x_size) || michael@0: !table.ReadS16(&os2->superscript_y_size) || michael@0: !table.ReadS16(&os2->superscript_x_offset) || michael@0: !table.ReadS16(&os2->superscript_y_offset) || michael@0: !table.ReadS16(&os2->strikeout_size) || michael@0: !table.ReadS16(&os2->strikeout_position) || michael@0: !table.ReadS16(&os2->family_class)) { michael@0: return OTS_FAILURE_MSG("Failed toi read basic os2 elements"); michael@0: } michael@0: michael@0: if (os2->version > 4) { michael@0: return OTS_FAILURE_MSG("os2 version too high %d", os2->version); michael@0: } michael@0: michael@0: // Some linux fonts (e.g., Kedage-t.ttf and LucidaSansDemiOblique.ttf) have michael@0: // weird weight/width classes. Overwrite them with FW_NORMAL/1/9. michael@0: if (os2->weight_class < 100 || michael@0: os2->weight_class > 900 || michael@0: os2->weight_class % 100) { michael@0: OTS_WARNING("bad weight: %u", os2->weight_class); michael@0: os2->weight_class = 400; // FW_NORMAL michael@0: } michael@0: if (os2->width_class < 1) { michael@0: OTS_WARNING("bad width: %u", os2->width_class); michael@0: os2->width_class = 1; michael@0: } else if (os2->width_class > 9) { michael@0: OTS_WARNING("bad width: %u", os2->width_class); michael@0: os2->width_class = 9; michael@0: } michael@0: michael@0: // lowest 3 bits of fsType are exclusive. michael@0: if (os2->type & 0x2) { michael@0: // mask bits 2 & 3. michael@0: os2->type &= 0xfff3u; michael@0: } else if (os2->type & 0x4) { michael@0: // mask bits 1 & 3. michael@0: os2->type &= 0xfff4u; michael@0: } else if (os2->type & 0x8) { michael@0: // mask bits 1 & 2. michael@0: os2->type &= 0xfff9u; michael@0: } michael@0: michael@0: // mask reserved bits. use only 0..3, 8, 9 bits. michael@0: os2->type &= 0x30f; michael@0: michael@0: if (os2->subscript_x_size < 0) { michael@0: OTS_WARNING("bad subscript_x_size: %d", os2->subscript_x_size); michael@0: os2->subscript_x_size = 0; michael@0: } michael@0: if (os2->subscript_y_size < 0) { michael@0: OTS_WARNING("bad subscript_y_size: %d", os2->subscript_y_size); michael@0: os2->subscript_y_size = 0; michael@0: } michael@0: if (os2->superscript_x_size < 0) { michael@0: OTS_WARNING("bad superscript_x_size: %d", os2->superscript_x_size); michael@0: os2->superscript_x_size = 0; michael@0: } michael@0: if (os2->superscript_y_size < 0) { michael@0: OTS_WARNING("bad superscript_y_size: %d", os2->superscript_y_size); michael@0: os2->superscript_y_size = 0; michael@0: } michael@0: if (os2->strikeout_size < 0) { michael@0: OTS_WARNING("bad strikeout_size: %d", os2->strikeout_size); michael@0: os2->strikeout_size = 0; michael@0: } michael@0: michael@0: for (unsigned i = 0; i < 10; ++i) { michael@0: if (!table.ReadU8(&os2->panose[i])) { michael@0: return OTS_FAILURE_MSG("Failed to read panose in os2 table"); michael@0: } michael@0: } michael@0: michael@0: if (!table.ReadU32(&os2->unicode_range_1) || michael@0: !table.ReadU32(&os2->unicode_range_2) || michael@0: !table.ReadU32(&os2->unicode_range_3) || michael@0: !table.ReadU32(&os2->unicode_range_4) || michael@0: !table.ReadU32(&os2->vendor_id) || michael@0: !table.ReadU16(&os2->selection) || michael@0: !table.ReadU16(&os2->first_char_index) || michael@0: !table.ReadU16(&os2->last_char_index) || michael@0: !table.ReadS16(&os2->typo_ascender) || michael@0: !table.ReadS16(&os2->typo_descender) || michael@0: !table.ReadS16(&os2->typo_linegap) || michael@0: !table.ReadU16(&os2->win_ascent) || michael@0: !table.ReadU16(&os2->win_descent)) { michael@0: return OTS_FAILURE_MSG("Failed to read more basic os2 fields"); michael@0: } michael@0: michael@0: // If bit 6 is set, then bits 0 and 5 must be clear. michael@0: if (os2->selection & 0x40) { michael@0: os2->selection &= 0xffdeu; michael@0: } michael@0: michael@0: // the settings of bits 0 and 1 must be reflected in the macStyle bits michael@0: // in the 'head' table. michael@0: if (!file->head) { michael@0: return OTS_FAILURE_MSG("Head table missing from font as needed by os2 table"); michael@0: } michael@0: if ((os2->selection & 0x1) && michael@0: !(file->head->mac_style & 0x2)) { michael@0: OTS_WARNING("adjusting Mac style (italic)"); michael@0: file->head->mac_style |= 0x2; michael@0: } michael@0: if ((os2->selection & 0x2) && michael@0: !(file->head->mac_style & 0x4)) { michael@0: OTS_WARNING("adjusting Mac style (underscore)"); michael@0: file->head->mac_style |= 0x4; michael@0: } michael@0: michael@0: // While bit 6 on implies that bits 0 and 1 of macStyle are clear, michael@0: // the reverse is not true. michael@0: if ((os2->selection & 0x40) && michael@0: (file->head->mac_style & 0x3)) { michael@0: OTS_WARNING("adjusting Mac style (regular)"); michael@0: file->head->mac_style &= 0xfffcu; michael@0: } michael@0: michael@0: if ((os2->version < 4) && michael@0: (os2->selection & 0x300)) { michael@0: // bit 8 and 9 must be unset in OS/2 table versions less than 4. michael@0: return OTS_FAILURE_MSG("OS2 version %d incompatible with selection %d", os2->version, os2->selection); michael@0: } michael@0: michael@0: // mask reserved bits. use only 0..9 bits. michael@0: os2->selection &= 0x3ff; michael@0: michael@0: if (os2->first_char_index > os2->last_char_index) { michael@0: return OTS_FAILURE_MSG("first char index %d > last char index %d in os2", os2->first_char_index, os2->last_char_index); michael@0: } michael@0: if (os2->typo_linegap < 0) { michael@0: OTS_WARNING("bad linegap: %d", os2->typo_linegap); michael@0: os2->typo_linegap = 0; michael@0: } michael@0: michael@0: if (os2->version < 1) { michael@0: // http://www.microsoft.com/typography/otspec/os2ver0.htm michael@0: return true; michael@0: } michael@0: michael@0: if (length < offsetof(OpenTypeOS2, code_page_range_2)) { michael@0: OTS_WARNING("bad version number: %u", os2->version); michael@0: // Some fonts (e.g., kredit1.ttf and quinquef.ttf) have weird version michael@0: // numbers. Fix them. michael@0: os2->version = 0; michael@0: return true; michael@0: } michael@0: michael@0: if (!table.ReadU32(&os2->code_page_range_1) || michael@0: !table.ReadU32(&os2->code_page_range_2)) { michael@0: return OTS_FAILURE_MSG("Failed to read codepage ranges"); michael@0: } michael@0: michael@0: if (os2->version < 2) { michael@0: // http://www.microsoft.com/typography/otspec/os2ver1.htm michael@0: return true; michael@0: } michael@0: michael@0: if (length < offsetof(OpenTypeOS2, max_context)) { michael@0: OTS_WARNING("bad version number: %u", os2->version); michael@0: // some Japanese fonts (e.g., mona.ttf) have weird version number. michael@0: // fix them. michael@0: os2->version = 1; michael@0: return true; michael@0: } michael@0: michael@0: if (!table.ReadS16(&os2->x_height) || michael@0: !table.ReadS16(&os2->cap_height) || michael@0: !table.ReadU16(&os2->default_char) || michael@0: !table.ReadU16(&os2->break_char) || michael@0: !table.ReadU16(&os2->max_context)) { michael@0: return OTS_FAILURE_MSG("Failed to read os2 version 2 information"); michael@0: } michael@0: michael@0: if (os2->x_height < 0) { michael@0: OTS_WARNING("bad x_height: %d", os2->x_height); michael@0: os2->x_height = 0; michael@0: } michael@0: if (os2->cap_height < 0) { michael@0: OTS_WARNING("bad cap_height: %d", os2->cap_height); michael@0: os2->cap_height = 0; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_os2_should_serialise(OpenTypeFile *file) { michael@0: return file->os2 != NULL; michael@0: } michael@0: michael@0: bool ots_os2_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: const OpenTypeOS2 *os2 = file->os2; michael@0: michael@0: if (!out->WriteU16(os2->version) || michael@0: !out->WriteS16(os2->avg_char_width) || michael@0: !out->WriteU16(os2->weight_class) || michael@0: !out->WriteU16(os2->width_class) || michael@0: !out->WriteU16(os2->type) || michael@0: !out->WriteS16(os2->subscript_x_size) || michael@0: !out->WriteS16(os2->subscript_y_size) || michael@0: !out->WriteS16(os2->subscript_x_offset) || michael@0: !out->WriteS16(os2->subscript_y_offset) || michael@0: !out->WriteS16(os2->superscript_x_size) || michael@0: !out->WriteS16(os2->superscript_y_size) || michael@0: !out->WriteS16(os2->superscript_x_offset) || michael@0: !out->WriteS16(os2->superscript_y_offset) || michael@0: !out->WriteS16(os2->strikeout_size) || michael@0: !out->WriteS16(os2->strikeout_position) || michael@0: !out->WriteS16(os2->family_class)) { michael@0: return OTS_FAILURE_MSG("Failed to write basic OS2 information"); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < 10; ++i) { michael@0: if (!out->Write(&os2->panose[i], 1)) { michael@0: return OTS_FAILURE_MSG("Failed to write os2 panose information"); michael@0: } michael@0: } michael@0: michael@0: if (!out->WriteU32(os2->unicode_range_1) || michael@0: !out->WriteU32(os2->unicode_range_2) || michael@0: !out->WriteU32(os2->unicode_range_3) || michael@0: !out->WriteU32(os2->unicode_range_4) || michael@0: !out->WriteU32(os2->vendor_id) || michael@0: !out->WriteU16(os2->selection) || michael@0: !out->WriteU16(os2->first_char_index) || michael@0: !out->WriteU16(os2->last_char_index) || michael@0: !out->WriteS16(os2->typo_ascender) || michael@0: !out->WriteS16(os2->typo_descender) || michael@0: !out->WriteS16(os2->typo_linegap) || michael@0: !out->WriteU16(os2->win_ascent) || michael@0: !out->WriteU16(os2->win_descent)) { michael@0: return OTS_FAILURE_MSG("Failed to write os2 version 1 information"); michael@0: } michael@0: michael@0: if (os2->version < 1) { michael@0: return true; michael@0: } michael@0: michael@0: if (!out->WriteU32(os2->code_page_range_1) || michael@0: !out->WriteU32(os2->code_page_range_2)) { michael@0: return OTS_FAILURE_MSG("Failed to write codepage ranges"); michael@0: } michael@0: michael@0: if (os2->version < 2) { michael@0: return true; michael@0: } michael@0: michael@0: if (!out->WriteS16(os2->x_height) || michael@0: !out->WriteS16(os2->cap_height) || michael@0: !out->WriteU16(os2->default_char) || michael@0: !out->WriteU16(os2->break_char) || michael@0: !out->WriteU16(os2->max_context)) { michael@0: return OTS_FAILURE_MSG("Failed to write os2 version 2 information"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_os2_free(OpenTypeFile *file) { michael@0: delete file->os2; michael@0: } michael@0: michael@0: } // namespace ots