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