Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / ots / src / ltsh.cc
blob5b34cf4ad8b6efe67fc037e421ec921352495b28
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.
5 #include "ltsh.h"
7 #include "maxp.h"
9 // LTSH - Linear Threshold
10 // http://www.microsoft.com/typography/otspec/ltsh.htm
12 #define TABLE_NAME "LTSH"
14 #define DROP_THIS_TABLE(...) \
15 do { \
16 OTS_FAILURE_MSG_(font->file, TABLE_NAME ": " __VA_ARGS__); \
17 OTS_FAILURE_MSG("Table discarded"); \
18 delete font->ltsh; \
19 font->ltsh = 0; \
20 } while (0)
22 namespace ots {
24 bool ots_ltsh_parse(Font *font, const uint8_t *data, size_t length) {
25 Buffer table(data, length);
27 if (!font->maxp) {
28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh");
31 OpenTypeLTSH *ltsh = new OpenTypeLTSH;
32 font->ltsh = ltsh;
34 uint16_t num_glyphs = 0;
35 if (!table.ReadU16(&ltsh->version) ||
36 !table.ReadU16(&num_glyphs)) {
37 return OTS_FAILURE_MSG("Failed to read ltsh header");
40 if (ltsh->version != 0) {
41 DROP_THIS_TABLE("bad version: %u", ltsh->version);
42 return true;
45 if (num_glyphs != font->maxp->num_glyphs) {
46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs);
47 return true;
50 ltsh->ypels.reserve(num_glyphs);
51 for (unsigned i = 0; i < num_glyphs; ++i) {
52 uint8_t pel = 0;
53 if (!table.ReadU8(&pel)) {
54 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i);
56 ltsh->ypels.push_back(pel);
59 return true;
62 bool ots_ltsh_should_serialise(Font *font) {
63 if (!font->glyf) return false; // this table is not for CFF fonts.
64 return font->ltsh != NULL;
67 bool ots_ltsh_serialise(OTSStream *out, Font *font) {
68 const OpenTypeLTSH *ltsh = font->ltsh;
70 const uint16_t num_ypels = static_cast<uint16_t>(ltsh->ypels.size());
71 if (num_ypels != ltsh->ypels.size() ||
72 !out->WriteU16(ltsh->version) ||
73 !out->WriteU16(num_ypels)) {
74 return OTS_FAILURE_MSG("Failed to write pels size");
76 for (uint16_t i = 0; i < num_ypels; ++i) {
77 if (!out->Write(&(ltsh->ypels[i]), 1)) {
78 return OTS_FAILURE_MSG("Failed to write pixel size for glyph %d", i);
82 return true;
85 void ots_ltsh_reuse(Font *font, Font *other) {
86 font->ltsh = other->ltsh;
87 font->ltsh_reused = true;
90 void ots_ltsh_free(Font *font) {
91 delete font->ltsh;
94 } // namespace ots
96 #undef TABLE_NAME
97 #undef DROP_THIS_TABLE