Home | History | Annotate | Download | only in src
      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.
      4 
      5 #include "vorg.h"
      6 
      7 #include <vector>
      8 
      9 // VORG - Vertical Origin Table
     10 // http://www.microsoft.com/opentype/otspec/vorg.htm
     11 
     12 #define DROP_THIS_TABLE \
     13   do { delete file->vorg; file->vorg = 0; } while (0)
     14 
     15 namespace ots {
     16 
     17 bool ots_vorg_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
     18   Buffer table(data, length);
     19   file->vorg = new OpenTypeVORG;
     20   OpenTypeVORG * const vorg = file->vorg;
     21 
     22   uint16_t num_recs;
     23   if (!table.ReadU16(&vorg->major_version) ||
     24       !table.ReadU16(&vorg->minor_version) ||
     25       !table.ReadS16(&vorg->default_vert_origin_y) ||
     26       !table.ReadU16(&num_recs)) {
     27     return OTS_FAILURE();
     28   }
     29   if (vorg->major_version != 1) {
     30     OTS_WARNING("bad major version: %u", vorg->major_version);
     31     DROP_THIS_TABLE;
     32     return true;
     33   }
     34   if (vorg->minor_version != 0) {
     35     OTS_WARNING("bad minor version: %u", vorg->minor_version);
     36     DROP_THIS_TABLE;
     37     return true;
     38   }
     39 
     40   // num_recs might be zero (e.g., DFHSMinchoPro5-W3-Demo.otf).
     41   if (!num_recs) {
     42     return true;
     43   }
     44 
     45   uint16_t last_glyph_index = 0;
     46   vorg->metrics.reserve(num_recs);
     47   for (unsigned i = 0; i < num_recs; ++i) {
     48     OpenTypeVORGMetrics rec;
     49 
     50     if (!table.ReadU16(&rec.glyph_index) ||
     51         !table.ReadS16(&rec.vert_origin_y)) {
     52       return OTS_FAILURE();
     53     }
     54     if ((i != 0) && (rec.glyph_index <= last_glyph_index)) {
     55       OTS_WARNING("the table is not sorted");
     56       DROP_THIS_TABLE;
     57       return true;
     58     }
     59     last_glyph_index = rec.glyph_index;
     60 
     61     vorg->metrics.push_back(rec);
     62   }
     63 
     64   return true;
     65 }
     66 
     67 bool ots_vorg_should_serialise(OpenTypeFile *file) {
     68   if (!file->cff) return false;  // this table is not for fonts with TT glyphs.
     69   return file->vorg != NULL;
     70 }
     71 
     72 bool ots_vorg_serialise(OTSStream *out, OpenTypeFile *file) {
     73   OpenTypeVORG * const vorg = file->vorg;
     74 
     75   if (!out->WriteU16(vorg->major_version) ||
     76       !out->WriteU16(vorg->minor_version) ||
     77       !out->WriteS16(vorg->default_vert_origin_y) ||
     78       !out->WriteU16(vorg->metrics.size())) {
     79     return OTS_FAILURE();
     80   }
     81 
     82   for (unsigned i = 0; i < vorg->metrics.size(); ++i) {
     83     const OpenTypeVORGMetrics& rec = vorg->metrics[i];
     84     if (!out->WriteU16(rec.glyph_index) ||
     85         !out->WriteS16(rec.vert_origin_y)) {
     86       return OTS_FAILURE();
     87     }
     88   }
     89 
     90   return true;
     91 }
     92 
     93 void ots_vorg_free(OpenTypeFile *file) {
     94   delete file->vorg;
     95 }
     96 
     97 }  // namespace ots
     98