| 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 "vdmx.h" | 5 #include "vdmx.h" |
| 6 | 6 |
| 7 // VDMX - Vertical Device Metrics | 7 // VDMX - Vertical Device Metrics |
| 8 // http://www.microsoft.com/opentype/otspec/vdmx.htm | 8 // http://www.microsoft.com/typography/otspec/vdmx.htm |
| 9 | 9 |
| 10 #define DROP_THIS_TABLE \ | 10 #define TABLE_NAME "VDMX" |
| 11 do { delete file->vdmx; file->vdmx = 0; } while (0) | 11 |
| 12 #define DROP_THIS_TABLE(...) \ |
| 13 do { \ |
| 14 delete file->vdmx; \ |
| 15 file->vdmx = 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_vdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 22 bool ots_vdmx_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { |
| 16 Buffer table(data, length); | 23 Buffer table(data, length); |
| 17 file->vdmx = new OpenTypeVDMX; | 24 file->vdmx = new OpenTypeVDMX; |
| 18 OpenTypeVDMX * const vdmx = file->vdmx; | 25 OpenTypeVDMX * const vdmx = file->vdmx; |
| 19 | 26 |
| 20 if (!table.ReadU16(&vdmx->version) || | 27 if (!table.ReadU16(&vdmx->version) || |
| 21 !table.ReadU16(&vdmx->num_recs) || | 28 !table.ReadU16(&vdmx->num_recs) || |
| 22 !table.ReadU16(&vdmx->num_ratios)) { | 29 !table.ReadU16(&vdmx->num_ratios)) { |
| 23 return OTS_FAILURE(); | 30 return OTS_FAILURE_MSG("Failed to read table header"); |
| 24 } | 31 } |
| 25 | 32 |
| 26 if (vdmx->version > 1) { | 33 if (vdmx->version > 1) { |
| 27 OTS_WARNING("bad version: %u", vdmx->version); | 34 DROP_THIS_TABLE("bad version: %u", vdmx->version); |
| 28 DROP_THIS_TABLE; | |
| 29 return true; // continue transcoding | 35 return true; // continue transcoding |
| 30 } | 36 } |
| 31 | 37 |
| 32 vdmx->rat_ranges.reserve(vdmx->num_ratios); | 38 vdmx->rat_ranges.reserve(vdmx->num_ratios); |
| 33 for (unsigned i = 0; i < vdmx->num_ratios; ++i) { | 39 for (unsigned i = 0; i < vdmx->num_ratios; ++i) { |
| 34 OpenTypeVDMXRatioRecord rec; | 40 OpenTypeVDMXRatioRecord rec; |
| 35 | 41 |
| 36 if (!table.ReadU8(&rec.charset) || | 42 if (!table.ReadU8(&rec.charset) || |
| 37 !table.ReadU8(&rec.x_ratio) || | 43 !table.ReadU8(&rec.x_ratio) || |
| 38 !table.ReadU8(&rec.y_start_ratio) || | 44 !table.ReadU8(&rec.y_start_ratio) || |
| 39 !table.ReadU8(&rec.y_end_ratio)) { | 45 !table.ReadU8(&rec.y_end_ratio)) { |
| 40 return OTS_FAILURE(); | 46 return OTS_FAILURE_MSG("Failed to read ratio header %d", i); |
| 41 } | 47 } |
| 42 | 48 |
| 43 if (rec.charset > 1) { | 49 if (rec.charset > 1) { |
| 44 OTS_WARNING("bad charset: %u", rec.charset); | 50 DROP_THIS_TABLE("bad charset: %u", rec.charset); |
| 45 DROP_THIS_TABLE; | |
| 46 return true; | 51 return true; |
| 47 } | 52 } |
| 48 | 53 |
| 49 if (rec.y_start_ratio > rec.y_end_ratio) { | 54 if (rec.y_start_ratio > rec.y_end_ratio) { |
| 50 OTS_WARNING("bad y ratio"); | 55 DROP_THIS_TABLE("bad y ratio"); |
| 51 DROP_THIS_TABLE; | |
| 52 return true; | 56 return true; |
| 53 } | 57 } |
| 54 | 58 |
| 55 // All values set to zero signal the default grouping to use; | 59 // All values set to zero signal the default grouping to use; |
| 56 // if present, this must be the last Ratio group in the table. | 60 // if present, this must be the last Ratio group in the table. |
| 57 if ((i < vdmx->num_ratios - 1u) && | 61 if ((i < vdmx->num_ratios - 1u) && |
| 58 (rec.x_ratio == 0) && | 62 (rec.x_ratio == 0) && |
| 59 (rec.y_start_ratio == 0) && | 63 (rec.y_start_ratio == 0) && |
| 60 (rec.y_end_ratio == 0)) { | 64 (rec.y_end_ratio == 0)) { |
| 61 // workaround for fonts which have 2 or more {0, 0, 0} terminators. | 65 // workaround for fonts which have 2 or more {0, 0, 0} terminators. |
| 62 OTS_WARNING("superfluous terminator found"); | 66 DROP_THIS_TABLE("superfluous terminator found"); |
| 63 DROP_THIS_TABLE; | |
| 64 return true; | 67 return true; |
| 65 } | 68 } |
| 66 | 69 |
| 67 vdmx->rat_ranges.push_back(rec); | 70 vdmx->rat_ranges.push_back(rec); |
| 68 } | 71 } |
| 69 | 72 |
| 70 vdmx->offsets.reserve(vdmx->num_ratios); | 73 vdmx->offsets.reserve(vdmx->num_ratios); |
| 71 const size_t current_offset = table.offset(); | 74 const size_t current_offset = table.offset(); |
| 72 // current_offset is less than (2 bytes * 3) + (4 bytes * USHRT_MAX) = 256k. | 75 // current_offset is less than (2 bytes * 3) + (4 bytes * USHRT_MAX) = 256k. |
| 73 for (unsigned i = 0; i < vdmx->num_ratios; ++i) { | 76 for (unsigned i = 0; i < vdmx->num_ratios; ++i) { |
| 74 uint16_t offset; | 77 uint16_t offset; |
| 75 if (!table.ReadU16(&offset)) { | 78 if (!table.ReadU16(&offset)) { |
| 76 return OTS_FAILURE(); | 79 return OTS_FAILURE_MSG("Failed to read ratio offset %d", i); |
| 77 } | 80 } |
| 78 if (current_offset + offset >= length) { // thus doesn't overflow. | 81 if (current_offset + offset >= length) { // thus doesn't overflow. |
| 79 return OTS_FAILURE(); | 82 return OTS_FAILURE_MSG("Bad ratio offset %d for ration %d", offset, i); |
| 80 } | 83 } |
| 81 | 84 |
| 82 vdmx->offsets.push_back(offset); | 85 vdmx->offsets.push_back(offset); |
| 83 } | 86 } |
| 84 | 87 |
| 85 vdmx->groups.reserve(vdmx->num_recs); | 88 vdmx->groups.reserve(vdmx->num_recs); |
| 86 for (unsigned i = 0; i < vdmx->num_recs; ++i) { | 89 for (unsigned i = 0; i < vdmx->num_recs; ++i) { |
| 87 OpenTypeVDMXGroup group; | 90 OpenTypeVDMXGroup group; |
| 88 if (!table.ReadU16(&group.recs) || | 91 if (!table.ReadU16(&group.recs) || |
| 89 !table.ReadU8(&group.startsz) || | 92 !table.ReadU8(&group.startsz) || |
| 90 !table.ReadU8(&group.endsz)) { | 93 !table.ReadU8(&group.endsz)) { |
| 91 return OTS_FAILURE(); | 94 return OTS_FAILURE_MSG("Failed to read record header %d", i); |
| 92 } | 95 } |
| 93 group.entries.reserve(group.recs); | 96 group.entries.reserve(group.recs); |
| 94 for (unsigned j = 0; j < group.recs; ++j) { | 97 for (unsigned j = 0; j < group.recs; ++j) { |
| 95 OpenTypeVDMXVTable vt; | 98 OpenTypeVDMXVTable vt; |
| 96 if (!table.ReadU16(&vt.y_pel_height) || | 99 if (!table.ReadU16(&vt.y_pel_height) || |
| 97 !table.ReadS16(&vt.y_max) || | 100 !table.ReadS16(&vt.y_max) || |
| 98 !table.ReadS16(&vt.y_min)) { | 101 !table.ReadS16(&vt.y_min)) { |
| 99 return OTS_FAILURE(); | 102 return OTS_FAILURE_MSG("Failed to read reacord %d group %d", i, j); |
| 100 } | 103 } |
| 101 if (vt.y_max < vt.y_min) { | 104 if (vt.y_max < vt.y_min) { |
| 102 OTS_WARNING("bad y min/max"); | 105 DROP_THIS_TABLE("bad y min/max"); |
| 103 DROP_THIS_TABLE; | |
| 104 return true; | 106 return true; |
| 105 } | 107 } |
| 106 | 108 |
| 107 // This table must appear in sorted order (sorted by yPelHeight), | 109 // This table must appear in sorted order (sorted by yPelHeight), |
| 108 // but need not be continuous. | 110 // but need not be continuous. |
| 109 if ((j != 0) && (group.entries[j - 1].y_pel_height >= vt.y_pel_height)) { | 111 if ((j != 0) && (group.entries[j - 1].y_pel_height >= vt.y_pel_height)) { |
| 110 OTS_WARNING("the table is not sorted"); | 112 DROP_THIS_TABLE("the table is not sorted"); |
| 111 DROP_THIS_TABLE; | |
| 112 return true; | 113 return true; |
| 113 } | 114 } |
| 114 | 115 |
| 115 group.entries.push_back(vt); | 116 group.entries.push_back(vt); |
| 116 } | 117 } |
| 117 vdmx->groups.push_back(group); | 118 vdmx->groups.push_back(group); |
| 118 } | 119 } |
| 119 | 120 |
| 120 return true; | 121 return true; |
| 121 } | 122 } |
| 122 | 123 |
| 123 bool ots_vdmx_should_serialise(OpenTypeFile *file) { | 124 bool ots_vdmx_should_serialise(OpenTypeFile *file) { |
| 124 if (!file->glyf) return false; // this table is not for CFF fonts. | 125 if (!file->glyf) return false; // this table is not for CFF fonts. |
| 125 return file->vdmx != NULL; | 126 return file->vdmx != NULL; |
| 126 } | 127 } |
| 127 | 128 |
| 128 bool ots_vdmx_serialise(OTSStream *out, OpenTypeFile *file) { | 129 bool ots_vdmx_serialise(OTSStream *out, OpenTypeFile *file) { |
| 129 OpenTypeVDMX * const vdmx = file->vdmx; | 130 OpenTypeVDMX * const vdmx = file->vdmx; |
| 130 | 131 |
| 131 if (!out->WriteU16(vdmx->version) || | 132 if (!out->WriteU16(vdmx->version) || |
| 132 !out->WriteU16(vdmx->num_recs) || | 133 !out->WriteU16(vdmx->num_recs) || |
| 133 !out->WriteU16(vdmx->num_ratios)) { | 134 !out->WriteU16(vdmx->num_ratios)) { |
| 134 return OTS_FAILURE(); | 135 return OTS_FAILURE_MSG("Failed to write table header"); |
| 135 } | 136 } |
| 136 | 137 |
| 137 for (unsigned i = 0; i < vdmx->rat_ranges.size(); ++i) { | 138 for (unsigned i = 0; i < vdmx->rat_ranges.size(); ++i) { |
| 138 const OpenTypeVDMXRatioRecord& rec = vdmx->rat_ranges[i]; | 139 const OpenTypeVDMXRatioRecord& rec = vdmx->rat_ranges[i]; |
| 139 if (!out->Write(&rec.charset, 1) || | 140 if (!out->Write(&rec.charset, 1) || |
| 140 !out->Write(&rec.x_ratio, 1) || | 141 !out->Write(&rec.x_ratio, 1) || |
| 141 !out->Write(&rec.y_start_ratio, 1) || | 142 !out->Write(&rec.y_start_ratio, 1) || |
| 142 !out->Write(&rec.y_end_ratio, 1)) { | 143 !out->Write(&rec.y_end_ratio, 1)) { |
| 143 return OTS_FAILURE(); | 144 return OTS_FAILURE_MSG("Failed to write ratio %d", i); |
| 144 } | 145 } |
| 145 } | 146 } |
| 146 | 147 |
| 147 for (unsigned i = 0; i < vdmx->offsets.size(); ++i) { | 148 for (unsigned i = 0; i < vdmx->offsets.size(); ++i) { |
| 148 if (!out->WriteU16(vdmx->offsets[i])) { | 149 if (!out->WriteU16(vdmx->offsets[i])) { |
| 149 return OTS_FAILURE(); | 150 return OTS_FAILURE_MSG("Failed to write ratio offset %d", i); |
| 150 } | 151 } |
| 151 } | 152 } |
| 152 | 153 |
| 153 for (unsigned i = 0; i < vdmx->groups.size(); ++i) { | 154 for (unsigned i = 0; i < vdmx->groups.size(); ++i) { |
| 154 const OpenTypeVDMXGroup& group = vdmx->groups[i]; | 155 const OpenTypeVDMXGroup& group = vdmx->groups[i]; |
| 155 if (!out->WriteU16(group.recs) || | 156 if (!out->WriteU16(group.recs) || |
| 156 !out->Write(&group.startsz, 1) || | 157 !out->Write(&group.startsz, 1) || |
| 157 !out->Write(&group.endsz, 1)) { | 158 !out->Write(&group.endsz, 1)) { |
| 158 return OTS_FAILURE(); | 159 return OTS_FAILURE_MSG("Failed to write group %d", i); |
| 159 } | 160 } |
| 160 for (unsigned j = 0; j < group.entries.size(); ++j) { | 161 for (unsigned j = 0; j < group.entries.size(); ++j) { |
| 161 const OpenTypeVDMXVTable& vt = group.entries[j]; | 162 const OpenTypeVDMXVTable& vt = group.entries[j]; |
| 162 if (!out->WriteU16(vt.y_pel_height) || | 163 if (!out->WriteU16(vt.y_pel_height) || |
| 163 !out->WriteS16(vt.y_max) || | 164 !out->WriteS16(vt.y_max) || |
| 164 !out->WriteS16(vt.y_min)) { | 165 !out->WriteS16(vt.y_min)) { |
| 165 return OTS_FAILURE(); | 166 return OTS_FAILURE_MSG("Failed to write group %d entry %d", i, j); |
| 166 } | 167 } |
| 167 } | 168 } |
| 168 } | 169 } |
| 169 | 170 |
| 170 return true; | 171 return true; |
| 171 } | 172 } |
| 172 | 173 |
| 173 void ots_vdmx_free(OpenTypeFile *file) { | 174 void ots_vdmx_free(OpenTypeFile *file) { |
| 174 delete file->vdmx; | 175 delete file->vdmx; |
| 175 } | 176 } |
| 176 | 177 |
| 177 } // namespace ots | 178 } // namespace ots |
| 179 |
| 180 #undef TABLE_NAME |
| 181 #undef DROP_THIS_TABLE |
| OLD | NEW |