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 "vorg.h" michael@0: michael@0: #include michael@0: michael@0: // VORG - Vertical Origin Table michael@0: // http://www.microsoft.com/typography/otspec/vorg.htm michael@0: michael@0: #define TABLE_NAME "VORG" michael@0: michael@0: #define DROP_THIS_TABLE \ michael@0: do { \ michael@0: delete file->vorg; \ michael@0: file->vorg = 0; \ michael@0: OTS_FAILURE_MSG("Table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_vorg_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: file->vorg = new OpenTypeVORG; michael@0: OpenTypeVORG * const vorg = file->vorg; michael@0: michael@0: uint16_t num_recs; michael@0: if (!table.ReadU16(&vorg->major_version) || michael@0: !table.ReadU16(&vorg->minor_version) || michael@0: !table.ReadS16(&vorg->default_vert_origin_y) || michael@0: !table.ReadU16(&num_recs)) { michael@0: return OTS_FAILURE_MSG("Failed to read header"); michael@0: } michael@0: if (vorg->major_version != 1) { michael@0: OTS_WARNING("bad major version: %u", vorg->major_version); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: if (vorg->minor_version != 0) { michael@0: OTS_WARNING("bad minor version: %u", vorg->minor_version); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: // num_recs might be zero (e.g., DFHSMinchoPro5-W3-Demo.otf). michael@0: if (!num_recs) { michael@0: return true; michael@0: } michael@0: michael@0: uint16_t last_glyph_index = 0; michael@0: vorg->metrics.reserve(num_recs); michael@0: for (unsigned i = 0; i < num_recs; ++i) { michael@0: OpenTypeVORGMetrics rec; michael@0: michael@0: if (!table.ReadU16(&rec.glyph_index) || michael@0: !table.ReadS16(&rec.vert_origin_y)) { michael@0: return OTS_FAILURE_MSG("Failed to read record %d", i); michael@0: } michael@0: if ((i != 0) && (rec.glyph_index <= last_glyph_index)) { michael@0: OTS_WARNING("the table is not sorted"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: last_glyph_index = rec.glyph_index; michael@0: michael@0: vorg->metrics.push_back(rec); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_vorg_should_serialise(OpenTypeFile *file) { michael@0: if (!file->cff) return false; // this table is not for fonts with TT glyphs. michael@0: return file->vorg != NULL; michael@0: } michael@0: michael@0: bool ots_vorg_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: OpenTypeVORG * const vorg = file->vorg; michael@0: michael@0: if (!out->WriteU16(vorg->major_version) || michael@0: !out->WriteU16(vorg->minor_version) || michael@0: !out->WriteS16(vorg->default_vert_origin_y) || michael@0: !out->WriteU16(vorg->metrics.size())) { michael@0: return OTS_FAILURE_MSG("Failed to write table header"); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < vorg->metrics.size(); ++i) { michael@0: const OpenTypeVORGMetrics& rec = vorg->metrics[i]; michael@0: if (!out->WriteU16(rec.glyph_index) || michael@0: !out->WriteS16(rec.vert_origin_y)) { michael@0: return OTS_FAILURE_MSG("Failed to write record %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_vorg_free(OpenTypeFile *file) { michael@0: delete file->vorg; michael@0: } michael@0: michael@0: } // namespace ots