1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  Oracle designates this
7  * particular file as subject to the "Classpath" exception as provided
8  * by Oracle in the LICENSE file that accompanied this code.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 /*
27  *
28  * (C) Copyright IBM Corp.  and others 1998-2013 - All Rights Reserved
29  *
30  */
31 
32 #include "LETypes.h"
33 #include "MorphTables.h"
34 #include "StateTables.h"
35 #include "MorphStateTables.h"
36 #include "SubtableProcessor2.h"
37 #include "StateTableProcessor2.h"
38 #include "ContextualGlyphSubstProc2.h"
39 #include "LEGlyphStorage.h"
40 #include "LESwaps.h"
41 
42 U_NAMESPACE_BEGIN
43 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ContextualGlyphSubstitutionProcessor2)44 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(ContextualGlyphSubstitutionProcessor2)
45 
46 ContextualGlyphSubstitutionProcessor2::ContextualGlyphSubstitutionProcessor2(
47                                   const LEReferenceTo<MorphSubtableHeader2> &morphSubtableHeader, LEErrorCode &success)
48   : StateTableProcessor2(morphSubtableHeader, success), contextualGlyphHeader(morphSubtableHeader, success)
49 {
50     if(LE_FAILURE(success)) return;
51     le_uint32 perGlyphTableOffset = SWAPL(contextualGlyphHeader->perGlyphTableOffset);
52     perGlyphTable = LEReferenceToArrayOf<le_uint32> (stHeader, success, perGlyphTableOffset, LE_UNBOUNDED_ARRAY);
53     entryTable = LEReferenceToArrayOf<ContextualGlyphStateEntry2>(stHeader, success, entryTableOffset, LE_UNBOUNDED_ARRAY);
54 }
55 
~ContextualGlyphSubstitutionProcessor2()56 ContextualGlyphSubstitutionProcessor2::~ContextualGlyphSubstitutionProcessor2()
57 {
58 }
59 
beginStateTable()60 void ContextualGlyphSubstitutionProcessor2::beginStateTable()
61 {
62     markGlyph = 0;
63 }
64 
processStateEntry(LEGlyphStorage & glyphStorage,le_int32 & currGlyph,EntryTableIndex2 index,LEErrorCode & success)65 le_uint16 ContextualGlyphSubstitutionProcessor2::processStateEntry(LEGlyphStorage &glyphStorage, le_int32 &currGlyph,
66     EntryTableIndex2 index, LEErrorCode &success)
67 {
68     if(LE_FAILURE(success)) return 0;
69     const ContextualGlyphStateEntry2 *entry = entryTable.getAlias(index, success);
70     if(LE_FAILURE(success)) return 0;
71     le_uint16 newState = SWAPW(entry->newStateIndex);
72     le_uint16 flags = SWAPW(entry->flags);
73     le_uint16 markIndex = SWAPW(entry->markIndex);
74     le_uint16 currIndex = SWAPW(entry->currIndex);
75 
76     if (markIndex != 0x0FFFF) {
77         if (markGlyph < 0 || markGlyph >= glyphStorage.getGlyphCount()) {
78            success = LE_INDEX_OUT_OF_BOUNDS_ERROR;
79            return 0;
80         }
81         le_uint32 offset = SWAPL(perGlyphTable(markIndex, success));
82         LEGlyphID mGlyph = glyphStorage[markGlyph];
83         TTGlyphID newGlyph = lookup(offset, mGlyph, success);
84         glyphStorage[markGlyph] = LE_SET_GLYPH(mGlyph, newGlyph);
85     }
86 
87     if (currIndex != 0x0FFFF) {
88         if (currGlyph < 0 || currGlyph >= glyphStorage.getGlyphCount()) {
89            success = LE_INDEX_OUT_OF_BOUNDS_ERROR;
90            return 0;
91         }
92         le_uint32 offset = SWAPL(perGlyphTable(currIndex, success));
93         LEGlyphID thisGlyph = glyphStorage[currGlyph];
94         TTGlyphID newGlyph = lookup(offset, thisGlyph, success);
95         glyphStorage[currGlyph] = LE_SET_GLYPH(thisGlyph, newGlyph);
96     }
97 
98     if (flags & cgsSetMark) {
99         markGlyph = currGlyph;
100     }
101 
102     if (!(flags & cgsDontAdvance)) {
103         currGlyph += dir;
104     }
105 
106     return newState;
107 }
108 
lookup(le_uint32 offset,LEGlyphID gid,LEErrorCode & success)109 TTGlyphID ContextualGlyphSubstitutionProcessor2::lookup(le_uint32 offset, LEGlyphID gid, LEErrorCode &success)
110 {
111     TTGlyphID newGlyph = 0xFFFF;
112     if(LE_FAILURE(success))  return newGlyph;
113     LEReferenceTo<LookupTable> lookupTable(perGlyphTable, success, offset);
114     if(LE_FAILURE(success))  return newGlyph;
115     le_int16 format = SWAPW(lookupTable->format);
116 
117     switch (format) {
118         case ltfSimpleArray: {
119 #ifdef TEST_FORMAT
120             // Disabled pending for design review
121             LEReferenceTo<SimpleArrayLookupTable> lookupTable0(lookupTable, success);
122             LEReferenceToArrayOf<LookupValue> valueArray(lookupTable0, success, &lookupTable0->valueArray[0], LE_UNBOUNDED_ARRAY);
123             if(LE_FAILURE(success))  return newGlyph;
124             TTGlyphID glyphCode = (TTGlyphID) LE_GET_GLYPH(gid);
125             newGlyph = SWAPW(lookupTable0->valueArray(glyphCode, success));
126 #endif
127             break;
128         }
129         case ltfSegmentSingle: {
130 #ifdef TEST_FORMAT
131             // Disabled pending for design review
132             LEReferenceTo<SegmentSingleLookupTable> lookupTable2 = (SegmentSingleLookupTable *) lookupTable;
133             const LookupSegment *segment = lookupTable2->lookupSegment(lookupTable2->segments, gid);
134             if (segment != NULL) {
135                 newGlyph = SWAPW(segment->value);
136             }
137 #endif
138             break;
139         }
140         case ltfSegmentArray: {
141             //printf("Context Lookup Table Format4: specific interpretation needed!\n");
142             break;
143         }
144         case ltfSingleTable:
145         {
146 #ifdef TEST_FORMAT
147             // Disabled pending for design review
148             LEReferenceTo<SingleTableLookupTable> lookupTable6 = (SingleTableLookupTable *) lookupTable;
149             const LEReferenceTo<LookupSingle> segment = lookupTable6->lookupSingle(lookupTable6->entries, gid);
150             if (segment != NULL) {
151                 newGlyph = SWAPW(segment->value);
152             }
153 #endif
154             break;
155         }
156         case ltfTrimmedArray: {
157             LEReferenceTo<TrimmedArrayLookupTable> lookupTable8(lookupTable, success);
158             if (LE_FAILURE(success)) return newGlyph;
159             TTGlyphID firstGlyph = SWAPW(lookupTable8->firstGlyph);
160             TTGlyphID glyphCount = SWAPW(lookupTable8->glyphCount);
161             TTGlyphID lastGlyph  = firstGlyph + glyphCount;
162             TTGlyphID glyphCode = (TTGlyphID) LE_GET_GLYPH(gid);
163             if ((glyphCode >= firstGlyph) && (glyphCode < lastGlyph)) {
164               LEReferenceToArrayOf<LookupValue> valueArray(lookupTable8, success, &lookupTable8->valueArray[0], glyphCount);
165               if (LE_FAILURE(success)) { return newGlyph; }
166               newGlyph = SWAPW(valueArray(glyphCode - firstGlyph, success));
167             }
168         }
169         default:
170             break;
171     }
172     return newGlyph;
173 }
174 
endStateTable()175 void ContextualGlyphSubstitutionProcessor2::endStateTable()
176 {
177 }
178 
179 U_NAMESPACE_END
180