OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ltsh.h" | 5 #include "ltsh.h" |
6 | 6 |
7 #include "maxp.h" | 7 #include "maxp.h" |
8 | 8 |
9 // LTSH - Linear Threshold | 9 // LTSH - Linear Threshold |
10 // http://www.microsoft.com/typography/otspec/ltsh.htm | 10 // http://www.microsoft.com/typography/otspec/ltsh.htm |
11 | 11 |
12 #define DROP_THIS_TABLE \ | 12 #define TABLE_NAME "LTSH" |
13 do { delete file->ltsh; file->ltsh = 0; } while (0) | 13 |
| 14 #define DROP_THIS_TABLE(...) \ |
| 15 do { \ |
| 16 delete file->ltsh; \ |
| 17 file->ltsh = 0; \ |
| 18 OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__); \ |
| 19 OTS_FAILURE_MSG("Table discarded"); \ |
| 20 } while (0) |
14 | 21 |
15 namespace ots { | 22 namespace ots { |
16 | 23 |
17 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 24 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
18 Buffer table(data, length); | 25 Buffer table(data, length); |
19 | 26 |
20 if (!file->maxp) { | 27 if (!file->maxp) { |
21 return OTS_FAILURE(); | 28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh"); |
22 } | 29 } |
23 | 30 |
24 OpenTypeLTSH *ltsh = new OpenTypeLTSH; | 31 OpenTypeLTSH *ltsh = new OpenTypeLTSH; |
25 file->ltsh = ltsh; | 32 file->ltsh = ltsh; |
26 | 33 |
27 uint16_t num_glyphs = 0; | 34 uint16_t num_glyphs = 0; |
28 if (!table.ReadU16(<sh->version) || | 35 if (!table.ReadU16(<sh->version) || |
29 !table.ReadU16(&num_glyphs)) { | 36 !table.ReadU16(&num_glyphs)) { |
30 return OTS_FAILURE(); | 37 return OTS_FAILURE_MSG("Failed to read ltsh header"); |
31 } | 38 } |
32 | 39 |
33 if (ltsh->version != 0) { | 40 if (ltsh->version != 0) { |
34 OTS_WARNING("bad version: %u", ltsh->version); | 41 DROP_THIS_TABLE("bad version: %u", ltsh->version); |
35 DROP_THIS_TABLE; | |
36 return true; | 42 return true; |
37 } | 43 } |
38 | 44 |
39 if (num_glyphs != file->maxp->num_glyphs) { | 45 if (num_glyphs != file->maxp->num_glyphs) { |
40 OTS_WARNING("bad num_glyphs: %u", num_glyphs); | 46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs); |
41 DROP_THIS_TABLE; | |
42 return true; | 47 return true; |
43 } | 48 } |
44 | 49 |
45 ltsh->ypels.reserve(num_glyphs); | 50 ltsh->ypels.reserve(num_glyphs); |
46 for (unsigned i = 0; i < num_glyphs; ++i) { | 51 for (unsigned i = 0; i < num_glyphs; ++i) { |
47 uint8_t pel = 0; | 52 uint8_t pel = 0; |
48 if (!table.ReadU8(&pel)) { | 53 if (!table.ReadU8(&pel)) { |
49 return OTS_FAILURE(); | 54 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i); |
50 } | 55 } |
51 ltsh->ypels.push_back(pel); | 56 ltsh->ypels.push_back(pel); |
52 } | 57 } |
53 | 58 |
54 return true; | 59 return true; |
55 } | 60 } |
56 | 61 |
57 bool ots_ltsh_should_serialise(OpenTypeFile *file) { | 62 bool ots_ltsh_should_serialise(OpenTypeFile *file) { |
58 if (!file->glyf) return false; // this table is not for CFF fonts. | 63 if (!file->glyf) return false; // this table is not for CFF fonts. |
59 return file->ltsh != NULL; | 64 return file->ltsh != NULL; |
60 } | 65 } |
61 | 66 |
62 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) { | 67 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) { |
63 const OpenTypeLTSH *ltsh = file->ltsh; | 68 const OpenTypeLTSH *ltsh = file->ltsh; |
| 69 |
64 const uint16_t num_ypels = static_cast<uint16_t>(ltsh->ypels.size()); | 70 const uint16_t num_ypels = static_cast<uint16_t>(ltsh->ypels.size()); |
65 if (num_ypels != ltsh->ypels.size() || | 71 if (num_ypels != ltsh->ypels.size() || |
66 !out->WriteU16(ltsh->version) || | 72 !out->WriteU16(ltsh->version) || |
67 !out->WriteU16(num_ypels)) { | 73 !out->WriteU16(num_ypels)) { |
68 return OTS_FAILURE(); | 74 return OTS_FAILURE_MSG("Failed to write pels size"); |
69 } | 75 } |
70 for (uint16_t i = 0; i < num_ypels; ++i) { | 76 for (uint16_t i = 0; i < num_ypels; ++i) { |
71 if (!out->Write(&(ltsh->ypels[i]), 1)) { | 77 if (!out->Write(&(ltsh->ypels[i]), 1)) { |
72 return OTS_FAILURE(); | 78 return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i); |
73 } | 79 } |
74 } | 80 } |
75 | 81 |
76 return true; | 82 return true; |
77 } | 83 } |
78 | 84 |
79 void ots_ltsh_free(OpenTypeFile *file) { | 85 void ots_ltsh_free(OpenTypeFile *file) { |
80 delete file->ltsh; | 86 delete file->ltsh; |
81 } | 87 } |
82 | 88 |
83 } // namespace ots | 89 } // namespace ots |
| 90 |
| 91 #undef TABLE_NAME |
| 92 #undef DROP_THIS_TABLE |
OLD | NEW |