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 "glyf.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "head.h" michael@0: #include "loca.h" michael@0: #include "maxp.h" michael@0: michael@0: // glyf - Glyph Data michael@0: // http://www.microsoft.com/typography/otspec/glyf.htm michael@0: michael@0: #define TABLE_NAME "glyf" michael@0: michael@0: namespace { michael@0: michael@0: bool ParseFlagsForSimpleGlyph(ots::OpenTypeFile *file, michael@0: ots::Buffer *table, michael@0: uint32_t gly_length, michael@0: uint32_t num_flags, michael@0: uint32_t *flags_count_logical, michael@0: uint32_t *flags_count_physical, michael@0: uint32_t *xy_coordinates_length) { michael@0: uint8_t flag = 0; michael@0: if (!table->ReadU8(&flag)) { michael@0: return OTS_FAILURE_MSG("Can't read flag"); michael@0: } michael@0: michael@0: uint32_t delta = 0; michael@0: if (flag & (1u << 1)) { // x-Short michael@0: ++delta; michael@0: } else if (!(flag & (1u << 4))) { michael@0: delta += 2; michael@0: } michael@0: michael@0: if (flag & (1u << 2)) { // y-Short michael@0: ++delta; michael@0: } else if (!(flag & (1u << 5))) { michael@0: delta += 2; michael@0: } michael@0: michael@0: if (flag & (1u << 3)) { // repeat michael@0: if (*flags_count_logical + 1 >= num_flags) { michael@0: return OTS_FAILURE_MSG("Count too high (%d + 1 >= %d)", *flags_count_logical, num_flags); michael@0: } michael@0: uint8_t repeat = 0; michael@0: if (!table->ReadU8(&repeat)) { michael@0: return OTS_FAILURE_MSG("Can't read repeat value"); michael@0: } michael@0: if (repeat == 0) { michael@0: return OTS_FAILURE_MSG("Zero repeat"); michael@0: } michael@0: delta += (delta * repeat); michael@0: michael@0: *flags_count_logical += repeat; michael@0: if (*flags_count_logical >= num_flags) { michael@0: return OTS_FAILURE_MSG("Count too high (%d >= %d)", *flags_count_logical, num_flags); michael@0: } michael@0: ++(*flags_count_physical); michael@0: } michael@0: michael@0: if ((flag & (1u << 6)) || (flag & (1u << 7))) { // reserved flags michael@0: return OTS_FAILURE_MSG("Bad flag value (%d)", flag); michael@0: } michael@0: michael@0: *xy_coordinates_length += delta; michael@0: if (gly_length < *xy_coordinates_length) { michael@0: return OTS_FAILURE_MSG("Glyph coordinates length too low (%d < %d)", gly_length, *xy_coordinates_length); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ParseSimpleGlyph(ots::OpenTypeFile *file, const uint8_t *data, michael@0: ots::Buffer *table, int16_t num_contours, michael@0: uint32_t gly_offset, uint32_t gly_length, michael@0: uint32_t *new_size) { michael@0: ots::OpenTypeGLYF *glyf = file->glyf; michael@0: michael@0: // read the end-points array michael@0: uint16_t num_flags = 0; michael@0: for (int i = 0; i < num_contours; ++i) { michael@0: uint16_t tmp_index = 0; michael@0: if (!table->ReadU16(&tmp_index)) { michael@0: return OTS_FAILURE_MSG("Can't read contour index %d", i); michael@0: } michael@0: if (tmp_index == 0xffffu) { michael@0: return OTS_FAILURE_MSG("Bad contour index %d", i); michael@0: } michael@0: // check if the indices are monotonically increasing michael@0: if (i && (tmp_index + 1 <= num_flags)) { michael@0: return OTS_FAILURE_MSG("Decreasing contour index %d + 1 <= %d", tmp_index, num_flags); michael@0: } michael@0: num_flags = tmp_index + 1; michael@0: } michael@0: michael@0: uint16_t bytecode_length = 0; michael@0: if (!table->ReadU16(&bytecode_length)) { michael@0: return OTS_FAILURE_MSG("Can't read bytecode length"); michael@0: } michael@0: if ((file->maxp->version_1) && michael@0: (file->maxp->max_size_glyf_instructions < bytecode_length)) { michael@0: return OTS_FAILURE_MSG("Bytecode length too high %d", bytecode_length); michael@0: } michael@0: michael@0: const uint32_t gly_header_length = 10 + num_contours * 2 + 2; michael@0: if (gly_length < (gly_header_length + bytecode_length)) { michael@0: return OTS_FAILURE_MSG("Glyph header length too high %d", gly_header_length); michael@0: } michael@0: michael@0: if (ots::g_transcode_hints) { michael@0: glyf->iov.push_back(std::make_pair( michael@0: data + gly_offset, michael@0: static_cast(gly_header_length + bytecode_length))); michael@0: } else { michael@0: // enqueue two vectors: the glyph data up to the bytecode length, then michael@0: // a pointer to a static uint16_t 0 to overwrite the length. michael@0: glyf->iov.push_back(std::make_pair( michael@0: data + gly_offset, michael@0: static_cast(gly_header_length - 2))); michael@0: glyf->iov.push_back(std::make_pair((const uint8_t*) "\x00\x00", michael@0: static_cast(2))); michael@0: } michael@0: michael@0: if (!table->Skip(bytecode_length)) { michael@0: return OTS_FAILURE_MSG("Can't skip bytecode of length %d", bytecode_length); michael@0: } michael@0: michael@0: uint32_t flags_count_physical = 0; // on memory michael@0: uint32_t xy_coordinates_length = 0; michael@0: for (uint32_t flags_count_logical = 0; michael@0: flags_count_logical < num_flags; michael@0: ++flags_count_logical, ++flags_count_physical) { michael@0: if (!ParseFlagsForSimpleGlyph(file, michael@0: table, michael@0: gly_length, michael@0: num_flags, michael@0: &flags_count_logical, michael@0: &flags_count_physical, michael@0: &xy_coordinates_length)) { michael@0: return OTS_FAILURE_MSG("Failed to parse glyph flags %d", flags_count_logical); michael@0: } michael@0: } michael@0: michael@0: if (gly_length < (gly_header_length + bytecode_length + michael@0: flags_count_physical + xy_coordinates_length)) { michael@0: return OTS_FAILURE_MSG("Glyph too short %d", gly_length); michael@0: } michael@0: michael@0: if (gly_length - (gly_header_length + bytecode_length + michael@0: flags_count_physical + xy_coordinates_length) > 3) { michael@0: // We allow 0-3 bytes difference since gly_length is 4-bytes aligned, michael@0: // zero-padded length. michael@0: return OTS_FAILURE_MSG("Invalid glyph length %d", gly_length); michael@0: } michael@0: michael@0: glyf->iov.push_back(std::make_pair( michael@0: data + gly_offset + gly_header_length + bytecode_length, michael@0: static_cast(flags_count_physical + xy_coordinates_length))); michael@0: michael@0: *new_size michael@0: = gly_header_length + flags_count_physical + xy_coordinates_length; michael@0: if (ots::g_transcode_hints) { michael@0: *new_size += bytecode_length; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: } // namespace michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_glyf_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: michael@0: if (!file->maxp || !file->loca || !file->head) { michael@0: return OTS_FAILURE_MSG("Missing maxp or loca or head table needed by glyf table"); michael@0: } michael@0: michael@0: OpenTypeGLYF *glyf = new OpenTypeGLYF; michael@0: file->glyf = glyf; michael@0: michael@0: const unsigned num_glyphs = file->maxp->num_glyphs; michael@0: std::vector &offsets = file->loca->offsets; michael@0: michael@0: if (offsets.size() != num_glyphs + 1) { michael@0: return OTS_FAILURE_MSG("Invalide glyph offsets size %ld != %d", offsets.size(), num_glyphs + 1); michael@0: } michael@0: michael@0: std::vector resulting_offsets(num_glyphs + 1); michael@0: uint32_t current_offset = 0; michael@0: michael@0: for (unsigned i = 0; i < num_glyphs; ++i) { michael@0: const unsigned gly_offset = offsets[i]; michael@0: // The LOCA parser checks that these values are monotonic michael@0: const unsigned gly_length = offsets[i + 1] - offsets[i]; michael@0: if (!gly_length) { michael@0: // this glyph has no outline (e.g. the space charactor) michael@0: resulting_offsets[i] = current_offset; michael@0: continue; michael@0: } michael@0: michael@0: if (gly_offset >= length) { michael@0: return OTS_FAILURE_MSG("Glyph %d offset %d too high %ld", i, gly_offset, length); michael@0: } michael@0: // Since these are unsigned types, the compiler is not allowed to assume michael@0: // that they never overflow. michael@0: if (gly_offset + gly_length < gly_offset) { michael@0: return OTS_FAILURE_MSG("Glyph %d length (%d < 0)!", i, gly_length); michael@0: } michael@0: if (gly_offset + gly_length > length) { michael@0: return OTS_FAILURE_MSG("Glyph %d length %d too high", i, gly_length); michael@0: } michael@0: michael@0: table.set_offset(gly_offset); michael@0: int16_t num_contours, xmin, ymin, xmax, ymax; michael@0: if (!table.ReadS16(&num_contours) || michael@0: !table.ReadS16(&xmin) || michael@0: !table.ReadS16(&ymin) || michael@0: !table.ReadS16(&xmax) || michael@0: !table.ReadS16(&ymax)) { michael@0: return OTS_FAILURE_MSG("Can't read glyph %d header", i); michael@0: } michael@0: michael@0: if (num_contours <= -2) { michael@0: // -2, -3, -4, ... are reserved for future use. michael@0: return OTS_FAILURE_MSG("Bad number of contours %d in glyph %d", num_contours, i); michael@0: } michael@0: michael@0: // workaround for fonts in http://www.princexml.com/fonts/ michael@0: if ((xmin == 32767) && michael@0: (xmax == -32767) && michael@0: (ymin == 32767) && michael@0: (ymax == -32767)) { michael@0: OTS_WARNING("bad xmin/xmax/ymin/ymax values"); michael@0: xmin = xmax = ymin = ymax = 0; michael@0: } michael@0: michael@0: if (xmin > xmax || ymin > ymax) { michael@0: return OTS_FAILURE_MSG("Bad bounding box values bl=(%d, %d), tr=(%d, %d) in glyph %d", xmin, ymin, xmax, ymax, i); michael@0: } michael@0: michael@0: unsigned new_size = 0; michael@0: if (num_contours >= 0) { michael@0: // this is a simple glyph and might contain bytecode michael@0: if (!ParseSimpleGlyph(file, data, &table, michael@0: num_contours, gly_offset, gly_length, &new_size)) { michael@0: return OTS_FAILURE_MSG("Failed to parse glyph %d", i); michael@0: } michael@0: } else { michael@0: // it's a composite glyph without any bytecode. Enqueue the whole thing michael@0: glyf->iov.push_back(std::make_pair(data + gly_offset, michael@0: static_cast(gly_length))); michael@0: new_size = gly_length; michael@0: } michael@0: michael@0: resulting_offsets[i] = current_offset; michael@0: // glyphs must be four byte aligned michael@0: // TODO(yusukes): investigate whether this padding is really necessary. michael@0: // Which part of the spec requires this? michael@0: const unsigned padding = (4 - (new_size & 3)) % 4; michael@0: if (padding) { michael@0: glyf->iov.push_back(std::make_pair( michael@0: reinterpret_cast("\x00\x00\x00\x00"), michael@0: static_cast(padding))); michael@0: new_size += padding; michael@0: } michael@0: current_offset += new_size; michael@0: } michael@0: resulting_offsets[num_glyphs] = current_offset; michael@0: michael@0: const uint16_t max16 = std::numeric_limits::max(); michael@0: if ((*std::max_element(resulting_offsets.begin(), michael@0: resulting_offsets.end()) >= (max16 * 2u)) && michael@0: (file->head->index_to_loc_format != 1)) { michael@0: OTS_WARNING("2-bytes indexing is not possible (due to the padding above)"); michael@0: file->head->index_to_loc_format = 1; michael@0: } michael@0: michael@0: file->loca->offsets = resulting_offsets; michael@0: return true; michael@0: } michael@0: michael@0: bool ots_glyf_should_serialise(OpenTypeFile *file) { michael@0: return file->glyf != NULL; michael@0: } michael@0: michael@0: bool ots_glyf_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: const OpenTypeGLYF *glyf = file->glyf; michael@0: michael@0: for (unsigned i = 0; i < glyf->iov.size(); ++i) { michael@0: if (!out->Write(glyf->iov[i].first, glyf->iov[i].second)) { michael@0: return OTS_FAILURE_MSG("Falied to write glyph %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_glyf_free(OpenTypeFile *file) { michael@0: delete file->glyf; michael@0: } michael@0: michael@0: } // namespace ots