gfx/ots/src/vorg.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/ots/src/vorg.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     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 "vorg.h"
     1.9 +
    1.10 +#include <vector>
    1.11 +
    1.12 +// VORG - Vertical Origin Table
    1.13 +// http://www.microsoft.com/typography/otspec/vorg.htm
    1.14 +
    1.15 +#define TABLE_NAME "VORG"
    1.16 +
    1.17 +#define DROP_THIS_TABLE \
    1.18 +  do { \
    1.19 +    delete file->vorg; \
    1.20 +    file->vorg = 0; \
    1.21 +    OTS_FAILURE_MSG("Table discarded"); \
    1.22 +  } while (0)
    1.23 +
    1.24 +namespace ots {
    1.25 +
    1.26 +bool ots_vorg_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
    1.27 +  Buffer table(data, length);
    1.28 +  file->vorg = new OpenTypeVORG;
    1.29 +  OpenTypeVORG * const vorg = file->vorg;
    1.30 +
    1.31 +  uint16_t num_recs;
    1.32 +  if (!table.ReadU16(&vorg->major_version) ||
    1.33 +      !table.ReadU16(&vorg->minor_version) ||
    1.34 +      !table.ReadS16(&vorg->default_vert_origin_y) ||
    1.35 +      !table.ReadU16(&num_recs)) {
    1.36 +    return OTS_FAILURE_MSG("Failed to read header");
    1.37 +  }
    1.38 +  if (vorg->major_version != 1) {
    1.39 +    OTS_WARNING("bad major version: %u", vorg->major_version);
    1.40 +    DROP_THIS_TABLE;
    1.41 +    return true;
    1.42 +  }
    1.43 +  if (vorg->minor_version != 0) {
    1.44 +    OTS_WARNING("bad minor version: %u", vorg->minor_version);
    1.45 +    DROP_THIS_TABLE;
    1.46 +    return true;
    1.47 +  }
    1.48 +
    1.49 +  // num_recs might be zero (e.g., DFHSMinchoPro5-W3-Demo.otf).
    1.50 +  if (!num_recs) {
    1.51 +    return true;
    1.52 +  }
    1.53 +
    1.54 +  uint16_t last_glyph_index = 0;
    1.55 +  vorg->metrics.reserve(num_recs);
    1.56 +  for (unsigned i = 0; i < num_recs; ++i) {
    1.57 +    OpenTypeVORGMetrics rec;
    1.58 +
    1.59 +    if (!table.ReadU16(&rec.glyph_index) ||
    1.60 +        !table.ReadS16(&rec.vert_origin_y)) {
    1.61 +      return OTS_FAILURE_MSG("Failed to read record %d", i);
    1.62 +    }
    1.63 +    if ((i != 0) && (rec.glyph_index <= last_glyph_index)) {
    1.64 +      OTS_WARNING("the table is not sorted");
    1.65 +      DROP_THIS_TABLE;
    1.66 +      return true;
    1.67 +    }
    1.68 +    last_glyph_index = rec.glyph_index;
    1.69 +
    1.70 +    vorg->metrics.push_back(rec);
    1.71 +  }
    1.72 +
    1.73 +  return true;
    1.74 +}
    1.75 +
    1.76 +bool ots_vorg_should_serialise(OpenTypeFile *file) {
    1.77 +  if (!file->cff) return false;  // this table is not for fonts with TT glyphs.
    1.78 +  return file->vorg != NULL;
    1.79 +}
    1.80 +
    1.81 +bool ots_vorg_serialise(OTSStream *out, OpenTypeFile *file) {
    1.82 +  OpenTypeVORG * const vorg = file->vorg;
    1.83 +
    1.84 +  if (!out->WriteU16(vorg->major_version) ||
    1.85 +      !out->WriteU16(vorg->minor_version) ||
    1.86 +      !out->WriteS16(vorg->default_vert_origin_y) ||
    1.87 +      !out->WriteU16(vorg->metrics.size())) {
    1.88 +    return OTS_FAILURE_MSG("Failed to write table header");
    1.89 +  }
    1.90 +
    1.91 +  for (unsigned i = 0; i < vorg->metrics.size(); ++i) {
    1.92 +    const OpenTypeVORGMetrics& rec = vorg->metrics[i];
    1.93 +    if (!out->WriteU16(rec.glyph_index) ||
    1.94 +        !out->WriteS16(rec.vert_origin_y)) {
    1.95 +      return OTS_FAILURE_MSG("Failed to write record %d", i);
    1.96 +    }
    1.97 +  }
    1.98 +
    1.99 +  return true;
   1.100 +}
   1.101 +
   1.102 +void ots_vorg_free(OpenTypeFile *file) {
   1.103 +  delete file->vorg;
   1.104 +}
   1.105 +
   1.106 +}  // namespace ots

mercurial