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 "LigatureSubstProc2.h"
  39 #include "LEGlyphStorage.h"
  40 #include "LESwaps.h"
  41 
  42 U_NAMESPACE_BEGIN
  43 
  44 #define ExtendedComplement(m) ((le_int32) (~((le_uint32) (m))))
  45 #define SignBit(m) ((ExtendedComplement(m) >> 1) & (le_int32)(m))
  46 #define SignExtend(v,m) (((v) & SignBit(m))? ((v) | ExtendedComplement(m)): (v))
  47 
  48 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(LigatureSubstitutionProcessor2)
  49 
  50 LigatureSubstitutionProcessor2::LigatureSubstitutionProcessor2(const LEReferenceTo<MorphSubtableHeader2> &morphSubtableHeader, LEErrorCode &success)
  51   : StateTableProcessor2(morphSubtableHeader, success),
  52   ligActionOffset(0),
  53   ligatureSubstitutionHeader(morphSubtableHeader, success), componentOffset(0), ligatureOffset(0), entryTable()
  54 {
  55     if (LE_FAILURE(success)) return;
  56 
  57     ligActionOffset = SWAPL(ligatureSubstitutionHeader->ligActionOffset);
  58     componentOffset = SWAPL(ligatureSubstitutionHeader->componentOffset);
  59     ligatureOffset = SWAPL(ligatureSubstitutionHeader->ligatureOffset);
  60 
  61     entryTable = LEReferenceToArrayOf<LigatureSubstitutionStateEntry2>(stHeader, success, entryTableOffset, LE_UNBOUNDED_ARRAY);
  62 }
  63 
  64 LigatureSubstitutionProcessor2::~LigatureSubstitutionProcessor2()
  65 {
  66 }
  67 
  68 void LigatureSubstitutionProcessor2::beginStateTable()
  69 {
  70     m = -1;
  71 }
  72 
  73 le_uint16 LigatureSubstitutionProcessor2::processStateEntry(LEGlyphStorage &glyphStorage, le_int32 &currGlyph, EntryTableIndex2 index, LEErrorCode &success)
  74 {
  75     const LigatureSubstitutionStateEntry2 *entry = entryTable.getAlias(index, success);
  76     if(LE_FAILURE(success)) return 0;
  77 
  78     le_uint16 nextStateIndex = SWAPW(entry->nextStateIndex);
  79     le_uint16 flags = SWAPW(entry->entryFlags);
  80     le_uint16 ligActionIndex = SWAPW(entry->ligActionIndex);
  81 
  82     if (flags & lsfSetComponent) {
  83         if (++m >= nComponents) {
  84             m = 0;
  85         }
  86         componentStack[m] = currGlyph;
  87     } else if ( m == -1) {
  88         // bad font- skip this glyph.
  89         //LE_DEBUG_BAD_FONT("m==-1 (componentCount went negative)")
  90         currGlyph+= dir;
  91         return nextStateIndex;
  92     }
  93 
  94     ByteOffset actionOffset = flags & lsfPerformAction;
  95 
  96     if (actionOffset != 0) {
  97         LEReferenceTo<LigatureActionEntry> ap(stHeader, success, ligActionOffset); // byte offset
  98         if (LE_FAILURE(success)) {
  99             currGlyph+= dir;
 100             return nextStateIndex;
 101         }
 102         ap.addObject(ligActionIndex, success);
 103         LEReferenceToArrayOf<TTGlyphID> ligatureTable(stHeader, success, ligatureOffset, LE_UNBOUNDED_ARRAY);
 104         LigatureActionEntry action;
 105         le_int32 offset, i = 0, j = 0;
 106         le_int32 stack[nComponents];
 107         le_int16 mm = -1;
 108 
 109         LEReferenceToArrayOf<le_uint16> componentTable(stHeader, success, componentOffset, LE_UNBOUNDED_ARRAY);
 110         if(LE_FAILURE(success)) {
 111             currGlyph+= dir;
 112             return nextStateIndex; // get out! bad font
 113         }
 114 
 115         do {
 116             le_uint32 componentGlyph = componentStack[m--]; // pop off
 117 
 118             if (j++ > 0) {
 119                 ap.addObject(success);
 120             }
 121             if (LE_FAILURE(success)) {
 122                 currGlyph+= dir;
 123                 return nextStateIndex;
 124             }
 125 
 126             action = SWAPL(*ap.getAlias());
 127 
 128             if (m < 0) {
 129                 m = nComponents - 1;
 130             }
 131 
 132             offset = action & lafComponentOffsetMask;
 133             if (offset != 0) {
 134                 if(componentGlyph >= glyphStorage.getGlyphCount()) {
 135                   LE_DEBUG_BAD_FONT("preposterous componentGlyph");
 136                   currGlyph+= dir;
 137                   return nextStateIndex; // get out! bad font
 138                 }
 139                 i += SWAPW(componentTable(LE_GET_GLYPH(glyphStorage[componentGlyph]) + (SignExtend(offset, lafComponentOffsetMask)),success));
 140                 if (LE_FAILURE(success)) {
 141                     currGlyph+= dir;
 142                     return nextStateIndex;
 143                 }
 144 
 145                 if (action & (lafLast | lafStore))  {
 146                     TTGlyphID ligatureGlyph = SWAPW(ligatureTable(i,success));
 147                     if (LE_FAILURE(success)) {
 148                         currGlyph+= dir;
 149                         return nextStateIndex;
 150                     }
 151                     glyphStorage[componentGlyph] = LE_SET_GLYPH(glyphStorage[componentGlyph], ligatureGlyph);
 152                     ++mm;
 153                     if (mm == nComponents) {
 154                       LE_DEBUG_BAD_FONT("exceeded nComponents");
 155                       mm--; // don't overrun the stack.
 156                     }
 157                     stack[mm] = componentGlyph;
 158                     i = 0;
 159                 } else {
 160                     glyphStorage[componentGlyph] = LE_SET_GLYPH(glyphStorage[componentGlyph], 0xFFFF);
 161                 }
 162             }
 163 #if LE_ASSERT_BAD_FONT
 164             if(m<0) {
 165               LE_DEBUG_BAD_FONT("m<0")
 166             }
 167 #endif
 168         } while (LE_SUCCESS(success) && !(action & lafLast) && (m>=0) ); // stop if last bit is set, or if run out of items
 169 
 170         while (mm >= 0) {
 171             if (++m >= nComponents) {
 172                 m = 0;
 173             }
 174 
 175             componentStack[m] = stack[mm--];
 176         }
 177     }
 178 
 179     if (!(flags & lsfDontAdvance)) {
 180         currGlyph += dir;
 181     }
 182 
 183     return nextStateIndex;
 184 }
 185 
 186 void LigatureSubstitutionProcessor2::endStateTable()
 187 {
 188 }
 189 
 190 U_NAMESPACE_END