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