gfx/ots/src/post.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/ots/src/post.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,182 @@
     1.4 +// Copyright (c) 2009 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "post.h"
     1.9 +
    1.10 +#include "maxp.h"
    1.11 +
    1.12 +// post - PostScript
    1.13 +// http://www.microsoft.com/typography/otspec/post.htm
    1.14 +
    1.15 +#define TABLE_NAME "post"
    1.16 +
    1.17 +namespace ots {
    1.18 +
    1.19 +bool ots_post_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
    1.20 +  Buffer table(data, length);
    1.21 +
    1.22 +  OpenTypePOST *post = new OpenTypePOST;
    1.23 +  file->post = post;
    1.24 +
    1.25 +  if (!table.ReadU32(&post->version) ||
    1.26 +      !table.ReadU32(&post->italic_angle) ||
    1.27 +      !table.ReadS16(&post->underline) ||
    1.28 +      !table.ReadS16(&post->underline_thickness) ||
    1.29 +      !table.ReadU32(&post->is_fixed_pitch)) {
    1.30 +    return OTS_FAILURE_MSG("Failed to read post header");
    1.31 +  }
    1.32 +
    1.33 +  if (post->underline_thickness < 0) {
    1.34 +    post->underline_thickness = 1;
    1.35 +  }
    1.36 +
    1.37 +  if (post->version == 0x00010000) {
    1.38 +    return true;
    1.39 +  } else if (post->version == 0x00030000) {
    1.40 +    return true;
    1.41 +  } else if (post->version != 0x00020000) {
    1.42 +    // 0x00025000 is deprecated. We don't accept it.
    1.43 +    return OTS_FAILURE_MSG("Bad post version %x", post->version);
    1.44 +  }
    1.45 +
    1.46 +  // We have a version 2 table with a list of Pascal strings at the end
    1.47 +
    1.48 +  // We don't care about the memory usage fields. We'll set all these to zero
    1.49 +  // when serialising
    1.50 +  if (!table.Skip(16)) {
    1.51 +    return OTS_FAILURE_MSG("Failed to skip memory usage in post table");
    1.52 +  }
    1.53 +
    1.54 +  uint16_t num_glyphs = 0;
    1.55 +  if (!table.ReadU16(&num_glyphs)) {
    1.56 +    return OTS_FAILURE_MSG("Failed to read number of glyphs");
    1.57 +  }
    1.58 +
    1.59 +  if (!file->maxp) {
    1.60 +    return OTS_FAILURE_MSG("No maxp table required by post table");
    1.61 +  }
    1.62 +
    1.63 +  if (num_glyphs == 0) {
    1.64 +    if (file->maxp->num_glyphs > 258) {
    1.65 +      return OTS_FAILURE_MSG("Can't have no glyphs in the post table if there are more than 256 glyphs in the font");
    1.66 +    }
    1.67 +    OTS_WARNING("table version is 1, but no glyf names are found");
    1.68 +    // workaround for fonts in http://www.fontsquirrel.com/fontface
    1.69 +    // (e.g., yataghan.ttf).
    1.70 +    post->version = 0x00010000;
    1.71 +    return true;
    1.72 +  }
    1.73 +
    1.74 +  if (num_glyphs != file->maxp->num_glyphs) {
    1.75 +    // Note: Fixedsys500c.ttf seems to have inconsistent num_glyphs values.
    1.76 +    return OTS_FAILURE_MSG("Bad number of glyphs in post table %d", num_glyphs);
    1.77 +  }
    1.78 +
    1.79 +  post->glyph_name_index.resize(num_glyphs);
    1.80 +  for (unsigned i = 0; i < num_glyphs; ++i) {
    1.81 +    if (!table.ReadU16(&post->glyph_name_index[i])) {
    1.82 +      return OTS_FAILURE_MSG("Failed to read post information for glyph %d", i);
    1.83 +    }
    1.84 +    // Note: A strict interpretation of the specification requires name indexes
    1.85 +    // are less than 32768. This, however, excludes fonts like unifont.ttf
    1.86 +    // which cover all of unicode.
    1.87 +  }
    1.88 +
    1.89 +  // Now we have an array of Pascal strings. We have to check that they are all
    1.90 +  // valid and read them in.
    1.91 +  const size_t strings_offset = table.offset();
    1.92 +  const uint8_t *strings = data + strings_offset;
    1.93 +  const uint8_t *strings_end = data + length;
    1.94 +
    1.95 +  for (;;) {
    1.96 +    if (strings == strings_end) break;
    1.97 +    const unsigned string_length = *strings;
    1.98 +    if (strings + 1 + string_length > strings_end) {
    1.99 +      return OTS_FAILURE_MSG("Bad string length %d", string_length);
   1.100 +    }
   1.101 +    if (std::memchr(strings + 1, '\0', string_length)) {
   1.102 +      return OTS_FAILURE_MSG("Bad string of length %d", string_length);
   1.103 +    }
   1.104 +    post->names.push_back(
   1.105 +        std::string(reinterpret_cast<const char*>(strings + 1), string_length));
   1.106 +    strings += 1 + string_length;
   1.107 +  }
   1.108 +  const unsigned num_strings = post->names.size();
   1.109 +
   1.110 +  // check that all the references are within bounds
   1.111 +  for (unsigned i = 0; i < num_glyphs; ++i) {
   1.112 +    unsigned offset = post->glyph_name_index[i];
   1.113 +    if (offset < 258) {
   1.114 +      continue;
   1.115 +    }
   1.116 +
   1.117 +    offset -= 258;
   1.118 +    if (offset >= num_strings) {
   1.119 +      return OTS_FAILURE_MSG("Bad string index %d", offset);
   1.120 +    }
   1.121 +  }
   1.122 +
   1.123 +  return true;
   1.124 +}
   1.125 +
   1.126 +bool ots_post_should_serialise(OpenTypeFile *file) {
   1.127 +  return file->post != NULL;
   1.128 +}
   1.129 +
   1.130 +bool ots_post_serialise(OTSStream *out, OpenTypeFile *file) {
   1.131 +  const OpenTypePOST *post = file->post;
   1.132 +
   1.133 +  // OpenType with CFF glyphs must have v3 post table.
   1.134 +  if (file->post && file->cff && file->post->version != 0x00030000) {
   1.135 +    return OTS_FAILURE_MSG("Bad post version %x", post->version);
   1.136 +  }
   1.137 +
   1.138 +  if (!out->WriteU32(post->version) ||
   1.139 +      !out->WriteU32(post->italic_angle) ||
   1.140 +      !out->WriteS16(post->underline) ||
   1.141 +      !out->WriteS16(post->underline_thickness) ||
   1.142 +      !out->WriteU32(post->is_fixed_pitch) ||
   1.143 +      !out->WriteU32(0) ||
   1.144 +      !out->WriteU32(0) ||
   1.145 +      !out->WriteU32(0) ||
   1.146 +      !out->WriteU32(0)) {
   1.147 +    return OTS_FAILURE_MSG("Failed to write post header");
   1.148 +  }
   1.149 +
   1.150 +  if (post->version != 0x00020000) {
   1.151 +    return true;  // v1.0 and v3.0 does not have glyph names.
   1.152 +  }
   1.153 +
   1.154 +  if (!out->WriteU16(post->glyph_name_index.size())) {
   1.155 +    return OTS_FAILURE_MSG("Failed to write number of indices");
   1.156 +  }
   1.157 +
   1.158 +  for (unsigned i = 0; i < post->glyph_name_index.size(); ++i) {
   1.159 +    if (!out->WriteU16(post->glyph_name_index[i])) {
   1.160 +      return OTS_FAILURE_MSG("Failed to write name index %d", i);
   1.161 +    }
   1.162 +  }
   1.163 +
   1.164 +  // Now we just have to write out the strings in the correct order
   1.165 +  for (unsigned i = 0; i < post->names.size(); ++i) {
   1.166 +    const std::string& s = post->names[i];
   1.167 +    const uint8_t string_length = s.size();
   1.168 +    if (!out->Write(&string_length, 1)) {
   1.169 +      return OTS_FAILURE_MSG("Failed to write string %d", i);
   1.170 +    }
   1.171 +    // Some ttf fonts (e.g., frank.ttf on Windows Vista) have zero-length name.
   1.172 +    // We allow them.
   1.173 +    if (string_length > 0 && !out->Write(s.data(), string_length)) {
   1.174 +      return OTS_FAILURE_MSG("Failed to write string length for string %d", i);
   1.175 +    }
   1.176 +  }
   1.177 +
   1.178 +  return true;
   1.179 +}
   1.180 +
   1.181 +void ots_post_free(OpenTypeFile *file) {
   1.182 +  delete file->post;
   1.183 +}
   1.184 +
   1.185 +}  // namespace ots

mercurial