ContextualSubstSubtables.cpp 27.9 KB
Newer Older
D
duke 已提交
1 2 3 4 5
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
6
 * published by the Free Software Foundation.  Oracle designates this
D
duke 已提交
7
 * particular file as subject to the "Classpath" exception as provided
8
 * by Oracle in the LICENSE file that accompanied this code.
D
duke 已提交
9 10 11 12 13 14 15 16 17 18 19
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
20 21 22
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *
 */

/*
 * (C) Copyright IBM Corp. 1998-2005 - All Rights Reserved
 *
 */

#include "LETypes.h"
#include "LEFontInstance.h"
#include "OpenTypeTables.h"
#include "GlyphSubstitutionTables.h"
#include "ContextualSubstSubtables.h"
#include "GlyphIterator.h"
#include "LookupProcessor.h"
#include "CoverageTables.h"
#include "LESwaps.h"

41 42
U_NAMESPACE_BEGIN

D
duke 已提交
43 44 45 46 47 48 49 50
/*
    NOTE: This could be optimized somewhat by keeping track
    of the previous sequenceIndex in the loop and doing next()
    or prev() of the delta between that and the current
    sequenceIndex instead of always resetting to the front.
*/
void ContextualSubstitutionBase::applySubstitutionLookups(
        const LookupProcessor *lookupProcessor,
P
prr 已提交
51
        const LEReferenceToArrayOf<SubstitutionLookupRecord>& substLookupRecordArray,
D
duke 已提交
52 53 54
        le_uint16 substCount,
        GlyphIterator *glyphIterator,
        const LEFontInstance *fontInstance,
S
srl 已提交
55 56
        le_int32 position,
        LEErrorCode& success)
D
duke 已提交
57
{
S
srl 已提交
58 59 60 61
    if (LE_FAILURE(success)) {
        return;
    }

D
duke 已提交
62
    GlyphIterator tempIterator(*glyphIterator);
P
prr 已提交
63
    const SubstitutionLookupRecord *substLookupRecordArrayPtr = substLookupRecordArray.getAlias(); // OK to dereference, range checked against substCount below.
D
duke 已提交
64

S
srl 已提交
65
    for (le_int16 subst = 0; subst < substCount && LE_SUCCESS(success); subst += 1) {
P
prr 已提交
66 67
        le_uint16 sequenceIndex = SWAPW(substLookupRecordArrayPtr[subst].sequenceIndex);
        le_uint16 lookupListIndex = SWAPW(substLookupRecordArrayPtr[subst].lookupListIndex);
D
duke 已提交
68 69 70 71

        tempIterator.setCurrStreamPosition(position);
        tempIterator.next(sequenceIndex);

S
srl 已提交
72
        lookupProcessor->applySingleLookup(lookupListIndex, &tempIterator, fontInstance, success);
D
duke 已提交
73 74 75
    }
}

P
prr 已提交
76
le_bool ContextualSubstitutionBase::matchGlyphIDs(const LEReferenceToArrayOf<TTGlyphID>& glyphArray, le_uint16 glyphCount,
D
duke 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
                                               GlyphIterator *glyphIterator, le_bool backtrack)
{
    le_int32 direction = 1;
    le_int32 match = 0;

    if (backtrack) {
        match = glyphCount -1;
        direction = -1;
    }

    while (glyphCount > 0) {
        if (! glyphIterator->next()) {
            return FALSE;
        }

        TTGlyphID glyph = (TTGlyphID) glyphIterator->getCurrGlyphID();

        if (glyph != SWAPW(glyphArray[match])) {
            return FALSE;
        }

        glyphCount -= 1;
        match += direction;
    }

    return TRUE;
}

P
prr 已提交
105 106 107 108 109 110 111
le_bool ContextualSubstitutionBase::matchGlyphClasses(
    const LEReferenceToArrayOf<le_uint16> &classArray,
    le_uint16 glyphCount,
    GlyphIterator *glyphIterator,
    const LEReferenceTo<ClassDefinitionTable> &classDefinitionTable,
    LEErrorCode &success,
    le_bool backtrack)
