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 "vdmx.h" michael@0: michael@0: // VDMX - Vertical Device Metrics michael@0: // http://www.microsoft.com/typography/otspec/vdmx.htm michael@0: michael@0: #define TABLE_NAME "VDMX" michael@0: michael@0: #define DROP_THIS_TABLE \ michael@0: do { \ michael@0: delete file->vdmx; \ michael@0: file->vdmx = 0; \ michael@0: OTS_FAILURE_MSG("Table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_vdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: file->vdmx = new OpenTypeVDMX; michael@0: OpenTypeVDMX * const vdmx = file->vdmx; michael@0: michael@0: if (!table.ReadU16(&vdmx->version) || michael@0: !table.ReadU16(&vdmx->num_recs) || michael@0: !table.ReadU16(&vdmx->num_ratios)) { michael@0: return OTS_FAILURE_MSG("Failed to read table header"); michael@0: } michael@0: michael@0: if (vdmx->version > 1) { michael@0: OTS_WARNING("bad version: %u", vdmx->version); michael@0: DROP_THIS_TABLE; michael@0: return true; // continue transcoding michael@0: } michael@0: michael@0: vdmx->rat_ranges.reserve(vdmx->num_ratios); michael@0: for (unsigned i = 0; i < vdmx->num_ratios; ++i) { michael@0: OpenTypeVDMXRatioRecord rec; michael@0: michael@0: if (!table.ReadU8(&rec.charset) || michael@0: !table.ReadU8(&rec.x_ratio) || michael@0: !table.ReadU8(&rec.y_start_ratio) || michael@0: !table.ReadU8(&rec.y_end_ratio)) { michael@0: return OTS_FAILURE_MSG("Failed to read ratio header %d", i); michael@0: } michael@0: michael@0: if (rec.charset > 1) { michael@0: OTS_WARNING("bad charset: %u", rec.charset); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: if (rec.y_start_ratio > rec.y_end_ratio) { michael@0: OTS_WARNING("bad y ratio"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: // All values set to zero signal the default grouping to use; michael@0: // if present, this must be the last Ratio group in the table. michael@0: if ((i < vdmx->num_ratios - 1u) && michael@0: (rec.x_ratio == 0) && michael@0: (rec.y_start_ratio == 0) && michael@0: (rec.y_end_ratio == 0)) { michael@0: // workaround for fonts which have 2 or more {0, 0, 0} terminators. michael@0: OTS_WARNING("superfluous terminator found"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: vdmx->rat_ranges.push_back(rec); michael@0: } michael@0: michael@0: vdmx->offsets.reserve(vdmx->num_ratios); michael@0: const size_t current_offset = table.offset(); michael@0: // current_offset is less than (2 bytes * 3) + (4 bytes * USHRT_MAX) = 256k. michael@0: for (unsigned i = 0; i < vdmx->num_ratios; ++i) { michael@0: uint16_t offset; michael@0: if (!table.ReadU16(&offset)) { michael@0: return OTS_FAILURE_MSG("Failed to read ratio offset %d", i); michael@0: } michael@0: if (current_offset + offset >= length) { // thus doesn't overflow. michael@0: return OTS_FAILURE_MSG("Bad ratio offset %d for ration %d", offset, i); michael@0: } michael@0: michael@0: vdmx->offsets.push_back(offset); michael@0: } michael@0: michael@0: vdmx->groups.reserve(vdmx->num_recs); michael@0: for (unsigned i = 0; i < vdmx->num_recs; ++i) { michael@0: OpenTypeVDMXGroup group; michael@0: if (!table.ReadU16(&group.recs) || michael@0: !table.ReadU8(&group.startsz) || michael@0: !table.ReadU8(&group.endsz)) { michael@0: return OTS_FAILURE_MSG("Failed to read record header %d", i); michael@0: } michael@0: group.entries.reserve(group.recs); michael@0: for (unsigned j = 0; j < group.recs; ++j) { michael@0: OpenTypeVDMXVTable vt; michael@0: if (!table.ReadU16(&vt.y_pel_height) || michael@0: !table.ReadS16(&vt.y_max) || michael@0: !table.ReadS16(&vt.y_min)) { michael@0: return OTS_FAILURE_MSG("Failed to read reacord %d group %d", i, j); michael@0: } michael@0: if (vt.y_max < vt.y_min) { michael@0: OTS_WARNING("bad y min/max"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: // This table must appear in sorted order (sorted by yPelHeight), michael@0: // but need not be continuous. michael@0: if ((j != 0) && (group.entries[j - 1].y_pel_height >= vt.y_pel_height)) { michael@0: OTS_WARNING("the table is not sorted"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: group.entries.push_back(vt); michael@0: } michael@0: vdmx->groups.push_back(group); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_vdmx_should_serialise(OpenTypeFile *file) { michael@0: if (!file->glyf) return false; // this table is not for CFF fonts. michael@0: return file->vdmx != NULL; michael@0: } michael@0: michael@0: bool ots_vdmx_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: OpenTypeVDMX * const vdmx = file->vdmx; michael@0: michael@0: if (!out->WriteU16(vdmx->version) || michael@0: !out->WriteU16(vdmx->num_recs) || michael@0: !out->WriteU16(vdmx->num_ratios)) { michael@0: return OTS_FAILURE_MSG("Failed to write table header"); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < vdmx->rat_ranges.size(); ++i) { michael@0: const OpenTypeVDMXRatioRecord& rec = vdmx->rat_ranges[i]; michael@0: if (!out->Write(&rec.charset, 1) || michael@0: !out->Write(&rec.x_ratio, 1) || michael@0: !out->Write(&rec.y_start_ratio, 1) || michael@0: !out->Write(&rec.y_end_ratio, 1)) { michael@0: return OTS_FAILURE_MSG("Failed to write ratio %d", i); michael@0: } michael@0: } michael@0: michael@0: for (unsigned i = 0; i < vdmx->offsets.size(); ++i) { michael@0: if (!out->WriteU16(vdmx->offsets[i])) { michael@0: return OTS_FAILURE_MSG("Failed to write ratio offset %d", i); michael@0: } michael@0: } michael@0: michael@0: for (unsigned i = 0; i < vdmx->groups.size(); ++i) { michael@0: const OpenTypeVDMXGroup& group = vdmx->groups[i]; michael@0: if (!out->WriteU16(group.recs) || michael@0: !out->Write(&group.startsz, 1) || michael@0: !out->Write(&group.endsz, 1)) { michael@0: return OTS_FAILURE_MSG("Failed to write group %d", i); michael@0: } michael@0: for (unsigned j = 0; j < group.entries.size(); ++j) { michael@0: const OpenTypeVDMXVTable& vt = group.entries[j]; michael@0: if (!out->WriteU16(vt.y_pel_height) || michael@0: !out->WriteS16(vt.y_max) || michael@0: !out->WriteS16(vt.y_min)) { michael@0: return OTS_FAILURE_MSG("Failed to write group %d entry %d", i, j); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_vdmx_free(OpenTypeFile *file) { michael@0: delete file->vdmx; michael@0: } michael@0: michael@0: } // namespace ots