gfx/ots/src/post.cc

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

     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(OpenTypeFile *file, const uint8_t *data, size_t length) {
    17   Buffer table(data, length);
    19   OpenTypePOST *post = new OpenTypePOST;
    20   file->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");
    28   }
    30   if (post->underline_thickness < 0) {
    31     post->underline_thickness = 1;
    32   }
    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   }
    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");
    49   }
    51   uint16_t num_glyphs = 0;
    52   if (!table.ReadU16(&num_glyphs)) {
    53     return OTS_FAILURE_MSG("Failed to read number of glyphs");
    54   }
    56   if (!file->maxp) {
    57     return OTS_FAILURE_MSG("No maxp table required by post table");
    58   }
    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 are 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   }
    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   }
    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   }
    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);
    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();
   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     }
   114     offset -= 258;
   115     if (offset >= num_strings) {
   116       return OTS_FAILURE_MSG("Bad string index %d", offset);
   117     }
   118   }
   120   return true;
   121 }
   123 bool ots_post_should_serialise(OpenTypeFile *file) {
   124   return file->post != NULL;
   125 }
   127 bool ots_post_serialise(OTSStream *out, OpenTypeFile *file) {
   128   const OpenTypePOST *post = file->post;
   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   }
   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   }
   147   if (post->version != 0x00020000) {
   148     return true;  // v1.0 and v3.0 does not have glyph names.
   149   }
   151   if (!out->WriteU16(post->glyph_name_index.size())) {
   152     return OTS_FAILURE_MSG("Failed to write number of indices");
   153   }
   155   for (unsigned i = 0; i < post->glyph_name_index.size(); ++i) {
   156     if (!out->WriteU16(post->glyph_name_index[i])) {
   157       return OTS_FAILURE_MSG("Failed to write name index %d", i);
   158     }
   159   }
   161   // Now we just have to write out the strings in the correct order
   162   for (unsigned i = 0; i < post->names.size(); ++i) {
   163     const std::string& s = post->names[i];
   164     const uint8_t string_length = s.size();
   165     if (!out->Write(&string_length, 1)) {
   166       return OTS_FAILURE_MSG("Failed to write string %d", i);
   167     }
   168     // Some ttf fonts (e.g., frank.ttf on Windows Vista) have zero-length name.
   169     // We allow them.
   170     if (string_length > 0 && !out->Write(s.data(), string_length)) {
   171       return OTS_FAILURE_MSG("Failed to write string length for string %d", i);
   172     }
   173   }
   175   return true;
   176 }
   178 void ots_post_free(OpenTypeFile *file) {
   179   delete file->post;
   180 }
   182 }  // namespace ots

mercurial