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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
/* GRAPHITE2 LICENSING
Copyright 2010, SIL International
All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should also have received a copy of the GNU Lesser General Public
License along with this library in the file named "LICENSE".
If not, write to the Free Software Foundation, 51 Franklin Street,
Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
internet at http://www.fsf.org/licenses/lgpl.html.
Alternatively, the contents of this file may be used under the terms of the
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
License, as published by the Free Software Foundation, either version 2
of the License or (at your option) any later version.
*/
#include <cstring>
#include "inc/Main.h"
#include "inc/bits.h"
#include "inc/Endian.h"
#include "inc/FeatureMap.h"
#include "inc/FeatureVal.h"
#include "graphite2/Font.h"
#include "inc/TtfUtil.h"
#include <cstdlib>
#include "inc/Face.h"
using namespace graphite2;
namespace
{
static int cmpNameAndFeatures(const void *ap, const void *bp)
{
const NameAndFeatureRef & a = *static_cast<const NameAndFeatureRef *>(ap),
& b = *static_cast<const NameAndFeatureRef *>(bp);
return (a < b ? -1 : (b < a ? 1 : 0));
}
const size_t FEAT_HEADER = sizeof(uint32) + 2*sizeof(uint16) + sizeof(uint32),
FEATURE_SIZE = sizeof(uint32)
+ 2*sizeof(uint16)
+ sizeof(uint32)
+ 2*sizeof(uint16),
FEATURE_SETTING_SIZE = sizeof(int16) + sizeof(uint16);
uint16 readFeatureSettings(const byte * p, FeatureSetting * s, size_t num_settings)
{
uint16 max_val = 0;
for (FeatureSetting * const end = s + num_settings; s != end; ++s)
{
const int16 value = be::read<int16>(p);
::new (s) FeatureSetting(value, be::read<uint16>(p));
if (uint16(value) > max_val) max_val = value;
}
return max_val;
}
}
FeatureRef::FeatureRef(const Face & face,
unsigned short & bits_offset, uint32 max_val,
uint32 name, uint16 uiName, flags_t flags,
FeatureSetting *settings, uint16 num_set) throw()
: m_face(&face),
m_nameValues(settings),
m_mask(mask_over_val(max_val)),
m_max(max_val),
m_id(name),
m_nameid(uiName),
m_numSet(num_set),
m_flags(flags)
{
const uint8 need_bits = bit_set_count(m_mask);
m_index = (bits_offset + need_bits) / SIZEOF_CHUNK;
if (m_index > bits_offset / SIZEOF_CHUNK)
bits_offset = m_index*SIZEOF_CHUNK;
m_bits = bits_offset % SIZEOF_CHUNK;
bits_offset += need_bits;
m_mask <<= m_bits;
}
FeatureRef::~FeatureRef() throw()
{
free(m_nameValues);
}
bool FeatureMap::readFeats(const Face & face)
{
const Face::Table feat(face, TtfUtil::Tag::Feat);
const byte * p = feat;
if (!p) return true;
if (feat.size() < FEAT_HEADER) return false;
const byte *const feat_start = p,
*const feat_end = p + feat.size();
const uint32 version = be::read<uint32>(p);
m_numFeats = be::read<uint16>(p);
be::skip<uint16>(p);
be::skip<uint32>(p);
// Sanity checks
if (m_numFeats == 0) return true;
if (version < 0x00010000 ||
p + m_numFeats*FEATURE_SIZE > feat_end)
{ //defensive
m_numFeats = 0;
return false;
}
m_feats = new FeatureRef [m_numFeats];
uint16 * const defVals = gralloc<uint16>(m_numFeats);
if (!defVals || !m_feats) return false;
unsigned short bits = 0; //to cause overflow on first Feature
for (int i = 0, ie = m_numFeats; i != ie; i++)
{
const uint32 label = version < 0x00020000 ? be::read<uint16>(p) : be::read<uint32>(p);
const uint16 num_settings = be::read<uint16>(p);
if (version >= 0x00020000)
be::skip<uint16>(p);
const uint32 settings_offset = be::read<uint32>(p);
const uint16 flags = be::read<uint16>(p),
uiName = be::read<uint16>(p);
if (settings_offset > size_t(feat_end - feat_start)
|| settings_offset + num_settings * FEATURE_SETTING_SIZE > size_t(feat_end - feat_start))
{
free(defVals);
return false;
}
FeatureSetting *uiSet;
uint32 maxVal;
if (num_settings != 0)
{
uiSet = gralloc<FeatureSetting>(num_settings);
if (!uiSet)
{
free(defVals);
return false;
}
maxVal = readFeatureSettings(feat_start + settings_offset, uiSet, num_settings);
defVals[i] = uiSet[0].value();
}
else
{
uiSet = 0;
maxVal = 0xffffffff;
defVals[i] = 0;
}
::new (m_feats + i) FeatureRef (face, bits, maxVal,
label, uiName,
FeatureRef::flags_t(flags),
uiSet, num_settings);
}
new (&m_defaultFeatures) Features(bits/(sizeof(uint32)*8) + 1, *this);
m_pNamedFeats = new NameAndFeatureRef[m_numFeats];
if (!m_pNamedFeats)
{
free(defVals);
return false;
}
for (int i = 0; i < m_numFeats; ++i)
{
m_feats[i].applyValToFeature(defVals[i], m_defaultFeatures);
m_pNamedFeats[i] = m_feats[i];
}
free(defVals);
qsort(m_pNamedFeats, m_numFeats, sizeof(NameAndFeatureRef), &cmpNameAndFeatures);
return true;
}
bool SillMap::readFace(const Face & face)
{
if (!m_FeatureMap.readFeats(face)) return false;
if (!readSill(face)) return false;
return true;
}
bool SillMap::readSill(const Face & face)
{
const Face::Table sill(face, TtfUtil::Tag::Sill);
const byte *p = sill;
if (!p) return true;
if (sill.size() < 12) return false;
if (be::read<uint32>(p) != 0x00010000UL) return false;
m_numLanguages = be::read<uint16>(p);
m_langFeats = new LangFeaturePair[m_numLanguages];
if (!m_langFeats || !m_FeatureMap.m_numFeats) { m_numLanguages = 0; return true; } //defensive
p += 6; // skip the fast search
if (sill.size() < m_numLanguages * 8U + 12) return false;
for (int i = 0; i < m_numLanguages; i++)
{
uint32 langid = be::read<uint32>(p);
uint16 numSettings = be::read<uint16>(p);
uint16 offset = be::read<uint16>(p);
if (offset + 8U * numSettings > sill.size() && numSettings > 0) return false;
Features* feats = new Features(m_FeatureMap.m_defaultFeatures);
if (!feats) return false;
const byte *pLSet = sill + offset;
// Apply langauge specific settings
for (int j = 0; j < numSettings; j++)
{
uint32 name = be::read<uint32>(pLSet);
uint16 val = be::read<uint16>(pLSet);
pLSet += 2;
const FeatureRef* pRef = m_FeatureMap.findFeatureRef(name);
if (pRef) pRef->applyValToFeature(val, *feats);
}
// Add the language id feature which is always feature id 1
const FeatureRef* pRef = m_FeatureMap.findFeatureRef(1);
if (pRef) pRef->applyValToFeature(langid, *feats);
m_langFeats[i].m_lang = langid;
m_langFeats[i].m_pFeatures = feats;
}
return true;
}
Features* SillMap::cloneFeatures(uint32 langname/*0 means default*/) const
{
if (langname)
{
// the number of languages in a font is usually small e.g. 8 in Doulos
// so this loop is not very expensive
for (uint16 i = 0; i < m_numLanguages; i++)
{
if (m_langFeats[i].m_lang == langname)
return new Features(*m_langFeats[i].m_pFeatures);
}
}
return new Features (m_FeatureMap.m_defaultFeatures);
}
const FeatureRef *FeatureMap::findFeatureRef(uint32 name) const
{
NameAndFeatureRef *it;
for (it = m_pNamedFeats; it < m_pNamedFeats + m_numFeats; ++it)
if (it->m_name == name)
return it->m_pFRef;
return NULL;
}
bool FeatureRef::applyValToFeature(uint32 val, Features & pDest) const
{
if (val>maxVal() || !m_face)
return false;
if (pDest.m_pMap==NULL)
pDest.m_pMap = &m_face->theSill().theFeatureMap();
else
if (pDest.m_pMap!=&m_face->theSill().theFeatureMap())
return false; //incompatible
if (m_index >= pDest.size())
pDest.resize(m_index+1);
pDest[m_index] &= ~m_mask;
pDest[m_index] |= (uint32(val) << m_bits);
return true;
}
uint32 FeatureRef::getFeatureVal(const Features& feats) const
{
if (m_index < feats.size() && m_face
&& &m_face->theSill().theFeatureMap()==feats.m_pMap)
return (feats[m_index] & m_mask) >> m_bits;
else
return 0;
}
|