Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / third_party / ots / src / metrics.cc
blob8d59b953b18ac2cbf973014a812f2b633982b2c5
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.
5 #include "metrics.h"
7 #include "head.h"
8 #include "maxp.h"
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
14 #define TABLE_NAME "metrics" // XXX: use individual table names
16 namespace ots {
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");
33 if (header->ascent < 0) {
34 OTS_WARNING("bad ascent: %d", header->ascent);
35 header->ascent = 0;
37 if (header->linegap < 0) {
38 OTS_WARNING("bad linegap: %d", header->linegap);
39 header->linegap = 0;
42 if (!file->head) {
43 return OTS_FAILURE_MSG("Missing head font table");
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;
53 // skip the reserved bytes
54 if (!table->Skip(8)) {
55 return OTS_FAILURE_MSG("Failed to skip reserverd bytes");
58 int16_t data_format;
59 if (!table->ReadS16(&data_format)) {
60 return OTS_FAILURE_MSG("Failed to read data format");
62 if (data_format) {
63 return OTS_FAILURE_MSG("Bad data format %d", data_format);
66 if (!table->ReadU16(&header->num_metrics)) {
67 return OTS_FAILURE_MSG("Failed to read number of metrics");
70 if (!file->maxp) {
71 return OTS_FAILURE_MSG("Missing maxp font table");
74 if (header->num_metrics > file->maxp->num_glyphs) {
75 return OTS_FAILURE_MSG("Bad number of metrics %d", header->num_metrics);
78 return true;
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");
101 return true;
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;
113 if (num_metrics > num_glyphs) {
114 return OTS_FAILURE_MSG("Bad number of metrics %d", num_metrics);
116 if (!num_metrics) {
117 return OTS_FAILURE_MSG("No metrics!");
119 const unsigned num_sbs = num_glyphs - num_metrics;
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);
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;
139 if (sb < header->min_sb1) {
140 OTS_WARNING("bad sb: %d < %d", sb, header->min_sb1);
141 sb = header->min_sb1;
143 #endif
145 metrics->entries.push_back(std::make_pair(adv, sb));
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);
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;
164 #endif
166 metrics->sbs.push_back(sb);
169 return true;
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);
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->entries.size());
188 return true;
191 } // namespace ots
193 #undef TABLE_NAME