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 "hdmx.h" | 5 #include "hdmx.h" |
6 #include "head.h" | 6 #include "head.h" |
7 #include "maxp.h" | 7 #include "maxp.h" |
8 | 8 |
9 // hdmx - Horizontal Device Metrics | 9 // hdmx - Horizontal Device Metrics |
10 // http://www.microsoft.com/opentype/otspec/hdmx.htm | 10 // http://www.microsoft.com/typography/otspec/hdmx.htm |
11 | 11 |
12 #define DROP_THIS_TABLE \ | 12 #define TABLE_NAME "hdmx" |
13 do { delete file->hdmx; file->hdmx = 0; } while (0) | 13 |
| 14 #define DROP_THIS_TABLE(...) \ |
| 15 do { \ |
| 16 delete file->hdmx; \ |
| 17 file->hdmx = 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_hdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 24 bool ots_hdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
18 Buffer table(data, length); | 25 Buffer table(data, length); |
19 file->hdmx = new OpenTypeHDMX; | 26 file->hdmx = new OpenTypeHDMX; |
20 OpenTypeHDMX * const hdmx = file->hdmx; | 27 OpenTypeHDMX * const hdmx = file->hdmx; |
21 | 28 |
22 if (!file->head || !file->maxp) { | 29 if (!file->head || !file->maxp) { |
23 return OTS_FAILURE(); | 30 return OTS_FAILURE_MSG("Missing maxp or head tables in font, needed by hdmx"
); |
24 } | 31 } |
25 | 32 |
26 if ((file->head->flags & 0x14) == 0) { | 33 if ((file->head->flags & 0x14) == 0) { |
27 // http://www.microsoft.com/typography/otspec/recom.htm | 34 // http://www.microsoft.com/typography/otspec/recom.htm |
28 OTS_WARNING("the table should not be present when bit 2 and 4 of the " | 35 DROP_THIS_TABLE("the table should not be present when bit 2 and 4 of the " |
29 "head->flags are not set"); | 36 "head->flags are not set"); |
30 DROP_THIS_TABLE; | |
31 return true; | 37 return true; |
32 } | 38 } |
33 | 39 |
34 int16_t num_recs; | 40 int16_t num_recs; |
35 if (!table.ReadU16(&hdmx->version) || | 41 if (!table.ReadU16(&hdmx->version) || |
36 !table.ReadS16(&num_recs) || | 42 !table.ReadS16(&num_recs) || |
37 !table.ReadS32(&hdmx->size_device_record)) { | 43 !table.ReadS32(&hdmx->size_device_record)) { |
38 return OTS_FAILURE(); | 44 return OTS_FAILURE_MSG("Failed to read hdmx header"); |
39 } | 45 } |
40 if (hdmx->version != 0) { | 46 if (hdmx->version != 0) { |
41 OTS_WARNING("bad version: %u", hdmx->version); | 47 DROP_THIS_TABLE("bad version: %u", hdmx->version); |
42 DROP_THIS_TABLE; | |
43 return true; | 48 return true; |
44 } | 49 } |
45 if (num_recs <= 0) { | 50 if (num_recs <= 0) { |
46 OTS_WARNING("bad num_recs: %d", num_recs); | 51 DROP_THIS_TABLE("bad num_recs: %d", num_recs); |
47 DROP_THIS_TABLE; | |
48 return true; | 52 return true; |
49 } | 53 } |
50 const int32_t actual_size_device_record = file->maxp->num_glyphs + 2; | 54 const int32_t actual_size_device_record = file->maxp->num_glyphs + 2; |
51 if (hdmx->size_device_record < actual_size_device_record) { | 55 if (hdmx->size_device_record < actual_size_device_record) { |
52 OTS_WARNING("bad hdmx->size_device_record: %d", hdmx->size_device_record); | 56 DROP_THIS_TABLE("bad hdmx->size_device_record: %d", hdmx->size_device_record
); |
53 DROP_THIS_TABLE; | |
54 return true; | 57 return true; |
55 } | 58 } |
56 | 59 |
57 hdmx->pad_len = hdmx->size_device_record - actual_size_device_record; | 60 hdmx->pad_len = hdmx->size_device_record - actual_size_device_record; |
58 if (hdmx->pad_len > 3) { | 61 if (hdmx->pad_len > 3) { |
59 return OTS_FAILURE(); | 62 return OTS_FAILURE_MSG("Bad padding %d", hdmx->pad_len); |
60 } | 63 } |
61 | 64 |
62 uint8_t last_pixel_size = 0; | 65 uint8_t last_pixel_size = 0; |
63 hdmx->records.reserve(num_recs); | 66 hdmx->records.reserve(num_recs); |
64 for (int i = 0; i < num_recs; ++i) { | 67 for (int i = 0; i < num_recs; ++i) { |
65 OpenTypeHDMXDeviceRecord rec; | 68 OpenTypeHDMXDeviceRecord rec; |
66 | 69 |
67 if (!table.ReadU8(&rec.pixel_size) || | 70 if (!table.ReadU8(&rec.pixel_size) || |
68 !table.ReadU8(&rec.max_width)) { | 71 !table.ReadU8(&rec.max_width)) { |
69 return OTS_FAILURE(); | 72 return OTS_FAILURE_MSG("Failed to read hdmx record %d", i); |
70 } | 73 } |
71 if ((i != 0) && | 74 if ((i != 0) && |
72 (rec.pixel_size <= last_pixel_size)) { | 75 (rec.pixel_size <= last_pixel_size)) { |
73 OTS_WARNING("records are not sorted"); | 76 DROP_THIS_TABLE("records are not sorted"); |
74 DROP_THIS_TABLE; | |
75 return true; | 77 return true; |
76 } | 78 } |
77 last_pixel_size = rec.pixel_size; | 79 last_pixel_size = rec.pixel_size; |
78 | 80 |
79 rec.widths.reserve(file->maxp->num_glyphs); | 81 rec.widths.reserve(file->maxp->num_glyphs); |
80 for (unsigned j = 0; j < file->maxp->num_glyphs; ++j) { | 82 for (unsigned j = 0; j < file->maxp->num_glyphs; ++j) { |
81 uint8_t width; | 83 uint8_t width; |
82 if (!table.ReadU8(&width)) { | 84 if (!table.ReadU8(&width)) { |
83 return OTS_FAILURE(); | 85 return OTS_FAILURE_MSG("Failed to read glyph width %d in record %d", j,
i); |
84 } | 86 } |
85 rec.widths.push_back(width); | 87 rec.widths.push_back(width); |
86 } | 88 } |
87 | 89 |
88 if ((hdmx->pad_len > 0) && | 90 if ((hdmx->pad_len > 0) && |
89 !table.Skip(hdmx->pad_len)) { | 91 !table.Skip(hdmx->pad_len)) { |
90 return OTS_FAILURE(); | 92 return OTS_FAILURE_MSG("Failed to skip padding %d", hdmx->pad_len); |
91 } | 93 } |
92 | 94 |
93 hdmx->records.push_back(rec); | 95 hdmx->records.push_back(rec); |
94 } | 96 } |
95 | 97 |
96 return true; | 98 return true; |
97 } | 99 } |
98 | 100 |
99 bool ots_hdmx_should_serialise(OpenTypeFile *file) { | 101 bool ots_hdmx_should_serialise(OpenTypeFile *file) { |
100 if (!file->hdmx) return false; | 102 if (!file->hdmx) return false; |
101 if (!file->glyf) return false; // this table is not for CFF fonts. | 103 if (!file->glyf) return false; // this table is not for CFF fonts. |
102 return true; | 104 return true; |
103 } | 105 } |
104 | 106 |
105 bool ots_hdmx_serialise(OTSStream *out, OpenTypeFile *file) { | 107 bool ots_hdmx_serialise(OTSStream *out, OpenTypeFile *file) { |
106 OpenTypeHDMX * const hdmx = file->hdmx; | 108 OpenTypeHDMX * const hdmx = file->hdmx; |
107 | 109 |
108 const int16_t num_recs = static_cast<int16_t>(hdmx->records.size()); | 110 const int16_t num_recs = static_cast<int16_t>(hdmx->records.size()); |
109 if (hdmx->records.size() > | 111 if (hdmx->records.size() > |
110 static_cast<size_t>(std::numeric_limits<int16_t>::max()) || | 112 static_cast<size_t>(std::numeric_limits<int16_t>::max()) || |
111 !out->WriteU16(hdmx->version) || | 113 !out->WriteU16(hdmx->version) || |
112 !out->WriteS16(num_recs) || | 114 !out->WriteS16(num_recs) || |
113 !out->WriteS32(hdmx->size_device_record)) { | 115 !out->WriteS32(hdmx->size_device_record)) { |
114 return OTS_FAILURE(); | 116 return OTS_FAILURE_MSG("Failed to write hdmx header"); |
115 } | 117 } |
116 | 118 |
117 for (int16_t i = 0; i < num_recs; ++i) { | 119 for (int16_t i = 0; i < num_recs; ++i) { |
118 const OpenTypeHDMXDeviceRecord& rec = hdmx->records[i]; | 120 const OpenTypeHDMXDeviceRecord& rec = hdmx->records[i]; |
119 if (!out->Write(&rec.pixel_size, 1) || | 121 if (!out->Write(&rec.pixel_size, 1) || |
120 !out->Write(&rec.max_width, 1) || | 122 !out->Write(&rec.max_width, 1) || |
121 !out->Write(&rec.widths[0], rec.widths.size())) { | 123 !out->Write(&rec.widths[0], rec.widths.size())) { |
122 return OTS_FAILURE(); | 124 return OTS_FAILURE_MSG("Failed to write hdmx record %d", i); |
123 } | 125 } |
124 if ((hdmx->pad_len > 0) && | 126 if ((hdmx->pad_len > 0) && |
125 !out->Write((const uint8_t *)"\x00\x00\x00", hdmx->pad_len)) { | 127 !out->Write((const uint8_t *)"\x00\x00\x00", hdmx->pad_len)) { |
126 return OTS_FAILURE(); | 128 return OTS_FAILURE_MSG("Failed to write hdmx padding of length %d", hdmx->
pad_len); |
127 } | 129 } |
128 } | 130 } |
129 | 131 |
130 return true; | 132 return true; |
131 } | 133 } |
132 | 134 |
133 void ots_hdmx_free(OpenTypeFile *file) { | 135 void ots_hdmx_free(OpenTypeFile *file) { |
134 delete file->hdmx; | 136 delete file->hdmx; |
135 } | 137 } |
136 | 138 |
137 } // namespace ots | 139 } // namespace ots |
| 140 |
| 141 #undef TABLE_NAME |
| 142 #undef DROP_THIS_TABLE |
OLD | NEW |