annotate src/share/native/sun/font/layout/LookupProcessor.cpp @ 5555:1d8a3ef381f7

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