D
duke 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
    le_int32 direction = 1;
    le_int32 match = 0;

    if (backtrack) {
        match = glyphCount - 1;
        direction = -1;
    }

    while (glyphCount > 0) {
        if (! glyphIterator->next()) {
            return FALSE;
        }

        LEGlyphID glyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
127
        le_int32 glyphClass = classDefinitionTable->getGlyphClass(classDefinitionTable, glyph, success);
D
duke 已提交
128 129 130 131 132 133 134
        le_int32 matchClass = SWAPW(classArray[match]);

        if (glyphClass != matchClass) {
            // Some fonts, e.g. Traditional Arabic, have classes
            // in the class array which aren't in the class definition
            // table. If we're looking for such a class, pretend that
            // we found it.
P
prr 已提交
135
            if (classDefinitionTable->hasGlyphClass(classDefinitionTable, matchClass, success)) {
D
duke 已提交
136 137 138 139 140 141 142 143 144 145 146
                return FALSE;
            }
        }

        glyphCount -= 1;
        match += direction;
    }

    return TRUE;
}

P
prr 已提交
147 148
le_bool ContextualSubstitutionBase::matchGlyphCoverages(const LEReferenceToArrayOf<Offset> &coverageTableOffsetArray, le_uint16 glyphCount,
GlyphIterator *glyphIterator, const LETableReference &offsetBase, LEErrorCode &success, le_bool backtrack)
D
duke 已提交
149 150 151 152 153 154 155 156 157 158 159
{
    le_int32 direction = 1;
    le_int32 glyph = 0;

    if (backtrack) {
        glyph = glyphCount - 1;
        direction = -1;
    }

    while (glyphCount > 0) {
        Offset coverageTableOffset = SWAPW(coverageTableOffsetArray[glyph]);
P
prr 已提交
160
        LEReferenceTo<CoverageTable> coverageTable(offsetBase, success, coverageTableOffset);
D
duke 已提交
161

P
prr 已提交
162
        if (LE_FAILURE(success) || ! glyphIterator->next()) {
D
duke 已提交
163 164 165
            return FALSE;
        }

P
prr 已提交
166 167 168
        if (coverageTable->getGlyphCoverage(coverageTable,
                                            (LEGlyphID) glyphIterator->getCurrGlyphID(),
                                            success) < 0) {
D
duke 已提交
169 170 171 172 173 174 175 176 177 178
            return FALSE;
        }

        glyphCount -= 1;
        glyph += direction;
    }

    return TRUE;
}

P
prr 已提交
179
le_uint32 ContextualSubstitutionSubtable::process(const LETableReference &base, const LookupProcessor *lookupProcessor,
S
srl 已提交
180 181 182
                                                  GlyphIterator *glyphIterator,
                                                  const LEFontInstance *fontInstance,
                                                  LEErrorCode& success) const
D
duke 已提交
183
{
S
srl 已提交
184 185 186 187
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
188 189 190 191 192 193 194
    switch(SWAPW(subtableFormat))
    {
    case 0:
        return 0;

    case 1:
    {
P
prr 已提交
195 196 197 198 199
      LEReferenceTo<ContextualSubstitutionFormat1Subtable> subtable(base, success, (const ContextualSubstitutionFormat1Subtable *) this);
      if( LE_FAILURE(success) ) {
        return 0;
      }
      return subtable->process(subtable, lookupProcessor, glyphIterator, fontInstance, success);
D
duke 已提交
200 201 202 203
    }

    case 2:
    {
P
prr 已提交
204 205 206 207 208
      LEReferenceTo<ContextualSubstitutionFormat2Subtable> subtable(base, success, (const ContextualSubstitutionFormat2Subtable *) this);
      if( LE_FAILURE(success) ) {
        return 0;
      }
      return subtable->process(subtable, lookupProcessor, glyphIterator, fontInstance, success);
D
duke 已提交
209 210 211 212
    }

    case 3:
    {
P
prr 已提交
213 214 215 216 217
      LEReferenceTo<ContextualSubstitutionFormat3Subtable> subtable(base, success, (const ContextualSubstitutionFormat3Subtable *) this);
      if( LE_FAILURE(success) ) {
        return 0;
      }
      return subtable->process(subtable, lookupProcessor, glyphIterator, fontInstance, success);
D
duke 已提交
218 219 220 221 222 223 224
    }

    default:
        return 0;
    }
}

