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