1/*
2 *
3 * (C) Copyright IBM Corp.  and others 1998-2013 - All Rights Reserved
4 *
5 */
6
7#include "LETypes.h"
8#include "MorphTables.h"
9#include "StateTables.h"
10#include "MorphStateTables.h"
11#include "SubtableProcessor2.h"
12#include "StateTableProcessor2.h"
13#include "ContextualGlyphSubstProc2.h"
14#include "LEGlyphStorage.h"
15#include "LESwaps.h"
16
17U_NAMESPACE_BEGIN
18
19UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ContextualGlyphSubstitutionProcessor2)
20
21ContextualGlyphSubstitutionProcessor2::ContextualGlyphSubstitutionProcessor2(
22                                  const LEReferenceTo<MorphSubtableHeader2> &morphSubtableHeader, LEErrorCode &success)
23  : StateTableProcessor2(morphSubtableHeader, success), contextualGlyphHeader(morphSubtableHeader, success)
24{
25    if(LE_FAILURE(success)) return;
26    le_uint32 perGlyphTableOffset = SWAPL(contextualGlyphHeader->perGlyphTableOffset);
27    perGlyphTable = LEReferenceToArrayOf<le_uint32> (stHeader, success, perGlyphTableOffset, LE_UNBOUNDED_ARRAY);
28    entryTable = LEReferenceToArrayOf<ContextualGlyphStateEntry2>(stHeader, success, entryTableOffset, LE_UNBOUNDED_ARRAY);
29}
30
31ContextualGlyphSubstitutionProcessor2::~ContextualGlyphSubstitutionProcessor2()
32{
33}
34
35void ContextualGlyphSubstitutionProcessor2::beginStateTable()
36{
37    markGlyph = 0;
38}
39
40le_uint16 ContextualGlyphSubstitutionProcessor2::processStateEntry(LEGlyphStorage &glyphStorage, le_int32 &currGlyph,
41    EntryTableIndex2 index, LEErrorCode &success)
42{
43    if(LE_FAILURE(success)) return 0;
44    const ContextualGlyphStateEntry2 *entry = entryTable.getAlias(index, success);
45    if(LE_FAILURE(success)) return 0;
46    le_uint16 newState = SWAPW(entry->newStateIndex);
47    le_uint16 flags = SWAPW(entry->flags);
48    le_int16 markIndex = SWAPW(entry->markIndex);
49    le_int16 currIndex = SWAPW(entry->currIndex);
50
51    if (markIndex != -1) {
52        le_uint32 offset = SWAPL(perGlyphTable(markIndex, success));
53        LEGlyphID mGlyph = glyphStorage[markGlyph];
54        TTGlyphID newGlyph = lookup(offset, mGlyph, success);
55        glyphStorage[markGlyph] = LE_SET_GLYPH(mGlyph, newGlyph);
56    }
57
58    if (currIndex != -1) {
59        le_uint32 offset = SWAPL(perGlyphTable(currIndex, success));
60        LEGlyphID thisGlyph = glyphStorage[currGlyph];
61        TTGlyphID newGlyph = lookup(offset, thisGlyph, success);
62        glyphStorage[currGlyph] = LE_SET_GLYPH(thisGlyph, newGlyph);
63    }
64
65    if (flags & cgsSetMark) {
66        markGlyph = currGlyph;
67    }
68
69    if (!(flags & cgsDontAdvance)) {
70        currGlyph += dir;
71    }
72
73    return newState;
74}
75
76TTGlyphID ContextualGlyphSubstitutionProcessor2::lookup(le_uint32 offset, LEGlyphID gid, LEErrorCode &success)
77{
78    TTGlyphID newGlyph = 0xFFFF;
79    if(LE_FAILURE(success))  return newGlyph;
80    LEReferenceTo<LookupTable> lookupTable(perGlyphTable, success, offset);
81    if(LE_FAILURE(success))  return newGlyph;
82    le_int16 format = SWAPW(lookupTable->format);
83
84    switch (format) {
85        case ltfSimpleArray: {
86#ifdef TEST_FORMAT
87            // Disabled pending for design review
88            LEReferenceTo<SimpleArrayLookupTable> lookupTable0(lookupTable, success);
89            LEReferenceToArrayOf<LookupValue> valueArray(lookupTable0, success, &lookupTable0->valueArray[0], LE_UNBOUNDED_ARRAY);
90            if(LE_FAILURE(success))  return newGlyph;
91            TTGlyphID glyphCode = (TTGlyphID) LE_GET_GLYPH(gid);
92            newGlyph = SWAPW(lookupTable0->valueArray(glyphCode, success));
93#endif
94            break;
95        }
96        case ltfSegmentSingle: {
97#ifdef TEST_FORMAT
98            // Disabled pending for design review
99            LEReferenceTo<SegmentSingleLookupTable> lookupTable2 = (SegmentSingleLookupTable *) lookupTable;
100            const LookupSegment *segment = lookupTable2->lookupSegment(lookupTable2->segments, gid);
101            if (segment != NULL) {
102                newGlyph = SWAPW(segment->value);
103            }
104#endif
105            break;
106        }
107        case ltfSegmentArray: {
108            //printf("Context Lookup Table Format4: specific interpretation needed!\n");
109            break;
110        }
111        case ltfSingleTable:
112        {
113#ifdef TEST_FORMAT
114            // Disabled pending for design review
115            LEReferenceTo<SingleTableLookupTable> lookupTable6 = (SingleTableLookupTable *) lookupTable;
116            const LEReferenceTo<LookupSingle> segment = lookupTable6->lookupSingle(lookupTable6->entries, gid);
117            if (segment != NULL) {
118                newGlyph = SWAPW(segment->value);
119            }
120#endif
121            break;
122        }
123        case ltfTrimmedArray: {
124            LEReferenceTo<TrimmedArrayLookupTable> lookupTable8(lookupTable, success);
125            if (LE_FAILURE(success)) return newGlyph;
126            TTGlyphID firstGlyph = SWAPW(lookupTable8->firstGlyph);
127            TTGlyphID glyphCount = SWAPW(lookupTable8->glyphCount);
128            TTGlyphID lastGlyph  = firstGlyph + glyphCount;
129            TTGlyphID glyphCode = (TTGlyphID) LE_GET_GLYPH(gid);
130            if ((glyphCode >= firstGlyph) && (glyphCode < lastGlyph)) {
131              LEReferenceToArrayOf<LookupValue> valueArray(lookupTable8, success, &lookupTable8->valueArray[0], glyphCount);
132              newGlyph = SWAPW(valueArray(glyphCode - firstGlyph, success));
133            }
134        }
135        default:
136            break;
137    }
138    return newGlyph;
139}
140
141void ContextualGlyphSubstitutionProcessor2::endStateTable()
142{
143}
144
145U_NAMESPACE_END
146