P
prr 已提交
225
le_uint32 ContextualSubstitutionFormat1Subtable::process(const LETableReference &base, const LookupProcessor *lookupProcessor,
S
srl 已提交
226 227 228
                                                         GlyphIterator *glyphIterator,
                                                         const LEFontInstance *fontInstance,
                                                         LEErrorCode& success) const
D
duke 已提交
229
{
S
srl 已提交
230 231 232 233
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
234
    LEGlyphID glyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
235
    le_int32 coverageIndex = getGlyphCoverage(lookupProcessor->getReference(), glyph, success);
236 237 238
    if (LE_FAILURE(success)) {
        return 0;
    }
D
duke 已提交
239 240 241 242 243 244

    if (coverageIndex >= 0) {
        le_uint16 srSetCount = SWAPW(subRuleSetCount);

        if (coverageIndex < srSetCount) {
            Offset subRuleSetTableOffset = SWAPW(subRuleSetTableOffsetArray[coverageIndex]);
P
prr 已提交
245 246
            LEReferenceTo<SubRuleSetTable>
                 subRuleSetTable(base, success, (const SubRuleSetTable *) ((char *) this + subRuleSetTableOffset));
D
duke 已提交
247 248 249 250 251 252
            le_uint16 subRuleCount = SWAPW(subRuleSetTable->subRuleCount);
            le_int32 position = glyphIterator->getCurrStreamPosition();

            for (le_uint16 subRule = 0; subRule < subRuleCount; subRule += 1) {
                Offset subRuleTableOffset =
                    SWAPW(subRuleSetTable->subRuleTableOffsetArray[subRule]);
P
prr 已提交
253 254
                LEReferenceTo<SubRuleTable>
                     subRuleTable(subRuleSetTable, success, subRuleTableOffset);
D
duke 已提交
255 256
                le_uint16 matchCount = SWAPW(subRuleTable->glyphCount) - 1;
                le_uint16 substCount = SWAPW(subRuleTable->substCount);
P
prr 已提交
257 258 259 260
                LEReferenceToArrayOf<TTGlyphID> inputGlyphArray(base, success, subRuleTable->inputGlyphArray, matchCount+2);
                if (matchGlyphIDs(inputGlyphArray, matchCount, glyphIterator)) {
                  LEReferenceToArrayOf<SubstitutionLookupRecord>
                    substLookupRecordArray(base, success, (const SubstitutionLookupRecord *) &subRuleTable->inputGlyphArray[matchCount], substCount);
D
duke 已提交
261

S
srl 已提交
262
                    applySubstitutionLookups(lookupProcessor, substLookupRecordArray, substCount, glyphIterator, fontInstance, position, success);
D
duke 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276

                    return matchCount + 1;
                }

                glyphIterator->setCurrStreamPosition(position);
            }
        }

        // XXX If we get here, the table is mal-formed...
    }

    return 0;
}

P
prr 已提交
277 278 279 280 281
le_uint32 ContextualSubstitutionFormat2Subtable::process(const LETableReference &base,
         const LookupProcessor *lookupProcessor,
         GlyphIterator *glyphIterator,
         const LEFontInstance *fontInstance,
         LEErrorCode& success) const
