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 "gasp.h" michael@0: michael@0: // gasp - Grid-fitting And Scan-conversion Procedure michael@0: // http://www.microsoft.com/typography/otspec/gasp.htm michael@0: michael@0: #define TABLE_NAME "gasp" michael@0: michael@0: #define DROP_THIS_TABLE \ michael@0: do { \ michael@0: delete file->gasp; \ michael@0: file->gasp = 0; \ michael@0: OTS_FAILURE_MSG("Table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_gasp_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: michael@0: OpenTypeGASP *gasp = new OpenTypeGASP; michael@0: file->gasp = gasp; michael@0: michael@0: uint16_t num_ranges = 0; michael@0: if (!table.ReadU16(&gasp->version) || michael@0: !table.ReadU16(&num_ranges)) { michael@0: return OTS_FAILURE_MSG("Failed to read table header"); michael@0: } michael@0: michael@0: if (gasp->version > 1) { michael@0: // Lots of Linux fonts have bad version numbers... michael@0: OTS_WARNING("bad version: %u", gasp->version); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: if (num_ranges == 0) { michael@0: OTS_WARNING("num_ranges is zero"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: gasp->gasp_ranges.reserve(num_ranges); michael@0: for (unsigned i = 0; i < num_ranges; ++i) { michael@0: uint16_t max_ppem = 0; michael@0: uint16_t behavior = 0; michael@0: if (!table.ReadU16(&max_ppem) || michael@0: !table.ReadU16(&behavior)) { michael@0: return OTS_FAILURE_MSG("Failed to read subrange %d", i); michael@0: } michael@0: if ((i > 0) && (gasp->gasp_ranges[i - 1].first >= max_ppem)) { michael@0: // The records in the gaspRange[] array must be sorted in order of michael@0: // increasing rangeMaxPPEM value. michael@0: OTS_WARNING("ranges are not sorted"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: if ((i == num_ranges - 1u) && // never underflow. michael@0: (max_ppem != 0xffffu)) { michael@0: OTS_WARNING("The last record should be 0xFFFF as a sentinel value " michael@0: "for rangeMaxPPEM"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: if (behavior >> 8) { michael@0: OTS_WARNING("undefined bits are used: %x", behavior); michael@0: // mask undefined bits. michael@0: behavior &= 0x000fu; michael@0: } michael@0: michael@0: if (gasp->version == 0 && (behavior >> 2) != 0) { michael@0: OTS_WARNING("changed the version number to 1"); michael@0: gasp->version = 1; michael@0: } michael@0: michael@0: gasp->gasp_ranges.push_back(std::make_pair(max_ppem, behavior)); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_gasp_should_serialise(OpenTypeFile *file) { michael@0: return file->gasp != NULL; michael@0: } michael@0: michael@0: bool ots_gasp_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: const OpenTypeGASP *gasp = file->gasp; michael@0: michael@0: if (!out->WriteU16(gasp->version) || michael@0: !out->WriteU16(gasp->gasp_ranges.size())) { michael@0: return OTS_FAILURE_MSG("failed to write gasp header"); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < gasp->gasp_ranges.size(); ++i) { michael@0: if (!out->WriteU16(gasp->gasp_ranges[i].first) || michael@0: !out->WriteU16(gasp->gasp_ranges[i].second)) { michael@0: return OTS_FAILURE_MSG("Failed to write gasp subtable %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_gasp_free(OpenTypeFile *file) { michael@0: delete file->gasp; michael@0: } michael@0: michael@0: } // namespace ots