summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/ots/src/ltsh.cc
blob: 9145527dc745ed670a78053bf9f67c03f4dba950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ltsh.h"

#include "maxp.h"

// LTSH - Linear Threshold
// http://www.microsoft.com/typography/otspec/ltsh.htm

#define DROP_THIS_TABLE \
  do { delete file->ltsh; file->ltsh = 0; } while (0)

namespace ots {

bool ots_ltsh_parse(OpenTypeFile *file, const uint8_t *data, size_t length) {
  Buffer table(data, length);

  if (!file->maxp) {
    return OTS_FAILURE();
  }

  OpenTypeLTSH *ltsh = new OpenTypeLTSH;
  file->ltsh = ltsh;

  uint16_t num_glyphs = 0;
  if (!table.ReadU16(&ltsh->version) ||
      !table.ReadU16(&num_glyphs)) {
    return OTS_FAILURE();
  }

  if (ltsh->version != 0) {
    OTS_WARNING("bad version: %u", ltsh->version);
    DROP_THIS_TABLE;
    return true;
  }

  if (num_glyphs != file->maxp->num_glyphs) {
    OTS_WARNING("bad num_glyphs: %u", num_glyphs);
    DROP_THIS_TABLE;
    return true;
  }

  ltsh->ypels.reserve(num_glyphs);
  for (unsigned i = 0; i < num_glyphs; ++i) {
    uint8_t pel = 0;
    if (!table.ReadU8(&pel)) {
      return OTS_FAILURE();
    }
    ltsh->ypels.push_back(pel);
  }

  return true;
}

bool ots_ltsh_should_serialise(OpenTypeFile *file) {
  if (!file->glyf) return false;  // this table is not for CFF fonts.
  return file->ltsh != NULL;
}

bool ots_ltsh_serialise(OTSStream *out, OpenTypeFile *file) {
  const OpenTypeLTSH *ltsh = file->ltsh;

  if (!out->WriteU16(ltsh->version) ||
      !out->WriteU16(ltsh->ypels.size())) {
    return OTS_FAILURE();
  }
  for (unsigned i = 0; i < ltsh->ypels.size(); ++i) {
    if (!out->Write(&(ltsh->ypels[i]), 1)) {
      return OTS_FAILURE();
    }
  }

  return true;
}

void ots_ltsh_free(OpenTypeFile *file) {
  delete file->ltsh;
}

}  // namespace ots