1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/ots/src/ltsh.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "ltsh.h" 1.9 + 1.10 +#include "maxp.h" 1.11 + 1.12 +// LTSH - Linear Threshold 1.13 +// http://www.microsoft.com/typography/otspec/ltsh.htm 1.14 + 1.15 +#define TABLE_NAME "LTSH" 1.16 + 1.17 +#define DROP_THIS_TABLE \ 1.18 + do { \ 1.19 + delete file->ltsh; \ 1.20 + file->ltsh = 0; \ 1.21 + OTS_FAILURE_MSG("Table discarded"); \ 1.22 + } while (0) 1.23 + 1.24 +namespace ots { 1.25 + 1.26 +bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { 1.27 + Buffer table(data, length); 1.28 + 1.29 + if (!file->maxp) { 1.30 + return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh"); 1.31 + } 1.32 + 1.33 + OpenTypeLTSH *ltsh = new OpenTypeLTSH; 1.34 + file->ltsh = ltsh; 1.35 + 1.36 + uint16_t num_glyphs = 0; 1.37 + if (!table.ReadU16(<sh->version) || 1.38 + !table.ReadU16(&num_glyphs)) { 1.39 + return OTS_FAILURE_MSG("Failed to read ltsh header"); 1.40 + } 1.41 + 1.42 + if (ltsh->version != 0) { 1.43 + OTS_WARNING("bad version: %u", ltsh->version); 1.44 + DROP_THIS_TABLE; 1.45 + return true; 1.46 + } 1.47 + 1.48 + if (num_glyphs != file->maxp->num_glyphs) { 1.49 + OTS_WARNING("bad num_glyphs: %u", num_glyphs); 1.50 + DROP_THIS_TABLE; 1.51 + return true; 1.52 + } 1.53 + 1.54 + ltsh->ypels.reserve(num_glyphs); 1.55 + for (unsigned i = 0; i < num_glyphs; ++i) { 1.56 + uint8_t pel = 0; 1.57 + if (!table.ReadU8(&pel)) { 1.58 + return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i); 1.59 + } 1.60 + ltsh->ypels.push_back(pel); 1.61 + } 1.62 + 1.63 + return true; 1.64 +} 1.65 + 1.66 +bool ots_ltsh_should_serialise(OpenTypeFile *file) { 1.67 + if (!file->glyf) return false; // this table is not for CFF fonts. 1.68 + return file->ltsh != NULL; 1.69 +} 1.70 + 1.71 +bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) { 1.72 + const OpenTypeLTSH *ltsh = file->ltsh; 1.73 + 1.74 + if (!out->WriteU16(ltsh->version) || 1.75 + !out->WriteU16(ltsh->ypels.size())) { 1.76 + return OTS_FAILURE_MSG("Failed to write pels size"); 1.77 + } 1.78 + for (unsigned i = 0; i < ltsh->ypels.size(); ++i) { 1.79 + if (!out->Write(&(ltsh->ypels[i]), 1)) { 1.80 + return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i); 1.81 + } 1.82 + } 1.83 + 1.84 + return true; 1.85 +} 1.86 + 1.87 +void ots_ltsh_free(OpenTypeFile *file) { 1.88 + delete file->ltsh; 1.89 +} 1.90 + 1.91 +} // namespace ots