Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / ots / src / ltsh.cc
blob418c1598e43237a249003cd9c3fad64c4112ad02
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ltsh.h"
7 #include "maxp.h"
9 // LTSH - Linear Threshold
10 // http://www.microsoft.com/typography/otspec/ltsh.htm
12 #define TABLE_NAME "LTSH"
14 #define DROP_THIS_TABLE(...) \
15 do { \
16 OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__); \
17 OTS_FAILURE_MSG("Table discarded"); \
18 delete file->ltsh; \
19 file->ltsh = 0; \
20 } while (0)
22 namespace ots {
24 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
25 Buffer table(data, length);
27 if (!file->maxp) {
28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh");
31 OpenTypeLTSH *ltsh = new OpenTypeLTSH;
32 file->ltsh = ltsh;
34 uint16_t num_glyphs = 0;
35 if (!table.ReadU16(&ltsh->version) ||
36 !table.ReadU16(&num_glyphs)) {
37 return OTS_FAILURE_MSG("Failed to read ltsh header");
40 if (ltsh->version != 0) {
41 DROP_THIS_TABLE("bad version: %u", ltsh->version);
42 return true;
45 if (num_glyphs != file->maxp->num_glyphs) {
46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs);
47 return true;
50 ltsh->ypels.reserve(num_glyphs);
51 for (unsigned i = 0; i < num_glyphs; ++i) {
52 uint8_t pel = 0;
53 if (!table.ReadU8(&pel)) {
54 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i);
56 ltsh->ypels.push_back(pel);
59 return true;
62 bool ots_ltsh_should_serialise(OpenTypeFile *file) {
63 if (!file->glyf) return false; // this table is not for CFF fonts.
64 return file->ltsh != NULL;
67 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) {
68 const OpenTypeLTSH *ltsh = file->ltsh;
70 const uint16_t num_ypels = static_cast<uint16_t>(ltsh->ypels.size());
71 if (num_ypels != ltsh->ypels.size() ||
72 !out->WriteU16(ltsh->version) ||
73 !out->WriteU16(num_ypels)) {
74 return OTS_FAILURE_MSG("Failed to write pels size");
76 for (uint16_t i = 0; i < num_ypels; ++i) {
77 if (!out->Write(&(ltsh->ypels[i]), 1)) {
78 return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i);
82 return true;
85 void ots_ltsh_free(OpenTypeFile *file) {
86 delete file->ltsh;
89 } // namespace ots
91 #undef TABLE_NAME
92 #undef DROP_THIS_TABLE