Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 "loca.h"
7 #include "head.h"
8 #include "maxp.h"
10 // loca - Index to Location
11 // http://www.microsoft.com/typography/otspec/loca.htm
13 #define TABLE_NAME "loca"
15 namespace ots {
17 bool ots_loca_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
18 Buffer table(data, length);
20 // We can't do anything useful in validating this data except to ensure that
21 // the values are monotonically increasing.
23 OpenTypeLOCA *loca = new OpenTypeLOCA;
24 file->loca = loca;
26 if (!file->maxp || !file->head) {
27 return OTS_FAILURE_MSG("maxp or head tables missing from font, needed by loca");
28 }
30 const unsigned num_glyphs = file->maxp->num_glyphs;
31 unsigned last_offset = 0;
32 loca->offsets.resize(num_glyphs + 1);
33 // maxp->num_glyphs is uint16_t, thus the addition never overflows.
35 if (file->head->index_to_loc_format == 0) {
36 // Note that the <= here (and below) is correct. There is one more offset
37 // than the number of glyphs in order to give the length of the final
38 // glyph.
39 for (unsigned i = 0; i <= num_glyphs; ++i) {
40 uint16_t offset = 0;
41 if (!table.ReadU16(&offset)) {
42 return OTS_FAILURE_MSG("Failed to read offset for glyph %d", i);
43 }
44 if (offset < last_offset) {
45 return OTS_FAILURE_MSG("Out of order offset %d < %d for glyph %d", offset, last_offset, i);
46 }
47 last_offset = offset;
48 loca->offsets[i] = offset * 2;
49 }
50 } else {
51 for (unsigned i = 0; i <= num_glyphs; ++i) {
52 uint32_t offset = 0;
53 if (!table.ReadU32(&offset)) {
54 return OTS_FAILURE_MSG("Failed to read offset for glyph %d", i);
55 }
56 if (offset < last_offset) {
57 return OTS_FAILURE_MSG("Out of order offset %d < %d for glyph %d", offset, last_offset, i);
58 }
59 last_offset = offset;
60 loca->offsets[i] = offset;
61 }
62 }
64 return true;
65 }
67 bool ots_loca_should_serialise(OpenTypeFile *file) {
68 return file->loca != NULL;
69 }
71 bool ots_loca_serialise(OTSStream *out, OpenTypeFile *file) {
72 const OpenTypeLOCA *loca = file->loca;
73 const OpenTypeHEAD *head = file->head;
75 if (!head) {
76 return OTS_FAILURE_MSG("Missing head table in font needed by loca");
77 }
79 if (head->index_to_loc_format == 0) {
80 for (unsigned i = 0; i < loca->offsets.size(); ++i) {
81 if (!out->WriteU16(loca->offsets[i] >> 1)) {
82 return OTS_FAILURE_MSG("Failed to write glyph offset for glyph %d", i);
83 }
84 }
85 } else {
86 for (unsigned i = 0; i < loca->offsets.size(); ++i) {
87 if (!out->WriteU32(loca->offsets[i])) {
88 return OTS_FAILURE_MSG("Failed to write glyph offset for glyph %d", i);
89 }
90 }
91 }
93 return true;
94 }
96 void ots_loca_free(OpenTypeFile *file) {
97 delete file->loca;
98 }
100 } // namespace ots