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 "ltsh.h" michael@0: michael@0: #include "maxp.h" michael@0: michael@0: // LTSH - Linear Threshold michael@0: // http://www.microsoft.com/typography/otspec/ltsh.htm michael@0: michael@0: #define TABLE_NAME "LTSH" michael@0: michael@0: #define DROP_THIS_TABLE \ michael@0: do { \ michael@0: delete file->ltsh; \ michael@0: file->ltsh = 0; \ michael@0: OTS_FAILURE_MSG("Table discarded"); \ michael@0: } while (0) michael@0: michael@0: namespace ots { michael@0: michael@0: bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { michael@0: Buffer table(data, length); michael@0: michael@0: if (!file->maxp) { michael@0: return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh"); michael@0: } michael@0: michael@0: OpenTypeLTSH *ltsh = new OpenTypeLTSH; michael@0: file->ltsh = ltsh; michael@0: michael@0: uint16_t num_glyphs = 0; michael@0: if (!table.ReadU16(<sh->version) || michael@0: !table.ReadU16(&num_glyphs)) { michael@0: return OTS_FAILURE_MSG("Failed to read ltsh header"); michael@0: } michael@0: michael@0: if (ltsh->version != 0) { michael@0: OTS_WARNING("bad version: %u", ltsh->version); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: if (num_glyphs != file->maxp->num_glyphs) { michael@0: OTS_WARNING("bad num_glyphs: %u", num_glyphs); michael@0: DROP_THIS_TABLE; michael@0: return true; michael@0: } michael@0: michael@0: ltsh->ypels.reserve(num_glyphs); michael@0: for (unsigned i = 0; i < num_glyphs; ++i) { michael@0: uint8_t pel = 0; michael@0: if (!table.ReadU8(&pel)) { michael@0: return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i); michael@0: } michael@0: ltsh->ypels.push_back(pel); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ots_ltsh_should_serialise(OpenTypeFile *file) { michael@0: if (!file->glyf) return false; // this table is not for CFF fonts. michael@0: return file->ltsh != NULL; michael@0: } michael@0: michael@0: bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) { michael@0: const OpenTypeLTSH *ltsh = file->ltsh; michael@0: michael@0: if (!out->WriteU16(ltsh->version) || michael@0: !out->WriteU16(ltsh->ypels.size())) { michael@0: return OTS_FAILURE_MSG("Failed to write pels size"); michael@0: } michael@0: for (unsigned i = 0; i < ltsh->ypels.size(); ++i) { michael@0: if (!out->Write(&(ltsh->ypels[i]), 1)) { michael@0: return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i); michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void ots_ltsh_free(OpenTypeFile *file) { michael@0: delete file->ltsh; michael@0: } michael@0: michael@0: } // namespace ots