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