| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ltsh.h" | 5 #include "ltsh.h" |
| 6 | 6 |
| 7 #include "maxp.h" | 7 #include "maxp.h" |
| 8 | 8 |
| 9 // LTSH - Linear Threshold | 9 // LTSH - Linear Threshold |
| 10 // http://www.microsoft.com/typography/otspec/ltsh.htm | 10 // http://www.microsoft.com/typography/otspec/ltsh.htm |
| 11 | 11 |
| 12 #define TABLE_NAME "LTSH" | 12 #define TABLE_NAME "LTSH" |
| 13 | 13 |
| 14 #define DROP_THIS_TABLE(...) \ | 14 #define DROP_THIS_TABLE(...) \ |
| 15 do { \ | 15 do { \ |
| 16 OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__); \ | 16 OTS_FAILURE_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__); \ |
| 17 OTS_FAILURE_MSG("Table discarded"); \ | 17 OTS_FAILURE_MSG("Table discarded"); \ |
| 18 delete file->ltsh; \ | 18 delete font->ltsh; \ |
| 19 file->ltsh = 0; \ | 19 font->ltsh = 0; \ |
| 20 } while (0) | 20 } while (0) |
| 21 | 21 |
| 22 namespace ots { | 22 namespace ots { |
| 23 | 23 |
| 24 bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) { | 24 bool ots_ltsh_parse(Font *font, const uint8_t *data, size_t length) { |
| 25 Buffer table(data, length); | 25 Buffer table(data, length); |
| 26 | 26 |
| 27 if (!file->maxp) { | 27 if (!font->maxp) { |
| 28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh"); | 28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh"); |
| 29 } | 29 } |
| 30 | 30 |
| 31 OpenTypeLTSH *ltsh = new OpenTypeLTSH; | 31 OpenTypeLTSH *ltsh = new OpenTypeLTSH; |
| 32 file->ltsh = ltsh; | 32 font->ltsh = ltsh; |
| 33 | 33 |
| 34 uint16_t num_glyphs = 0; | 34 uint16_t num_glyphs = 0; |
| 35 if (!table.ReadU16(<sh->version) || | 35 if (!table.ReadU16(<sh->version) || |
| 36 !table.ReadU16(&num_glyphs)) { | 36 !table.ReadU16(&num_glyphs)) { |
| 37 return OTS_FAILURE_MSG("Failed to read ltsh header"); | 37 return OTS_FAILURE_MSG("Failed to read ltsh header"); |
| 38 } | 38 } |
| 39 | 39 |
| 40 if (ltsh->version != 0) { | 40 if (ltsh->version != 0) { |
| 41 DROP_THIS_TABLE("bad version: %u", ltsh->version); | 41 DROP_THIS_TABLE("bad version: %u", ltsh->version); |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 if (num_glyphs != file->maxp->num_glyphs) { | 45 if (num_glyphs != font->maxp->num_glyphs) { |
| 46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs); | 46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs); |
| 47 return true; | 47 return true; |
| 48 } | 48 } |
| 49 | 49 |
| 50 ltsh->ypels.reserve(num_glyphs); | 50 ltsh->ypels.reserve(num_glyphs); |
| 51 for (unsigned i = 0; i < num_glyphs; ++i) { | 51 for (unsigned i = 0; i < num_glyphs; ++i) { |
| 52 uint8_t pel = 0; | 52 uint8_t pel = 0; |
| 53 if (!table.ReadU8(&pel)) { | 53 if (!table.ReadU8(&pel)) { |
| 54 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i); | 54 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i); |
| 55 } | 55 } |
| 56 ltsh->ypels.push_back(pel); | 56 ltsh->ypels.push_back(pel); |
| 57 } | 57 } |
| 58 | 58 |
| 59 return true; | 59 return true; |
| 60 } | 60 } |
| 61 | 61 |
| 62 bool ots_ltsh_should_serialise(OpenTypeFile *file) { | 62 bool ots_ltsh_should_serialise(Font *font) { |
| 63 if (!file->glyf) return false; // this table is not for CFF fonts. | 63 if (!font->glyf) return false; // this table is not for CFF fonts. |
| 64 return file->ltsh != NULL; | 64 return font->ltsh != NULL; |
| 65 } | 65 } |
| 66 | 66 |
| 67 bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) { | 67 bool ots_ltsh_serialise(OTSStream *out, Font *font) { |
| 68 const OpenTypeLTSH *ltsh = file->ltsh; | 68 const OpenTypeLTSH *ltsh = font->ltsh; |
| 69 | 69 |
| 70 const uint16_t num_ypels = static_cast<uint16_t>(ltsh->ypels.size()); | 70 const uint16_t num_ypels = static_cast<uint16_t>(ltsh->ypels.size()); |
| 71 if (num_ypels != ltsh->ypels.size() || | 71 if (num_ypels != ltsh->ypels.size() || |
| 72 !out->WriteU16(ltsh->version) || | 72 !out->WriteU16(ltsh->version) || |
| 73 !out->WriteU16(num_ypels)) { | 73 !out->WriteU16(num_ypels)) { |
| 74 return OTS_FAILURE_MSG("Failed to write pels size"); | 74 return OTS_FAILURE_MSG("Failed to write pels size"); |
| 75 } | 75 } |
| 76 for (uint16_t i = 0; i < num_ypels; ++i) { | 76 for (uint16_t i = 0; i < num_ypels; ++i) { |
| 77 if (!out->Write(&(ltsh->ypels[i]), 1)) { | 77 if (!out->Write(&(ltsh->ypels[i]), 1)) { |
| 78 return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i); | 78 return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 return true; | 82 return true; |
| 83 } | 83 } |
| 84 | 84 |
| 85 void ots_ltsh_free(OpenTypeFile *file) { | 85 void ots_ltsh_reuse(Font *font, Font *other) { |
| 86 delete file->ltsh; | 86 font->ltsh = other->ltsh; |
| 87 font->ltsh_reused = true; |
| 88 } |
| 89 |
| 90 void ots_ltsh_free(Font *font) { |
| 91 delete font->ltsh; |
| 87 } | 92 } |
| 88 | 93 |
| 89 } // namespace ots | 94 } // namespace ots |
| 90 | 95 |
| 91 #undef TABLE_NAME | 96 #undef TABLE_NAME |
| 92 #undef DROP_THIS_TABLE | 97 #undef DROP_THIS_TABLE |
| OLD | NEW |