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 "maxp.h" michael@0: michael@0: // maxp - Maximum Profile michael@0: // http://www.microsoft.com/typography/otspec/maxp.htm michael@0: michael@0: #define TABLE_NAME "maxp" michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_maxp_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: michael@0: OpenTypeMAXP *maxp = new OpenTypeMAXP; michael@0: file->maxp = maxp; michael@0: michael@0: uint32_t version = 0; michael@0: if (!table.ReadU32(&version)) { michael@0: return OTS_FAILURE_MSG("Failed to read version of maxp table"); michael@0: } michael@0: michael@0: if (version >> 16 > 1) { michael@0: return OTS_FAILURE_MSG("Bad maxp version %d", version); michael@0: } michael@0: michael@0: if (!table.ReadU16(&maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to read number of glyphs from maxp table"); michael@0: } michael@0: michael@0: if (!maxp->num_glyphs) { michael@0: return OTS_FAILURE_MSG("Bad number of glyphs 0 in maxp table"); michael@0: } michael@0: michael@0: if (version >> 16 == 1) { michael@0: maxp->version_1 = true; michael@0: if (!table.ReadU16(&maxp->max_points) || michael@0: !table.ReadU16(&maxp->max_contours) || michael@0: !table.ReadU16(&maxp->max_c_points) || michael@0: !table.ReadU16(&maxp->max_c_contours) || michael@0: !table.ReadU16(&maxp->max_zones) || michael@0: !table.ReadU16(&maxp->max_t_points) || michael@0: !table.ReadU16(&maxp->max_storage) || michael@0: !table.ReadU16(&maxp->max_fdefs) || michael@0: !table.ReadU16(&maxp->max_idefs) || michael@0: !table.ReadU16(&maxp->max_stack) || michael@0: !table.ReadU16(&maxp->max_size_glyf_instructions) || michael@0: !table.ReadU16(&maxp->max_c_components) || michael@0: !table.ReadU16(&maxp->max_c_depth)) { michael@0: return OTS_FAILURE_MSG("Failed to read maxp table"); michael@0: } michael@0: michael@0: if (maxp->max_zones == 0) { michael@0: // workaround for ipa*.ttf Japanese fonts. michael@0: OTS_WARNING("bad max_zones: %u", maxp->max_zones); michael@0: maxp->max_zones = 1; michael@0: } else if (maxp->max_zones == 3) { michael@0: // workaround for Ecolier-*.ttf fonts. michael@0: OTS_WARNING("bad max_zones: %u", maxp->max_zones); michael@0: maxp->max_zones = 2; michael@0: } michael@0: michael@0: if ((maxp->max_zones != 1) && (maxp->max_zones != 2)) { michael@0: return OTS_FAILURE_MSG("Bad max zones %d in maxp", maxp->max_zones); michael@0: } michael@0: } else { michael@0: maxp->version_1 = false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_maxp_should_serialise(OpenTypeFile *file) { michael@0: return file->maxp != NULL; michael@0: } michael@0: michael@0: bool ots_maxp_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: const OpenTypeMAXP *maxp = file->maxp; michael@0: michael@0: if (!out->WriteU32(maxp->version_1 ? 0x00010000 : 0x00005000) || michael@0: !out->WriteU16(maxp->num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to write maxp version or number of glyphs"); michael@0: } michael@0: michael@0: if (!maxp->version_1) return true; michael@0: michael@0: if (!out->WriteU16(maxp->max_points) || michael@0: !out->WriteU16(maxp->max_contours) || michael@0: !out->WriteU16(maxp->max_c_points) || michael@0: !out->WriteU16(maxp->max_c_contours)) { michael@0: return OTS_FAILURE_MSG("Failed to write maxp"); michael@0: } michael@0: michael@0: if (g_transcode_hints) { michael@0: if (!out->WriteU16(maxp->max_zones) || michael@0: !out->WriteU16(maxp->max_t_points) || michael@0: !out->WriteU16(maxp->max_storage) || michael@0: !out->WriteU16(maxp->max_fdefs) || michael@0: !out->WriteU16(maxp->max_idefs) || michael@0: !out->WriteU16(maxp->max_stack) || michael@0: !out->WriteU16(maxp->max_size_glyf_instructions)) { michael@0: return OTS_FAILURE_MSG("Failed to write more maxp"); michael@0: } michael@0: } else { michael@0: if (!out->WriteU16(1) || // max zones michael@0: !out->WriteU16(0) || // max twilight points michael@0: !out->WriteU16(0) || // max storage michael@0: !out->WriteU16(0) || // max function defs michael@0: !out->WriteU16(0) || // max instruction defs michael@0: !out->WriteU16(0) || // max stack elements michael@0: !out->WriteU16(0)) { // max instruction byte count michael@0: return OTS_FAILURE_MSG("Failed to write more maxp"); michael@0: } michael@0: } michael@0: michael@0: if (!out->WriteU16(maxp->max_c_components) || michael@0: !out->WriteU16(maxp->max_c_depth)) { michael@0: return OTS_FAILURE_MSG("Failed to write yet more maxp"); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_maxp_free(OpenTypeFile *file) { michael@0: delete file->maxp; michael@0: } michael@0: michael@0: } // namespace ots