D
duke 已提交
282
{
S
srl 已提交
283 284 285 286
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
287
    LEGlyphID glyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
288
    le_int32 coverageIndex = getGlyphCoverage(lookupProcessor->getReference(), glyph, success);
289 290 291
    if (LE_FAILURE(success)) {
        return 0;
    }
D
duke 已提交
292 293

    if (coverageIndex >= 0) {
P
prr 已提交
294 295
        LEReferenceTo<ClassDefinitionTable> classDefinitionTable(base, success,
                                                                 (const ClassDefinitionTable *) ((char *) this + SWAPW(classDefTableOffset)));
D
duke 已提交
296
        le_uint16 scSetCount = SWAPW(subClassSetCount);
P
prr 已提交
297 298 299
        le_int32 setClass = classDefinitionTable->getGlyphClass(classDefinitionTable,
                                                                glyphIterator->getCurrGlyphID(),
                                                                success);
D
duke 已提交
300 301 302

        if (setClass < scSetCount && subClassSetTableOffsetArray[setClass] != 0) {
            Offset subClassSetTableOffset = SWAPW(subClassSetTableOffsetArray[setClass]);
P
prr 已提交
303 304
            LEReferenceTo<SubClassSetTable>
                 subClassSetTable(base, success, (const SubClassSetTable *) ((char *) this + subClassSetTableOffset));
D
duke 已提交
305 306 307 308 309 310
            le_uint16 subClassRuleCount = SWAPW(subClassSetTable->subClassRuleCount);
            le_int32 position = glyphIterator->getCurrStreamPosition();

            for (le_uint16 scRule = 0; scRule < subClassRuleCount; scRule += 1) {
                Offset subClassRuleTableOffset =
                    SWAPW(subClassSetTable->subClassRuleTableOffsetArray[scRule]);
P
prr 已提交
311 312
                LEReferenceTo<SubClassRuleTable>
                     subClassRuleTable(subClassSetTable, success, subClassRuleTableOffset);
D
duke 已提交
313 314 315
                le_uint16 matchCount = SWAPW(subClassRuleTable->glyphCount) - 1;
                le_uint16 substCount = SWAPW(subClassRuleTable->substCount);

P
prr 已提交
316 317 318 319 320
                LEReferenceToArrayOf<le_uint16> classArray(base, success, subClassRuleTable->classArray, matchCount+1);

                if (matchGlyphClasses(classArray, matchCount, glyphIterator, classDefinitionTable, success)) {
                    LEReferenceToArrayOf<SubstitutionLookupRecord>
                      substLookupRecordArray(base, success, (const SubstitutionLookupRecord *) &subClassRuleTable->classArray[matchCount], substCount);
D
duke 已提交
321

S
srl 已提交
322
                    applySubstitutionLookups(lookupProcessor, substLookupRecordArray, substCount, glyphIterator, fontInstance, position, success);
D
duke 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336

                    return matchCount + 1;
                }

                glyphIterator->setCurrStreamPosition(position);
            }
        }

        // XXX If we get here, the table is mal-formed...
    }

    return 0;
}

P
prr 已提交
337 338
le_uint32 ContextualSubstitutionFormat3Subtable::process(const LETableReference &base,
                                                         const LookupProcessor *lookupProcessor,
S
srl 已提交
339 340 341
                                                         GlyphIterator *glyphIterator,
                                                         const LEFontInstance *fontInstance,
                                                         LEErrorCode& success)const
D
duke 已提交
342
{
S
srl 已提交
343 344 345 346
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
347 348 349 350 351 352 353 354 355 356
    le_uint16 gCount = SWAPW(glyphCount);
    le_uint16 subCount = SWAPW(substCount);
    le_int32 position = glyphIterator->getCurrStreamPosition();

    // Back up the glyph iterator so that we
    // can call next() before the check, which
    // will leave it pointing at the last glyph
    // that matched when we're done.
    glyphIterator->prev();

P
prr 已提交
357 358 359 360 361 362 363
    LEReferenceToArrayOf<Offset> covTableOffsetArray(base, success, coverageTableOffsetArray, gCount);

    if( LE_FAILURE(success) ) { return 0; }

    if (ContextualSubstitutionBase::matchGlyphCoverages(covTableOffsetArray, gCount, glyphIterator, base, success)) {
        LEReferenceToArrayOf<SubstitutionLookupRecord>
          substLookupRecordArray(base, success, (const SubstitutionLookupRecord *) &coverageTableOffsetArray[gCount], subCount);
D
duke 已提交
364

S
srl 已提交
365
        ContextualSubstitutionBase::applySubstitutionLookups(lookupProcessor, substLookupRecordArray, subCount, glyphIterator, fontInstance, position, success);
D
duke 已提交
366 367 368 369 370 371 372 373 374

        return gCount + 1;
    }

    glyphIterator->setCurrStreamPosition(position);

    return 0;
}

P
prr 已提交
375 376
le_uint32 ChainingContextualSubstitutionSubtable::process(const LEReferenceTo<ChainingContextualSubstitutionSubtable> &base,
                                                          const LookupProcessor *lookupProcessor,
S
srl 已提交
377 378 379
                                                          GlyphIterator *glyphIterator,
                                                          const LEFontInstance *fontInstance,
                                                          LEErrorCode& success) const
