vorg.cc (2358B)
1 // Copyright (c) 2009-2017 The OTS 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/typography/otspec/vorg.htm 11 12 namespace ots { 13 14 bool OpenTypeVORG::Parse(const uint8_t *data, size_t length) { 15 Buffer table(data, length); 16 17 uint16_t num_recs; 18 if (!table.ReadU16(&this->major_version) || 19 !table.ReadU16(&this->minor_version) || 20 !table.ReadS16(&this->default_vert_origin_y) || 21 !table.ReadU16(&num_recs)) { 22 return Error("Failed to read header"); 23 } 24 if (this->major_version != 1) { 25 return Drop("Unsupported majorVersion: %u", this->major_version); 26 } 27 if (this->minor_version != 0) { 28 return Drop("Unsupported minorVersion: %u", this->minor_version); 29 } 30 31 // num_recs might be zero (e.g., DFHSMinchoPro5-W3-Demo.otf). 32 if (!num_recs) { 33 return true; 34 } 35 36 uint16_t last_glyph_index = 0; 37 this->metrics.reserve(num_recs); 38 for (unsigned i = 0; i < num_recs; ++i) { 39 OpenTypeVORGMetrics rec; 40 41 if (!table.ReadU16(&rec.glyph_index) || 42 !table.ReadS16(&rec.vert_origin_y)) { 43 return Error("Failed to read record %d", i); 44 } 45 if ((i != 0) && (rec.glyph_index <= last_glyph_index)) { 46 return Drop("The table is not sorted"); 47 } 48 last_glyph_index = rec.glyph_index; 49 50 this->metrics.push_back(rec); 51 } 52 53 return true; 54 } 55 56 bool OpenTypeVORG::Serialize(OTSStream *out) { 57 const uint16_t num_metrics = static_cast<uint16_t>(this->metrics.size()); 58 if (num_metrics != this->metrics.size() || 59 !out->WriteU16(this->major_version) || 60 !out->WriteU16(this->minor_version) || 61 !out->WriteS16(this->default_vert_origin_y) || 62 !out->WriteU16(num_metrics)) { 63 return Error("Failed to write table header"); 64 } 65 66 for (uint16_t i = 0; i < num_metrics; ++i) { 67 const OpenTypeVORGMetrics& rec = this->metrics[i]; 68 if (!out->WriteU16(rec.glyph_index) || 69 !out->WriteS16(rec.vert_origin_y)) { 70 return Error("Failed to write record %d", i); 71 } 72 } 73 74 return true; 75 } 76 77 bool OpenTypeVORG::ShouldSerialize() { 78 return Table::ShouldSerialize() && 79 // this table is not for fonts with TT glyphs. 80 GetFont()->GetTable(OTS_TAG_CFF) != NULL; 81 } 82 83 } // namespace ots