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