ltsh.cc (1841B)
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 "ltsh.h" 6 7 #include "maxp.h" 8 9 // LTSH - Linear Threshold 10 // http://www.microsoft.com/typography/otspec/ltsh.htm 11 12 namespace ots { 13 14 bool OpenTypeLTSH::Parse(const uint8_t *data, size_t length) { 15 Buffer table(data, length); 16 17 OpenTypeMAXP *maxp = static_cast<OpenTypeMAXP*>( 18 GetFont()->GetTypedTable(OTS_TAG_MAXP)); 19 if (!maxp) { 20 return Error("Required maxp table is missing"); 21 } 22 23 uint16_t num_glyphs = 0; 24 if (!table.ReadU16(&this->version) || 25 !table.ReadU16(&num_glyphs)) { 26 return Error("Failed to read table header"); 27 } 28 29 if (this->version != 0) { 30 return Drop("Unsupported version: %u", this->version); 31 } 32 33 if (num_glyphs != maxp->num_glyphs) { 34 return Drop("Bad numGlyphs: %u", num_glyphs); 35 } 36 37 this->ypels.reserve(num_glyphs); 38 for (unsigned i = 0; i < num_glyphs; ++i) { 39 uint8_t pel = 0; 40 if (!table.ReadU8(&pel)) { 41 return Error("Failed to read pixels for glyph %d", i); 42 } 43 this->ypels.push_back(pel); 44 } 45 46 return true; 47 } 48 49 bool OpenTypeLTSH::Serialize(OTSStream *out) { 50 const uint16_t num_ypels = static_cast<uint16_t>(this->ypels.size()); 51 if (num_ypels != this->ypels.size() || 52 !out->WriteU16(this->version) || 53 !out->WriteU16(num_ypels)) { 54 return Error("Failed to write table header"); 55 } 56 for (uint16_t i = 0; i < num_ypels; ++i) { 57 if (!out->Write(&(this->ypels[i]), 1)) { 58 return Error("Failed to write pixel size for glyph %d", i); 59 } 60 } 61 62 return true; 63 } 64 65 bool OpenTypeLTSH::ShouldSerialize() { 66 return Table::ShouldSerialize() && 67 // this table is not for CFF fonts. 68 GetFont()->GetTable(OTS_TAG_GLYF) != NULL; 69 } 70 71 } // namespace ots