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.
4
5 #include "ltsh.h"
6
7 #include "maxp.h"
8
9 // LTSH - Linear Threshold
10 // http://www.microsoft.com/typography/otspec/ltsh.htm
11
12 #define DROP_THIS_TABLE \
13 do { delete file->ltsh; file->ltsh = 0; } while (0)
14
15 namespace ots {
16
ots_ltsh_parse(OpenTypeFile * file,const uint8_t * data,size_t length)17 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
18 Buffer table(data, length);
19
20 if (!file->maxp) {
21 return OTS_FAILURE();
22 }
23
24 OpenTypeLTSH *ltsh = new OpenTypeLTSH;
25 file->ltsh = ltsh;
26
27 uint16_t num_glyphs = 0;
28 if (!table.ReadU16(<sh->version) ||
29 !table.ReadU16(&num_glyphs)) {
30 return OTS_FAILURE();
31 }
32
33 if (ltsh->version != 0) {
34 OTS_WARNING("bad version: %u", ltsh->version);
35 DROP_THIS_TABLE;
36 return true;
37 }
38
39 if (num_glyphs != file->maxp->num_glyphs) {
40 OTS_WARNING("bad num_glyphs: %u", num_glyphs);
41 DROP_THIS_TABLE;
42 return true;
43 }
44
45 ltsh->ypels.reserve(num_glyphs);
46 for (unsigned i = 0; i < num_glyphs; ++i) {
47 uint8_t pel = 0;
48 if (!table.ReadU8(&pel)) {
49 return OTS_FAILURE();
50 }
51 ltsh->ypels.push_back(pel);
52 }
53
54 return true;
55 }
56
ots_ltsh_should_serialise(OpenTypeFile * file)57 bool ots_ltsh_should_serialise(OpenTypeFile *file) {
58 if (!file->glyf) return false; // this table is not for CFF fonts.
59 return file->ltsh != NULL;
60 }
61
ots_ltsh_serialise(OTSStream * out,OpenTypeFile * file)62 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) {
63 const OpenTypeLTSH *ltsh = file->ltsh;
64
65 if (!out->WriteU16(ltsh->version) ||
66 !out->WriteU16(ltsh->ypels.size())) {
67 return OTS_FAILURE();
68 }
69 for (unsigned i = 0; i < ltsh->ypels.size(); ++i) {
70 if (!out->Write(&(ltsh->ypels[i]), 1)) {
71 return OTS_FAILURE();
72 }
73 }
74
75 return true;
76 }
77
ots_ltsh_free(OpenTypeFile * file)78 void ots_ltsh_free(OpenTypeFile *file) {
79 delete file->ltsh;
80 }
81
82 } // namespace ots
83