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 "gasp.h" | 5 #include "gasp.h" |
6 | 6 |
7 // gasp - Grid-fitting And Scan-conversion Procedure | 7 // gasp - Grid-fitting And Scan-conversion Procedure |
8 // http://www.microsoft.com/opentype/otspec/gasp.htm | 8 // http://www.microsoft.com/typography/otspec/gasp.htm |
9 | 9 |
10 #define DROP_THIS_TABLE \ | 10 #define TABLE_NAME "gasp" |
11 do { delete file->gasp; file->gasp = 0; } while (0) | 11 |
| 12 #define DROP_THIS_TABLE(...) \ |
| 13 do { \ |
| 14 delete file->gasp; \ |
| 15 file->gasp = 0; \ |
| 16 OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__); \ |
| 17 OTS_FAILURE_MSG("Table discarded"); \ |
| 18 } while (0) |
12 | 19 |
13 namespace ots { | 20 namespace ots { |
14 | 21 |
15 bool ots_gasp_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 22 bool ots_gasp_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
16 Buffer table(data, length); | 23 Buffer table(data, length); |
17 | 24 |
18 OpenTypeGASP *gasp = new OpenTypeGASP; | 25 OpenTypeGASP *gasp = new OpenTypeGASP; |
19 file->gasp = gasp; | 26 file->gasp = gasp; |
20 | 27 |
21 uint16_t num_ranges = 0; | 28 uint16_t num_ranges = 0; |
22 if (!table.ReadU16(&gasp->version) || | 29 if (!table.ReadU16(&gasp->version) || |
23 !table.ReadU16(&num_ranges)) { | 30 !table.ReadU16(&num_ranges)) { |
24 return OTS_FAILURE(); | 31 return OTS_FAILURE_MSG("Failed to read table header"); |
25 } | 32 } |
26 | 33 |
27 if (gasp->version > 1) { | 34 if (gasp->version > 1) { |
28 // Lots of Linux fonts have bad version numbers... | 35 // Lots of Linux fonts have bad version numbers... |
29 OTS_WARNING("bad version: %u", gasp->version); | 36 DROP_THIS_TABLE("bad version: %u", gasp->version); |
30 DROP_THIS_TABLE; | |
31 return true; | 37 return true; |
32 } | 38 } |
33 | 39 |
34 if (num_ranges == 0) { | 40 if (num_ranges == 0) { |
35 OTS_WARNING("num_ranges is zero"); | 41 DROP_THIS_TABLE("num_ranges is zero"); |
36 DROP_THIS_TABLE; | |
37 return true; | 42 return true; |
38 } | 43 } |
39 | 44 |
40 gasp->gasp_ranges.reserve(num_ranges); | 45 gasp->gasp_ranges.reserve(num_ranges); |
41 for (unsigned i = 0; i < num_ranges; ++i) { | 46 for (unsigned i = 0; i < num_ranges; ++i) { |
42 uint16_t max_ppem = 0; | 47 uint16_t max_ppem = 0; |
43 uint16_t behavior = 0; | 48 uint16_t behavior = 0; |
44 if (!table.ReadU16(&max_ppem) || | 49 if (!table.ReadU16(&max_ppem) || |
45 !table.ReadU16(&behavior)) { | 50 !table.ReadU16(&behavior)) { |
46 return OTS_FAILURE(); | 51 return OTS_FAILURE_MSG("Failed to read subrange %d", i); |
47 } | 52 } |
48 if ((i > 0) && (gasp->gasp_ranges[i - 1].first >= max_ppem)) { | 53 if ((i > 0) && (gasp->gasp_ranges[i - 1].first >= max_ppem)) { |
49 // The records in the gaspRange[] array must be sorted in order of | 54 // The records in the gaspRange[] array must be sorted in order of |
50 // increasing rangeMaxPPEM value. | 55 // increasing rangeMaxPPEM value. |
51 OTS_WARNING("ranges are not sorted"); | 56 DROP_THIS_TABLE("ranges are not sorted"); |
52 DROP_THIS_TABLE; | |
53 return true; | 57 return true; |
54 } | 58 } |
55 if ((i == num_ranges - 1u) && // never underflow. | 59 if ((i == num_ranges - 1u) && // never underflow. |
56 (max_ppem != 0xffffu)) { | 60 (max_ppem != 0xffffu)) { |
57 OTS_WARNING("The last record should be 0xFFFF as a sentinel value " | 61 DROP_THIS_TABLE("The last record should be 0xFFFF as a sentinel value " |
58 "for rangeMaxPPEM"); | 62 "for rangeMaxPPEM"); |
59 DROP_THIS_TABLE; | |
60 return true; | 63 return true; |
61 } | 64 } |
62 | 65 |
63 if (behavior >> 8) { | 66 if (behavior >> 8) { |
64 OTS_WARNING("undefined bits are used: %x", behavior); | 67 OTS_WARNING("undefined bits are used: %x", behavior); |
65 // mask undefined bits. | 68 // mask undefined bits. |
66 behavior &= 0x000fu; | 69 behavior &= 0x000fu; |
67 } | 70 } |
68 | 71 |
69 if (gasp->version == 0 && (behavior >> 2) != 0) { | 72 if (gasp->version == 0 && (behavior >> 2) != 0) { |
(...skipping 11 matching lines...) Expand all Loading... |
81 return file->gasp != NULL; | 84 return file->gasp != NULL; |
82 } | 85 } |
83 | 86 |
84 bool ots_gasp_serialise(OTSStream *out, OpenTypeFile *file) { | 87 bool ots_gasp_serialise(OTSStream *out, OpenTypeFile *file) { |
85 const OpenTypeGASP *gasp = file->gasp; | 88 const OpenTypeGASP *gasp = file->gasp; |
86 | 89 |
87 const uint16_t num_ranges = static_cast<uint16_t>(gasp->gasp_ranges.size()); | 90 const uint16_t num_ranges = static_cast<uint16_t>(gasp->gasp_ranges.size()); |
88 if (num_ranges != gasp->gasp_ranges.size() || | 91 if (num_ranges != gasp->gasp_ranges.size() || |
89 !out->WriteU16(gasp->version) || | 92 !out->WriteU16(gasp->version) || |
90 !out->WriteU16(num_ranges)) { | 93 !out->WriteU16(num_ranges)) { |
91 return OTS_FAILURE(); | 94 return OTS_FAILURE_MSG("failed to write gasp header"); |
92 } | 95 } |
93 | 96 |
94 for (uint16_t i = 0; i < num_ranges; ++i) { | 97 for (uint16_t i = 0; i < num_ranges; ++i) { |
95 if (!out->WriteU16(gasp->gasp_ranges[i].first) || | 98 if (!out->WriteU16(gasp->gasp_ranges[i].first) || |
96 !out->WriteU16(gasp->gasp_ranges[i].second)) { | 99 !out->WriteU16(gasp->gasp_ranges[i].second)) { |
97 return OTS_FAILURE(); | 100 return OTS_FAILURE_MSG("Failed to write gasp subtable %d", i); |
98 } | 101 } |
99 } | 102 } |
100 | 103 |
101 return true; | 104 return true; |
102 } | 105 } |
103 | 106 |
104 void ots_gasp_free(OpenTypeFile *file) { | 107 void ots_gasp_free(OpenTypeFile *file) { |
105 delete file->gasp; | 108 delete file->gasp; |
106 } | 109 } |
107 | 110 |
108 } // namespace ots | 111 } // namespace ots |
| 112 |
| 113 #undef TABLE_NAME |
| 114 #undef DROP_THIS_TABLE |
OLD | NEW |