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