D
duke 已提交
380
{
S
srl 已提交
381 382 383 384
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
385 386 387 388 389 390 391
    switch(SWAPW(subtableFormat))
    {
    case 0:
        return 0;

    case 1:
    {
P
prr 已提交
392 393 394
      LEReferenceTo<ChainingContextualSubstitutionFormat1Subtable> subtable(base, success,  (ChainingContextualSubstitutionFormat1Subtable *) this);
      if(LE_FAILURE(success)) return 0;
      return subtable->process(subtable, lookupProcessor, glyphIterator, fontInstance, success);
D
duke 已提交
395 396 397 398
    }

    case 2:
    {
P
prr 已提交
399 400 401
      LEReferenceTo<ChainingContextualSubstitutionFormat2Subtable> subtable(base, success, (const ChainingContextualSubstitutionFormat2Subtable *) this);
      if( LE_FAILURE(success) ) { return 0; }
      return subtable->process(subtable, lookupProcessor, glyphIterator, fontInstance, success);
D
duke 已提交
402 403 404 405
    }

    case 3:
    {
P
prr 已提交
406 407 408
      LEReferenceTo<ChainingContextualSubstitutionFormat3Subtable> subtable(base, success, (const ChainingContextualSubstitutionFormat3Subtable *) this);
      if( LE_FAILURE(success) ) { return 0; }
      return subtable->process(subtable, lookupProcessor, glyphIterator, fontInstance, success);
D
duke 已提交
409 410 411 412 413 414 415 416 417
    }

    default:
        return 0;
    }
}

// NOTE: This could be a #define, but that seems to confuse
// the Visual Studio .NET 2003 compiler on the calls to the
418
// GlyphIterator constructor. It somehow can't decide if
D
duke 已提交
419 420 421
// emptyFeatureList matches an le_uint32 or an le_uint16...
static const FeatureMask emptyFeatureList = 0x00000000UL;

P
prr 已提交
422
le_uint32 ChainingContextualSubstitutionFormat1Subtable::process(const LETableReference &base, const LookupProcessor *lookupProcessor,
S
srl 已提交
423 424 425
                                                                 GlyphIterator *glyphIterator,
                                                                 const LEFontInstance *fontInstance,
                                                                 LEErrorCode& success) const
