OLD | NEW |
| (Empty) |
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 TABLE_NAME "LTSH" | |
13 | |
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) | |
21 | |
22 namespace ots { | |
23 | |
24 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | |
25 Buffer table(data, length); | |
26 | |
27 if (!file->maxp) { | |
28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh"); | |
29 } | |
30 | |
31 OpenTypeLTSH *ltsh = new OpenTypeLTSH; | |
32 file->ltsh = ltsh; | |
33 | |
34 uint16_t num_glyphs = 0; | |
35 if (!table.ReadU16(<sh->version) || | |
36 !table.ReadU16(&num_glyphs)) { | |
37 return OTS_FAILURE_MSG("Failed to read ltsh header"); | |
38 } | |
39 | |
40 if (ltsh->version != 0) { | |
41 DROP_THIS_TABLE("bad version: %u", ltsh->version); | |
42 return true; | |
43 } | |
44 | |
45 if (num_glyphs != file->maxp->num_glyphs) { | |
46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs); | |
47 return true; | |
48 } | |
49 | |
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); | |
55 } | |
56 ltsh->ypels.push_back(pel); | |
57 } | |
58 | |
59 return true; | |
60 } | |
61 | |
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; | |
65 } | |
66 | |
67 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) { | |
68 const OpenTypeLTSH *ltsh = file->ltsh; | |
69 | |
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"); | |
75 } | |
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); | |
79 } | |
80 } | |
81 | |
82 return true; | |
83 } | |
84 | |
85 void ots_ltsh_free(OpenTypeFile *file) { | |
86 delete file->ltsh; | |
87 } | |
88 | |
89 } // namespace ots | |
90 | |
91 #undef TABLE_NAME | |
92 #undef DROP_THIS_TABLE | |
OLD | NEW |