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.
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
18 bool ParseMetricsHeader(Font
*font
, 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
);
37 if (header
->linegap
< 0) {
38 OTS_WARNING("bad linegap: %d", header
->linegap
);
43 return OTS_FAILURE_MSG("Missing head font table");
46 // if the font is non-slanted, caret_offset should be zero.
47 if (!(font
->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");
59 if (!table
->ReadS16(&data_format
)) {
60 return OTS_FAILURE_MSG("Failed to read 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");
71 return OTS_FAILURE_MSG("Missing maxp font table");
74 if (header
->num_metrics
> font
->maxp
->num_glyphs
) {
75 return OTS_FAILURE_MSG("Bad number of metrics %d", header
->num_metrics
);
81 bool SerialiseMetricsHeader(const ots::Font
*font
,
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");
104 bool ParseMetricsTable(const ots::Font
*font
,
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
);
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
) {
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
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
;
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
) {
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
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
;
166 metrics
->sbs
.push_back(sb
);
172 bool SerialiseMetricsTable(const ots::Font
*font
,
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());