D
duke 已提交
426
{
S
srl 已提交
427 428 429 430
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
431
    LEGlyphID glyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
432
    le_int32 coverageIndex = getGlyphCoverage(lookupProcessor->getReference(), glyph, success);
433 434 435
    if (LE_FAILURE(success)) {
        return 0;
    }
D
duke 已提交
436 437 438 439 440 441

    if (coverageIndex >= 0) {
        le_uint16 srSetCount = SWAPW(chainSubRuleSetCount);

        if (coverageIndex < srSetCount) {
            Offset chainSubRuleSetTableOffset = SWAPW(chainSubRuleSetTableOffsetArray[coverageIndex]);
P
prr 已提交
442 443
            LEReferenceTo<ChainSubRuleSetTable>
                 chainSubRuleSetTable(base, success, (const ChainSubRuleSetTable *) ((char *) this + chainSubRuleSetTableOffset));
D
duke 已提交
444 445 446 447 448 449 450
            le_uint16 chainSubRuleCount = SWAPW(chainSubRuleSetTable->chainSubRuleCount);
            le_int32 position = glyphIterator->getCurrStreamPosition();
            GlyphIterator tempIterator(*glyphIterator, emptyFeatureList);

            for (le_uint16 subRule = 0; subRule < chainSubRuleCount; subRule += 1) {
                Offset chainSubRuleTableOffset =
                    SWAPW(chainSubRuleSetTable->chainSubRuleTableOffsetArray[subRule]);
P
prr 已提交
451 452 453
                LEReferenceTo<ChainSubRuleTable>
                     chainSubRuleTable = LEReferenceTo<ChainSubRuleTable>(chainSubRuleSetTable, success, chainSubRuleTableOffset);
                if( LE_FAILURE(success) ) { return 0; }
D
duke 已提交
454
                le_uint16 backtrackGlyphCount = SWAPW(chainSubRuleTable->backtrackGlyphCount);
P
prr 已提交
455 456
                LEReferenceToArrayOf<TTGlyphID> backtrackGlyphArray(base, success, chainSubRuleTable->backtrackGlyphArray, backtrackGlyphCount);
                if( LE_FAILURE(success) ) { return 0; }
D
duke 已提交
457
                le_uint16 inputGlyphCount = (le_uint16) SWAPW(chainSubRuleTable->backtrackGlyphArray[backtrackGlyphCount]) - 1;
P
prr 已提交
458 459 460
                LEReferenceToArrayOf<TTGlyphID>   inputGlyphArray(base, success, &chainSubRuleTable->backtrackGlyphArray[backtrackGlyphCount + 1], inputGlyphCount+2);

                if( LE_FAILURE(success) ) { return 0; }
D
duke 已提交
461
                le_uint16 lookaheadGlyphCount = (le_uint16) SWAPW(inputGlyphArray[inputGlyphCount]);
P
prr 已提交
462 463
                LEReferenceToArrayOf<TTGlyphID>   lookaheadGlyphArray(base, success, inputGlyphArray.getAlias(inputGlyphCount + 1,success), lookaheadGlyphCount+2);
                if( LE_FAILURE(success) ) { return 0; }
D
duke 已提交
464 465 466 467 468 469 470 471 472
                le_uint16 substCount = (le_uint16) SWAPW(lookaheadGlyphArray[lookaheadGlyphCount]);

                tempIterator.setCurrStreamPosition(position);

                if (! tempIterator.prev(backtrackGlyphCount)) {
                    continue;
                }

                tempIterator.prev();
P
prr 已提交
473 474

                if (! matchGlyphIDs(backtrackGlyphArray, backtrackGlyphCount, &tempIterator, TRUE)) {
D
duke 已提交
475 476 477 478 479 480 481 482 483 484
                    continue;
                }

                tempIterator.setCurrStreamPosition(position);
                tempIterator.next(inputGlyphCount);
                if (!matchGlyphIDs(lookaheadGlyphArray, lookaheadGlyphCount, &tempIterator)) {
                    continue;
                }

                if (matchGlyphIDs(inputGlyphArray, inputGlyphCount, glyphIterator)) {
P
prr 已提交
485 486
                    LEReferenceToArrayOf<SubstitutionLookupRecord>
                      substLookupRecordArray(base, success, (const SubstitutionLookupRecord *) lookaheadGlyphArray.getAlias(lookaheadGlyphCount + 1,success), substCount);
D
duke 已提交
487

S
srl 已提交
488
                    applySubstitutionLookups(lookupProcessor, substLookupRecordArray, substCount, glyphIterator, fontInstance, position, success);
D
duke 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502

                    return inputGlyphCount + 1;
                }

                glyphIterator->setCurrStreamPosition(position);
            }
        }

        // XXX If we get here, the table is mal-formed...
    }

    return 0;
}

P
prr 已提交
503
le_uint32 ChainingContextualSubstitutionFormat2Subtable::process(const LETableReference &base, const LookupProcessor *lookupProcessor,
S
srl 已提交
504 505 506
                                                                 GlyphIterator *glyphIterator,
                                                                 const LEFontInstance *fontInstance,
                                                                 LEErrorCode& success) const
