OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "metrics.h" |
| 6 |
| 7 #include "head.h" |
| 8 #include "maxp.h" |
| 9 |
| 10 // OpenType horizontal and vertical common header format |
| 11 // http://www.microsoft.com/typography/otspec/hhea.htm |
| 12 // http://www.microsoft.com/typography/otspec/vhea.htm |
| 13 |
| 14 #define TABLE_NAME "metrics" // XXX: use individual table names |
| 15 |
| 16 namespace ots { |
| 17 |
| 18 bool ParseMetricsHeader(OpenTypeFile *file, Buffer *table, |
| 19 OpenTypeMetricsHeader *header) { |
| 20 if (!table->ReadS16(&header->ascent) || |
| 21 !table->ReadS16(&header->descent) || |
| 22 !table->ReadS16(&header->linegap) || |
| 23 !table->ReadU16(&header->adv_width_max) || |
| 24 !table->ReadS16(&header->min_sb1) || |
| 25 !table->ReadS16(&header->min_sb2) || |
| 26 !table->ReadS16(&header->max_extent) || |
| 27 !table->ReadS16(&header->caret_slope_rise) || |
| 28 !table->ReadS16(&header->caret_slope_run) || |
| 29 !table->ReadS16(&header->caret_offset)) { |
| 30 return OTS_FAILURE_MSG("Failed to read metrics header"); |
| 31 } |
| 32 |
| 33 if (header->ascent < 0) { |
| 34 OTS_WARNING("bad ascent: %d", header->ascent); |
| 35 header->ascent = 0; |
| 36 } |
| 37 if (header->linegap < 0) { |
| 38 OTS_WARNING("bad linegap: %d", header->linegap); |
| 39 header->linegap = 0; |
| 40 } |
| 41 |
| 42 if (!file->head) { |
| 43 return OTS_FAILURE_MSG("Missing head font table"); |
| 44 } |
| 45 |
| 46 // if the font is non-slanted, caret_offset should be zero. |
| 47 if (!(file->head->mac_style & 2) && |
| 48 (header->caret_offset != 0)) { |
| 49 OTS_WARNING("bad caret offset: %d", header->caret_offset); |
| 50 header->caret_offset = 0; |
| 51 } |
| 52 |
| 53 // skip the reserved bytes |
| 54 if (!table->Skip(8)) { |
| 55 return OTS_FAILURE_MSG("Failed to skip reserverd bytes"); |
| 56 } |
| 57 |
| 58 int16_t data_format; |
| 59 if (!table->ReadS16(&data_format)) { |
| 60 return OTS_FAILURE_MSG("Failed to read data format"); |
| 61 } |
| 62 if (data_format) { |
| 63 return OTS_FAILURE_MSG("Bad data format %d", data_format); |
| 64 } |
| 65 |
| 66 if (!table->ReadU16(&header->num_metrics)) { |
| 67 return OTS_FAILURE_MSG("Failed to read number of metrics"); |
| 68 } |
| 69 |
| 70 if (!file->maxp) { |
| 71 return OTS_FAILURE_MSG("Missing maxp font table"); |
| 72 } |
| 73 |
| 74 if (header->num_metrics > file->maxp->num_glyphs) { |
| 75 return OTS_FAILURE_MSG("Bad number of metrics %d", header->num_metrics); |
| 76 } |
| 77 |
| 78 return true; |
| 79 } |
| 80 |
| 81 bool SerialiseMetricsHeader(const ots::OpenTypeFile *file, |
| 82 OTSStream *out, |
| 83 const OpenTypeMetricsHeader *header) { |
| 84 if (!out->WriteU32(header->version) || |
| 85 !out->WriteS16(header->ascent) || |
| 86 !out->WriteS16(header->descent) || |
| 87 !out->WriteS16(header->linegap) || |
| 88 !out->WriteU16(header->adv_width_max) || |
| 89 !out->WriteS16(header->min_sb1) || |
| 90 !out->WriteS16(header->min_sb2) || |
| 91 !out->WriteS16(header->max_extent) || |
| 92 !out->WriteS16(header->caret_slope_rise) || |
| 93 !out->WriteS16(header->caret_slope_run) || |
| 94 !out->WriteS16(header->caret_offset) || |
| 95 !out->WriteR64(0) || // reserved |
| 96 !out->WriteS16(0) || // metric data format |
| 97 !out->WriteU16(header->num_metrics)) { |
| 98 return OTS_FAILURE_MSG("Failed to write metrics"); |
| 99 } |
| 100 |
| 101 return true; |
| 102 } |
| 103 |
| 104 bool ParseMetricsTable(const ots::OpenTypeFile *file, |
| 105 Buffer *table, |
| 106 const uint16_t num_glyphs, |
| 107 const OpenTypeMetricsHeader *header, |
| 108 OpenTypeMetricsTable *metrics) { |
| 109 // |num_metrics| is a uint16_t, so it's bounded < 65536. This limits that |
| 110 // amount of memory that we'll allocate for this to a sane amount. |
| 111 const unsigned num_metrics = header->num_metrics; |
| 112 |
| 113 if (num_metrics > num_glyphs) { |
| 114 return OTS_FAILURE_MSG("Bad number of metrics %d", num_metrics); |
| 115 } |
| 116 if (!num_metrics) { |
| 117 return OTS_FAILURE_MSG("No metrics!"); |
| 118 } |
| 119 const unsigned num_sbs = num_glyphs - num_metrics; |
| 120 |
| 121 metrics->entries.reserve(num_metrics); |
| 122 for (unsigned i = 0; i < num_metrics; ++i) { |
| 123 uint16_t adv = 0; |
| 124 int16_t sb = 0; |
| 125 if (!table->ReadU16(&adv) || !table->ReadS16(&sb)) { |
| 126 return OTS_FAILURE_MSG("Failed to read metric %d", i); |
| 127 } |
| 128 |
| 129 // This check is bogus, see https://github.com/khaledhosny/ots/issues/36 |
| 130 #if 0 |
| 131 // Since so many fonts don't have proper value on |adv| and |sb|, |
| 132 // we should not call ots_failure() here. For example, about 20% of fonts |
| 133 // in http://www.princexml.com/fonts/ (200+ fonts) fails these tests. |
| 134 if (adv > header->adv_width_max) { |
| 135 OTS_WARNING("bad adv: %u > %u", adv, header->adv_width_max); |
| 136 adv = header->adv_width_max; |
| 137 } |
| 138 |
| 139 if (sb < header->min_sb1) { |
| 140 OTS_WARNING("bad sb: %d < %d", sb, header->min_sb1); |
| 141 sb = header->min_sb1; |
| 142 } |
| 143 #endif |
| 144 |
| 145 metrics->entries.push_back(std::make_pair(adv, sb)); |
| 146 } |
| 147 |
| 148 metrics->sbs.reserve(num_sbs); |
| 149 for (unsigned i = 0; i < num_sbs; ++i) { |
| 150 int16_t sb; |
| 151 if (!table->ReadS16(&sb)) { |
| 152 // Some Japanese fonts (e.g., mona.ttf) fail this test. |
| 153 return OTS_FAILURE_MSG("Failed to read side bearing %d", i + num_metrics); |
| 154 } |
| 155 |
| 156 // This check is bogus, see https://github.com/khaledhosny/ots/issues/36 |
| 157 #if 0 |
| 158 if (sb < header->min_sb1) { |
| 159 // The same as above. Three fonts in http://www.fontsquirrel.com/fontface |
| 160 // (e.g., Notice2Std.otf) have weird lsb values. |
| 161 OTS_WARNING("bad lsb: %d < %d", sb, header->min_sb1); |
| 162 sb = header->min_sb1; |
| 163 } |
| 164 #endif |
| 165 |
| 166 metrics->sbs.push_back(sb); |
| 167 } |
| 168 |
| 169 return true; |
| 170 } |
| 171 |
| 172 bool SerialiseMetricsTable(const ots::OpenTypeFile *file, |
| 173 OTSStream *out, |
| 174 const OpenTypeMetricsTable *metrics) { |
| 175 for (unsigned i = 0; i < metrics->entries.size(); ++i) { |
| 176 if (!out->WriteU16(metrics->entries[i].first) || |
| 177 !out->WriteS16(metrics->entries[i].second)) { |
| 178 return OTS_FAILURE_MSG("Failed to write metric %d", i); |
| 179 } |
| 180 } |
| 181 |
| 182 for (unsigned i = 0; i < metrics->sbs.size(); ++i) { |
| 183 if (!out->WriteS16(metrics->sbs[i])) { |
| 184 return OTS_FAILURE_MSG("Failed to write side bearing %ld", i + metrics->en
tries.size()); |
| 185 } |
| 186 } |
| 187 |
| 188 return true; |
| 189 } |
| 190 |
| 191 } // namespace ots |
| 192 |
| 193 #undef TABLE_NAME |
OLD | NEW |