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