D
duke 已提交
507
{
S
srl 已提交
508 509 510 511
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
512
    LEGlyphID glyph = glyphIterator->getCurrGlyphID();
P
prr 已提交
513
    le_int32 coverageIndex = getGlyphCoverage(lookupProcessor->getReference(), glyph, success);
514 515 516
    if (LE_FAILURE(success)) {
        return 0;
    }
D
duke 已提交
517 518

    if (coverageIndex >= 0) {
P
prr 已提交
519 520 521 522 523 524
        LEReferenceTo<ClassDefinitionTable>
             backtrackClassDefinitionTable(base, success, (const ClassDefinitionTable *) ((char *) this + SWAPW(backtrackClassDefTableOffset)));
        LEReferenceTo<ClassDefinitionTable>
             inputClassDefinitionTable(base, success, (const ClassDefinitionTable *) ((char *) this + SWAPW(inputClassDefTableOffset)));
        LEReferenceTo<ClassDefinitionTable>
             lookaheadClassDefinitionTable(base, success, (const ClassDefinitionTable *) ((char *) this + SWAPW(lookaheadClassDefTableOffset)));
D
duke 已提交
525
        le_uint16 scSetCount = SWAPW(chainSubClassSetCount);
P
prr 已提交
526 527 528
        le_int32 setClass = inputClassDefinitionTable->getGlyphClass(inputClassDefinitionTable,
                                                                     glyphIterator->getCurrGlyphID(),
                                                                     success);
D
duke 已提交
529 530 531

        if (setClass < scSetCount && chainSubClassSetTableOffsetArray[setClass] != 0) {
            Offset chainSubClassSetTableOffset = SWAPW(chainSubClassSetTableOffsetArray[setClass]);
P
prr 已提交
532 533
            LEReferenceTo<ChainSubClassSetTable>
                 chainSubClassSetTable(base, success, (const ChainSubClassSetTable *) ((char *) this + chainSubClassSetTableOffset));
D
duke 已提交
534 535 536 537 538 539 540
            le_uint16 chainSubClassRuleCount = SWAPW(chainSubClassSetTable->chainSubClassRuleCount);
            le_int32 position = glyphIterator->getCurrStreamPosition();
            GlyphIterator tempIterator(*glyphIterator, emptyFeatureList);

            for (le_uint16 scRule = 0; scRule < chainSubClassRuleCount; scRule += 1) {
                Offset chainSubClassRuleTableOffset =
                    SWAPW(chainSubClassSetTable->chainSubClassRuleTableOffsetArray[scRule]);
P
prr 已提交
541 542
                LEReferenceTo<ChainSubClassRuleTable>
                     chainSubClassRuleTable(chainSubClassSetTable, success, chainSubClassRuleTableOffset);
D
duke 已提交
543 544
                le_uint16 backtrackGlyphCount = SWAPW(chainSubClassRuleTable->backtrackGlyphCount);
                le_uint16 inputGlyphCount = SWAPW(chainSubClassRuleTable->backtrackClassArray[backtrackGlyphCount]) - 1;
P
prr 已提交
545 546 547 548 549
                LEReferenceToArrayOf<le_uint16>   inputClassArray(base, success, &chainSubClassRuleTable->backtrackClassArray[backtrackGlyphCount + 1],inputGlyphCount+2); // +2 for the lookaheadGlyphCount count
                le_uint16 lookaheadGlyphCount = SWAPW(inputClassArray.getObject(inputGlyphCount, success));
                LEReferenceToArrayOf<le_uint16>   lookaheadClassArray(base, success, inputClassArray.getAlias(inputGlyphCount + 1,success), lookaheadGlyphCount+2); // +2 for the substCount

                if( LE_FAILURE(success) ) { return 0; }
D
duke 已提交
550 551 552 553 554 555 556 557 558 559
                le_uint16 substCount = SWAPW(lookaheadClassArray[lookaheadGlyphCount]);


                tempIterator.setCurrStreamPosition(position);

                if (! tempIterator.prev(backtrackGlyphCount)) {
                    continue;
                }

                tempIterator.prev();
P
prr 已提交
560 561 562 563
                LEReferenceToArrayOf<le_uint16>   backtrackClassArray(base, success, chainSubClassRuleTable->backtrackClassArray, backtrackGlyphCount);
                if( LE_FAILURE(success) ) { return 0; }
                if (! matchGlyphClasses(backtrackClassArray, backtrackGlyphCount,
                                        &tempIterator, backtrackClassDefinitionTable, success, TRUE)) {
D
duke 已提交
564 565 566 567 568
                    continue;
                }

                tempIterator.setCurrStreamPosition(position);
                tempIterator.next(inputGlyphCount);
P
prr 已提交
569
                if (! matchGlyphClasses(lookaheadClassArray, lookaheadGlyphCount, &tempIterator, lookaheadClassDefinitionTable, success)) {
D
duke 已提交
570 571 572
                    continue;
                }

P
prr 已提交
573 574 575
                if (matchGlyphClasses(inputClassArray, inputGlyphCount, glyphIterator, inputClassDefinitionTable, success)) {
                    LEReferenceToArrayOf<SubstitutionLookupRecord>
                      substLookupRecordArray(base, success, (const SubstitutionLookupRecord *) lookaheadClassArray.getAlias(lookaheadGlyphCount + 1, success), substCount);
D
duke 已提交
576

S
srl 已提交
577
                    applySubstitutionLookups(lookupProcessor, substLookupRecordArray, substCount, glyphIterator, fontInstance, position, success);
D
duke 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591

                    return inputGlyphCount + 1;
                }

                glyphIterator->setCurrStreamPosition(position);
            }
        }

        // XXX If we get here, the table is mal-formed...
    }

    return 0;
}

