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 "hdmx.h" michael@0: #include "head.h" michael@0: #include "maxp.h" michael@0: michael@0: // hdmx - Horizontal Device Metrics michael@0: // http://www.microsoft.com/typography/otspec/hdmx.htm michael@0: michael@0: #define TABLE_NAME "hdmx" michael@0: michael@0: #define DROP_THIS_TABLE \ michael@0: do { \ michael@0: delete file->hdmx; \ michael@0: file->hdmx = 0; \ michael@0: OTS_FAILURE_MSG("Table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_hdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: file->hdmx = new OpenTypeHDMX; michael@0: OpenTypeHDMX * const hdmx = file->hdmx; michael@0: michael@0: if (!file->head || !file->maxp) { michael@0: return OTS_FAILURE_MSG("Missing maxp or head tables in font, needed by hdmx"); michael@0: } michael@0: michael@0: if ((file->head->flags & 0x14) == 0) { michael@0: // http://www.microsoft.com/typography/otspec/recom.htm michael@0: OTS_WARNING("the table should not be present when bit 2 and 4 of the " michael@0: "head->flags are not set"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: int16_t num_recs; michael@0: if (!table.ReadU16(&hdmx->version) || michael@0: !table.ReadS16(&num_recs) || michael@0: !table.ReadS32(&hdmx->size_device_record)) { michael@0: return OTS_FAILURE_MSG("Failed to read hdmx header"); michael@0: } michael@0: if (hdmx->version != 0) { michael@0: OTS_WARNING("bad version: %u", hdmx->version); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: if (num_recs <= 0) { michael@0: OTS_WARNING("bad num_recs: %d", num_recs); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: const int32_t actual_size_device_record = file->maxp->num_glyphs + 2; michael@0: if (hdmx->size_device_record < actual_size_device_record) { michael@0: OTS_WARNING("bad hdmx->size_device_record: %d", hdmx->size_device_record); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: hdmx->pad_len = hdmx->size_device_record - actual_size_device_record; michael@0: if (hdmx->pad_len > 3) { michael@0: return OTS_FAILURE_MSG("Bad padding %d", hdmx->pad_len); michael@0: } michael@0: michael@0: uint8_t last_pixel_size = 0; michael@0: hdmx->records.reserve(num_recs); michael@0: for (int i = 0; i < num_recs; ++i) { michael@0: OpenTypeHDMXDeviceRecord rec; michael@0: michael@0: if (!table.ReadU8(&rec.pixel_size) || michael@0: !table.ReadU8(&rec.max_width)) { michael@0: return OTS_FAILURE_MSG("Failed to read hdmx record %d", i); michael@0: } michael@0: if ((i != 0) && michael@0: (rec.pixel_size <= last_pixel_size)) { michael@0: OTS_WARNING("records are not sorted"); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: last_pixel_size = rec.pixel_size; michael@0: michael@0: rec.widths.reserve(file->maxp->num_glyphs); michael@0: for (unsigned j = 0; j < file->maxp->num_glyphs; ++j) { michael@0: uint8_t width; michael@0: if (!table.ReadU8(&width)) { michael@0: return OTS_FAILURE_MSG("Failed to read glyph width %d in record %d", j, i); michael@0: } michael@0: rec.widths.push_back(width); michael@0: } michael@0: michael@0: if ((hdmx->pad_len > 0) && michael@0: !table.Skip(hdmx->pad_len)) { michael@0: return OTS_FAILURE_MSG("Failed to skip padding %d", hdmx->pad_len); michael@0: } michael@0: michael@0: hdmx->records.push_back(rec); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_hdmx_should_serialise(OpenTypeFile *file) { michael@0: if (!file->hdmx) return false; michael@0: if (!file->glyf) return false; // this table is not for CFF fonts. michael@0: return true; michael@0: } michael@0: michael@0: bool ots_hdmx_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: OpenTypeHDMX * const hdmx = file->hdmx; michael@0: michael@0: if (!out->WriteU16(hdmx->version) || michael@0: !out->WriteS16(hdmx->records.size()) || michael@0: !out->WriteS32(hdmx->size_device_record)) { michael@0: return OTS_FAILURE_MSG("Failed to write hdmx header"); michael@0: } michael@0: michael@0: for (unsigned i = 0; i < hdmx->records.size(); ++i) { michael@0: const OpenTypeHDMXDeviceRecord& rec = hdmx->records[i]; michael@0: if (!out->Write(&rec.pixel_size, 1) || michael@0: !out->Write(&rec.max_width, 1) || michael@0: !out->Write(&rec.widths[0], rec.widths.size())) { michael@0: return OTS_FAILURE_MSG("Failed to write hdmx record %d", i); michael@0: } michael@0: if ((hdmx->pad_len > 0) && michael@0: !out->Write((const uint8_t *)"\x00\x00\x00", hdmx->pad_len)) { michael@0: return OTS_FAILURE_MSG("Failed to write hdmx padding of length %d", hdmx->pad_len); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_hdmx_free(OpenTypeFile *file) { michael@0: delete file->hdmx; michael@0: } michael@0: michael@0: } // namespace ots