2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
23 |
*
|
|
24 |
*/
|
|
25 |
|
|
26 |
/*
|
|
27 |
*
|
7486
|
28 |
* (C) Copyright IBM Corp. 1998-2010 - All Rights Reserved
|
2
|
29 |
*
|
|
30 |
*/
|
|
31 |
|
|
32 |
#include "LETypes.h"
|
|
33 |
#include "OpenTypeUtilities.h"
|
|
34 |
#include "LEFontInstance.h"
|
|
35 |
#include "OpenTypeTables.h"
|
7486
|
36 |
#include "ICUFeatures.h"
|
2
|
37 |
#include "Lookups.h"
|
|
38 |
#include "ScriptAndLanguage.h"
|
|
39 |
#include "GlyphDefinitionTables.h"
|
|
40 |
#include "GlyphIterator.h"
|
|
41 |
#include "LookupProcessor.h"
|
|
42 |
#include "LEGlyphStorage.h"
|
|
43 |
#include "LESwaps.h"
|
|
44 |
|
3935
|
45 |
U_NAMESPACE_BEGIN
|
|
46 |
|
2
|
47 |
le_uint32 LookupProcessor::applyLookupTable(const LookupTable *lookupTable, GlyphIterator *glyphIterator,
|
7486
|
48 |
const LEFontInstance *fontInstance, LEErrorCode& success) const
|
2
|
49 |
{
|
7486
|
50 |
if (LE_FAILURE(success)) {
|
|
51 |
return 0;
|
|
52 |
}
|
|
53 |
|
2
|
54 |
le_uint16 lookupType = SWAPW(lookupTable->lookupType);
|
|
55 |
le_uint16 subtableCount = SWAPW(lookupTable->subTableCount);
|
|
56 |
le_int32 startPosition = glyphIterator->getCurrStreamPosition();
|
|
57 |
le_uint32 delta;
|
|
58 |
|
|
59 |
for (le_uint16 subtable = 0; subtable < subtableCount; subtable += 1) {
|
|
60 |
const LookupSubtable *lookupSubtable = lookupTable->getLookupSubtable(subtable);
|
|
61 |
|
7486
|
62 |
delta = applySubtable(lookupSubtable, lookupType, glyphIterator, fontInstance, success);
|
2
|
63 |
|
7486
|
64 |
if (delta > 0 && LE_FAILURE(success)) {
|
2
|
65 |
return 1;
|
|
66 |
}
|
|
67 |
|
|
68 |
glyphIterator->setCurrStreamPosition(startPosition);
|
|
69 |
}
|
|
70 |
|
|
71 |
return 1;
|
|
72 |
}
|
|
73 |
|
3935
|
74 |
le_int32 LookupProcessor::process(LEGlyphStorage &glyphStorage, GlyphPositionAdjustments *glyphPositionAdjustments,
|
|
75 |
le_bool rightToLeft, const GlyphDefinitionTableHeader *glyphDefinitionTableHeader,
|
7486
|
76 |
const LEFontInstance *fontInstance, LEErrorCode& success) const
|
2
|
77 |
{
|
7486
|
78 |
if (LE_FAILURE(success)) {
|
|
79 |
return 0;
|
|
80 |
}
|
|
81 |
|
2
|
82 |
le_int32 glyphCount = glyphStorage.getGlyphCount();
|
|
83 |
|
|
84 |
if (lookupSelectArray == NULL) {
|
|
85 |
return glyphCount;
|
|
86 |
}
|
|
87 |
|
|
88 |
GlyphIterator glyphIterator(glyphStorage, glyphPositionAdjustments,
|
|
89 |
rightToLeft, 0, 0, glyphDefinitionTableHeader);
|
|
90 |
le_int32 newGlyphCount = glyphCount;
|
|
91 |
|
|
92 |
for (le_uint16 order = 0; order < lookupOrderCount; order += 1) {
|
|
93 |
le_uint16 lookup = lookupOrderArray[order];
|
|
94 |
FeatureMask selectMask = lookupSelectArray[lookup];
|
|
95 |
|
|
96 |
if (selectMask != 0) {
|
|
97 |
const LookupTable *lookupTable = lookupListTable->getLookupTable(lookup);
|
13036
|
98 |
|
|
99 |
if (!lookupTable)
|
|
100 |
continue;
|
|
101 |
|
2
|
102 |
le_uint16 lookupFlags = SWAPW(lookupTable->lookupFlags);
|
|
103 |
|
|
104 |
glyphIterator.reset(lookupFlags, selectMask);
|
|
105 |
|
|
106 |
while (glyphIterator.findFeatureTag()) {
|
7486
|
107 |
applyLookupTable(lookupTable, &glyphIterator, fontInstance, success);
|
|
108 |
if (LE_FAILURE(success)) {
|
|
109 |
return 0;
|
2
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
newGlyphCount = glyphIterator.applyInsertions();
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
return newGlyphCount;
|
|
118 |
}
|
|
119 |
|
|
120 |
le_uint32 LookupProcessor::applySingleLookup(le_uint16 lookupTableIndex, GlyphIterator *glyphIterator,
|
7486
|
121 |
const LEFontInstance *fontInstance, LEErrorCode& success) const
|
2
|
122 |
{
|
7486
|
123 |
if (LE_FAILURE(success)) {
|
|
124 |
return 0;
|
|
125 |
}
|
|
126 |
|
2
|
127 |
const LookupTable *lookupTable = lookupListTable->getLookupTable(lookupTableIndex);
|
|
128 |
le_uint16 lookupFlags = SWAPW(lookupTable->lookupFlags);
|
|
129 |
GlyphIterator tempIterator(*glyphIterator, lookupFlags);
|
7486
|
130 |
le_uint32 delta = applyLookupTable(lookupTable, &tempIterator, fontInstance, success);
|
2
|
131 |
|
|
132 |
return delta;
|
|
133 |
}
|
|
134 |
|
|
135 |
le_int32 LookupProcessor::selectLookups(const FeatureTable *featureTable, FeatureMask featureMask, le_int32 order)
|
|
136 |
{
|
|
137 |
le_uint16 lookupCount = featureTable? SWAPW(featureTable->lookupCount) : 0;
|
|
138 |
le_int32 store = order;
|
|
139 |
|
|
140 |
for (le_uint16 lookup = 0; lookup < lookupCount; lookup += 1) {
|
|
141 |
le_uint16 lookupListIndex = SWAPW(featureTable->lookupListIndexArray[lookup]);
|
|
142 |
|
13036
|
143 |
if (lookupListIndex >= lookupSelectCount)
|
|
144 |
continue;
|
|
145 |
|
2
|
146 |
lookupSelectArray[lookupListIndex] |= featureMask;
|
|
147 |
lookupOrderArray[store++] = lookupListIndex;
|
|
148 |
}
|
|
149 |
|
|
150 |
return store - order;
|
|
151 |
}
|
|
152 |
|
|
153 |
LookupProcessor::LookupProcessor(const char *baseAddress,
|
|
154 |
Offset scriptListOffset, Offset featureListOffset, Offset lookupListOffset,
|
7486
|
155 |
LETag scriptTag, LETag languageTag, const FeatureMap *featureMap, le_int32 featureMapCount, le_bool orderFeatures,
|
|
156 |
LEErrorCode& success)
|
13036
|
157 |
: lookupListTable(NULL), featureListTable(NULL), lookupSelectArray(NULL), lookupSelectCount(0),
|
2
|
158 |
lookupOrderArray(NULL), lookupOrderCount(0)
|
|
159 |
{
|
|
160 |
const ScriptListTable *scriptListTable = NULL;
|
|
161 |
const LangSysTable *langSysTable = NULL;
|
|
162 |
le_uint16 featureCount = 0;
|
|
163 |
le_uint16 lookupListCount = 0;
|
|
164 |
le_uint16 requiredFeatureIndex;
|
|
165 |
|
7486
|
166 |
if (LE_FAILURE(success)) {
|
|
167 |
return;
|
|
168 |
}
|
|
169 |
|
2
|
170 |
if (scriptListOffset != 0) {
|
|
171 |
scriptListTable = (const ScriptListTable *) (baseAddress + scriptListOffset);
|
|
172 |
langSysTable = scriptListTable->findLanguage(scriptTag, languageTag);
|
|
173 |
|
|
174 |
if (langSysTable != 0) {
|
|
175 |
featureCount = SWAPW(langSysTable->featureCount);
|
|
176 |
}
|
|
177 |
}
|
|
178 |
|
|
179 |
if (featureListOffset != 0) {
|
|
180 |
featureListTable = (const FeatureListTable *) (baseAddress + featureListOffset);
|
|
181 |
}
|
|
182 |
|
|
183 |
if (lookupListOffset != 0) {
|
|
184 |
lookupListTable = (const LookupListTable *) (baseAddress + lookupListOffset);
|
|
185 |
lookupListCount = SWAPW(lookupListTable->lookupCount);
|
|
186 |
}
|
|
187 |
|
|
188 |
if (langSysTable == NULL || featureListTable == NULL || lookupListTable == NULL ||
|
|
189 |
featureCount == 0 || lookupListCount == 0) {
|
|
190 |
return;
|
|
191 |
}
|
|
192 |
|
|
193 |
requiredFeatureIndex = SWAPW(langSysTable->reqFeatureIndex);
|
|
194 |
|
|
195 |
lookupSelectArray = LE_NEW_ARRAY(FeatureMask, lookupListCount);
|
7486
|
196 |
if (lookupSelectArray == NULL) {
|
|
197 |
success = LE_MEMORY_ALLOCATION_ERROR;
|
|
198 |
return;
|
|
199 |
}
|
2
|
200 |
|
|
201 |
for (int i = 0; i < lookupListCount; i += 1) {
|
|
202 |
lookupSelectArray[i] = 0;
|
|
203 |
}
|
|
204 |
|
13036
|
205 |
lookupSelectCount = lookupListCount;
|
|
206 |
|
2
|
207 |
le_int32 count, order = 0;
|
|
208 |
le_int32 featureReferences = 0;
|
|
209 |
const FeatureTable *featureTable = NULL;
|
|
210 |
LETag featureTag;
|
|
211 |
|
|
212 |
const FeatureTable *requiredFeatureTable = NULL;
|
|
213 |
LETag requiredFeatureTag = 0x00000000U;
|
|
214 |
|
|
215 |
// Count the total number of lookups referenced by all features. This will
|
|
216 |
// be the maximum number of entries in the lookupOrderArray. We can't use
|
|
217 |
// lookupListCount because some lookups might be referenced by more than
|
|
218 |
// one feature.
|
|
219 |
for (le_int32 feature = 0; feature < featureCount; feature += 1) {
|
|
220 |
le_uint16 featureIndex = SWAPW(langSysTable->featureIndexArray[feature]);
|
|
221 |
|
|
222 |
featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
|
13036
|
223 |
|
|
224 |
if (!featureTable)
|
|
225 |
continue;
|
|
226 |
|
2
|
227 |
featureReferences += SWAPW(featureTable->lookupCount);
|
|
228 |
}
|
|
229 |
|
|
230 |
if (requiredFeatureIndex != 0xFFFF) {
|
|
231 |
requiredFeatureTable = featureListTable->getFeatureTable(requiredFeatureIndex, &requiredFeatureTag);
|
|
232 |
featureReferences += SWAPW(featureTable->lookupCount);
|
|
233 |
}
|
|
234 |
|
|
235 |
lookupOrderArray = LE_NEW_ARRAY(le_uint16, featureReferences);
|
7486
|
236 |
if (lookupOrderArray == NULL) {
|
|
237 |
success = LE_MEMORY_ALLOCATION_ERROR;
|
|
238 |
return;
|
|
239 |
}
|
2
|
240 |
|
|
241 |
for (le_int32 f = 0; f < featureMapCount; f += 1) {
|
|
242 |
FeatureMap fm = featureMap[f];
|
|
243 |
count = 0;
|
|
244 |
|
|
245 |
// If this is the required feature, add its lookups
|
|
246 |
if (requiredFeatureTag == fm.tag) {
|
|
247 |
count += selectLookups(requiredFeatureTable, fm.mask, order);
|
|
248 |
}
|
|
249 |
|
|
250 |
if (orderFeatures) {
|
|
251 |
// If we added lookups from the required feature, sort them
|
|
252 |
if (count > 1) {
|
|
253 |
OpenTypeUtilities::sort(lookupOrderArray, order);
|
|
254 |
}
|
|
255 |
|
|
256 |
for (le_uint16 feature = 0; feature < featureCount; feature += 1) {
|
|
257 |
le_uint16 featureIndex = SWAPW(langSysTable->featureIndexArray[feature]);
|
|
258 |
|
|
259 |
// don't add the required feature to the list more than once...
|
|
260 |
// TODO: Do we need this check? (Spec. says required feature won't be in feature list...)
|
|
261 |
if (featureIndex == requiredFeatureIndex) {
|
|
262 |
continue;
|
|
263 |
}
|
|
264 |
|
|
265 |
featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
|
|
266 |
|
|
267 |
if (featureTag == fm.tag) {
|
|
268 |
count += selectLookups(featureTable, fm.mask, order + count);
|
|
269 |
}
|
|
270 |
}
|
|
271 |
|
|
272 |
if (count > 1) {
|
|
273 |
OpenTypeUtilities::sort(&lookupOrderArray[order], count);
|
|
274 |
}
|
|
275 |
|
|
276 |
order += count;
|
|
277 |
} else {
|
|
278 |
for (le_uint16 feature = 0; feature < featureCount; feature += 1) {
|
|
279 |
le_uint16 featureIndex = SWAPW(langSysTable->featureIndexArray[feature]);
|
|
280 |
|
|
281 |
// don't add the required feature to the list more than once...
|
|
282 |
// NOTE: This check is commented out because the spec. says that
|
|
283 |
// the required feature won't be in the feature list, and because
|
|
284 |
// any duplicate entries will be removed below.
|
|
285 |
#if 0
|
|
286 |
if (featureIndex == requiredFeatureIndex) {
|
|
287 |
continue;
|
|
288 |
}
|
|
289 |
#endif
|
|
290 |
|
|
291 |
featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
|
|
292 |
|
|
293 |
if (featureTag == fm.tag) {
|
|
294 |
order += selectLookups(featureTable, fm.mask, order);
|
|
295 |
}
|
|
296 |
}
|
|
297 |
}
|
|
298 |
}
|
|
299 |
|
|
300 |
if (!orderFeatures && (order > 1)) {
|
|
301 |
OpenTypeUtilities::sort(lookupOrderArray, order);
|
|
302 |
|
|
303 |
// If there's no specified feature order,
|
|
304 |
// we will apply the lookups in the order
|
|
305 |
// that they're in the font. If a particular
|
|
306 |
// lookup may be referenced by more than one feature,
|
|
307 |
// it will apprear in the lookupOrderArray more than
|
|
308 |
// once, so remove any duplicate entries in the sorted array.
|
|
309 |
le_int32 out = 1;
|
|
310 |
|
|
311 |
for (le_int32 in = 1; in < order; in += 1) {
|
|
312 |
if (lookupOrderArray[out - 1] != lookupOrderArray[in]) {
|
|
313 |
if (out != in) {
|
|
314 |
lookupOrderArray[out] = lookupOrderArray[in];
|
|
315 |
}
|
|
316 |
|
|
317 |
out += 1;
|
|
318 |
}
|
|
319 |
}
|
|
320 |
|
|
321 |
order = out;
|
|
322 |
}
|
|
323 |
|
|
324 |
lookupOrderCount = order;
|
|
325 |
}
|
|
326 |
|
|
327 |
LookupProcessor::LookupProcessor()
|
|
328 |
{
|
7486
|
329 |
lookupOrderArray = NULL;
|
|
330 |
lookupSelectArray = NULL;
|
2
|
331 |
}
|
|
332 |
|
|
333 |
LookupProcessor::~LookupProcessor()
|
|
334 |
{
|
|
335 |
LE_DELETE_ARRAY(lookupOrderArray);
|
|
336 |
LE_DELETE_ARRAY(lookupSelectArray);
|
|
337 |
}
|
3935
|
338 |
|
|
339 |
U_NAMESPACE_END
|