P
prr 已提交
592
le_uint32 ChainingContextualSubstitutionFormat3Subtable::process(const LETableReference &base, const LookupProcessor *lookupProcessor,
S
srl 已提交
593 594 595
                                                                 GlyphIterator *glyphIterator,
                                                                 const LEFontInstance *fontInstance,
                                                                 LEErrorCode & success) const
D
duke 已提交
596
{
S
srl 已提交
597 598 599 600
    if (LE_FAILURE(success)) {
        return 0;
    }

D
duke 已提交
601 602
    le_uint16 backtrkGlyphCount = SWAPW(backtrackGlyphCount);
    le_uint16 inputGlyphCount = (le_uint16) SWAPW(backtrackCoverageTableOffsetArray[backtrkGlyphCount]);
P
prr 已提交
603
    LEReferenceToArrayOf<Offset>   inputCoverageTableOffsetArray(base, success, &backtrackCoverageTableOffsetArray[backtrkGlyphCount + 1], inputGlyphCount+2); // offset
D
duke 已提交
604
    const le_uint16 lookaheadGlyphCount = (le_uint16) SWAPW(inputCoverageTableOffsetArray[inputGlyphCount]);
P
prr 已提交
605 606 607 608 609

    if( LE_FAILURE(success) ) { return 0; }
    LEReferenceToArrayOf<Offset>   lookaheadCoverageTableOffsetArray(base, success, inputCoverageTableOffsetArray.getAlias(inputGlyphCount + 1, success), lookaheadGlyphCount+2);

    if( LE_FAILURE(success) ) { return 0; }
D
duke 已提交
610 611 612 613 614 615 616 617 618 619
    le_uint16 substCount = (le_uint16) SWAPW(lookaheadCoverageTableOffsetArray[lookaheadGlyphCount]);
    le_int32 position = glyphIterator->getCurrStreamPosition();
    GlyphIterator tempIterator(*glyphIterator, emptyFeatureList);

    if (! tempIterator.prev(backtrkGlyphCount)) {
        return 0;
    }

    tempIterator.prev();
    if (! ContextualSubstitutionBase::matchGlyphCoverages(backtrackCoverageTableOffsetArray,
P
prr 已提交
620
                       backtrkGlyphCount, &tempIterator, base, success, TRUE)) {
D
duke 已提交
621 622 623 624 625 626
        return 0;
    }

    tempIterator.setCurrStreamPosition(position);
    tempIterator.next(inputGlyphCount - 1);
    if (! ContextualSubstitutionBase::matchGlyphCoverages(lookaheadCoverageTableOffsetArray,
P
prr 已提交
627
                        lookaheadGlyphCount, &tempIterator, base, success)) {
D
duke 已提交
628 629 630 631 632 633 634 635 636 637
        return 0;
    }

    // Back up the glyph iterator so that we
    // can call next() before the check, which
    // will leave it pointing at the last glyph
    // that matched when we're done.
    glyphIterator->prev();

    if (ContextualSubstitutionBase::matchGlyphCoverages(inputCoverageTableOffsetArray,
P
prr 已提交
638 639 640 641
                                                        inputGlyphCount, glyphIterator, base, success)) {
        LEReferenceToArrayOf<SubstitutionLookupRecord>
          substLookupRecordArray(base, success,
                                 (const SubstitutionLookupRecord *) lookaheadCoverageTableOffsetArray.getAlias(lookaheadGlyphCount + 1,success), substCount);
D
duke 已提交
642

S
srl 已提交
643
        ContextualSubstitutionBase::applySubstitutionLookups(lookupProcessor, substLookupRecordArray, substCount, glyphIterator, fontInstance, position, success);
D
duke 已提交
644 645 646 647 648 649 650 651

        return inputGlyphCount;
    }

    glyphIterator->setCurrStreamPosition(position);

    return 0;
}
652 653

U_NAMESPACE_END