3 * (C) Copyright IBM Corp and Others. 1998-2014 - All Rights Reserved
8 #include "MorphTables.h"
9 #include "StateTables.h"
10 #include "MorphStateTables.h"
11 #include "SubtableProcessor2.h"
12 #include "StateTableProcessor2.h"
13 #include "LigatureSubstProc2.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(LigatureSubstitutionProcessor2
)
25 LigatureSubstitutionProcessor2::LigatureSubstitutionProcessor2(const LEReferenceTo
<MorphSubtableHeader2
> &morphSubtableHeader
, LEErrorCode
&success
)
26 : StateTableProcessor2(morphSubtableHeader
, success
),
28 componentOffset(0), ligatureOffset(0), entryTable(), ligatureSubstitutionHeader(morphSubtableHeader
, success
)
30 if (LE_FAILURE(success
)) return;
32 ligActionOffset
= SWAPL(ligatureSubstitutionHeader
->ligActionOffset
);
33 componentOffset
= SWAPL(ligatureSubstitutionHeader
->componentOffset
);
34 ligatureOffset
= SWAPL(ligatureSubstitutionHeader
->ligatureOffset
);
36 entryTable
= LEReferenceToArrayOf
<LigatureSubstitutionStateEntry2
>(stHeader
, success
, entryTableOffset
, LE_UNBOUNDED_ARRAY
);
39 LigatureSubstitutionProcessor2::~LigatureSubstitutionProcessor2()
43 void LigatureSubstitutionProcessor2::beginStateTable()
48 le_uint16
LigatureSubstitutionProcessor2::processStateEntry(LEGlyphStorage
&glyphStorage
, le_int32
&currGlyph
, EntryTableIndex2 index
, LEErrorCode
&success
)
50 const LigatureSubstitutionStateEntry2
*entry
= entryTable
.getAlias(index
, success
);
51 if(LE_FAILURE(success
)) return 0;
53 le_uint16 nextStateIndex
= SWAPW(entry
->nextStateIndex
);
54 le_uint16 flags
= SWAPW(entry
->entryFlags
);
55 le_uint16 ligActionIndex
= SWAPW(entry
->ligActionIndex
);
57 if (flags
& lsfSetComponent
) {
58 if (++m
>= nComponents
) {
61 componentStack
[m
] = currGlyph
;
62 } else if ( m
== -1) {
63 // bad font- skip this glyph.
64 //LE_DEBUG_BAD_FONT("m==-1 (componentCount went negative)")
66 return nextStateIndex
;
69 ByteOffset actionOffset
= flags
& lsfPerformAction
;
71 if (actionOffset
!= 0) {
72 LEReferenceTo
<LigatureActionEntry
> ap(stHeader
, success
, ligActionOffset
); // byte offset
73 ap
.addObject(ligActionIndex
- 1, success
); // index offset ( one before the actual start, because we will pre-increment)
74 LEReferenceToArrayOf
<TTGlyphID
> ligatureTable(stHeader
, success
, ligatureOffset
, LE_UNBOUNDED_ARRAY
);
75 LigatureActionEntry action
;
76 le_int32 offset
, i
= 0;
77 le_int32 stack
[nComponents
];
80 LEReferenceToArrayOf
<le_uint16
> componentTable(stHeader
, success
, componentOffset
, LE_UNBOUNDED_ARRAY
);
81 if(LE_FAILURE(success
)) {
83 return nextStateIndex
; // get out! bad font
87 le_int32 componentGlyph
= componentStack
[m
--]; // pop off
89 ap
.addObject(success
);
90 action
= SWAPL(*ap
.getAlias());
96 offset
= action
& lafComponentOffsetMask
;
98 if(componentGlyph
> glyphStorage
.getGlyphCount() || componentGlyph
< 0) {
99 LE_DEBUG_BAD_FONT("preposterous componentGlyph");
101 return nextStateIndex
; // get out! bad font
103 i
+= SWAPW(componentTable(LE_GET_GLYPH(glyphStorage
[componentGlyph
]) + (SignExtend(offset
, lafComponentOffsetMask
)),success
));
105 if (action
& (lafLast
| lafStore
)) {
106 TTGlyphID ligatureGlyph
= SWAPW(ligatureTable(i
,success
));
107 glyphStorage
[componentGlyph
] = LE_SET_GLYPH(glyphStorage
[componentGlyph
], ligatureGlyph
);
108 if(mm
==nComponents
) {
109 LE_DEBUG_BAD_FONT("exceeded nComponents");
110 mm
--; // don't overrun the stack.
112 stack
[++mm
] = componentGlyph
;
115 glyphStorage
[componentGlyph
] = LE_SET_GLYPH(glyphStorage
[componentGlyph
], 0xFFFF);
118 #if LE_ASSERT_BAD_FONT
120 LE_DEBUG_BAD_FONT("m<0")
123 } while (!(action
& lafLast
) && (m
>=0) ); // stop if last bit is set, or if run out of items
126 if (++m
>= nComponents
) {
130 componentStack
[m
] = stack
[mm
--];
134 if (!(flags
& lsfDontAdvance
)) {
138 return nextStateIndex
;
141 void LigatureSubstitutionProcessor2::endStateTable()