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 "loca.h"
6 :
7 : #include "head.h"
8 : #include "maxp.h"
9 :
10 : // loca - Index to Location
11 : // http://www.microsoft.com/typography/otspec/loca.htm
12 :
13 : #define TABLE_NAME "loca"
14 :
15 : namespace ots {
16 :
17 0 : bool ots_loca_parse(Font *font, const uint8_t *data, size_t length) {
18 0 : Buffer table(data, length);
19 :
20 : // We can't do anything useful in validating this data except to ensure that
21 : // the values are monotonically increasing.
22 :
23 0 : OpenTypeLOCA *loca = new OpenTypeLOCA;
24 0 : font->loca = loca;
25 :
26 0 : if (!font->maxp || !font->head) {
27 0 : return OTS_FAILURE_MSG("maxp or head tables missing from font, needed by loca");
28 : }
29 :
30 0 : const unsigned num_glyphs = font->maxp->num_glyphs;
31 0 : unsigned last_offset = 0;
32 0 : loca->offsets.resize(num_glyphs + 1);
33 : // maxp->num_glyphs is uint16_t, thus the addition never overflows.
34 :
35 0 : if (font->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 0 : for (unsigned i = 0; i <= num_glyphs; ++i) {
40 0 : uint16_t offset = 0;
41 0 : if (!table.ReadU16(&offset)) {
42 0 : return OTS_FAILURE_MSG("Failed to read offset for glyph %d", i);
43 : }
44 0 : if (offset < last_offset) {
45 0 : return OTS_FAILURE_MSG("Out of order offset %d < %d for glyph %d", offset, last_offset, i);
46 : }
47 0 : last_offset = offset;
48 0 : loca->offsets[i] = offset * 2;
49 : }
50 : } else {
51 0 : for (unsigned i = 0; i <= num_glyphs; ++i) {
52 0 : uint32_t offset = 0;
53 0 : if (!table.ReadU32(&offset)) {
54 0 : return OTS_FAILURE_MSG("Failed to read offset for glyph %d", i);
55 : }
56 0 : if (offset < last_offset) {
57 0 : return OTS_FAILURE_MSG("Out of order offset %d < %d for glyph %d", offset, last_offset, i);
58 : }
59 0 : last_offset = offset;
60 0 : loca->offsets[i] = offset;
61 : }
62 : }
63 :
64 0 : return true;
65 : }
66 :
67 0 : bool ots_loca_should_serialise(Font *font) {
68 0 : return font->loca != NULL;
69 : }
70 :
71 0 : bool ots_loca_serialise(OTSStream *out, Font *font) {
72 0 : const OpenTypeLOCA *loca = font->loca;
73 0 : const OpenTypeHEAD *head = font->head;
74 :
75 0 : if (!head) {
76 0 : return OTS_FAILURE_MSG("Missing head table in font needed by loca");
77 : }
78 :
79 0 : if (head->index_to_loc_format == 0) {
80 0 : for (unsigned i = 0; i < loca->offsets.size(); ++i) {
81 0 : const uint16_t offset = static_cast<uint16_t>(loca->offsets[i] >> 1);
82 0 : if ((offset != (loca->offsets[i] >> 1)) ||
83 0 : !out->WriteU16(offset)) {
84 0 : return OTS_FAILURE_MSG("Failed to write glyph offset for glyph %d", i);
85 : }
86 : }
87 : } else {
88 0 : for (unsigned i = 0; i < loca->offsets.size(); ++i) {
89 0 : if (!out->WriteU32(loca->offsets[i])) {
90 0 : return OTS_FAILURE_MSG("Failed to write glyph offset for glyph %d", i);
91 : }
92 : }
93 : }
94 :
95 0 : return true;
96 : }
97 :
98 0 : void ots_loca_reuse(Font *font, Font *other) {
99 0 : font->loca = other->loca;
100 0 : font->loca_reused = true;
101 0 : }
102 :
103 0 : void ots_loca_free(Font *font) {
104 0 : delete font->loca;
105 0 : }
106 :
107 : } // namespace ots
108 :
109 : #undef TABLE_NAME
|