1 // -*- related-file-name: "../include/efont/ttfkern.hh" -*-
2 
3 /* ttfkern.{cc,hh} -- TrueType kern table
4  *
5  * Copyright (c) 2009-2019 Eddie Kohler
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version. This program is distributed in the hope that it will be
11  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13  * Public License for more details.
14  */
15 
16 #ifdef HAVE_CONFIG_H
17 # include <config.h>
18 #endif
19 #include <efont/ttfkern.hh>
20 #include <lcdf/error.hh>
21 #include <lcdf/straccum.hh>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <algorithm>
26 
27 namespace Efont { namespace OpenType {
28 
29 inline Data
subtable(uint32_t & off_in_out) const30 KernTable::subtable(uint32_t &off_in_out) const
31 {
32     uint32_t off = off_in_out, len;
33     if (_version == 0) {
34         if (_d.u16(off) != 0)
35             throw new Format("kern subtable");
36         len = _d.u16(off + 2);
37     } else
38         len = _d.u32(off);
39     if (len < 6 || off + len > (uint32_t) _d.length())
40         throw new Bounds();
41     off_in_out = off + len;
42     return _d.substring(off, len);
43 }
44 
KernTable(const Data & d,ErrorHandler *)45 KernTable::KernTable(const Data &d, ErrorHandler *)
46     : _d(d), _error(-1)
47 {
48     // USHORT   Version
49     // USHORT   nTables
50     if (d.length() == 0)
51         throw BlankTable("kern");
52 
53     uint32_t ntables, off;
54     if (d.u16(0) == 0) {
55         ntables = d.u16(2);
56         _version = 0;
57         off = 4;
58     } else if (d.u16(0) == 1) {
59         ntables = d.u32(4);
60         _version = 1;
61         off = 8;
62     } else
63         throw Format("kern");
64 
65     for (uint32_t i = 0; i < ntables; ++i)
66         (void) subtable(off);
67 
68     _error = 0;
69 }
70 
71 bool
unparse_automatics(Vector<Positioning> & v,ErrorHandler * errh) const72 KernTable::unparse_automatics(Vector<Positioning> &v, ErrorHandler *errh) const
73 {
74     uint32_t ntables = this->ntables();
75     uint32_t off = first_offset();
76     int success = 0;
77     if (_error < 0)
78         return false;
79 
80     for (uint16_t i = 0; i < ntables; ++i) {
81         Data subt = _d.subtable(off);
82         uint16_t coverage = subt.u16(4);
83 
84         if (_version == 0) {
85             if ((coverage & COV_V0_HORIZONTAL) == 0
86                 || (coverage & (COV_V0_MINIMUM | COV_V0_CROSS_STREAM
87                                 | COV_V0_OVERRIDE)) != 0
88                 || (coverage & COV_V0_FORMAT) != COV_V0_FORMAT0)
89                 continue;
90         } else {
91             if ((coverage & (COV_V1_VERTICAL | COV_V1_CROSS_STREAM
92                              | COV_V1_VARIATION)) != 0
93                 || (coverage & COV_V1_FORMAT) != COV_V1_FORMAT0)
94                 continue;
95         }
96 
97         try {
98             uint32_t off = (_version ? 16 : 14);
99             uint16_t npairs = subt.u16(off - 8);
100             for (uint16_t j = 0; j < npairs; ++j, off += 6) {
101                 v.push_back(Positioning(Position(subt.u16(off), 0, 0, subt.s16(off + 4), 0),
102                                         Position(subt.u16(off + 2), 0, 0, 0, 0)));
103                 ++success;
104             }
105         } catch (Error e) {
106             if (errh)
107                 errh->warning("%s, continuing", e.description.c_str());
108         }
109     }
110 
111     return success > 0;
112 }
113 
114 }}
115 
116 #include <lcdf/vector.cc>
117