Roll src/third_party/WebKit 06cb9e9:a978ee5 (svn 202558:202559)
[chromium-blink-merge.git] / third_party / ots / src / post.cc
blobe7ed92b4a699d9a15b0b9d1e875a2a133c9644b8
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 "post.h"
7 #include "maxp.h"
9 // post - PostScript
10 // http://www.microsoft.com/typography/otspec/post.htm
12 #define TABLE_NAME "post"
14 namespace ots {
16 bool ots_post_parse(Font *font, const uint8_t *data, size_t length) {
17 Buffer table(data, length);
19 OpenTypePOST *post = new OpenTypePOST;
20 font->post = post;
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");
30 if (post->underline_thickness < 0) {
31 post->underline_thickness = 1;
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);
43 // We have a version 2 table with a list of Pascal strings at the end
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");
51 uint16_t num_glyphs = 0;
52 if (!table.ReadU16(&num_glyphs)) {
53 return OTS_FAILURE_MSG("Failed to read number of glyphs");
56 if (!font->maxp) {
57 return OTS_FAILURE_MSG("No maxp table required by post table");
60 if (num_glyphs == 0) {
61 if (font->maxp->num_glyphs > 258) {
62 return OTS_FAILURE_MSG("Can't have no glyphs in the post table if there are more than 256 glyphs in the font");
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;
71 if (num_glyphs != font->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);
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);
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.
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;
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);
98 if (std::memchr(strings + 1, '\0', string_length)) {
99 return OTS_FAILURE_MSG("Bad string of length %d", string_length);
101 post->names.push_back(
102 std::string(reinterpret_cast<const char*>(strings + 1), string_length));
103 strings += 1 + string_length;
105 const unsigned num_strings = post->names.size();
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;
114 offset -= 258;
115 if (offset >= num_strings) {
116 return OTS_FAILURE_MSG("Bad string index %d", offset);
120 return true;
123 bool ots_post_should_serialise(Font *font) {
124 return font->post != NULL;
127 bool ots_post_serialise(OTSStream *out, Font *font) {
128 const OpenTypePOST *post = font->post;
130 // OpenType with CFF glyphs must have v3 post table.
131 if (font->post && font->cff && font->post->version != 0x00030000) {
132 return OTS_FAILURE_MSG("Bad post version %x", post->version);
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");
147 if (post->version != 0x00020000) {
148 return true; // v1.0 and v3.0 does not have glyph names.
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");
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);
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);
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);
179 return true;
182 void ots_post_reuse(Font *font, Font *other) {
183 font->post = other->post;
184 font->post_reused = true;
187 void ots_post_free(Font *font) {
188 delete font->post;
191 } // namespace ots
193 #undef TABLE_NAME