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.
7 // gasp - Grid-fitting And Scan-conversion Procedure
8 // http://www.microsoft.com/typography/otspec/gasp.htm
10 #define TABLE_NAME "gasp"
12 #define DROP_THIS_TABLE(...) \
14 OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__); \
15 OTS_FAILURE_MSG("Table discarded"); \
22 bool ots_gasp_parse(OpenTypeFile
*file
, const uint8_t *data
, size_t length
) {
23 Buffer
table(data
, length
);
25 OpenTypeGASP
*gasp
= new OpenTypeGASP
;
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");
34 if (gasp
->version
> 1) {
35 // Lots of Linux fonts have bad version numbers...
36 DROP_THIS_TABLE("bad version: %u", gasp
->version
);
40 if (num_ranges
== 0) {
41 DROP_THIS_TABLE("num_ranges is zero");
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
);
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");
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 "
67 OTS_WARNING("undefined bits are used: %x", behavior
);
68 // mask undefined bits.
72 if (gasp
->version
== 0 && (behavior
>> 2) != 0) {
73 OTS_WARNING("changed the version number to 1");
77 gasp
->gasp_ranges
.push_back(std::make_pair(max_ppem
, behavior
));
83 bool ots_gasp_should_serialise(OpenTypeFile
*file
) {
84 return file
->gasp
!= NULL
;
87 bool ots_gasp_serialise(OTSStream
*out
, OpenTypeFile
*file
) {
88 const OpenTypeGASP
*gasp
= file
->gasp
;
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");
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
);
107 void ots_gasp_free(OpenTypeFile
*file
) {
114 #undef DROP_THIS_TABLE