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.
9 // LTSH - Linear Threshold
10 // http://www.microsoft.com/typography/otspec/ltsh.htm
12 #define TABLE_NAME "LTSH"
14 #define DROP_THIS_TABLE(...) \
16 OTS_FAILURE_MSG_(file, TABLE_NAME ": " __VA_ARGS__); \
17 OTS_FAILURE_MSG("Table discarded"); \
24 bool ots_ltsh_parse(OpenTypeFile
*file
, const uint8_t *data
, size_t length
) {
25 Buffer
table(data
, length
);
28 return OTS_FAILURE_MSG("Missing maxp table from font needed by ltsh");
31 OpenTypeLTSH
*ltsh
= new OpenTypeLTSH
;
34 uint16_t num_glyphs
= 0;
35 if (!table
.ReadU16(<sh
->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
);
45 if (num_glyphs
!= file
->maxp
->num_glyphs
) {
46 DROP_THIS_TABLE("bad num_glyphs: %u", num_glyphs
);
50 ltsh
->ypels
.reserve(num_glyphs
);
51 for (unsigned i
= 0; i
< num_glyphs
; ++i
) {
53 if (!table
.ReadU8(&pel
)) {
54 return OTS_FAILURE_MSG("Failed to read pixels for glyph %d", i
);
56 ltsh
->ypels
.push_back(pel
);
62 bool ots_ltsh_should_serialise(OpenTypeFile
*file
) {
63 if (!file
->glyf
) return false; // this table is not for CFF fonts.
64 return file
->ltsh
!= NULL
;
67 bool ots_ltsh_serialise(OTSStream
*out
, OpenTypeFile
*file
) {
68 const OpenTypeLTSH
*ltsh
= file
->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
);
85 void ots_ltsh_free(OpenTypeFile
*file
) {
92 #undef DROP_THIS_TABLE