Line data Source code
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.
4 :
5 : #include "gasp.h"
6 :
7 : // gasp - Grid-fitting And Scan-conversion Procedure
8 : // http://www.microsoft.com/typography/otspec/gasp.htm
9 :
10 : #define TABLE_NAME "gasp"
11 :
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)
19 :
20 : namespace ots {
21 :
22 0 : bool ots_gasp_parse(Font *font, const uint8_t *data, size_t length) {
23 0 : Buffer table(data, length);
24 :
25 0 : OpenTypeGASP *gasp = new OpenTypeGASP;
26 0 : font->gasp = gasp;
27 :
28 0 : uint16_t num_ranges = 0;
29 0 : if (!table.ReadU16(&gasp->version) ||
30 0 : !table.ReadU16(&num_ranges)) {
31 0 : return OTS_FAILURE_MSG("Failed to read table header");
32 : }
33 :
34 0 : if (gasp->version > 1) {
35 : // Lots of Linux fonts have bad version numbers...
36 0 : DROP_THIS_TABLE("bad version: %u", gasp->version);
37 0 : return true;
38 : }
39 :
40 0 : if (num_ranges == 0) {
41 0 : DROP_THIS_TABLE("num_ranges is zero");
42 0 : return true;
43 : }
44 :
45 0 : gasp->gasp_ranges.reserve(num_ranges);
46 0 : for (unsigned i = 0; i < num_ranges; ++i) {
47 0 : uint16_t max_ppem = 0;
48 0 : uint16_t behavior = 0;
49 0 : if (!table.ReadU16(&max_ppem) ||
50 0 : !table.ReadU16(&behavior)) {
51 0 : return OTS_FAILURE_MSG("Failed to read subrange %d", i);
52 : }
53 0 : 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 0 : DROP_THIS_TABLE("ranges are not sorted");
57 0 : return true;
58 : }
59 0 : if ((i == num_ranges - 1u) && // never underflow.
60 0 : (max_ppem != 0xffffu)) {
61 0 : DROP_THIS_TABLE("The last record should be 0xFFFF as a sentinel value "
62 : "for rangeMaxPPEM");
63 0 : return true;
64 : }
65 :
66 0 : if (behavior >> 8) {
67 0 : OTS_WARNING("undefined bits are used: %x", behavior);
68 : // mask undefined bits.
69 0 : behavior &= 0x000fu;
70 : }
71 :
72 0 : if (gasp->version == 0 && (behavior >> 2) != 0) {
73 0 : OTS_WARNING("changed the version number to 1");
74 0 : gasp->version = 1;
75 : }
76 :
77 0 : gasp->gasp_ranges.push_back(std::make_pair(max_ppem, behavior));
78 : }
79 :
80 0 : return true;
81 : }
82 :
83 0 : bool ots_gasp_should_serialise(Font *font) {
84 0 : return font->gasp != NULL;
85 : }
86 :
87 0 : bool ots_gasp_serialise(OTSStream *out, Font *font) {
88 0 : const OpenTypeGASP *gasp = font->gasp;
89 :
90 0 : const uint16_t num_ranges = static_cast<uint16_t>(gasp->gasp_ranges.size());
91 0 : if (num_ranges != gasp->gasp_ranges.size() ||
92 0 : !out->WriteU16(gasp->version) ||
93 0 : !out->WriteU16(num_ranges)) {
94 0 : return OTS_FAILURE_MSG("failed to write gasp header");
95 : }
96 :
97 0 : for (uint16_t i = 0; i < num_ranges; ++i) {
98 0 : if (!out->WriteU16(gasp->gasp_ranges[i].first) ||
99 0 : !out->WriteU16(gasp->gasp_ranges[i].second)) {
100 0 : return OTS_FAILURE_MSG("Failed to write gasp subtable %d", i);
101 : }
102 : }
103 :
104 0 : return true;
105 : }
106 :
107 0 : void ots_gasp_reuse(Font *font, Font *other) {
108 0 : font->gasp = other->gasp;
109 0 : font->gasp_reused = true;
110 0 : }
111 :
112 0 : void ots_gasp_free(Font *font) {
113 0 : delete font->gasp;
114 0 : }
115 :
116 : } // namespace ots
117 :
118 : #undef TABLE_NAME
119 : #undef DROP_THIS_TABLE
|