Bug 1601406 [wpt PR 20618] - Advertise DocumentPolicy & Network Err when receive...
[gecko.git] / gfx / ots / src / glyf.cc
blob0c19d6d7bcd77ec0f4d20ddc15af519ce834fa81
1 // Copyright (c) 2009-2017 The OTS 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 "glyf.h"
7 #include <algorithm>
8 #include <limits>
10 #include "head.h"
11 #include "loca.h"
12 #include "maxp.h"
14 // glyf - Glyph Data
15 // http://www.microsoft.com/typography/otspec/glyf.htm
17 namespace ots {
19 bool OpenTypeGLYF::ParseFlagsForSimpleGlyph(Buffer &glyph,
20 uint32_t num_flags,
21 uint32_t *flag_index,
22 uint32_t *coordinates_length) {
23 uint8_t flag = 0;
24 if (!glyph.ReadU8(&flag)) {
25 return Error("Can't read flag");
28 uint32_t delta = 0;
29 if (flag & (1u << 1)) { // x-Short
30 ++delta;
31 } else if (!(flag & (1u << 4))) {
32 delta += 2;
35 if (flag & (1u << 2)) { // y-Short
36 ++delta;
37 } else if (!(flag & (1u << 5))) {
38 delta += 2;
41 /* MS and Apple specs say this bit is reserved and must be set to zero, but
42 * Apple spec then contradicts itself and says it should be set on the first
43 * contour flag for simple glyphs with overlapping contours:
44 * https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6AATIntro.html
45 * (“Overlapping contours” section) */
46 if (flag & (1u << 6) && *flag_index != 0) {
47 return Error("Bad glyph flag (%d), "
48 "bit 6 must be set to zero for flag %d", flag, *flag_index);
51 if (flag & (1u << 3)) { // repeat
52 if (*flag_index + 1 >= num_flags) {
53 return Error("Count too high (%d + 1 >= %d)", *flag_index, num_flags);
55 uint8_t repeat = 0;
56 if (!glyph.ReadU8(&repeat)) {
57 return Error("Can't read repeat value");
59 if (repeat == 0) {
60 return Error("Zero repeat");
62 delta += (delta * repeat);
64 *flag_index += repeat;
65 if (*flag_index >= num_flags) {
66 return Error("Count too high (%d >= %d)", *flag_index, num_flags);
70 if (flag & (1u << 7)) { // reserved flag
71 return Error("Bad glyph flag (%d), reserved bit 7 must be set to zero", flag);
74 *coordinates_length += delta;
75 if (glyph.length() < *coordinates_length) {
76 return Error("Glyph coordinates length bigger than glyph length (%d > %d)",
77 *coordinates_length, glyph.length());
80 return true;
83 bool OpenTypeGLYF::ParseSimpleGlyph(Buffer &glyph,
84 int16_t num_contours) {
85 // read the end-points array
86 uint16_t num_flags = 0;
87 for (int i = 0; i < num_contours; ++i) {
88 uint16_t tmp_index = 0;
89 if (!glyph.ReadU16(&tmp_index)) {
90 return Error("Can't read contour index %d", i);
92 if (tmp_index == 0xffffu) {
93 return Error("Bad contour index %d", i);
95 // check if the indices are monotonically increasing
96 if (i && (tmp_index + 1 <= num_flags)) {
97 return Error("Decreasing contour index %d + 1 <= %d", tmp_index, num_flags);
99 num_flags = tmp_index + 1;
102 uint16_t bytecode_length = 0;
103 if (!glyph.ReadU16(&bytecode_length)) {
104 return Error("Can't read bytecode length");
107 if (this->maxp->version_1 &&
108 this->maxp->max_size_glyf_instructions < bytecode_length) {
109 this->maxp->max_size_glyf_instructions = bytecode_length;
110 Warning("Bytecode length is bigger than maxp.maxSizeOfInstructions %d: %d",
111 this->maxp->max_size_glyf_instructions, bytecode_length);
114 if (!glyph.Skip(bytecode_length)) {
115 return Error("Can't read bytecode of length %d", bytecode_length);
118 uint32_t coordinates_length = 0;
119 for (uint32_t i = 0; i < num_flags; ++i) {
120 if (!ParseFlagsForSimpleGlyph(glyph, num_flags, &i, &coordinates_length)) {
121 return Error("Failed to parse glyph flags %d", i);
125 if (!glyph.Skip(coordinates_length)) {
126 return Error("Glyph too short %d", glyph.length());
129 if (glyph.remaining() > 3) {
130 // We allow 0-3 bytes difference since gly_length is 4-bytes aligned,
131 // zero-padded length.
132 Warning("Extra bytes at end of the glyph: %d", glyph.remaining());
135 this->iov.push_back(std::make_pair(glyph.buffer(), glyph.offset()));
137 return true;
140 #define ARG_1_AND_2_ARE_WORDS (1u << 0)
141 #define WE_HAVE_A_SCALE (1u << 3)
142 #define MORE_COMPONENTS (1u << 5)
143 #define WE_HAVE_AN_X_AND_Y_SCALE (1u << 6)
144 #define WE_HAVE_A_TWO_BY_TWO (1u << 7)
145 #define WE_HAVE_INSTRUCTIONS (1u << 8)
147 bool OpenTypeGLYF::ParseCompositeGlyph(Buffer &glyph) {
148 uint16_t flags = 0;
149 uint16_t gid = 0;
150 do {
151 if (!glyph.ReadU16(&flags) || !glyph.ReadU16(&gid)) {
152 return Error("Can't read composite glyph flags or glyphIndex");
155 if (gid >= this->maxp->num_glyphs) {
156 return Error("Invalid glyph id used in composite glyph: %d", gid);
159 if (flags & ARG_1_AND_2_ARE_WORDS) {
160 int16_t argument1;
161 int16_t argument2;
162 if (!glyph.ReadS16(&argument1) || !glyph.ReadS16(&argument2)) {
163 return Error("Can't read argument1 or argument2");
165 } else {
166 uint8_t argument1;
167 uint8_t argument2;
168 if (!glyph.ReadU8(&argument1) || !glyph.ReadU8(&argument2)) {
169 return Error("Can't read argument1 or argument2");
173 if (flags & WE_HAVE_A_SCALE) {
174 int16_t scale;
175 if (!glyph.ReadS16(&scale)) {
176 return Error("Can't read scale");
178 } else if (flags & WE_HAVE_AN_X_AND_Y_SCALE) {
179 int16_t xscale;
180 int16_t yscale;
181 if (!glyph.ReadS16(&xscale) || !glyph.ReadS16(&yscale)) {
182 return Error("Can't read xscale or yscale");
184 } else if (flags & WE_HAVE_A_TWO_BY_TWO) {
185 int16_t xscale;
186 int16_t scale01;
187 int16_t scale10;
188 int16_t yscale;
189 if (!glyph.ReadS16(&xscale) ||
190 !glyph.ReadS16(&scale01) ||
191 !glyph.ReadS16(&scale10) ||
192 !glyph.ReadS16(&yscale)) {
193 return Error("Can't read transform");
196 } while (flags & MORE_COMPONENTS);
198 if (flags & WE_HAVE_INSTRUCTIONS) {
199 uint16_t bytecode_length;
200 if (!glyph.ReadU16(&bytecode_length)) {
201 return Error("Can't read instructions size");
204 if (this->maxp->version_1 &&
205 this->maxp->max_size_glyf_instructions < bytecode_length) {
206 this->maxp->max_size_glyf_instructions = bytecode_length;
207 Warning("Bytecode length is bigger than maxp.maxSizeOfInstructions "
208 "%d: %d",
209 this->maxp->max_size_glyf_instructions, bytecode_length);
212 if (!glyph.Skip(bytecode_length)) {
213 return Error("Can't read bytecode of length %d", bytecode_length);
217 this->iov.push_back(std::make_pair(glyph.buffer(), glyph.offset()));
219 return true;
222 bool OpenTypeGLYF::Parse(const uint8_t *data, size_t length) {
223 OpenTypeMAXP *maxp = static_cast<OpenTypeMAXP*>(
224 GetFont()->GetTypedTable(OTS_TAG_MAXP));
225 OpenTypeLOCA *loca = static_cast<OpenTypeLOCA*>(
226 GetFont()->GetTypedTable(OTS_TAG_LOCA));
227 OpenTypeHEAD *head = static_cast<OpenTypeHEAD*>(
228 GetFont()->GetTypedTable(OTS_TAG_HEAD));
229 if (!maxp || !loca || !head) {
230 return Error("Missing maxp or loca or head table needed by glyf table");
233 this->maxp = maxp;
235 const unsigned num_glyphs = maxp->num_glyphs;
236 std::vector<uint32_t> &offsets = loca->offsets;
238 if (offsets.size() != num_glyphs + 1) {
239 return Error("Invalide glyph offsets size %ld != %d", offsets.size(), num_glyphs + 1);
242 std::vector<uint32_t> resulting_offsets(num_glyphs + 1);
243 uint32_t current_offset = 0;
245 for (unsigned i = 0; i < num_glyphs; ++i) {
246 const unsigned gly_offset = offsets[i];
247 // The LOCA parser checks that these values are monotonic
248 const unsigned gly_length = offsets[i + 1] - offsets[i];
249 if (!gly_length) {
250 // this glyph has no outline (e.g. the space charactor)
251 resulting_offsets[i] = current_offset;
252 continue;
255 if (gly_offset >= length) {
256 return Error("Glyph %d offset %d too high %ld", i, gly_offset, length);
258 // Since these are unsigned types, the compiler is not allowed to assume
259 // that they never overflow.
260 if (gly_offset + gly_length < gly_offset) {
261 return Error("Glyph %d length (%d < 0)!", i, gly_length);
263 if (gly_offset + gly_length > length) {
264 return Error("Glyph %d length %d too high", i, gly_length);
267 Buffer glyph(data + gly_offset, gly_length);
269 int16_t num_contours, xmin, ymin, xmax, ymax;
270 if (!glyph.ReadS16(&num_contours) ||
271 !glyph.ReadS16(&xmin) ||
272 !glyph.ReadS16(&ymin) ||
273 !glyph.ReadS16(&xmax) ||
274 !glyph.ReadS16(&ymax)) {
275 return Error("Can't read glyph %d header", i);
278 if (num_contours <= -2) {
279 // -2, -3, -4, ... are reserved for future use.
280 return Error("Bad number of contours %d in glyph %d", num_contours, i);
283 // workaround for fonts in http://www.princexml.com/fonts/
284 if ((xmin == 32767) &&
285 (xmax == -32767) &&
286 (ymin == 32767) &&
287 (ymax == -32767)) {
288 Warning("bad xmin/xmax/ymin/ymax values");
289 xmin = xmax = ymin = ymax = 0;
292 if (xmin > xmax || ymin > ymax) {
293 return Error("Bad bounding box values bl=(%d, %d), tr=(%d, %d) in glyph %d", xmin, ymin, xmax, ymax, i);
296 if (num_contours == 0) {
297 // This is an empty glyph and shouldn’t have any glyph data, but if it
298 // does we will simply ignore it.
299 glyph.set_offset(0);
300 } else if (num_contours > 0) {
301 if (!ParseSimpleGlyph(glyph, num_contours)) {
302 return Error("Failed to parse glyph %d", i);
304 } else {
305 if (!ParseCompositeGlyph(glyph)) {
306 return Error("Failed to parse glyph %d", i);
310 size_t new_size = glyph.offset();
311 resulting_offsets[i] = current_offset;
312 // glyphs must be four byte aligned
313 // TODO(yusukes): investigate whether this padding is really necessary.
314 // Which part of the spec requires this?
315 const unsigned padding = (4 - (new_size & 3)) % 4;
316 if (padding) {
317 this->iov.push_back(std::make_pair(
318 reinterpret_cast<const uint8_t*>("\x00\x00\x00\x00"),
319 static_cast<size_t>(padding)));
320 new_size += padding;
322 current_offset += new_size;
324 resulting_offsets[num_glyphs] = current_offset;
326 const uint16_t max16 = std::numeric_limits<uint16_t>::max();
327 if ((*std::max_element(resulting_offsets.begin(),
328 resulting_offsets.end()) >= (max16 * 2u)) &&
329 (head->index_to_loc_format != 1)) {
330 head->index_to_loc_format = 1;
333 loca->offsets = resulting_offsets;
335 if (this->iov.empty()) {
336 // As a special case when all glyph in the font are empty, add a zero byte
337 // to the table, so that we don’t reject it down the way, and to make the
338 // table work on Windows as well.
339 // See https://github.com/khaledhosny/ots/issues/52
340 static const uint8_t kZero = 0;
341 this->iov.push_back(std::make_pair(&kZero, 1));
344 return true;
347 bool OpenTypeGLYF::Serialize(OTSStream *out) {
348 for (unsigned i = 0; i < this->iov.size(); ++i) {
349 if (!out->Write(this->iov[i].first, this->iov[i].second)) {
350 return Error("Falied to write glyph %d", i);
354 return true;
357 } // namespace ots