gfx/ots/src/post.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial