fontservices/textshaperplugin/IcuSource/layout/LookupProcessor.cpp
changeset 0 1fb32624e06b
equal deleted inserted replaced
-1:000000000000 0:1fb32624e06b
       
     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 const LETag LookupProcessor::notSelected    = 0x00000000;
       
    23 const LETag LookupProcessor::defaultFeature = 0xFFFFFFFF;
       
    24 
       
    25 static const LETag emptyTag = 0x00000000;
       
    26 
       
    27 
       
    28 le_uint32 LookupProcessor::applyLookupTable(const LookupTable *lookupTable,
       
    29     GlyphIterator *glyphIterator, const LEFontInstance *fontInstance,
       
    30     LEErrorCode& success) const
       
    31 {
       
    32     if (LE_FAILURE(success)) {
       
    33         return 0;
       
    34     }
       
    35 
       
    36     le_uint16 lookupType = SWAPW(lookupTable->lookupType);
       
    37     le_uint16 subtableCount = SWAPW(lookupTable->subTableCount);
       
    38     le_int32 startPosition = glyphIterator->getCurrStreamPosition();
       
    39     le_uint32 delta;
       
    40 
       
    41     for (le_uint16 subtable = 0; subtable < subtableCount; subtable += 1) {
       
    42         const LookupSubtable *lookupSubtable = lookupTable->getLookupSubtable(subtable);
       
    43 
       
    44         delta = applySubtable(lookupSubtable, lookupType, glyphIterator,
       
    45             fontInstance, success);
       
    46 
       
    47         if (delta > 0 || LE_FAILURE(success)) {
       
    48             return 1;
       
    49         }
       
    50 
       
    51         glyphIterator->setCurrStreamPosition(startPosition);
       
    52     }
       
    53 
       
    54     return 1;
       
    55 }
       
    56 
       
    57 le_int32 LookupProcessor::process(LEGlyphStorage &glyphStorage, GlyphPositionAdjustments *glyphPositionAdjustments,
       
    58                               le_bool rightToLeft, const GlyphDefinitionTableHeader *glyphDefinitionTableHeader,
       
    59                               const LEFontInstance *fontInstance, LEErrorCode& success) const
       
    60 {
       
    61     if (LE_FAILURE(success)) {
       
    62         return 0;
       
    63     }
       
    64 
       
    65     le_int32 glyphCount = glyphStorage.getGlyphCount();
       
    66 
       
    67     if (lookupSelectArray == NULL) {
       
    68         return glyphCount;
       
    69     }
       
    70 
       
    71     GlyphIterator glyphIterator(glyphStorage, glyphPositionAdjustments,
       
    72                                 rightToLeft, 0, 0, glyphDefinitionTableHeader);
       
    73     le_int32 newGlyphCount = glyphCount;
       
    74 
       
    75     for (le_uint16 order = 0; order < lookupOrderCount; order += 1) {
       
    76         le_uint16 lookup = lookupOrderArray[order];
       
    77         LETag selectTag = lookupSelectArray[lookup];
       
    78 
       
    79         if (selectTag != notSelected) {
       
    80             const LookupTable *lookupTable = lookupListTable->getLookupTable(lookup);
       
    81             le_uint16 lookupFlags = SWAPW(lookupTable->lookupFlags);
       
    82             
       
    83             glyphIterator.reset(lookupFlags, selectTag);
       
    84 
       
    85             while (glyphIterator.findFeatureTag()) {
       
    86                 le_uint32 delta = 1;
       
    87 
       
    88                 while (glyphIterator.next(delta)) {
       
    89                     delta = applyLookupTable(lookupTable, &glyphIterator, fontInstance, success);
       
    90                     if (LE_FAILURE(success)) {
       
    91                         return 0;
       
    92                     }
       
    93                 }
       
    94             }
       
    95 
       
    96             newGlyphCount = glyphIterator.applyInsertions();
       
    97         }
       
    98     }
       
    99 
       
   100     return newGlyphCount;
       
   101 }
       
   102 
       
   103 le_uint32 LookupProcessor::applySingleLookup(le_uint16 lookupTableIndex, GlyphIterator *glyphIterator,
       
   104     const LEFontInstance *fontInstance, LEErrorCode& success) const
       
   105 {
       
   106     if (LE_FAILURE(success)) {
       
   107         return 0;
       
   108     }
       
   109 
       
   110     const LookupTable *lookupTable = lookupListTable->getLookupTable(lookupTableIndex);
       
   111     le_uint16 lookupFlags = SWAPW(lookupTable->lookupFlags);
       
   112     GlyphIterator tempIterator(*glyphIterator, lookupFlags);
       
   113     le_uint32 delta = applyLookupTable(lookupTable, &tempIterator, fontInstance, success);
       
   114 
       
   115     return delta;
       
   116 }
       
   117 
       
   118 le_int32 LookupProcessor::selectLookups(const FeatureTable *featureTable, LETag featureTag, le_int32 order)
       
   119 {
       
   120     le_uint16 lookupCount = featureTable? SWAPW(featureTable->lookupCount) : 0;
       
   121     le_int32  store = order;
       
   122 
       
   123     for (le_uint16 lookup = 0; lookup < lookupCount; lookup += 1) {
       
   124         le_uint16 lookupListIndex = SWAPW(featureTable->lookupListIndexArray[lookup]);
       
   125 
       
   126         if (lookupSelectArray[lookupListIndex] == notSelected) { 
       
   127             lookupSelectArray[lookupListIndex] = featureTag;
       
   128             lookupOrderArray[store++]   = lookupListIndex;
       
   129         }
       
   130     }
       
   131 
       
   132     return store - order;
       
   133 }
       
   134 
       
   135 LookupProcessor::LookupProcessor(const char *baseAddress,
       
   136         Offset scriptListOffset, Offset featureListOffset, Offset lookupListOffset,
       
   137         LETag scriptTag, LETag languageTag, const LETag *featureOrder)
       
   138     : lookupListTable(NULL), featureListTable(NULL), lookupSelectArray(NULL),
       
   139       requiredFeatureTag(notSelected), 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(LETag, lookupListCount);
       
   173     if (!lookupSelectArray)
       
   174     	return;
       
   175 
       
   176     for (int i = 0; i < lookupListCount; i += 1) {
       
   177         lookupSelectArray[i] = notSelected;
       
   178     }
       
   179 
       
   180     le_int32 count, order = 0;
       
   181     const FeatureTable *featureTable = 0;
       
   182     LETag featureTag;
       
   183 
       
   184     lookupOrderArray = LE_NEW_ARRAY(le_uint16, lookupListCount);
       
   185     if (!lookupOrderArray)
       
   186     	return;
       
   187 
       
   188     if (requiredFeatureIndex != 0xFFFF) {
       
   189         featureTable = featureListTable->getFeatureTable(requiredFeatureIndex, &featureTag);
       
   190         order += selectLookups(featureTable, defaultFeature, order);
       
   191     }
       
   192 
       
   193     if (featureOrder != NULL) {
       
   194         if (order > 1) {
       
   195             OpenTypeUtilities::sort(lookupOrderArray, order);
       
   196         }
       
   197 
       
   198         for (le_int32 tag = 0; featureOrder[tag] != emptyTag; tag += 1) {
       
   199             featureTag = featureOrder[tag];
       
   200             count = 0;
       
   201 
       
   202             for (le_uint16 feature = 0; feature < featureCount; feature += 1) {
       
   203                 le_uint16 featureIndex = SWAPW(langSysTable->featureIndexArray[feature]);
       
   204 
       
   205                 featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
       
   206 
       
   207                 if (featureTag == featureOrder[tag]) {
       
   208                     count += selectLookups(featureTable, featureTag, order + count);
       
   209                 }
       
   210             }
       
   211 
       
   212             if (count > 1) {
       
   213                 OpenTypeUtilities::sort(&lookupOrderArray[order], count);
       
   214             }
       
   215 
       
   216             order += count;
       
   217         }
       
   218     } else {
       
   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             if (featureIndex == requiredFeatureIndex) {
       
   224                 continue;
       
   225             }
       
   226 
       
   227             featureTable = featureListTable->getFeatureTable(featureIndex, &featureTag);
       
   228             count = selectLookups(featureTable, featureTag, order);
       
   229             order += count;
       
   230         }
       
   231 
       
   232         if (order > 1) {
       
   233             OpenTypeUtilities::sort(lookupOrderArray, order);
       
   234         }
       
   235     }
       
   236 
       
   237     lookupOrderCount = order;
       
   238 }
       
   239 
       
   240 LookupProcessor::LookupProcessor()
       
   241 {
       
   242     lookupOrderArray = 0;
       
   243     lookupSelectArray = 0;
       
   244 }
       
   245 
       
   246 le_bool LookupProcessor::isBogus()
       
   247 {
       
   248     return lookupSelectArray && lookupSelectArray? FALSE : TRUE;
       
   249 }
       
   250 
       
   251 LookupProcessor::~LookupProcessor()
       
   252 {
       
   253     LE_DELETE_ARRAY(lookupOrderArray);
       
   254     LE_DELETE_ARRAY(lookupSelectArray);
       
   255 }
       
   256 
       
   257 U_NAMESPACE_END