Revert of Attempt to fix expansion of the goma directory in MB on windows. (patchset...
[chromium-blink-merge.git] / third_party / ots / src / gasp.cc
blob5ebf5d84b4b211b1fb87dbcadd297d30b421cd00
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.
5 #include "gasp.h"
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(...) \
13 do { \
14 OTS_FAILURE_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__); \
15 OTS_FAILURE_MSG("Table discarded"); \
16 delete font->gasp; \
17 font->gasp = 0; \
18 } while (0)
20 namespace ots {
22 bool ots_gasp_parse(Font *font, const uint8_t *data, size_t length) {
23 Buffer table(data, length);
25 OpenTypeGASP *gasp = new OpenTypeGASP;
26 font->gasp = gasp;
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);
37 return true;
40 if (num_ranges == 0) {
41 DROP_THIS_TABLE("num_ranges is zero");
42 return true;
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");
57 return true;
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;
66 if (behavior >> 8) {
67 OTS_WARNING("undefined bits are used: %x", behavior);
68 // mask undefined bits.
69 behavior &= 0x000fu;
72 if (gasp->version == 0 && (behavior >> 2) != 0) {
73 OTS_WARNING("changed the version number to 1");
74 gasp->version = 1;
77 gasp->gasp_ranges.push_back(std::make_pair(max_ppem, behavior));
80 return true;
83 bool ots_gasp_should_serialise(Font *font) {
84 return font->gasp != NULL;
87 bool ots_gasp_serialise(OTSStream *out, Font *font) {
88 const OpenTypeGASP *gasp = font->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);
104 return true;
107 void ots_gasp_reuse(Font *font, Font *other) {
108 font->gasp = other->gasp;
109 font->gasp_reused = true;
112 void ots_gasp_free(Font *font) {
113 delete font->gasp;
116 } // namespace ots
118 #undef TABLE_NAME
119 #undef DROP_THIS_TABLE