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