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