3 * (C) Copyright IBM Corp. 1998-2013 - All Rights Reserved
8 #include "MorphTables.h"
9 #include "StateTables.h"
10 #include "MorphStateTables.h"
11 #include "SubtableProcessor.h"
12 #include "StateTableProcessor.h"
13 #include "LigatureSubstProc.h"
14 #include "LEGlyphStorage.h"
19 #define ExtendedComplement(m) ((le_int32) (~((le_uint32) (m))))
20 #define SignBit(m) ((ExtendedComplement(m) >> 1) & (le_int32)(m))
21 #define SignExtend(v,m) (((v) & SignBit(m))? ((v) | ExtendedComplement(m)): (v))
23 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LigatureSubstitutionProcessor)
25 LigatureSubstitutionProcessor::LigatureSubstitutionProcessor(const LEReferenceTo<MorphSubtableHeader> &morphSubtableHeader, LEErrorCode &success)
26 : StateTableProcessor(morphSubtableHeader, success), ligatureSubstitutionHeader(morphSubtableHeader, success)
28 if(LE_FAILURE(success)) return;
29 ligatureActionTableOffset = SWAPW(ligatureSubstitutionHeader->ligatureActionTableOffset);
30 componentTableOffset = SWAPW(ligatureSubstitutionHeader->componentTableOffset);
31 ligatureTableOffset = SWAPW(ligatureSubstitutionHeader->ligatureTableOffset);
33 entryTable = LEReferenceToArrayOf<LigatureSubstitutionStateEntry>(stHeader, success, entryTableOffset, LE_UNBOUNDED_ARRAY);
36 LigatureSubstitutionProcessor::~LigatureSubstitutionProcessor()
40 void LigatureSubstitutionProcessor::beginStateTable()
45 ByteOffset LigatureSubstitutionProcessor::processStateEntry(LEGlyphStorage &glyphStorage, le_int32 &currGlyph, EntryTableIndex index)
47 LEErrorCode success = LE_NO_ERROR;
48 const LigatureSubstitutionStateEntry *entry = entryTable.getAlias(index, success);
50 ByteOffset newState = SWAPW(entry->newStateOffset);
51 le_int16 flags = SWAPW(entry->flags);
53 if (flags & lsfSetComponent) {
54 if (++m >= nComponents) {
58 componentStack[m] = currGlyph;
59 } else if ( m == -1) {
60 // bad font- skip this glyph.
65 ByteOffset actionOffset = flags & lsfActionOffsetMask;
67 if (actionOffset != 0) {
68 LEReferenceTo<LigatureActionEntry> ap(stHeader, success, actionOffset);
69 LigatureActionEntry action;
70 le_int32 offset, i = 0;
71 le_int32 stack[nComponents];
75 le_uint32 componentGlyph = componentStack[m--];
77 action = SWAPL(*ap.getAlias());
78 ap.addObject(success); // ap++
84 offset = action & lafComponentOffsetMask;
86 LEReferenceToArrayOf<le_int16> offsetTable(stHeader, success, 2 * SignExtend(offset, lafComponentOffsetMask), LE_UNBOUNDED_ARRAY);
88 if(LE_FAILURE(success)) {
90 LE_DEBUG_BAD_FONT("off end of ligature substitution header");
91 return newState; // get out! bad font
93 if(componentGlyph > (le_uint32)glyphStorage.getGlyphCount()) {
94 LE_DEBUG_BAD_FONT("preposterous componentGlyph");
96 return newState; // get out! bad font
98 i += SWAPW(offsetTable.getObject(LE_GET_GLYPH(glyphStorage[componentGlyph]), success));
100 if (action & (lafLast | lafStore)) {
101 LEReferenceTo<TTGlyphID> ligatureOffset(stHeader, success, i);
102 TTGlyphID ligatureGlyph = SWAPW(*ligatureOffset.getAlias());
104 glyphStorage[componentGlyph] = LE_SET_GLYPH(glyphStorage[componentGlyph], ligatureGlyph);
105 if(mm==nComponents) {
106 LE_DEBUG_BAD_FONT("exceeded nComponents");
107 mm--; // don't overrun the stack.
109 stack[++mm] = componentGlyph;
112 glyphStorage[componentGlyph] = LE_SET_GLYPH(glyphStorage[componentGlyph], 0xFFFF);
115 #if LE_ASSERT_BAD_FONT
117 LE_DEBUG_BAD_FONT("m<0")
120 } while (!(action & lafLast) && (m>=0) ); // stop if last bit is set, or if run out of items
123 if (++m >= nComponents) {
127 componentStack[m] = stack[mm--];
131 if (!(flags & lsfDontAdvance)) {
132 // should handle reverse too!
139 void LigatureSubstitutionProcessor::endStateTable()