Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: third_party/ots/src/post.cc

Issue 775893002: Updating OTS repo from https://github.com/khaledhosny/ots.git (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Updating with 4800 warning fix Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/ots/src/post.h ('k') | third_party/ots/src/prep.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "post.h"
6
7 #include "maxp.h"
8
9 // post - PostScript
10 // http://www.microsoft.com/typography/otspec/post.htm
11
12 #define TABLE_NAME "post"
13
14 namespace ots {
15
16 bool ots_post_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
17 Buffer table(data, length);
18
19 OpenTypePOST *post = new OpenTypePOST;
20 file->post = post;
21
22 if (!table.ReadU32(&post->version) ||
23 !table.ReadU32(&post->italic_angle) ||
24 !table.ReadS16(&post->underline) ||
25 !table.ReadS16(&post->underline_thickness) ||
26 !table.ReadU32(&post->is_fixed_pitch)) {
27 return OTS_FAILURE_MSG("Failed to read post header");
28 }
29
30 if (post->underline_thickness < 0) {
31 post->underline_thickness = 1;
32 }
33
34 if (post->version == 0x00010000) {
35 return true;
36 } else if (post->version == 0x00030000) {
37 return true;
38 } else if (post->version != 0x00020000) {
39 // 0x00025000 is deprecated. We don't accept it.
40 return OTS_FAILURE_MSG("Bad post version %x", post->version);
41 }
42
43 // We have a version 2 table with a list of Pascal strings at the end
44
45 // We don't care about the memory usage fields. We'll set all these to zero
46 // when serialising
47 if (!table.Skip(16)) {
48 return OTS_FAILURE_MSG("Failed to skip memory usage in post table");
49 }
50
51 uint16_t num_glyphs = 0;
52 if (!table.ReadU16(&num_glyphs)) {
53 return OTS_FAILURE_MSG("Failed to read number of glyphs");
54 }
55
56 if (!file->maxp) {
57 return OTS_FAILURE_MSG("No maxp table required by post table");
58 }
59
60 if (num_glyphs == 0) {
61 if (file->maxp->num_glyphs > 258) {
62 return OTS_FAILURE_MSG("Can't have no glyphs in the post table if there ar e more than 256 glyphs in the font");
63 }
64 OTS_WARNING("table version is 1, but no glyf names are found");
65 // workaround for fonts in http://www.fontsquirrel.com/fontface
66 // (e.g., yataghan.ttf).
67 post->version = 0x00010000;
68 return true;
69 }
70
71 if (num_glyphs != file->maxp->num_glyphs) {
72 // Note: Fixedsys500c.ttf seems to have inconsistent num_glyphs values.
73 return OTS_FAILURE_MSG("Bad number of glyphs in post table %d", num_glyphs);
74 }
75
76 post->glyph_name_index.resize(num_glyphs);
77 for (unsigned i = 0; i < num_glyphs; ++i) {
78 if (!table.ReadU16(&post->glyph_name_index[i])) {
79 return OTS_FAILURE_MSG("Failed to read post information for glyph %d", i);
80 }
81 // Note: A strict interpretation of the specification requires name indexes
82 // are less than 32768. This, however, excludes fonts like unifont.ttf
83 // which cover all of unicode.
84 }
85
86 // Now we have an array of Pascal strings. We have to check that they are all
87 // valid and read them in.
88 const size_t strings_offset = table.offset();
89 const uint8_t *strings = data + strings_offset;
90 const uint8_t *strings_end = data + length;
91
92 for (;;) {
93 if (strings == strings_end) break;
94 const unsigned string_length = *strings;
95 if (strings + 1 + string_length > strings_end) {
96 return OTS_FAILURE_MSG("Bad string length %d", string_length);
97 }
98 if (std::memchr(strings + 1, '\0', string_length)) {
99 return OTS_FAILURE_MSG("Bad string of length %d", string_length);
100 }
101 post->names.push_back(
102 std::string(reinterpret_cast<const char*>(strings + 1), string_length));
103 strings += 1 + string_length;
104 }
105 const unsigned num_strings = post->names.size();
106
107 // check that all the references are within bounds
108 for (unsigned i = 0; i < num_glyphs; ++i) {
109 unsigned offset = post->glyph_name_index[i];
110 if (offset < 258) {
111 continue;
112 }
113
114 offset -= 258;
115 if (offset >= num_strings) {
116 return OTS_FAILURE_MSG("Bad string index %d", offset);
117 }
118 }
119
120 return true;
121 }
122
123 bool ots_post_should_serialise(OpenTypeFile *file) {
124 return file->post != NULL;
125 }
126
127 bool ots_post_serialise(OTSStream *out, OpenTypeFile *file) {
128 const OpenTypePOST *post = file->post;
129
130 // OpenType with CFF glyphs must have v3 post table.
131 if (file->post && file->cff && file->post->version != 0x00030000) {
132 return OTS_FAILURE_MSG("Bad post version %x", post->version);
133 }
134
135 if (!out->WriteU32(post->version) ||
136 !out->WriteU32(post->italic_angle) ||
137 !out->WriteS16(post->underline) ||
138 !out->WriteS16(post->underline_thickness) ||
139 !out->WriteU32(post->is_fixed_pitch) ||
140 !out->WriteU32(0) ||
141 !out->WriteU32(0) ||
142 !out->WriteU32(0) ||
143 !out->WriteU32(0)) {
144 return OTS_FAILURE_MSG("Failed to write post header");
145 }
146
147 if (post->version != 0x00020000) {
148 return true; // v1.0 and v3.0 does not have glyph names.
149 }
150
151 const uint16_t num_indexes =
152 static_cast<uint16_t>(post->glyph_name_index.size());
153 if (num_indexes != post->glyph_name_index.size() ||
154 !out->WriteU16(num_indexes)) {
155 return OTS_FAILURE_MSG("Failed to write number of indices");
156 }
157
158 for (uint16_t i = 0; i < num_indexes; ++i) {
159 if (!out->WriteU16(post->glyph_name_index[i])) {
160 return OTS_FAILURE_MSG("Failed to write name index %d", i);
161 }
162 }
163
164 // Now we just have to write out the strings in the correct order
165 for (unsigned i = 0; i < post->names.size(); ++i) {
166 const std::string& s = post->names[i];
167 const uint8_t string_length = static_cast<uint8_t>(s.size());
168 if (string_length != s.size() ||
169 !out->Write(&string_length, 1)) {
170 return OTS_FAILURE_MSG("Failed to write string %d", i);
171 }
172 // Some ttf fonts (e.g., frank.ttf on Windows Vista) have zero-length name.
173 // We allow them.
174 if (string_length > 0 && !out->Write(s.data(), string_length)) {
175 return OTS_FAILURE_MSG("Failed to write string length for string %d", i);
176 }
177 }
178
179 return true;
180 }
181
182 void ots_post_free(OpenTypeFile *file) {
183 delete file->post;
184 }
185
186 } // namespace ots
187
188 #undef TABLE_NAME
OLDNEW
« no previous file with comments | « third_party/ots/src/post.h ('k') | third_party/ots/src/prep.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698