os/textandloc/fontservices/textshaperplugin/IcuSource/layout/LookupProcessor.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/textandloc/fontservices/textshaperplugin/IcuSource/layout/LookupProcessor.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,257 @@
     1.4 +/*
     1.5 + *
     1.6 + * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
     1.7 + *
     1.8 + */
     1.9 +
    1.10 +#include "LETypes.h"
    1.11 +#include "OpenTypeUtilities.h"
    1.12 +#include "LEFontInstance.h"
    1.13 +#include "OpenTypeTables.h"
    1.14 +#include "Features.h"
    1.15 +#include "Lookups.h"
    1.16 +#include "ScriptAndLanguage.h"
    1.17 +#include "GlyphDefinitionTables.h"
    1.18 +#include "GlyphIterator.h"
    1.19 +#include "LookupProcessor.h"
    1.20 +#include "LEGlyphStorage.h"
    1.21 +#include "LESwaps.h"
    1.22 +
    1.23 +U_NAMESPACE_BEGIN
    1.24 +
    1.25 +const LETag LookupProcessor::notSelected    = 0x00000000;
    1.26 +const LETag LookupProcessor::defaultFeature = 0xFFFFFFFF;
    1.27 +
    1.28 +static const LETag emptyTag = 0x00000000;
    1.29 +
    1.30 +
    1.31 +le_uint32 LookupProcessor::applyLookupTable(const LookupTable *lookupTable,
    1.32 +    GlyphIterator *glyphIterator, const LEFontInstance *fontInstance,
    1.33 +    LEErrorCode& success) const
    1.34 +{
    1.35 +    if (LE_FAILURE(success)) {
    1.36 +        return 0;
    1.37 +    }
    1.38 +
    1.39 +    le_uint16 lookupType = SWAPW(lookupTable->lookupType);
    1.40 +    le_uint16 subtableCount = SWAPW(lookupTable->subTableCount);
    1.41 +    le_int32 startPosition = glyphIterator->getCurrStreamPosition();
    1.42 +    le_uint32 delta;
    1.43 +
    1.44 +    for (le_uint16 subtable = 0; subtable < subtableCount; subtable += 1) {
    1.45 +        const LookupSubtable *lookupSubtable = lookupTable->getLookupSubtable(subtable);
    1.46 +
    1.47 +        delta = applySubtable(lookupSubtable, lookupType, glyphIterator,
    1.48 +            fontInstance, success);
    1.49 +
    1.50 +        if (delta > 0 || LE_FAILURE(success)) {
    1.51 +            return 1;
    1.52 +        }
    1.53 +
    1.54 +        glyphIterator->setCurrStreamPosition(startPosition);
    1.55 +    }
    1.56 +
    1.57 +    return 1;
    1.58 +}
    1.59 +
    1.60 +le_int32 LookupProcessor::process(LEGlyphStorage &glyphStorage, GlyphPositionAdjustments *glyphPositionAdjustments,
    1.61 +                              le_bool rightToLeft, const GlyphDefinitionTableHeader *glyphDefinitionTableHeader,
    1.62 +                              const LEFontInstance *fontInstance, LEErrorCode& success) const
    1.63 +{
    1.64 +    if (LE_FAILURE(success)) {
    1.65 +        return 0;
    1.66 +    }
    1.67 +
    1.68 +    le_int32 glyphCount = glyphStorage.getGlyphCount();
    1.69 +
    1.70 +    if (lookupSelectArray == NULL) {
    1.71 +        return glyphCount;
    1.72 +    }
    1.73 +
    1.74 +    GlyphIterator glyphIterator(glyphStorage, glyphPositionAdjustments,
    1.75 +                                rightToLeft, 0, 0, glyphDefinitionTableHeader);
    1.76 +    le_int32 newGlyphCount = glyphCount;
    1.77 +
    1.78 +    for (le_uint16 order = 0; order < lookupOrderCount; order += 1) {
    1.79 +        le_uint16 lookup = lookupOrderArray[order];
    1.80 +        LETag selectTag = lookupSelectArray[lookup];
    1.81 +
    1.82 +        if (selectTag != notSelected) {
    1.83 +            const LookupTable *lookupTable = lookupListTable->getLookupTable(lookup);
    1.84 +            le_uint16 lookupFlags = SWAPW(lookupTable->lookupFlags);
    1.85 +            
    1.86 +            glyphIterator.reset(lookupFlags, selectTag);
    1.87 +
    1.88 +            while (glyphIterator.findFeatureTag()) {
    1.89 +                le_uint32 delta = 1;
    1.90 +
    1.91 +                while (glyphIterator.next(delta)) {
    1.92 +                    delta = applyLookupTable(lookupTable, &glyphIterator, fontInstance, success);
    1.93 +                    if (LE_FAILURE(success)) {
    1.94 +                        return 0;
    1.95 +                    }
    1.96 +                }
    1.97 +            }
    1.98 +
    1.99 +            newGlyphCount = glyphIterator.applyInsertions();
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    return newGlyphCount;
   1.104 +}
   1.105 +
   1.106 +le_uint32 LookupProcessor::applySingleLookup(le_uint16 lookupTableIndex, GlyphIterator *glyphIterator,
   1.107 +    const LEFontInstance *fontInstance, LEErrorCode& success) const
   1.108 +{
   1.109 +    if (LE_FAILURE(success)) {
   1.110 +        return 0;
   1.111 +    }
   1.112 +
   1.113 +    const LookupTable *lookupTable = lookupListTable->getLookupTable(lookupTableIndex);
   1.114 +    le_uint16 lookupFlags = SWAPW(lookupTable->lookupFlags);
   1.115 +    GlyphIterator tempIterator(*glyphIterator, lookupFlags);
   1.116 +    le_uint32 delta = applyLookupTable(lookupTable, &tempIterator, fontInstance, success);
   1.117 +
   1.118 +    return delta;
   1.119 +}
   1.120 +
   1.121 +le_int32 LookupProcessor::selectLookups(const FeatureTable *featureTable, LETag featureTag, le_int32 order)
   1.122 +{
   1.123 +    le_uint16 lookupCount = featureTable? SWAPW(featureTable->lookupCount) : 0;
   1.124 +    le_int32  store = order;
   1.125 +
   1.126 +    for (le_uint16 lookup = 0; lookup < lookupCount; lookup += 1) {
   1.127 +        le_uint16 lookupListIndex = SWAPW(featureTable->lookupListIndexArray[lookup]);
   1.128 +
   1.129 +        if (lookupSelectArray[lookupListIndex] == notSelected) { 
   1.130 +            lookupSelectArray[lookupListIndex] = featureTag;
   1.131 +            lookupOrderArray[store++]   = lookupListIndex;
   1.132 +        }
   1.133 +    }
   1.134 +
   1.135 +    return store - order;
   1.136 +}
   1.137 +
   1.138 +LookupProcessor::LookupProcessor(const char *baseAddress,
   1.139 +        Offset scriptListOffset, Offset featureListOffset, Offset lookupListOffset,
   1.140 +        LETag scriptTag, LETag languageTag, const LETag *featureOrder)
   1.141 +    : lookupListTable(NULL), featureListTable(NULL), lookupSelectArray(NULL),
   1.142 +      requiredFeatureTag(notSelected), lookupOrderArray(NULL), lookupOrderCount(0)
   1.143 +{
   1.144 +    const ScriptListTable *scriptListTable = NULL;
   1.145 +    const LangSysTable *langSysTable = NULL;
   1.146 +    le_uint16 featureCount = 0;
   1.147 +    le_uint16 lookupListCount = 0;
   1.148 +    le_uint16 requiredFeatureIndex;
   1.149 +
   1.150 +    if (scriptListOffset != 0) {
   1.151 +        scriptListTable = (const ScriptListTable *) (baseAddress + scriptListOffset);
   1.152 +        langSysTable = scriptListTable->findLanguage(scriptTag, languageTag);
   1.153 +
   1.154 +        if (langSysTable != 0) {
   1.155 +            featureCount = SWAPW(langSysTable->featureCount);
   1.156 +        }
   1.157 +    }
   1.158 +
   1.159 +    if (featureListOffset != 0) {
   1.160 +        featureListTable = (const FeatureListTable *) (baseAddress + featureListOffset);
   1.161 +    }
   1.162 +
   1.163 +    if (lookupListOffset != 0) {
   1.164 +        lookupListTable = (const LookupListTable *) (baseAddress + lookupListOffset);
   1.165 +        lookupListCount = SWAPW(lookupListTable->lookupCount);
   1.166 +    }
   1.167 +    
   1.168 +    if (langSysTable == NULL || featureListTable == NULL || lookupListTable == NULL ||
   1.169 +        featureCount == 0 || lookupListCount == 0) {
   1.170 +        return;
   1.171 +    }
   1.172 + 
   1.173 +    requiredFeatureIndex = SWAPW(langSysTable->reqFeatureIndex);
   1.174 +
   1.175 +    lookupSelectArray = LE_NEW_ARRAY(LETag, lookupListCount);
   1.176 +    if (!lookupSelectArray)
   1.177 +    	return;
   1.178 +
   1.179 +    for (int i = 0; i < lookupListCount; i += 1) {
   1.180 +        lookupSelectArray[i] = notSelected;
   1.181 +    }
   1.182 +
   1.183 +    le_int32 count, order = 0;
   1.184 +    const FeatureTable *featureTable = 0;
   1.185 +    LETag featureTag;
   1.186 +
   1.187 +    lookupOrderArray = LE_NEW_ARRAY(le_uint16, lookupListCount);
   1.188 +    if (!lookupOrderArray)
   1.189 +    	return;
   1.190 +
   1.191 +    if (requiredFeatureIndex != 0xFFFF) {
   1.192 +        featureTable = featureListTable->getFeatureTable(requiredFeatureIndex, &featureTag);
   1.193 +        order += selectLookups(featureTable, defaultFeature, order);
   1.194 +    }
   1.195 +
   1.196 +    if (featureOrder != NULL) {
   1.197 +        if (order > 1) {
   1.198 +            OpenTypeUtilities::sort(lookupOrderArray, order);
   1.199 +        }
   1.200 +
   1.201 +        for (le_int32 tag = 0; featureOrder[tag] != emptyTag; tag += 1) {
   1.202 +            featureTag = featureOrder[tag];
   1.203 +            count = 0;
   1.204 +
   1.205 +            for (le_uint16 feature = 0; feature < featureCount; feature += 1) {
   1.206 +                le_uint16 featureIndex = SWAPW(langSysTable->featureIndexArray[feature]);
   1.207 +
   1.208 +                featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
   1.209 +
   1.210 +                if (featureTag == featureOrder[tag]) {
   1.211 +                    count += selectLookups(featureTable, featureTag, order + count);
   1.212 +                }
   1.213 +            }
   1.214 +
   1.215 +            if (count > 1) {
   1.216 +                OpenTypeUtilities::sort(&lookupOrderArray[order], count);
   1.217 +            }
   1.218 +
   1.219 +            order += count;
   1.220 +        }
   1.221 +    } else {
   1.222 +        for (le_uint16 feature = 0; feature < featureCount; feature += 1) {
   1.223 +            le_uint16 featureIndex = SWAPW(langSysTable->featureIndexArray[feature]);
   1.224 + 
   1.225 +            // don't add the required feature to the list more than once...
   1.226 +            if (featureIndex == requiredFeatureIndex) {
   1.227 +                continue;
   1.228 +            }
   1.229 +
   1.230 +            featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
   1.231 +            count = selectLookups(featureTable, featureTag, order);
   1.232 +            order += count;
   1.233 +        }
   1.234 +
   1.235 +        if (order > 1) {
   1.236 +            OpenTypeUtilities::sort(lookupOrderArray, order);
   1.237 +        }
   1.238 +    }
   1.239 +
   1.240 +    lookupOrderCount = order;
   1.241 +}
   1.242 +
   1.243 +LookupProcessor::LookupProcessor()
   1.244 +{
   1.245 +    lookupOrderArray = 0;
   1.246 +    lookupSelectArray = 0;
   1.247 +}
   1.248 +
   1.249 +le_bool LookupProcessor::isBogus()
   1.250 +{
   1.251 +    return lookupSelectArray && lookupSelectArray? FALSE : TRUE;
   1.252 +}
   1.253 +
   1.254 +LookupProcessor::~LookupProcessor()
   1.255 +{
   1.256 +    LE_DELETE_ARRAY(lookupOrderArray);
   1.257 +    LE_DELETE_ARRAY(lookupSelectArray);
   1.258 +}
   1.259 +
   1.260 +U_NAMESPACE_END